Skip to content

Commit dcb172e

Browse files
authored
add decode to BranchRestrictions (#129)
1 parent 9f51e41 commit dcb172e

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

branchrestrictions.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,53 @@ import (
55
"os"
66

77
"github.com/k0kubun/pp"
8+
"github.com/mitchellh/mapstructure"
89
)
910

1011
type BranchRestrictions struct {
1112
c *Client
13+
14+
ID int
15+
Pattern string
16+
Kind string
17+
Value *int
1218
}
1319

1420
func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, error) {
1521
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
1622
return b.c.execute("GET", urlStr, "")
1723
}
1824

19-
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (interface{}, error) {
25+
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
2026
data := b.buildBranchRestrictionsBody(bo)
2127
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
22-
return b.c.execute("POST", urlStr, data)
28+
response, err := b.c.execute("POST", urlStr, data)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
return decodeBranchRestriction(response)
2334
}
2435

25-
func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (interface{}, error) {
36+
func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
2637
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
27-
return b.c.execute("GET", urlStr, "")
38+
response, err := b.c.execute("GET", urlStr, "")
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
return decodeBranchRestriction(response)
2844
}
2945

3046
func (b *BranchRestrictions) Update(bo *BranchRestrictionsOptions) (interface{}, error) {
3147
data := b.buildBranchRestrictionsBody(bo)
3248
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
33-
return b.c.execute("PUT", urlStr, data)
49+
response, err := b.c.execute("PUT", urlStr, data)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return decodeBranchRestriction(response)
3455
}
3556

3657
func (b *BranchRestrictions) Delete(bo *BranchRestrictionsOptions) (interface{}, error) {
@@ -128,3 +149,18 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO
128149

129150
return string(data)
130151
}
152+
153+
func decodeBranchRestriction(branchResponse interface{}) (*BranchRestrictions, error) {
154+
branchMap := branchResponse.(map[string]interface{})
155+
156+
if branchMap["type"] == "error" {
157+
return nil, DecodeError(branchMap)
158+
}
159+
160+
var branchRestriction = new(BranchRestrictions)
161+
err := mapstructure.Decode(branchMap, branchRestriction)
162+
if err != nil {
163+
return nil, err
164+
}
165+
return branchRestriction, nil
166+
}

0 commit comments

Comments
 (0)