From dcb172e8143324717604918f3375a53e3788d23f Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Mon, 12 Apr 2021 16:24:41 +0200 Subject: [PATCH] add decode to BranchRestrictions (#129) --- branchrestrictions.go | 46 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/branchrestrictions.go b/branchrestrictions.go index 7c3f11d..57d9fa7 100644 --- a/branchrestrictions.go +++ b/branchrestrictions.go @@ -5,10 +5,16 @@ 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) { @@ -16,21 +22,36 @@ func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, e 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) { @@ -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 +}