-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathresource_teamaccess.go
130 lines (109 loc) · 3.5 KB
/
resource_teamaccess.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
/*
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 github
import (
"context"
"errors"
"github.com/fluxcd/go-git-providers/gitprovider"
)
func newTeamAccess(c *TeamAccessClient, ta gitprovider.TeamAccessInfo) *teamAccess {
return &teamAccess{
ta: ta,
c: c,
}
}
var _ gitprovider.TeamAccess = &teamAccess{}
type teamAccess struct {
ta gitprovider.TeamAccessInfo
c *TeamAccessClient
}
func (ta *teamAccess) Get() gitprovider.TeamAccessInfo {
return ta.ta
}
func (ta *teamAccess) Set(info gitprovider.TeamAccessInfo) error {
if err := info.ValidateInfo(); err != nil {
return err
}
ta.ta = info
return nil
}
func (ta *teamAccess) APIObject() interface{} {
return nil
}
func (ta *teamAccess) Repository() gitprovider.RepositoryRef {
return ta.c.ref
}
// Delete removes the given team from the repo's team access control list.
//
// ErrNotFound is returned if the resource does not exist.
func (ta *teamAccess) Delete(ctx context.Context) error {
// DELETE /orgs/{org}/teams/{team_slug}/repos/{owner}/{repo}
return ta.c.c.RemoveTeam(ctx, ta.c.ref.GetIdentity(), ta.c.ref.GetRepository(), ta.ta.Name)
}
func (ta *teamAccess) Update(ctx context.Context) error {
// Update the actual state to be the desired state
// by issuing a Create, which uses a PUT underneath.
resp, err := ta.c.Create(ctx, ta.Get())
if err != nil {
return err
}
return ta.Set(resp.Get())
}
// Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider.
//
// If req doesn't exist under the hood, it is created (actionTaken == true).
// If req doesn't equal the actual state, the resource will be deleted and recreated (actionTaken == true).
// If req is already the actual state, this is a no-op (actionTaken == false).
func (ta *teamAccess) Reconcile(ctx context.Context) (bool, error) {
req := ta.Get()
actual, err := ta.c.Get(ctx, req.Name)
if err != nil {
// Create if not found
if errors.Is(err, gitprovider.ErrNotFound) {
resp, err := ta.c.Create(ctx, req)
if err != nil {
return true, err
}
return true, ta.Set(resp.Get())
}
// Unexpected path, Get should succeed or return NotFound
return false, err
}
// If the desired matches the actual state, just return the actual state
if req.Equals(actual.Get()) {
return false, nil
}
return true, ta.Update(ctx)
}
//nolint:gochecknoglobals,gomnd
var permissionPriority = map[gitprovider.RepositoryPermission]int{
gitprovider.RepositoryPermissionPull: 1,
gitprovider.RepositoryPermissionTriage: 2,
gitprovider.RepositoryPermissionPush: 3,
gitprovider.RepositoryPermissionMaintain: 4,
gitprovider.RepositoryPermissionAdmin: 5,
}
func getPermissionFromMap(permissionMap map[string]bool) (permission *gitprovider.RepositoryPermission) {
lastPriority := 0
for key, ok := range permissionMap {
if ok {
p := gitprovider.RepositoryPermission(key)
priority, ok := permissionPriority[p]
if ok && priority > lastPriority {
permission = &p
lastPriority = priority
}
}
}
return
}