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