github.com/google/go-github/v42@v42.0.0/github/orgs.go (about) 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 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "time" 12 ) 13 14 // OrganizationsService provides access to the organization related functions 15 // in the GitHub API. 16 // 17 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/ 18 type OrganizationsService service 19 20 // Organization represents a GitHub organization account. 21 type Organization struct { 22 Login *string `json:"login,omitempty"` 23 ID *int64 `json:"id,omitempty"` 24 NodeID *string `json:"node_id,omitempty"` 25 AvatarURL *string `json:"avatar_url,omitempty"` 26 HTMLURL *string `json:"html_url,omitempty"` 27 Name *string `json:"name,omitempty"` 28 Company *string `json:"company,omitempty"` 29 Blog *string `json:"blog,omitempty"` 30 Location *string `json:"location,omitempty"` 31 Email *string `json:"email,omitempty"` 32 TwitterUsername *string `json:"twitter_username,omitempty"` 33 Description *string `json:"description,omitempty"` 34 PublicRepos *int `json:"public_repos,omitempty"` 35 PublicGists *int `json:"public_gists,omitempty"` 36 Followers *int `json:"followers,omitempty"` 37 Following *int `json:"following,omitempty"` 38 CreatedAt *time.Time `json:"created_at,omitempty"` 39 UpdatedAt *time.Time `json:"updated_at,omitempty"` 40 TotalPrivateRepos *int `json:"total_private_repos,omitempty"` 41 OwnedPrivateRepos *int `json:"owned_private_repos,omitempty"` 42 PrivateGists *int `json:"private_gists,omitempty"` 43 DiskUsage *int `json:"disk_usage,omitempty"` 44 Collaborators *int `json:"collaborators,omitempty"` 45 BillingEmail *string `json:"billing_email,omitempty"` 46 Type *string `json:"type,omitempty"` 47 Plan *Plan `json:"plan,omitempty"` 48 TwoFactorRequirementEnabled *bool `json:"two_factor_requirement_enabled,omitempty"` 49 IsVerified *bool `json:"is_verified,omitempty"` 50 HasOrganizationProjects *bool `json:"has_organization_projects,omitempty"` 51 HasRepositoryProjects *bool `json:"has_repository_projects,omitempty"` 52 53 // DefaultRepoPermission can be one of: "read", "write", "admin", or "none". (Default: "read"). 54 // It is only used in OrganizationsService.Edit. 55 DefaultRepoPermission *string `json:"default_repository_permission,omitempty"` 56 // DefaultRepoSettings can be one of: "read", "write", "admin", or "none". (Default: "read"). 57 // It is only used in OrganizationsService.Get. 58 DefaultRepoSettings *string `json:"default_repository_settings,omitempty"` 59 60 // MembersCanCreateRepos default value is true and is only used in Organizations.Edit. 61 MembersCanCreateRepos *bool `json:"members_can_create_repositories,omitempty"` 62 63 // https://developer.github.com/changes/2019-12-03-internal-visibility-changes/#rest-v3-api 64 MembersCanCreatePublicRepos *bool `json:"members_can_create_public_repositories,omitempty"` 65 MembersCanCreatePrivateRepos *bool `json:"members_can_create_private_repositories,omitempty"` 66 MembersCanCreateInternalRepos *bool `json:"members_can_create_internal_repositories,omitempty"` 67 68 // MembersAllowedRepositoryCreationType denotes if organization members can create repositories 69 // and the type of repositories they can create. Possible values are: "all", "private", or "none". 70 // 71 // Deprecated: Use MembersCanCreatePublicRepos, MembersCanCreatePrivateRepos, MembersCanCreateInternalRepos 72 // instead. The new fields overrides the existing MembersAllowedRepositoryCreationType during 'edit' 73 // operation and does not consider 'internal' repositories during 'get' operation 74 MembersAllowedRepositoryCreationType *string `json:"members_allowed_repository_creation_type,omitempty"` 75 76 // MembersCanCreatePages toggles whether organization members can create GitHub Pages sites. 77 MembersCanCreatePages *bool `json:"members_can_create_pages,omitempty"` 78 // MembersCanCreatePublicPages toggles whether organization members can create public GitHub Pages sites. 79 MembersCanCreatePublicPages *bool `json:"members_can_create_public_pages,omitempty"` 80 // MembersCanCreatePrivatePages toggles whether organization members can create private GitHub Pages sites. 81 MembersCanCreatePrivatePages *bool `json:"members_can_create_private_pages,omitempty"` 82 83 // API URLs 84 URL *string `json:"url,omitempty"` 85 EventsURL *string `json:"events_url,omitempty"` 86 HooksURL *string `json:"hooks_url,omitempty"` 87 IssuesURL *string `json:"issues_url,omitempty"` 88 MembersURL *string `json:"members_url,omitempty"` 89 PublicMembersURL *string `json:"public_members_url,omitempty"` 90 ReposURL *string `json:"repos_url,omitempty"` 91 } 92 93 // OrganizationInstallations represents GitHub app installations for an organization. 94 type OrganizationInstallations struct { 95 TotalCount *int `json:"total_count,omitempty"` 96 Installations []*Installation `json:"installations,omitempty"` 97 } 98 99 func (o Organization) String() string { 100 return Stringify(o) 101 } 102 103 // Plan represents the payment plan for an account. See plans at https://github.com/plans. 104 type Plan struct { 105 Name *string `json:"name,omitempty"` 106 Space *int `json:"space,omitempty"` 107 Collaborators *int `json:"collaborators,omitempty"` 108 PrivateRepos *int `json:"private_repos,omitempty"` 109 FilledSeats *int `json:"filled_seats,omitempty"` 110 Seats *int `json:"seats,omitempty"` 111 } 112 113 func (p Plan) String() string { 114 return Stringify(p) 115 } 116 117 // OrganizationsListOptions specifies the optional parameters to the 118 // OrganizationsService.ListAll method. 119 type OrganizationsListOptions struct { 120 // Since filters Organizations by ID. 121 Since int64 `url:"since,omitempty"` 122 123 // Note: Pagination is powered exclusively by the Since parameter, 124 // ListOptions.Page has no effect. 125 // ListOptions.PerPage controls an undocumented GitHub API parameter. 126 ListOptions 127 } 128 129 // ListAll lists all organizations, in the order that they were created on GitHub. 130 // 131 // Note: Pagination is powered exclusively by the since parameter. To continue 132 // listing the next set of organizations, use the ID of the last-returned organization 133 // as the opts.Since parameter for the next call. 134 // 135 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organizations 136 func (s *OrganizationsService) ListAll(ctx context.Context, opts *OrganizationsListOptions) ([]*Organization, *Response, error) { 137 u, err := addOptions("organizations", opts) 138 if err != nil { 139 return nil, nil, err 140 } 141 142 req, err := s.client.NewRequest("GET", u, nil) 143 if err != nil { 144 return nil, nil, err 145 } 146 147 orgs := []*Organization{} 148 resp, err := s.client.Do(ctx, req, &orgs) 149 if err != nil { 150 return nil, resp, err 151 } 152 return orgs, resp, nil 153 } 154 155 // List the organizations for a user. Passing the empty string will list 156 // organizations for the authenticated user. 157 // 158 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organizations-for-the-authenticated-user 159 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-organizations-for-a-user 160 func (s *OrganizationsService) List(ctx context.Context, user string, opts *ListOptions) ([]*Organization, *Response, error) { 161 var u string 162 if user != "" { 163 u = fmt.Sprintf("users/%v/orgs", user) 164 } else { 165 u = "user/orgs" 166 } 167 u, err := addOptions(u, opts) 168 if err != nil { 169 return nil, nil, err 170 } 171 172 req, err := s.client.NewRequest("GET", u, nil) 173 if err != nil { 174 return nil, nil, err 175 } 176 177 var orgs []*Organization 178 resp, err := s.client.Do(ctx, req, &orgs) 179 if err != nil { 180 return nil, resp, err 181 } 182 183 return orgs, resp, nil 184 } 185 186 // Get fetches an organization by name. 187 // 188 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#get-an-organization 189 func (s *OrganizationsService) Get(ctx context.Context, org string) (*Organization, *Response, error) { 190 u := fmt.Sprintf("orgs/%v", org) 191 req, err := s.client.NewRequest("GET", u, nil) 192 if err != nil { 193 return nil, nil, err 194 } 195 196 // TODO: remove custom Accept header when this API fully launches. 197 req.Header.Set("Accept", mediaTypeMemberAllowedRepoCreationTypePreview) 198 199 organization := new(Organization) 200 resp, err := s.client.Do(ctx, req, organization) 201 if err != nil { 202 return nil, resp, err 203 } 204 205 return organization, resp, nil 206 } 207 208 // GetByID fetches an organization. 209 // 210 // Note: GetByID uses the undocumented GitHub API endpoint /organizations/:id. 211 func (s *OrganizationsService) GetByID(ctx context.Context, id int64) (*Organization, *Response, error) { 212 u := fmt.Sprintf("organizations/%d", id) 213 req, err := s.client.NewRequest("GET", u, nil) 214 if err != nil { 215 return nil, nil, err 216 } 217 218 organization := new(Organization) 219 resp, err := s.client.Do(ctx, req, organization) 220 if err != nil { 221 return nil, resp, err 222 } 223 224 return organization, resp, nil 225 } 226 227 // Edit an organization. 228 // 229 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#update-an-organization 230 func (s *OrganizationsService) Edit(ctx context.Context, name string, org *Organization) (*Organization, *Response, error) { 231 u := fmt.Sprintf("orgs/%v", name) 232 req, err := s.client.NewRequest("PATCH", u, org) 233 if err != nil { 234 return nil, nil, err 235 } 236 237 // TODO: remove custom Accept header when this API fully launches. 238 req.Header.Set("Accept", mediaTypeMemberAllowedRepoCreationTypePreview) 239 240 o := new(Organization) 241 resp, err := s.client.Do(ctx, req, o) 242 if err != nil { 243 return nil, resp, err 244 } 245 246 return o, resp, nil 247 } 248 249 // ListInstallations lists installations for an organization. 250 // 251 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs/#list-app-installations-for-an-organization 252 func (s *OrganizationsService) ListInstallations(ctx context.Context, org string, opts *ListOptions) (*OrganizationInstallations, *Response, error) { 253 u := fmt.Sprintf("orgs/%v/installations", org) 254 255 u, err := addOptions(u, opts) 256 if err != nil { 257 return nil, nil, err 258 } 259 260 req, err := s.client.NewRequest("GET", u, nil) 261 if err != nil { 262 return nil, nil, err 263 } 264 265 result := new(OrganizationInstallations) 266 resp, err := s.client.Do(ctx, req, result) 267 if err != nil { 268 return nil, resp, err 269 } 270 271 return result, resp, nil 272 }