11package bitbucket
22
3+ import (
4+ "encoding/json"
5+ "io/ioutil"
6+ "net/url"
7+ "strconv"
8+
9+ "github.com/mitchellh/mapstructure"
10+ )
11+
312type 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+
734func (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+ }
0 commit comments