Skip to content

Commit

Permalink
add decode to BranchRestrictions (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroschupp authored Apr 12, 2021
1 parent 9f51e41 commit dcb172e
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions branchrestrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,53 @@ import (
"os"

"github.com/k0kubun/pp"
"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.execute("GET", urlStr, "")
}

func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (interface{}, error) {
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
data := b.buildBranchRestrictionsBody(bo)
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
return b.c.execute("POST", urlStr, data)
response, err := b.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}

return decodeBranchRestriction(response)
}

func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (interface{}, error) {
func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
return b.c.execute("GET", urlStr, "")
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 := b.buildBranchRestrictionsBody(bo)
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
return b.c.execute("PUT", urlStr, data)
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) {
Expand Down Expand Up @@ -128,3 +149,18 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO

return string(data)
}

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
}

0 comments on commit dcb172e

Please sign in to comment.