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