repos_invitations.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // TODO: remove custom Accept header when this API fully launches.
  37. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  38. invites := []*RepositoryInvitation{}
  39. resp, err := s.client.Do(ctx, req, &invites)
  40. if err != nil {
  41. return nil, resp, err
  42. }
  43. return invites, resp, nil
  44. }
  45. // DeleteInvitation deletes a repository invitation.
  46. //
  47. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#delete-a-repository-invitation
  48. func (s *RepositoriesService) DeleteInvitation(ctx context.Context, owner, repo string, invitationID int64) (*Response, error) {
  49. u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
  50. req, err := s.client.NewRequest("DELETE", u, nil)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // TODO: remove custom Accept header when this API fully launches.
  55. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  56. return s.client.Do(ctx, req, nil)
  57. }
  58. // UpdateInvitation updates the permissions associated with a repository
  59. // invitation.
  60. //
  61. // permissions represents the permissions that the associated user will have
  62. // on the repository. Possible values are: "read", "write", "admin".
  63. //
  64. // GitHub API docs: https://developer.github.com/v3/repos/invitations/#update-a-repository-invitation
  65. func (s *RepositoriesService) UpdateInvitation(ctx context.Context, owner, repo string, invitationID int64, permissions string) (*RepositoryInvitation, *Response, error) {
  66. opts := &struct {
  67. Permissions string `json:"permissions"`
  68. }{Permissions: permissions}
  69. u := fmt.Sprintf("repos/%v/%v/invitations/%v", owner, repo, invitationID)
  70. req, err := s.client.NewRequest("PATCH", u, opts)
  71. if err != nil {
  72. return nil, nil, err
  73. }
  74. // TODO: remove custom Accept header when this API fully launches.
  75. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  76. invite := &RepositoryInvitation{}
  77. resp, err := s.client.Do(ctx, req, invite)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return invite, resp, nil
  82. }