forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
org.go
214 lines (191 loc) · 6.09 KB
/
org.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/jenkins-x/go-scm/scm"
)
type organizationService struct {
client *wrapper
}
type plan struct {
Name string `json:"name"`
PrivateRepos int `json:"private_repos"`
}
type organization struct {
ID int `json:"id,omitempty"`
Login string `json:"login"`
Avatar string `json:"avatar_url"`
MembersCreatePublic bool `json:"members_can_create_public_repositories"`
MembersCreatePrivate bool `json:"members_can_create_private_repositories"`
MembersCreateInternal bool `json:"members_can_create_internal_repositories"`
Plan plan
}
type team struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description,omitempty"`
Privacy string `json:"privacy,omitempty"`
Parent *team `json:"parent,omitempty"` // Only present in responses
ParentTeamID *int `json:"parent_team_id,omitempty"` // Only valid in creates/edits
}
type teamMember struct {
Login string `json:"login"`
}
type membership struct {
Role string `json:"role"`
}
func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/members/%s", org, user)
res, err := s.client.do(ctx, "GET", path, nil, nil)
if err != nil && res == nil {
return false, res, err
}
code := res.Status
if code == 204 {
return true, res, nil
} else if code == 404 {
return false, res, nil
} else if code == 302 {
return false, res, fmt.Errorf("requester is not %s org member", org)
}
// Should be unreachable.
return false, res, fmt.Errorf("unexpected status: %d", code)
}
func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/memberships/%s", org, user)
out := membership{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
if err != nil {
return false, res, err
}
isAdmin := out.Role == "admin"
return isAdmin, res, nil
}
func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s", name)
out := new(organization)
req := &scm.Request{
Method: http.MethodGet,
Path: path,
Header: map[string][]string{
// This accept header adds member create repo permissions
"Accept": {"application/vnd.github.surtur-preview+json"},
},
}
res, err := s.client.doRequest(ctx, req, nil, out)
return convertOrganization(out), res, err
}
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("user/orgs?%s", encodeListOptions(opts))
out := []*organization{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertOrganizationList(out), res, err
}
func (s *organizationService) ListTeams(ctx context.Context, org string, opts scm.ListOptions) ([]*scm.Team, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/teams?%s", org, encodeListOptions(opts))
out := []*team{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertTeams(out), res, err
}
func (s *organizationService) ListOrgMembers(ctx context.Context, org string, ops scm.ListOptions) ([]*scm.TeamMember, *scm.Response, error) {
params := encodeListOptions(ops)
req := &scm.Request{
Method: http.MethodGet,
Path: fmt.Sprintf("orgs/%s/members?%s", org, params),
Header: map[string][]string{
// This accept header enables the nested teams preview.
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
"Accept": {"application/vnd.github.hellcat-preview+json"},
},
}
out := []*teamMember{}
res, err := s.client.doRequest(ctx, req, nil, &out)
return convertTeamMembers(out), res, err
}
func (s *organizationService) ListTeamMembers(ctx context.Context, id int, role string, opts scm.ListOptions) ([]*scm.TeamMember, *scm.Response, error) {
params := encodeListOptionsWith(opts, url.Values{
"role": []string{role},
})
req := &scm.Request{
Method: http.MethodGet,
Path: fmt.Sprintf("teams/%d/members?%s", id, params),
Header: map[string][]string{
// This accept header enables the nested teams preview.
// https://developer.github.com/changes/2017-08-30-preview-nested-teams/
"Accept": {"application/vnd.github.hellcat-preview+json"},
},
}
out := []*teamMember{}
res, err := s.client.doRequest(ctx, req, nil, &out)
return convertTeamMembers(out), res, err
}
func convertOrganizationList(from []*organization) []*scm.Organization {
to := []*scm.Organization{}
for _, v := range from {
to = append(to, convertOrganization(v))
}
return to
}
func convertOrganization(from *organization) *scm.Organization {
return &scm.Organization{
ID: from.ID,
Name: from.Login,
Avatar: from.Avatar,
Permissions: scm.Permissions{
MembersCreateInternal: from.MembersCreateInternal,
MembersCreatePublic: from.MembersCreatePublic,
MembersCreatePrivate: from.MembersCreatePrivate,
},
}
}
func convertTeams(from []*team) []*scm.Team {
to := []*scm.Team{}
for _, v := range from {
team := convertTeam(v)
if team != nil {
to = append(to, team)
}
}
return to
}
func convertTeam(from *team) *scm.Team {
if from == nil {
return nil
}
to := &scm.Team{
Description: from.Description,
ID: from.ID,
Name: from.Name,
Parent: convertTeam(from.Parent),
Privacy: from.Privacy,
Slug: from.Slug,
}
if from.ParentTeamID != nil {
to.ParentTeamID = *from.ParentTeamID
}
return to
}
func convertTeamMembers(from []*teamMember) []*scm.TeamMember {
to := []*scm.TeamMember{}
for _, v := range from {
member := convertTeamMember(v)
if member != nil {
to = append(to, member)
}
}
return to
}
func convertTeamMember(from *teamMember) *scm.TeamMember {
if from == nil {
return nil
}
return &scm.TeamMember{
Login: from.Login,
}
}