forked from fluxcd/go-git-providers
/
client_organization_teams.go
104 lines (84 loc) · 2.64 KB
/
client_organization_teams.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
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitlab
import (
"context"
"github.com/dinosk/go-git-providers/gitprovider"
"github.com/xanzy/go-gitlab"
)
// TeamsClient implements the gitprovider.TeamsClient interface.
var _ gitprovider.TeamsClient = &TeamsClient{}
// TeamsClient handles teams organization-wide.
type TeamsClient struct {
*clientContext
ref gitprovider.OrganizationRef
}
// Get a team within the specific organization.
//
// teamName may include slashes, to point to e.g. subgroups in GitLab.
// teamName must not be an empty string.
//
// ErrNotFound is returned if the resource does not exist.
func (c *TeamsClient) Get(ctx context.Context, teamName string) (gitprovider.Team, error) {
apiObjs, err := c.c.ListGroupMembers(ctx, c.ref.Organization)
if err != nil {
return nil, err
}
// Collect a list of the members' names. Login is validated to be non-nil in ListOrgTeamMembers.
logins := make([]string, 0, len(apiObjs))
for _, apiObj := range apiObjs {
// Login is validated to be non-nil in ListOrgTeamMembers
logins = append(logins, apiObj.Username)
}
return &team{
users: apiObjs,
info: gitprovider.TeamInfo{
Name: teamName,
Members: logins,
},
ref: c.ref,
}, nil
}
// List all teams (recursively, in terms of subgroups) within the specific organization.
//
// List returns all available organizations, using multiple paginated requests if needed.
func (c *TeamsClient) List(ctx context.Context) ([]gitprovider.Team, error) {
subgroups, err := c.c.ListSubgroups(ctx, c.ref.Organization)
if err != nil {
return nil, err
}
teams := make([]gitprovider.Team, 0, len(subgroups))
for _, subgroup := range subgroups {
team, err := c.Get(ctx, subgroup.Name)
if err != nil {
return nil, err
}
teams = append(teams, team)
}
return teams, nil
}
var _ gitprovider.Team = &team{}
type team struct {
users []*gitlab.GroupMember
info gitprovider.TeamInfo
ref gitprovider.OrganizationRef
}
func (t *team) Get() gitprovider.TeamInfo {
return t.info
}
func (t *team) APIObject() interface{} {
return t.users
}
func (t *team) Organization() gitprovider.OrganizationRef {
return t.ref
}