1// Copyright 2016 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// RepositoryInvitation represents an invitation to collaborate on a repo.
14type RepositoryInvitation struct {
15	ID      *int64      `json:"id,omitempty"`
16	Repo    *Repository `json:"repository,omitempty"`
17	Invitee *User       `json:"invitee,omitempty"`
18	Inviter *User       `json:"inviter,omitempty"`
19
20	// Permissions represents the permissions that the associated user will have
21	// on the repository. Possible values are: "read", "write", "admin".
22	Permissions *string    `json:"permissions,omitempty"`
23	CreatedAt   *Timestamp `json:"created_at,omitempty"`
24	URL         *string    `json:"url,omitempty"`
25	HTMLURL     *string    `json:"html_url,omitempty"`
26}
27
28// ListInvitations lists all currently-open repository invitations.
29//
30// GitHub API docs: https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository
31func (s *RepositoriesService) ListInvitations(ctx context.Context, owner, repo string, opt *ListOptions) ([]*RepositoryInvitation, *Response, error) {
32	u := fmt.Sprintf("repos/%v/%v/invitations", owner, repo)
33	u, err := addOptions(u, opt)
34	if err != nil {
35		return nil, nil, err
36	}
37
38	req, err := s.client.NewRequest("GET", u, nil)
39	if err != nil {
40		return nil, nil, err
41	}
42
43	// TODO: remove custom Accept header when this API fully launches.
44	req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
45
46	invites := []*RepositoryInvitation{}
47	resp, err := s.client.Do(ctx, req, &invites)
48	if err != nil {
49		return nil, resp, err
50	}
51
52	return invites, resp, nil
53}
54
55// DeleteInvitation deletes a repository invitation.
56//
57// GitHub API docs: https://developer.github.com/v3/repos/invitations/#delete-a-repository-invitation
58func (s *RepositoriesService) DeleteInvitation(ctx context.Context, owner, repo string, invitationID int64) (*Response, error) {
59	u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
60	req, err := s.client.NewRequest("DELETE", u, nil)
61	if err != nil {
62		return nil, err
63	}
64
65	// TODO: remove custom Accept header when this API fully launches.
66	req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
67
68	return s.client.Do(ctx, req, nil)
69}
70
71// UpdateInvitation updates the permissions associated with a repository
72// invitation.
73//
74// permissions represents the permissions that the associated user will have
75// on the repository. Possible values are: "read", "write", "admin".
76//
77// GitHub API docs: https://developer.github.com/v3/repos/invitations/#update-a-repository-invitation
78func (s *RepositoriesService) UpdateInvitation(ctx context.Context, owner, repo string, invitationID int64, permissions string) (*RepositoryInvitation, *Response, error) {
79	opts := &struct {
80		Permissions string `json:"permissions"`
81	}{Permissions: permissions}
82	u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
83	req, err := s.client.NewRequest("PATCH", u, opts)
84	if err != nil {
85		return nil, nil, err
86	}
87
88	// TODO: remove custom Accept header when this API fully launches.
89	req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
90
91	invite := &RepositoryInvitation{}
92	resp, err := s.client.Do(ctx, req, invite)
93	if err != nil {
94		return nil, resp, err
95	}
96
97	return invite, resp, nil
98}
99