1// Copyright 2013 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// ListAssignees fetches all available assignees (owners and collaborators) to
14// which issues may be assigned.
15//
16// GitHub API docs: https://developer.github.com/v3/issues/assignees/#list-assignees
17func (s *IssuesService) ListAssignees(ctx context.Context, owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
18	u := fmt.Sprintf("repos/%v/%v/assignees", owner, repo)
19	u, err := addOptions(u, opt)
20	if err != nil {
21		return nil, nil, err
22	}
23
24	req, err := s.client.NewRequest("GET", u, nil)
25	if err != nil {
26		return nil, nil, err
27	}
28	var assignees []*User
29	resp, err := s.client.Do(ctx, req, &assignees)
30	if err != nil {
31		return nil, resp, err
32	}
33
34	return assignees, resp, nil
35}
36
37// IsAssignee checks if a user is an assignee for the specified repository.
38//
39// GitHub API docs: https://developer.github.com/v3/issues/assignees/#check-assignee
40func (s *IssuesService) IsAssignee(ctx context.Context, owner, repo, user string) (bool, *Response, error) {
41	u := fmt.Sprintf("repos/%v/%v/assignees/%v", owner, repo, user)
42	req, err := s.client.NewRequest("GET", u, nil)
43	if err != nil {
44		return false, nil, err
45	}
46	resp, err := s.client.Do(ctx, req, nil)
47	assignee, err := parseBoolResponse(err)
48	return assignee, resp, err
49}
50
51// AddAssignees adds the provided GitHub users as assignees to the issue.
52//
53// GitHub API docs: https://developer.github.com/v3/issues/assignees/#add-assignees-to-an-issue
54func (s *IssuesService) AddAssignees(ctx context.Context, owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
55	users := &struct {
56		Assignees []string `json:"assignees,omitempty"`
57	}{Assignees: assignees}
58	u := fmt.Sprintf("repos/%v/%v/issues/%v/assignees", owner, repo, number)
59	req, err := s.client.NewRequest("POST", u, users)
60	if err != nil {
61		return nil, nil, err
62	}
63
64	issue := &Issue{}
65	resp, err := s.client.Do(ctx, req, issue)
66	return issue, resp, err
67}
68
69// RemoveAssignees removes the provided GitHub users as assignees from the issue.
70//
71// GitHub API docs: https://developer.github.com/v3/issues/assignees/#remove-assignees-from-an-issue
72func (s *IssuesService) RemoveAssignees(ctx context.Context, owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
73	users := &struct {
74		Assignees []string `json:"assignees,omitempty"`
75	}{Assignees: assignees}
76	u := fmt.Sprintf("repos/%v/%v/issues/%v/assignees", owner, repo, number)
77	req, err := s.client.NewRequest("DELETE", u, users)
78	if err != nil {
79		return nil, nil, err
80	}
81
82	issue := &Issue{}
83	resp, err := s.client.Do(ctx, req, issue)
84	return issue, resp, err
85}
86