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://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-invitations
31func (s *RepositoriesService) ListInvitations(ctx context.Context, owner, repo string, opts *ListOptions) ([]*RepositoryInvitation, *Response, error) {
32	u := fmt.Sprintf("repos/%v/%v/invitations", owner, repo)
33	u, err := addOptions(u, opts)
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	invites := []*RepositoryInvitation{}
44	resp, err := s.client.Do(ctx, req, &invites)
45	if err != nil {
46		return nil, resp, err
47	}
48
49	return invites, resp, nil
50}
51
52// DeleteInvitation deletes a repository invitation.
53//
54// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-repository-invitation
55func (s *RepositoriesService) DeleteInvitation(ctx context.Context, owner, repo string, invitationID int64) (*Response, error) {
56	u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
57	req, err := s.client.NewRequest("DELETE", u, nil)
58	if err != nil {
59		return nil, err
60	}
61
62	return s.client.Do(ctx, req, nil)
63}
64
65// UpdateInvitation updates the permissions associated with a repository
66// invitation.
67//
68// permissions represents the permissions that the associated user will have
69// on the repository. Possible values are: "read", "write", "admin".
70//
71// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-a-repository-invitation
72func (s *RepositoriesService) UpdateInvitation(ctx context.Context, owner, repo string, invitationID int64, permissions string) (*RepositoryInvitation, *Response, error) {
73	opts := &struct {
74		Permissions string `json:"permissions"`
75	}{Permissions: permissions}
76	u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
77	req, err := s.client.NewRequest("PATCH", u, opts)
78	if err != nil {
79		return nil, nil, err
80	}
81
82	invite := &RepositoryInvitation{}
83	resp, err := s.client.Do(ctx, req, invite)
84	if err != nil {
85		return nil, resp, err
86	}
87
88	return invite, resp, nil
89}
90