-
Notifications
You must be signed in to change notification settings - Fork 147
/
diff.go
150 lines (119 loc) · 3.59 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
143
144
145
146
147
148
149
150
package bitbucket
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
)
type Diff struct {
c *Client
}
type DiffStatRes struct {
Page int `json:"page,omitempty"`
Pagelen int `json:"pagelen,omitempty"`
Size int `json:"size,omitempty"`
Next string `json:"next,omitempty"`
Previous string `json:"previous,omitempty"`
DiffStats []*DiffStat `json:"values,omitempty"`
}
type DiffStat struct {
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
LinesRemoved int `json:"lines_removed,omitempty"`
LinedAdded int `json:"lines_added,omitempty"`
Old map[string]interface{} `json:"old,omitempty"`
New map[string]interface{} `json:"new,omitempty"`
}
func (d *Diff) GetDiff(do *DiffOptions) (interface{}, error) {
params := url.Values{}
if do.FromPullRequestID > 0 {
params.Add("from_pullrequest_id", strconv.Itoa(do.FromPullRequestID))
}
if do.Whitespace {
params.Add("ignore_whitespace", strconv.FormatBool(do.Whitespace))
}
if do.Context > 0 {
params.Add("context", strconv.Itoa(do.Context))
}
if do.Path != "" {
params.Add("path", do.Path)
}
if !do.Binary {
params.Add("binary", strconv.FormatBool(do.Binary))
}
if !do.Renames {
params.Add("renames", strconv.FormatBool(do.Renames))
}
if do.Topic {
params.Add("topic", strconv.FormatBool(do.Topic))
}
urlStr := d.c.requestUrl("/repositories/%s/%s/diff/%s?%s", do.Owner, do.RepoSlug, do.Spec, params.Encode())
return d.c.executeRaw("GET", urlStr, "")
}
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.FromPullRequestID > 0 {
params.Add("from_pullrequest_id", strconv.Itoa(dso.FromPullRequestID))
}
if dso.Whitespace {
params.Add("ignore_whitespace", strconv.FormatBool(dso.Whitespace))
}
if !dso.Merge {
params.Add("merge", strconv.FormatBool(dso.Merge))
}
if dso.Path != "" {
params.Add("path", dso.Path)
}
if !dso.Renames {
params.Add("renames", strconv.FormatBool(dso.Renames))
}
if dso.Topic {
params.Add("topic", strconv.FormatBool(dso.Topic))
}
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))
}
if len(dso.Fields) > 0 {
params.Add("fields", cleanFields(dso.Fields))
}
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 diffStatRes DiffStatRes
err := json.Unmarshal([]byte(diffStatResponseStr), &diffStatRes)
if err != nil {
return nil, fmt.Errorf("DiffStat decode error: %w", err)
}
return &diffStatRes, nil
}
// cleanFields combines all query params in the slice of field strigs into a sigle string
// and removes any whitespace before returing the string.
func cleanFields(fields []string) string {
interS := strings.Join(fields, ",")
s := strings.ReplaceAll(interS, " ", "")
return s
}