You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

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