-
Notifications
You must be signed in to change notification settings - Fork 9
/
project_repository_branch_restrictions.go
88 lines (67 loc) · 2.54 KB
/
project_repository_branch_restrictions.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
package bitclient
import (
"fmt"
)
type GetRepositoryBranchRestrictionRequest struct {
PagedRequest
Type string `url:"type,omitempty"`
MatcherType string `url:"matcherType,omitempty"`
MatcherId string `url:"matcherId,omitempty"`
Effective bool `url:"effective,omitempty"`
}
type GetRepositoryBranchRestrictionResponse struct {
PagedResponse
Values []BranchRestriction
}
func (bc *BitClient) GetRepositoryBranchRestrictions(projectKey, repositorySlug string, params GetRepositoryBranchRestrictionRequest) ([]BranchRestriction, error) {
rError := new(ErrorResponse)
response := GetRepositoryBranchRestrictionResponse{}
url := fmt.Sprintf("/rest/branch-permissions/2.0/projects/%s/repos/%s/restrictions", projectKey, repositorySlug)
resp, _ := bc.sling.New().Get(url).QueryStruct(params).Receive(&response, rError)
resp, err := bc.checkReponse(resp, rError)
return response.Values, err
}
type SetRepositoryBranchRestrictionsRequest struct {
Id int `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Matcher Matcher `json:"matcher,omitempty"`
Users []string `json:"users,omitempty"`
Groups []string `json:"groups,omitempty"`
}
func (bc *BitClient) SetRepositoryBranchRestrictions(projectKey, repositorySlug string, params SetRepositoryBranchRestrictionsRequest) error {
rError := new(ErrorResponse)
url := fmt.Sprintf("/rest/branch-permissions/2.0/projects/%s/repos/%s/restrictions", projectKey, repositorySlug)
resp, _ := bc.sling.New().Post(url).BodyJSON(params).Receive(nil, rError)
resp, err := bc.checkReponse(resp, rError)
return err
}
func (bc *BitClient) CloneRepositoryMasterBranchRestrictions(sourceProjectKey, sourceRepositorySlug, targetProjectKey, targetRepositorySlug string) error {
getParams := GetRepositoryBranchRestrictionRequest{
MatcherType: "BRANCH",
MatcherId: "refs/heads/master",
}
response, err := bc.GetRepositoryBranchRestrictions(sourceProjectKey, sourceRepositorySlug, getParams)
if err != nil {
return err
}
for _, branchRestriction := range response {
var users []string
for _, user := range branchRestriction.Users {
if user.Active == true {
users = append(users, user.Name)
}
}
postParams := SetRepositoryBranchRestrictionsRequest{
Id: branchRestriction.Id,
Type: branchRestriction.Type,
Matcher: branchRestriction.Matcher,
Groups: branchRestriction.Groups,
Users: users,
}
err := bc.SetRepositoryBranchRestrictions(targetProjectKey, targetRepositorySlug, postParams)
if err != nil {
return err
}
}
return nil
}