Skip to content

Commit 4130b29

Browse files
authored
Add git diff to resolve issue 142 (#145)
* Set up base options, decode func, and DiffStat func * Finalize GetDiffStat method and its corresponding test * Add back in pretty printer for test, similar to GetDiff test
1 parent dd20750 commit 4130b29

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

bitbucket.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,19 @@ type DiffOptions struct {
336336
Spec string `json:"spec"`
337337
}
338338

339+
type DiffStatOptions struct {
340+
Owner string `json:"owner"`
341+
RepoSlug string `json:"repo_slug"`
342+
Spec string `json:"spec"`
343+
Whitespace bool `json:"ignore_whitespace"`
344+
Merge bool `json:"merge"`
345+
Path string `json:"path"`
346+
Renames bool `json:"renames"`
347+
PageNum int `json:"page"`
348+
Pagelen int `json:"pagelen"`
349+
MaxDepth int `json:"max_depth"`
350+
}
351+
339352
type WebhooksOptions struct {
340353
Owner string `json:"owner"`
341354
RepoSlug string `json:"repo_slug"`

diff.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
package bitbucket
22

3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"net/url"
7+
"strconv"
8+
9+
"github.com/mitchellh/mapstructure"
10+
)
11+
312
type Diff struct {
413
c *Client
514
}
615

16+
type DiffStatRes struct {
17+
Page int
18+
Pagelen int
19+
MaxDepth int
20+
Size int
21+
Next string
22+
DiffStats []DiffStat
23+
}
24+
25+
type DiffStat struct {
26+
Type string
27+
Status string
28+
LinesRemoved int
29+
LinedAdded int
30+
Old map[string]interface{}
31+
New map[string]interface{}
32+
}
33+
734
func (d *Diff) GetDiff(do *DiffOptions) (interface{}, error) {
835
urlStr := d.c.requestUrl("/repositories/%s/%s/diff/%s", do.Owner, do.RepoSlug, do.Spec)
936
return d.c.executeRaw("GET", urlStr, "diff")
@@ -13,3 +40,103 @@ func (d *Diff) GetPatch(do *DiffOptions) (interface{}, error) {
1340
urlStr := d.c.requestUrl("/repositories/%s/%s/patch/%s", do.Owner, do.RepoSlug, do.Spec)
1441
return d.c.executeRaw("GET", urlStr, "")
1542
}
43+
44+
func (d *Diff) GetDiffStat(dso *DiffStatOptions) (*DiffStatRes, error) {
45+
46+
params := url.Values{}
47+
if dso.Whitespace == true {
48+
params.Add("ignore_whitespace", strconv.FormatBool(dso.Whitespace))
49+
}
50+
51+
if dso.Merge == false {
52+
params.Add("merge", strconv.FormatBool(dso.Merge))
53+
}
54+
55+
if dso.Path != "" {
56+
params.Add("path", dso.Path)
57+
}
58+
59+
if dso.Renames == false {
60+
params.Add("renames", strconv.FormatBool(dso.Renames))
61+
}
62+
63+
if dso.PageNum > 0 {
64+
params.Add("page", strconv.Itoa(dso.PageNum))
65+
}
66+
67+
if dso.Pagelen > 0 {
68+
params.Add("pagelen", strconv.Itoa(dso.Pagelen))
69+
}
70+
71+
if dso.MaxDepth > 0 {
72+
params.Add("max_depth", strconv.Itoa(dso.MaxDepth))
73+
}
74+
75+
urlStr := d.c.requestUrl("/repositories/%s/%s/diffstat/%s?%s", dso.Owner, dso.RepoSlug,
76+
dso.Spec,
77+
params.Encode())
78+
response, err := d.c.executeRaw("GET", urlStr, "")
79+
if err != nil {
80+
return nil, err
81+
}
82+
bodyBytes, err := ioutil.ReadAll(response)
83+
if err != nil {
84+
return nil, err
85+
}
86+
bodyString := string(bodyBytes)
87+
return decodeDiffStat(bodyString)
88+
}
89+
90+
func decodeDiffStat(diffStatResponseStr string) (*DiffStatRes, error) {
91+
92+
var diffStatResponseMap map[string]interface{}
93+
err := json.Unmarshal([]byte(diffStatResponseStr), &diffStatResponseMap)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
diffStatArray := diffStatResponseMap["values"].([]interface{})
99+
var diffStatsSlice []DiffStat
100+
for _, diffStatEntry := range diffStatArray {
101+
var diffStat DiffStat
102+
err = mapstructure.Decode(diffStatEntry, &diffStat)
103+
if err == nil {
104+
diffStatsSlice = append(diffStatsSlice, diffStat)
105+
}
106+
}
107+
108+
page, ok := diffStatResponseMap["page"].(float64)
109+
if !ok {
110+
page = 0
111+
}
112+
113+
pagelen, ok := diffStatResponseMap["pagelen"].(float64)
114+
if !ok {
115+
pagelen = 0
116+
}
117+
118+
max_depth, ok := diffStatResponseMap["max_depth"].(float64)
119+
if !ok {
120+
max_depth = 0
121+
}
122+
123+
size, ok := diffStatResponseMap["size"].(float64)
124+
if !ok {
125+
size = 0
126+
}
127+
128+
next, ok := diffStatResponseMap["next"].(string)
129+
if !ok {
130+
next = ""
131+
}
132+
133+
diffStats := DiffStatRes{
134+
Page: int(page),
135+
Pagelen: int(pagelen),
136+
MaxDepth: int(max_depth),
137+
Size: int(size),
138+
Next: next,
139+
DiffStats: diffStatsSlice,
140+
}
141+
return &diffStats, nil
142+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ktrysmt/go-bitbucket
33
go 1.14
44

55
require (
6-
github.com/golang/protobuf v1.0.0
6+
github.com/golang/protobuf v1.0.0 // indirect
77
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
88
github.com/k0kubun/pp v2.3.0+incompatible
99
github.com/mattn/go-colorable v0.0.9 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54 h1:4qAtdeqGYyXU2CfUvLomEFw0c
2727
golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2828
google.golang.org/appengine v1.0.0 h1:dN4LljjBKVChsv0XCSI+zbyzdqrkEwX5LQFUMRSGqOc=
2929
google.golang.org/appengine v1.0.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
30+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3031
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3132
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3233
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tests/diff_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,39 @@ func TestDiff(t *testing.T) {
4343
t.Error("It could not get the raw response.")
4444
}
4545
}
46+
47+
func TestGetDiffStat(t *testing.T) {
48+
49+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
50+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
51+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
52+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
53+
54+
if user == "" {
55+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
56+
}
57+
58+
if pass == "" {
59+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
60+
}
61+
62+
c := bitbucket.NewBasicAuth(user, pass)
63+
64+
spec := "master..develop"
65+
66+
opt := &bitbucket.DiffStatOptions{
67+
Owner: owner,
68+
RepoSlug: repo,
69+
Spec: spec,
70+
}
71+
res, err := c.Repositories.Diff.GetDiffStat(opt)
72+
if err != nil {
73+
t.Error(err)
74+
}
75+
76+
pp.Println(res)
77+
78+
if res == nil {
79+
t.Error("Cannot get diffstat.")
80+
}
81+
}

0 commit comments

Comments
 (0)