forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branchrestrictions.go
167 lines (145 loc) · 4.2 KB
/
branchrestrictions.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
package bitbucket
import (
"encoding/json"
"github.com/mitchellh/mapstructure"
)
type BranchRestrictions struct {
c *Client
ID int
Pattern string
Kind string
Value *int
}
func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, error) {
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
return b.c.executePaginated("GET", urlStr, "", nil)
}
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
data, err := b.buildBranchRestrictionsBody(bo)
if err != nil {
return nil, err
}
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
response, err := b.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}
return decodeBranchRestriction(response)
}
func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
response, err := b.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeBranchRestriction(response)
}
func (b *BranchRestrictions) Update(bo *BranchRestrictionsOptions) (interface{}, error) {
data, err := b.buildBranchRestrictionsBody(bo)
if err != nil {
return nil, err
}
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
response, err := b.c.execute("PUT", urlStr, data)
if err != nil {
return nil, err
}
return decodeBranchRestriction(response)
}
func (b *BranchRestrictions) Delete(bo *BranchRestrictionsOptions) (interface{}, error) {
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
return b.c.execute("DELETE", urlStr, "")
}
type branchRestrictionsBody struct {
Kind string `json:"kind"`
Pattern string `json:"pattern"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Value interface{} `json:"value"`
ID int `json:"id"`
Users []branchRestrictionsBodyUser `json:"users"`
Groups []branchRestrictionsBodyGroup `json:"groups"`
}
type branchRestrictionsBodyGroup struct {
Name string `json:"name"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Html struct {
Href string `json:"href"`
} `json:"html"`
} `json:"links"`
FullSlug string `json:"full_slug"`
Slug string `json:"slug"`
}
type branchRestrictionsBodyUser struct {
Username string `json:"username"`
Website string `json:"website"`
Display_name string `json:"display_name"`
UUID string `json:"uuid"`
Created_on string `json:"created_on"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Repositories struct {
Href string `json:"href"`
} `json:"repositories"`
Html struct {
Href string `json:"href"`
} `json:"html"`
Followers struct {
Href string `json:"href"`
} `json:"followers"`
Avatar struct {
Href string `json:"href"`
} `json:"avatar"`
Following struct {
Href string `json:"href"`
} `json:"following"`
} `json:"links"`
}
func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsOptions) (string, error) {
var users []branchRestrictionsBodyUser
var groups []branchRestrictionsBodyGroup
for _, u := range bo.Users {
user := branchRestrictionsBodyUser{
Username: u,
}
users = append(users, user)
}
for _, g := range bo.Groups {
group := branchRestrictionsBodyGroup{
Slug: g,
}
groups = append(groups, group)
}
body := branchRestrictionsBody{
Kind: bo.Kind,
Pattern: bo.Pattern,
Users: users,
Groups: groups,
Value: bo.Value,
}
data, err := json.Marshal(body)
if err != nil {
return "", err
}
return string(data), nil
}
func decodeBranchRestriction(branchResponse interface{}) (*BranchRestrictions, error) {
branchMap := branchResponse.(map[string]interface{})
if branchMap["type"] == "error" {
return nil, DecodeError(branchMap)
}
var branchRestriction = new(BranchRestrictions)
err := mapstructure.Decode(branchMap, branchRestriction)
if err != nil {
return nil, err
}
return branchRestriction, nil
}