orgs.go 6.6 KB

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