1// Copyright (c) 2015 Ableton AG, Berlin. 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 travis
7
8import (
9	"context"
10	"fmt"
11	"net/http"
12)
13
14// OrganizationsService handles communication with the
15// organization related methods of the Travis CI API.
16type OrganizationsService struct {
17	client *Client
18}
19
20// Organization is a standard representation of an individual organization
21//
22// Travis CI API docs: https://developer.travis-ci.com/resource/organization#standard-representation
23type Organization struct {
24	// Value uniquely identifying the organization
25	Id uint `json:"id,omitempty"`
26	// Login set on GitHub
27	Login string `json:"login,omitempty"`
28	// Name set on GitHub
29	Name string `json:"name,omitempty"`
30	// Id set on GitHub
31	GithubId uint `json:"github_id,omitempty"`
32	// Avatar_url set on GitHub
33	AvatarUrl string `json:"avatar_url,omitempty"`
34	// Whether or not the organization has an education account
35	Education bool `json:"education,omitempty"`
36	// Repositories belonging to this organization.
37	Repositories []*Repository `json:"repositories,omitempty"`
38	// Installation belonging to the organization
39	Installation *Installation `json:"installation,omitempty"`
40	*Metadata
41}
42
43// OrganizationOption specifies the optional parameters for organization endpoint
44type OrganizationOption struct {
45	// List of attributes to eager load
46	Include []string `url:"include,omitempty,comma"`
47}
48
49// OrganizationsOption specifies the optional parameters for organizations endpoint
50type OrganizationsOption struct {
51	// How many organizations to include in the response
52	Limit int `url:"limit,omitempty"`
53	// How many organizations to skip before the first entry in the response
54	Offset int `url:"offset,omitempty"`
55	// Attributes to sort organizations by
56	SortBy string `url:"sort_by,omitempty"`
57	// List of attributes to eager load
58	Include []string `url:"include,omitempty,comma"`
59}
60
61// organizationsResponse represents a response
62// from organizations endpoints
63type organizationsResponse struct {
64	Organizations []*Organization `json:"organizations,omitempty"`
65}
66
67// Find fetches an organization with the given id
68//
69// Travis CI API docs: https://developer.travis-ci.com/resource/organization#find
70func (os *OrganizationsService) Find(ctx context.Context, id uint, opt *OrganizationOption) (*Organization, *http.Response, error) {
71	u, err := urlWithOptions(fmt.Sprintf("/org/%d", id), opt)
72	if err != nil {
73		return nil, nil, err
74	}
75
76	req, err := os.client.NewRequest(http.MethodGet, u, nil, nil)
77	if err != nil {
78		return nil, nil, err
79	}
80
81	var org Organization
82	resp, err := os.client.Do(ctx, req, &org)
83	if err != nil {
84		return nil, resp, err
85	}
86
87	return &org, resp, err
88}
89
90// List fetches a list of organizations the current user is a member of
91//
92// Travis CI API docs: https://developer.travis-ci.com/resource/organizations#for_current_user
93func (os *OrganizationsService) List(ctx context.Context, opt *OrganizationsOption) ([]*Organization, *http.Response, error) {
94	u, err := urlWithOptions("/orgs", opt)
95	if err != nil {
96		return nil, nil, err
97	}
98
99	req, err := os.client.NewRequest(http.MethodGet, u, nil, nil)
100	if err != nil {
101		return nil, nil, err
102	}
103
104	var or organizationsResponse
105	resp, err := os.client.Do(ctx, req, &or)
106	if err != nil {
107		return nil, resp, err
108	}
109
110	return or.Organizations, resp, err
111}
112