forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
112 lines (91 loc) · 2.85 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
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) {
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))
}
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
}