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	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	Description       *string    `json:"description,omitempty"`
33	PublicRepos       *int       `json:"public_repos,omitempty"`
34	PublicGists       *int       `json:"public_gists,omitempty"`
35	Followers         *int       `json:"followers,omitempty"`
36	Following         *int       `json:"following,omitempty"`
37	CreatedAt         *time.Time `json:"created_at,omitempty"`
38	UpdatedAt         *time.Time `json:"updated_at,omitempty"`
39	TotalPrivateRepos *int       `json:"total_private_repos,omitempty"`
40	OwnedPrivateRepos *int       `json:"owned_private_repos,omitempty"`
41	PrivateGists      *int       `json:"private_gists,omitempty"`
42	DiskUsage         *int       `json:"disk_usage,omitempty"`
43	Collaborators     *int       `json:"collaborators,omitempty"`
44	BillingEmail      *string    `json:"billing_email,omitempty"`
45	Type              *string    `json:"type,omitempty"`
46	Plan              *Plan      `json:"plan,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 int64 `url:"since,omitempty"`
79
80	// Note: Pagination is powered exclusively by the Since parameter,
81	// ListOptions.Page has no effect.
82	// ListOptions.PerPage controls an undocumented GitHub API parameter.
83	ListOptions
84}
85
86// ListAll lists all organizations, in the order that they were created on GitHub.
87//
88// Note: Pagination is powered exclusively by the since parameter. To continue
89// listing the next set of organizations, use the ID of the last-returned organization
90// as the opts.Since parameter for the next call.
91//
92// GitHub API docs: https://developer.github.com/v3/orgs/#list-all-organizations
93func (s *OrganizationsService) ListAll(ctx context.Context, opt *OrganizationsListOptions) ([]*Organization, *Response, error) {
94	u, err := addOptions("organizations", opt)
95	if err != nil {
96		return nil, nil, err
97	}
98
99	req, err := s.client.NewRequest("GET", u, nil)
100	if err != nil {
101		return nil, nil, err
102	}
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	var orgs []*Organization
134	resp, err := s.client.Do(ctx, req, &orgs)
135	if err != nil {
136		return nil, resp, err
137	}
138
139	return orgs, resp, nil
140}
141
142// Get fetches an organization by name.
143//
144// GitHub API docs: https://developer.github.com/v3/orgs/#get-an-organization
145func (s *OrganizationsService) Get(ctx context.Context, org string) (*Organization, *Response, error) {
146	u := fmt.Sprintf("orgs/%v", org)
147	req, err := s.client.NewRequest("GET", u, nil)
148	if err != nil {
149		return nil, nil, err
150	}
151
152	organization := new(Organization)
153	resp, err := s.client.Do(ctx, req, organization)
154	if err != nil {
155		return nil, resp, err
156	}
157
158	return organization, resp, nil
159}
160
161// GetByID fetches an organization.
162//
163// Note: GetByID uses the undocumented GitHub API endpoint /organizations/:id.
164func (s *OrganizationsService) GetByID(ctx context.Context, id int64) (*Organization, *Response, error) {
165	u := fmt.Sprintf("organizations/%d", id)
166	req, err := s.client.NewRequest("GET", u, nil)
167	if err != nil {
168		return nil, nil, err
169	}
170
171	organization := new(Organization)
172	resp, err := s.client.Do(ctx, req, organization)
173	if err != nil {
174		return nil, resp, err
175	}
176
177	return organization, resp, nil
178}
179
180// Edit an organization.
181//
182// GitHub API docs: https://developer.github.com/v3/orgs/#edit-an-organization
183func (s *OrganizationsService) Edit(ctx context.Context, name string, org *Organization) (*Organization, *Response, error) {
184	u := fmt.Sprintf("orgs/%v", name)
185	req, err := s.client.NewRequest("PATCH", u, org)
186	if err != nil {
187		return nil, nil, err
188	}
189
190	o := new(Organization)
191	resp, err := s.client.Do(ctx, req, o)
192	if err != nil {
193		return nil, resp, err
194	}
195
196	return o, resp, nil
197}
198