1// Copyright 2017 The go-github AUTHORS. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package github
7
8import (
9	"context"
10	"fmt"
11)
12
13// ReviewersRequest specifies users and teams for a pull request review request.
14type ReviewersRequest struct {
15	Reviewers     []string `json:"reviewers,omitempty"`
16	TeamReviewers []string `json:"team_reviewers,omitempty"`
17}
18
19// Reviewers represents reviewers of a pull request.
20type Reviewers struct {
21	Users []*User `json:"users,omitempty"`
22	Teams []*Team `json:"teams,omitempty"`
23}
24
25// RequestReviewers creates a review request for the provided reviewers for the specified pull request.
26//
27// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#create-a-review-request
28func (s *PullRequestsService) RequestReviewers(ctx context.Context, owner, repo string, number int, reviewers ReviewersRequest) (*PullRequest, *Response, error) {
29	u := fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, number)
30	req, err := s.client.NewRequest("POST", u, &reviewers)
31	if err != nil {
32		return nil, nil, err
33	}
34
35	r := new(PullRequest)
36	resp, err := s.client.Do(ctx, req, r)
37	if err != nil {
38		return nil, resp, err
39	}
40
41	return r, resp, nil
42}
43
44// ListReviewers lists reviewers whose reviews have been requested on the specified pull request.
45//
46// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#list-review-requests
47func (s *PullRequestsService) ListReviewers(ctx context.Context, owner, repo string, number int, opt *ListOptions) (*Reviewers, *Response, error) {
48	u := fmt.Sprintf("repos/%v/%v/pulls/%d/requested_reviewers", owner, repo, number)
49	u, err := addOptions(u, opt)
50	if err != nil {
51		return nil, nil, err
52	}
53
54	req, err := s.client.NewRequest("GET", u, nil)
55	if err != nil {
56		return nil, nil, err
57	}
58
59	reviewers := new(Reviewers)
60	resp, err := s.client.Do(ctx, req, reviewers)
61	if err != nil {
62		return nil, resp, err
63	}
64
65	return reviewers, resp, nil
66}
67
68// RemoveReviewers removes the review request for the provided reviewers for the specified pull request.
69//
70// GitHub API docs: https://developer.github.com/v3/pulls/review_requests/#delete-a-review-request
71func (s *PullRequestsService) RemoveReviewers(ctx context.Context, owner, repo string, number int, reviewers ReviewersRequest) (*Response, error) {
72	u := fmt.Sprintf("repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, number)
73	req, err := s.client.NewRequest("DELETE", u, &reviewers)
74	if err != nil {
75		return nil, err
76	}
77
78	return s.client.Do(ctx, req, nil)
79}
80