Skip to content

Add git diff stat to resolve issue 142 #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
127 changes: 127 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
package bitbucket

import (
"encoding/json"
"io/ioutil"
"net/url"
"strconv"

"github.com/mitchellh/mapstructure"
)

type Diff struct {
c *Client
}

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) {
urlStr := d.c.requestUrl("/repositories/%s/%s/diff/%s", do.Owner, do.RepoSlug, do.Spec)
return d.c.executeRaw("GET", urlStr, "diff")
Expand All @@ -13,3 +40,103 @@ 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) (*DiffStatRes, 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) (*DiffStatRes, 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 := DiffStatRes{
Page: int(page),
Pagelen: int(pagelen),
MaxDepth: int(max_depth),
Size: int(size),
Next: next,
DiffStats: diffStatsSlice,
}
return &diffStats, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
36 changes: 36 additions & 0 deletions tests/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,39 @@ 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)
}

pp.Println(res)

if res == nil {
t.Error("Cannot get diffstat.")
}
}