From 421c90846f7dc8a3aae735a933dc357088133466 Mon Sep 17 00:00:00 2001 From: David Dansby Date: Tue, 22 Jun 2021 02:37:16 -0700 Subject: [PATCH 1/3] Set up base options, decode func, and DiffStat func --- bitbucket.go | 13 ++++++ diff.go | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 1 + 4 files changed, 134 insertions(+), 1 deletion(-) diff --git a/bitbucket.go b/bitbucket.go index d9a8889..0012d0a 100644 --- a/bitbucket.go +++ b/bitbucket.go @@ -325,6 +325,19 @@ type DiffOptions struct { Spec string `json:"spec"` } +type DiffStatOptions struct { + Owner string `json:"owner"` + RepoSlug string `json:"repo_slug"` + Spec string `json:"spec"` + Whitespace bool `json:"ignore_whitespace"` + Merge bool `json:"merge"` + Path string `json:"path"` + Renames bool `json:"renames"` + PageNum int `json:"page"` + Pagelen int `json:"pagelen"` + MaxDepth int `json:"max_depth"` +} + type WebhooksOptions struct { Owner string `json:"owner"` RepoSlug string `json:"repo_slug"` diff --git a/diff.go b/diff.go index 78f4954..d62d764 100644 --- a/diff.go +++ b/diff.go @@ -1,9 +1,30 @@ package bitbucket +import ( + "encoding/json" + "io/ioutil" + "net/url" + "strconv" + + "github.com/mitchellh/mapstructure" +) + type Diff struct { c *Client } +type DiffStats struct { + Page int + Pagelen int + MaxDepth int + Size int + Next string + DiffStat []DiffStat +} + +type DiffStat struct { +} + func (d *Diff) GetDiff(do *DiffOptions) (interface{}, error) { urlStr := d.c.requestUrl("/repositories/%s/%s/diff/%s", do.Owner, do.RepoSlug, do.Spec) return d.c.executeRaw("GET", urlStr, "diff") @@ -13,3 +34,101 @@ func (d *Diff) GetPatch(do *DiffOptions) (interface{}, error) { urlStr := d.c.requestUrl("/repositories/%s/%s/patch/%s", do.Owner, do.RepoSlug, do.Spec) return d.c.executeRaw("GET", urlStr, "") } + +func (d *Diff) GetDiffStat(dso *DiffStatOptions) (interface{}, error) { + + params := url.Values{} + if dso.Whitespace == true { + params.Add("ignore_whitespace", strconv.FormatBool(dso.Whitespace)) + } + + if dso.Merge == false { + params.Add("merge", strconv.FormatBool(dso.Merge)) + } + + if dso.Path != "" { + params.Add("path", dso.Path) + } + + if dso.Renames == false { + params.Add("renames", strconv.FormatBool(dso.Renames)) + } + + if dso.PageNum > 0 { + params.Add("page", strconv.Itoa(dso.PageNum)) + } + + if dso.Pagelen > 0 { + params.Add("pagelen", strconv.Itoa(dso.Pagelen)) + } + + if dso.MaxDepth > 0 { + params.Add("max_depth", strconv.Itoa(dso.MaxDepth)) + } + + urlStr := d.c.requestUrl("/repositories/%s/%s/diffstat/%s?%s", dso.Owner, dso.RepoSlug, + dso.Spec, + params.Encode()) + response, err := d.c.executeRaw("GET", urlStr, "") + if err != nil { + return nil, err + } + bodyBytes, err := ioutil.ReadAll(response) + if err != nil { + return nil, err + } + bodyString := string(bodyBytes) + return decodeDiffStat(bodyString) +} + +func decodeDiffStat(diffStatResponseStr string) (*DiffStats, error) { + + var diffStatResponseMap map[string]interface{} + err := json.Unmarshal([]byte(diffStatResponseStr), &diffStatResponseMap) + if err != nil { + return nil, err + } + + diffStatArray := diffStatResponseMap["values"].([]interface{}) + var diffStatsSlice []DiffStat + for _, diffStatEntry := range diffStatArray { + var diffStat DiffStat + err = mapstructure.Decode(diffStatEntry, &diffStat) + if err == nil { + diffStatsSlice = append(diffStatsSlice, diffStat) + } + } + + page, ok := diffStatResponseMap["page"].(float64) + if !ok { + page = 0 + } + + pagelen, ok := diffStatResponseMap["pagelen"].(float64) + if !ok { + pagelen = 0 + } + max_depth, ok := diffStatResponseMap["max_depth"].(float64) + if !ok { + max_depth = 0 + } + size, ok := diffStatResponseMap["size"].(float64) + if !ok { + size = 0 + } + + next, ok := diffStatResponseMap["next"].(string) + if !ok { + next = "" + } + + diffStats := DiffStats{ + Page: int(page), + Pagelen: int(pagelen), + MaxDepth: int(max_depth), + Size: int(size), + Next: next, + DiffStat: diffStatsSlice, + } + return &diffStats, nil +} diff --git a/go.mod b/go.mod index 2f44292..6b51e54 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ktrysmt/go-bitbucket go 1.14 require ( - github.com/golang/protobuf v1.0.0 + github.com/golang/protobuf v1.0.0 // indirect github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect github.com/k0kubun/pp v2.3.0+incompatible github.com/mattn/go-colorable v0.0.9 // indirect diff --git a/go.sum b/go.sum index acd29be..88238ba 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,7 @@ golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54 h1:4qAtdeqGYyXU2CfUvLomEFw0c golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= google.golang.org/appengine v1.0.0 h1:dN4LljjBKVChsv0XCSI+zbyzdqrkEwX5LQFUMRSGqOc= google.golang.org/appengine v1.0.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 7b65bd844d49a0b7ede323064acb558a21de70ef Mon Sep 17 00:00:00 2001 From: David Dansby Date: Sun, 4 Jul 2021 16:23:37 -0700 Subject: [PATCH 2/3] Finalize GetDiffStat method and its corresponding test --- diff.go | 40 ++++++++++++++++++++++++---------------- tests/diff_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/diff.go b/diff.go index d62d764..d98d8a9 100644 --- a/diff.go +++ b/diff.go @@ -13,16 +13,22 @@ type Diff struct { c *Client } -type DiffStats struct { - Page int - Pagelen int - MaxDepth int - Size int - Next string - DiffStat []DiffStat +type DiffStatRes struct { + Page int + Pagelen int + MaxDepth int + Size int + Next string + DiffStats []DiffStat } type DiffStat struct { + Type string + Status string + LinesRemoved int + LinedAdded int + Old map[string]interface{} + New map[string]interface{} } func (d *Diff) GetDiff(do *DiffOptions) (interface{}, error) { @@ -35,7 +41,7 @@ func (d *Diff) GetPatch(do *DiffOptions) (interface{}, error) { return d.c.executeRaw("GET", urlStr, "") } -func (d *Diff) GetDiffStat(dso *DiffStatOptions) (interface{}, error) { +func (d *Diff) GetDiffStat(dso *DiffStatOptions) (*DiffStatRes, error) { params := url.Values{} if dso.Whitespace == true { @@ -81,7 +87,7 @@ func (d *Diff) GetDiffStat(dso *DiffStatOptions) (interface{}, error) { return decodeDiffStat(bodyString) } -func decodeDiffStat(diffStatResponseStr string) (*DiffStats, error) { +func decodeDiffStat(diffStatResponseStr string) (*DiffStatRes, error) { var diffStatResponseMap map[string]interface{} err := json.Unmarshal([]byte(diffStatResponseStr), &diffStatResponseMap) @@ -108,10 +114,12 @@ func decodeDiffStat(diffStatResponseStr string) (*DiffStats, error) { if !ok { pagelen = 0 } + max_depth, ok := diffStatResponseMap["max_depth"].(float64) if !ok { max_depth = 0 } + size, ok := diffStatResponseMap["size"].(float64) if !ok { size = 0 @@ -122,13 +130,13 @@ func decodeDiffStat(diffStatResponseStr string) (*DiffStats, error) { next = "" } - diffStats := DiffStats{ - Page: int(page), - Pagelen: int(pagelen), - MaxDepth: int(max_depth), - Size: int(size), - Next: next, - DiffStat: diffStatsSlice, + diffStats := DiffStatRes{ + Page: int(page), + Pagelen: int(pagelen), + MaxDepth: int(max_depth), + Size: int(size), + Next: next, + DiffStats: diffStatsSlice, } return &diffStats, nil } diff --git a/tests/diff_test.go b/tests/diff_test.go index e655aec..0a7daa7 100644 --- a/tests/diff_test.go +++ b/tests/diff_test.go @@ -43,3 +43,37 @@ func TestDiff(t *testing.T) { t.Error("It could not get the raw response.") } } + +func TestGetDiffStat(t *testing.T) { + + user := os.Getenv("BITBUCKET_TEST_USERNAME") + pass := os.Getenv("BITBUCKET_TEST_PASSWORD") + owner := os.Getenv("BITBUCKET_TEST_OWNER") + repo := os.Getenv("BITBUCKET_TEST_REPOSLUG") + + if user == "" { + t.Error("BITBUCKET_TEST_USERNAME is empty.") + } + + if pass == "" { + t.Error("BITBUCKET_TEST_PASSWORD is empty.") + } + + c := bitbucket.NewBasicAuth(user, pass) + + spec := "master..develop" + + opt := &bitbucket.DiffStatOptions{ + Owner: owner, + RepoSlug: repo, + Spec: spec, + } + res, err := c.Repositories.Diff.GetDiffStat(opt) + if err != nil { + t.Error(err) + } + + if res == nil { + t.Error("Cannot get diffstat.") + } +} From f1eb8e329f8b7cf769fec85722a83394b14c14d7 Mon Sep 17 00:00:00 2001 From: David Dansby Date: Sun, 4 Jul 2021 16:43:01 -0700 Subject: [PATCH 3/3] Add back in pretty printer for test, similar to GetDiff test --- tests/diff_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/diff_test.go b/tests/diff_test.go index 0a7daa7..ff0116b 100644 --- a/tests/diff_test.go +++ b/tests/diff_test.go @@ -73,6 +73,8 @@ func TestGetDiffStat(t *testing.T) { t.Error(err) } + pp.Println(res) + if res == nil { t.Error("Cannot get diffstat.") }