@@ -5,32 +5,53 @@ import (
5
5
"os"
6
6
7
7
"github.com/k0kubun/pp"
8
+ "github.com/mitchellh/mapstructure"
8
9
)
9
10
10
11
type BranchRestrictions struct {
11
12
c * Client
13
+
14
+ ID int
15
+ Pattern string
16
+ Kind string
17
+ Value * int
12
18
}
13
19
14
20
func (b * BranchRestrictions ) Gets (bo * BranchRestrictionsOptions ) (interface {}, error ) {
15
21
urlStr := b .c .requestUrl ("/repositories/%s/%s/branch-restrictions" , bo .Owner , bo .RepoSlug )
16
22
return b .c .execute ("GET" , urlStr , "" )
17
23
}
18
24
19
- func (b * BranchRestrictions ) Create (bo * BranchRestrictionsOptions ) (interface {} , error ) {
25
+ func (b * BranchRestrictions ) Create (bo * BranchRestrictionsOptions ) (* BranchRestrictions , error ) {
20
26
data := b .buildBranchRestrictionsBody (bo )
21
27
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 )
23
34
}
24
35
25
- func (b * BranchRestrictions ) Get (bo * BranchRestrictionsOptions ) (interface {} , error ) {
36
+ func (b * BranchRestrictions ) Get (bo * BranchRestrictionsOptions ) (* BranchRestrictions , error ) {
26
37
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 )
28
44
}
29
45
30
46
func (b * BranchRestrictions ) Update (bo * BranchRestrictionsOptions ) (interface {}, error ) {
31
47
data := b .buildBranchRestrictionsBody (bo )
32
48
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 )
34
55
}
35
56
36
57
func (b * BranchRestrictions ) Delete (bo * BranchRestrictionsOptions ) (interface {}, error ) {
@@ -128,3 +149,18 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO
128
149
129
150
return string (data )
130
151
}
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