orgs.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 int64 `url:"since,omitempty"`
  70. // Note: Pagination is powered exclusively by the Since parameter,
  71. // ListOptions.Page has no effect.
  72. // ListOptions.PerPage controls an undocumented GitHub API parameter.
  73. ListOptions
  74. }
  75. // ListAll lists all organizations, in the order that they were created on GitHub.
  76. //
  77. // Note: Pagination is powered exclusively by the since parameter. To continue
  78. // listing the next set of organizations, use the ID of the last-returned organization
  79. // as the opts.Since parameter for the next call.
  80. //
  81. // GitHub API docs: https://developer.github.com/v3/orgs/#list-all-organizations
  82. func (s *OrganizationsService) ListAll(ctx context.Context, opt *OrganizationsListOptions) ([]*Organization, *Response, error) {
  83. u, err := addOptions("organizations", opt)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. req, err := s.client.NewRequest("GET", u, nil)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. // TODO: remove custom Accept header when this API fully launches.
  92. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  93. orgs := []*Organization{}
  94. resp, err := s.client.Do(ctx, req, &orgs)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return orgs, resp, nil
  99. }
  100. // List the organizations for a user. Passing the empty string will list
  101. // organizations for the authenticated user.
  102. //
  103. // GitHub API docs: https://developer.github.com/v3/orgs/#list-user-organizations
  104. func (s *OrganizationsService) List(ctx context.Context, user string, opt *ListOptions) ([]*Organization, *Response, error) {
  105. var u string
  106. if user != "" {
  107. u = fmt.Sprintf("users/%v/orgs", user)
  108. } else {
  109. u = "user/orgs"
  110. }
  111. u, err := addOptions(u, opt)
  112. if err != nil {
  113. return nil, nil, err
  114. }
  115. req, err := s.client.NewRequest("GET", u, nil)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. // TODO: remove custom Accept header when this API fully launches.
  120. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  121. var orgs []*Organization
  122. resp, err := s.client.Do(ctx, req, &orgs)
  123. if err != nil {
  124. return nil, resp, err
  125. }
  126. return orgs, resp, nil
  127. }
  128. // Get fetches an organization by name.
  129. //
  130. // GitHub API docs: https://developer.github.com/v3/orgs/#get-an-organization
  131. func (s *OrganizationsService) Get(ctx context.Context, org string) (*Organization, *Response, error) {
  132. u := fmt.Sprintf("orgs/%v", org)
  133. req, err := s.client.NewRequest("GET", u, nil)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. // TODO: remove custom Accept header when this API fully launches.
  138. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  139. organization := new(Organization)
  140. resp, err := s.client.Do(ctx, req, organization)
  141. if err != nil {
  142. return nil, resp, err
  143. }
  144. return organization, resp, nil
  145. }
  146. // GetByID fetches an organization.
  147. //
  148. // Note: GetByID uses the undocumented GitHub API endpoint /organizations/:id.
  149. func (s *OrganizationsService) GetByID(ctx context.Context, id int64) (*Organization, *Response, error) {
  150. u := fmt.Sprintf("organizations/%d", id)
  151. req, err := s.client.NewRequest("GET", u, nil)
  152. if err != nil {
  153. return nil, nil, err
  154. }
  155. // TODO: remove custom Accept header when this API fully launches.
  156. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  157. organization := new(Organization)
  158. resp, err := s.client.Do(ctx, req, organization)
  159. if err != nil {
  160. return nil, resp, err
  161. }
  162. return organization, resp, nil
  163. }
  164. // Edit an organization.
  165. //
  166. // GitHub API docs: https://developer.github.com/v3/orgs/#edit-an-organization
  167. func (s *OrganizationsService) Edit(ctx context.Context, name string, org *Organization) (*Organization, *Response, error) {
  168. u := fmt.Sprintf("orgs/%v", name)
  169. req, err := s.client.NewRequest("PATCH", u, org)
  170. if err != nil {
  171. return nil, nil, err
  172. }
  173. // TODO: remove custom Accept header when this API fully launches.
  174. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  175. o := new(Organization)
  176. resp, err := s.client.Do(ctx, req, o)
  177. if err != nil {
  178. return nil, resp, err
  179. }
  180. return o, resp, nil
  181. }