forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
142 lines (117 loc) · 2.96 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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")
}
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
}