-
Notifications
You must be signed in to change notification settings - Fork 9
/
project_repository_user_permissions.go
85 lines (64 loc) · 2.18 KB
/
project_repository_user_permissions.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
package bitclient
import (
"fmt"
)
type SetRepositoryUserPermissionRequest struct {
Username string `url:"name"`
Permission string `url:"permission"`
}
func (bc *BitClient) SetRepositoryUserPermission(projectKey string, repositorySlug string, params SetRepositoryUserPermissionRequest) error {
_, err := bc.DoPutUrl(
fmt.Sprintf("/projects/%s/repos/%s/permissions/users", projectKey, repositorySlug),
params,
nil,
)
return err
}
type UnsetRepositoryUserPermissionRequest struct {
Username string `url:"name"`
}
func (bc *BitClient) UnsetRepositoryUserPermission(projectKey string, repositorySlug string, params UnsetRepositoryUserPermissionRequest) error {
_, err := bc.DoDeleteUrl(
fmt.Sprintf("/projects/%s/repos/%s/permissions/users", projectKey, repositorySlug),
params,
nil,
)
return err
}
type GetRepositoryUserPermissionRequest struct {
PagedRequest
Filter string
}
type GetRepositoryUserPermissionResponse struct {
PagedResponse
Values []UserPermission
}
func (bc *BitClient) GetRepositoryUserPermission(projectKey string, repositorySlug string, params GetRepositoryUserPermissionRequest) (GetRepositoryUserPermissionResponse, error) {
response := GetRepositoryUserPermissionResponse{}
_, err := bc.DoGet(
fmt.Sprintf("/projects/%s/repos/%s/permissions/users", projectKey, repositorySlug),
params,
&response,
)
return response, err
}
func (bc *BitClient) CloneRepositoryUserPermissions(sourceProjectKey, sourceRepositorySlug, targetProjectKey, targetRepositorySlug string) error {
response, err := bc.GetRepositoryUserPermission(sourceProjectKey, sourceRepositorySlug, GetRepositoryUserPermissionRequest{})
if err != nil {
return err
}
for _, permission := range response.Values {
if permission.User.Active == false {
continue
}
params := SetRepositoryUserPermissionRequest{
Username: permission.User.Name,
Permission: permission.Permission,
}
err := bc.SetRepositoryUserPermission(targetProjectKey, targetRepositorySlug, params)
if err != nil {
return fmt.Errorf("error adding %s permission to user %s on %s/%s: %s", permission.Permission, permission.User.Name, targetProjectKey, targetRepositorySlug, err)
}
}
return nil
}