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.

orgs.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "time"
  10. )
  11. // OrganizationsService provides access to the organization related functions
  12. // in the GitHub API.
  13. //
  14. // GitHub API docs: https://developer.github.com/v3/orgs/
  15. type OrganizationsService service
  16. // Organization represents a GitHub organization account.
  17. type Organization struct {
  18. Login *string `json:"login,omitempty"`
  19. ID *int64 `json:"id,omitempty"`
  20. NodeID *string `json:"node_id,omitempty"`
  21. AvatarURL *string `json:"avatar_url,omitempty"`
  22. HTMLURL *string `json:"html_url,omitempty"`
  23. Name *string `json:"name,omitempty"`
  24. Company *string `json:"company,omitempty"`
  25. Blog *string `json:"blog,omitempty"`
  26. Location *string `json:"location,omitempty"`
  27. Email *string `json:"email,omitempty"`
  28. Description *string `json:"description,omitempty"`
  29. PublicRepos *int `json:"public_repos,omitempty"`
  30. PublicGists *int `json:"public_gists,omitempty"`
  31. Followers *int `json:"followers,omitempty"`
  32. Following *int `json:"following,omitempty"`
  33. CreatedAt *time.Time `json:"created_at,omitempty"`
  34. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  35. TotalPrivateRepos *int `json:"total_private_repos,omitempty"`
  36. OwnedPrivateRepos *int `json:"owned_private_repos,omitempty"`
  37. PrivateGists *int `json:"private_gists,omitempty"`
  38. DiskUsage *int `json:"disk_usage,omitempty"`
  39. Collaborators *int `json:"collaborators,omitempty"`
  40. BillingEmail *string `json:"billing_email,omitempty"`
  41. Type *string `json:"type,omitempty"`
  42. Plan *Plan `json:"plan,omitempty"`
  43. TwoFactorRequirementEnabled *bool `json:"two_factor_requirement_enabled,omitempty"`
  44. // DefaultRepoPermission can be one of: "read", "write", "admin", or "none". (Default: "read").
  45. // It is only used in OrganizationsService.Edit.
  46. DefaultRepoPermission *string `json:"default_repository_permission,omitempty"`
  47. // DefaultRepoSettings can be one of: "read", "write", "admin", or "none". (Default: "read").
  48. // It is only used in OrganizationsService.Get.
  49. DefaultRepoSettings *string `json:"default_repository_settings,omitempty"`
  50. // MembersCanCreateRepos default value is true and is only used in Organizations.Edit.
  51. MembersCanCreateRepos *bool `json:"members_can_create_repositories,omitempty"`
  52. // API URLs
  53. URL *string `json:"url,omitempty"`
  54. EventsURL *string `json:"events_url,omitempty"`
  55. HooksURL *string `json:"hooks_url,omitempty"`
  56. IssuesURL *string `json:"issues_url,omitempty"`
  57. MembersURL *string `json:"members_url,omitempty"`
  58. PublicMembersURL *string `json:"public_members_url,omitempty"`
  59. ReposURL *string `json:"repos_url,omitempty"`
  60. }
  61. func (o Organization) String() string {
  62. return Stringify(o)
  63. }
  64. // Plan represents the payment plan for an account. See plans at https://github.com/plans.
  65. type Plan struct {
  66. Name *string `json:"name,omitempty"`
  67. Space *int `json:"space,omitempty"`
  68. Collaborators *int `json:"collaborators,omitempty"`
  69. PrivateRepos *int `json:"private_repos,omitempty"`
  70. }
  71. func (p Plan) String() string {
  72. return Stringify(p)
  73. }
  74. // OrganizationsListOptions specifies the optional parameters to the
  75. // OrganizationsService.ListAll method.
  76. type OrganizationsListOptions struct {
  77. // Since filters Organizations by ID.
  78. Since int64 `url:"since,omitempty"`
  79. // Note: Pagination is powered exclusively by the Since parameter,
  80. // ListOptions.Page has no effect.
  81. // ListOptions.PerPage controls an undocumented GitHub API parameter.
  82. ListOptions
  83. }
  84. // ListAll lists all organizations, in the order that they were created on GitHub.
  85. //
  86. // Note: Pagination is powered exclusively by the since parameter. To continue
  87. // listing the next set of organizations, use the ID of the last-returned organization
  88. // as the opts.Since parameter for the next call.
  89. //
  90. // GitHub API docs: https://developer.github.com/v3/orgs/#list-all-organizations
  91. func (s *OrganizationsService) ListAll(ctx context.Context, opt *OrganizationsListOptions) ([]*Organization, *Response, error) {
  92. u, err := addOptions("organizations", opt)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. req, err := s.client.NewRequest("GET", u, nil)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. orgs := []*Organization{}
  101. resp, err := s.client.Do(ctx, req, &orgs)
  102. if err != nil {
  103. return nil, resp, err
  104. }
  105. return orgs, resp, nil
  106. }
  107. // List the organizations for a user. Passing the empty string will list
  108. // organizations for the authenticated user.
  109. //
  110. // GitHub API docs: https://developer.github.com/v3/orgs/#list-user-organizations
  111. func (s *OrganizationsService) List(ctx context.Context, user string, opt *ListOptions) ([]*Organization, *Response, error) {
  112. var u string
  113. if user != "" {
  114. u = fmt.Sprintf("users/%v/orgs", user)
  115. } else {
  116. u = "user/orgs"
  117. }
  118. u, err := addOptions(u, opt)
  119. if err != nil {
  120. return nil, nil, err
  121. }
  122. req, err := s.client.NewRequest("GET", u, nil)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. var orgs []*Organization
  127. resp, err := s.client.Do(ctx, req, &orgs)
  128. if err != nil {
  129. return nil, resp, err
  130. }
  131. return orgs, resp, nil
  132. }
  133. // Get fetches an organization by name.
  134. //
  135. // GitHub API docs: https://developer.github.com/v3/orgs/#get-an-organization
  136. func (s *OrganizationsService) Get(ctx context.Context, org string) (*Organization, *Response, error) {
  137. u := fmt.Sprintf("orgs/%v", org)
  138. req, err := s.client.NewRequest("GET", u, nil)
  139. if err != nil {
  140. return nil, nil, err
  141. }
  142. organization := new(Organization)
  143. resp, err := s.client.Do(ctx, req, organization)
  144. if err != nil {
  145. return nil, resp, err
  146. }
  147. return organization, resp, nil
  148. }
  149. // GetByID fetches an organization.
  150. //
  151. // Note: GetByID uses the undocumented GitHub API endpoint /organizations/:id.
  152. func (s *OrganizationsService) GetByID(ctx context.Context, id int64) (*Organization, *Response, error) {
  153. u := fmt.Sprintf("organizations/%d", id)
  154. req, err := s.client.NewRequest("GET", u, nil)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. organization := new(Organization)
  159. resp, err := s.client.Do(ctx, req, organization)
  160. if err != nil {
  161. return nil, resp, err
  162. }
  163. return organization, resp, nil
  164. }
  165. // Edit an organization.
  166. //
  167. // GitHub API docs: https://developer.github.com/v3/orgs/#edit-an-organization
  168. func (s *OrganizationsService) Edit(ctx context.Context, name string, org *Organization) (*Organization, *Response, error) {
  169. u := fmt.Sprintf("orgs/%v", name)
  170. req, err := s.client.NewRequest("PATCH", u, org)
  171. if err != nil {
  172. return nil, nil, err
  173. }
  174. o := new(Organization)
  175. resp, err := s.client.Do(ctx, req, o)
  176. if err != nil {
  177. return nil, resp, err
  178. }
  179. return o, resp, nil
  180. }