repos_collaborators.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2013 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. // ListCollaboratorsOptions specifies the optional parameters to the
  11. // RepositoriesService.ListCollaborators method.
  12. type ListCollaboratorsOptions struct {
  13. // Affiliation specifies how collaborators should be filtered by their affiliation.
  14. // Possible values are:
  15. // outside - All outside collaborators of an organization-owned repository
  16. // direct - All collaborators with permissions to an organization-owned repository,
  17. // regardless of organization membership status
  18. // all - All collaborators the authenticated user can see
  19. //
  20. // Default value is "all".
  21. Affiliation string `url:"affiliation,omitempty"`
  22. ListOptions
  23. }
  24. // ListCollaborators lists the GitHub users that have access to the repository.
  25. //
  26. // GitHub API docs: https://developer.github.com/v3/repos/collaborators/#list-collaborators
  27. func (s *RepositoriesService) ListCollaborators(ctx context.Context, owner, repo string, opt *ListCollaboratorsOptions) ([]*User, *Response, error) {
  28. u := fmt.Sprintf("repos/%v/%v/collaborators", owner, repo)
  29. u, err := addOptions(u, opt)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. req, err := s.client.NewRequest("GET", u, nil)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. req.Header.Set("Accept", mediaTypeNestedTeamsPreview)
  38. var users []*User
  39. resp, err := s.client.Do(ctx, req, &users)
  40. if err != nil {
  41. return nil, resp, err
  42. }
  43. return users, resp, nil
  44. }
  45. // IsCollaborator checks whether the specified GitHub user has collaborator
  46. // access to the given repo.
  47. // Note: This will return false if the user is not a collaborator OR the user
  48. // is not a GitHub user.
  49. //
  50. // GitHub API docs: https://developer.github.com/v3/repos/collaborators/#get
  51. func (s *RepositoriesService) IsCollaborator(ctx context.Context, owner, repo, user string) (bool, *Response, error) {
  52. u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
  53. req, err := s.client.NewRequest("GET", u, nil)
  54. if err != nil {
  55. return false, nil, err
  56. }
  57. resp, err := s.client.Do(ctx, req, nil)
  58. isCollab, err := parseBoolResponse(err)
  59. return isCollab, resp, err
  60. }
  61. // RepositoryPermissionLevel represents the permission level an organization
  62. // member has for a given repository.
  63. type RepositoryPermissionLevel struct {
  64. // Possible values: "admin", "write", "read", "none"
  65. Permission *string `json:"permission,omitempty"`
  66. User *User `json:"user,omitempty"`
  67. }
  68. // GetPermissionLevel retrieves the specific permission level a collaborator has for a given repository.
  69. // GitHub API docs: https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
  70. func (s *RepositoriesService) GetPermissionLevel(ctx context.Context, owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) {
  71. u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user)
  72. req, err := s.client.NewRequest("GET", u, nil)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. rpl := new(RepositoryPermissionLevel)
  77. resp, err := s.client.Do(ctx, req, rpl)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return rpl, resp, nil
  82. }
  83. // RepositoryAddCollaboratorOptions specifies the optional parameters to the
  84. // RepositoriesService.AddCollaborator method.
  85. type RepositoryAddCollaboratorOptions struct {
  86. // Permission specifies the permission to grant the user on this repository.
  87. // Possible values are:
  88. // pull - team members can pull, but not push to or administer this repository
  89. // push - team members can pull and push, but not administer this repository
  90. // admin - team members can pull, push and administer this repository
  91. //
  92. // Default value is "push". This option is only valid for organization-owned repositories.
  93. Permission string `json:"permission,omitempty"`
  94. }
  95. // AddCollaborator sends an invitation to the specified GitHub user
  96. // to become a collaborator to the given repo.
  97. //
  98. // GitHub API docs: https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator
  99. func (s *RepositoriesService) AddCollaborator(ctx context.Context, owner, repo, user string, opt *RepositoryAddCollaboratorOptions) (*Response, error) {
  100. u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
  101. req, err := s.client.NewRequest("PUT", u, opt)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // TODO: remove custom Accept header when this API fully launches.
  106. req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview)
  107. return s.client.Do(ctx, req, nil)
  108. }
  109. // RemoveCollaborator removes the specified GitHub user as collaborator from the given repo.
  110. // Note: Does not return error if a valid user that is not a collaborator is removed.
  111. //
  112. // GitHub API docs: https://developer.github.com/v3/repos/collaborators/#remove-collaborator
  113. func (s *RepositoriesService) RemoveCollaborator(ctx context.Context, owner, repo, user string) (*Response, error) {
  114. u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
  115. req, err := s.client.NewRequest("DELETE", u, nil)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return s.client.Do(ctx, req, nil)
  120. }