Skip to content

Commit 1579795

Browse files
authored
Fix compare change api result for harness (#267)
* Fix compare change api result for harness
1 parent 6906e84 commit 1579795

File tree

6 files changed

+133
-11
lines changed

6 files changed

+133
-11
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/drone/go-scm
22

33
require (
4+
github.com/bluekeyes/go-gitdiff v0.7.1
45
github.com/google/go-cmp v0.2.0
56
github.com/h2non/gock v1.0.9
67
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/bluekeyes/go-gitdiff v0.7.1 h1:graP4ElLRshr8ecu0UtqfNTCHrtSyZd3DABQm/DWesQ=
2+
github.com/bluekeyes/go-gitdiff v0.7.1/go.mod h1:QpfYYO1E0fTVHVZAZKiRjtSGY9823iCdvGXBcEzHGbM=
13
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
24
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
35
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=

scm/driver/harness/git.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ package harness
77
import (
88
"context"
99
"fmt"
10-
"io"
1110
"strings"
1211
"time"
1312

13+
"github.com/bluekeyes/go-gitdiff/gitdiff"
1414
"github.com/drone/go-scm/scm"
1515
)
1616

@@ -81,17 +81,9 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
8181
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
8282
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
8383
path := fmt.Sprintf("api/v1/repos/%s/compare/%s...%s", harnessURI, source, target)
84-
res, err := s.client.do(ctx, "GET", path, nil, nil)
85-
// convert response to a string
8684
buf := new(strings.Builder)
87-
_, _ = io.Copy(buf, res.Body)
88-
changes := []*scm.Change{
89-
{
90-
Path: "not implemented",
91-
Sha: buf.String(),
92-
},
93-
}
94-
return changes, res, err
85+
res, err := s.client.do(ctx, "GET", path, nil, buf)
86+
return convertCompareChanges(buf.String()), res, err
9587
}
9688

9789
// native data structures
@@ -172,6 +164,26 @@ func convertCommitList(src []*commitInfo) []*scm.Commit {
172164
return dst
173165
}
174166

167+
func convertCompareChanges(src string) []*scm.Change {
168+
files, _, err := gitdiff.Parse(strings.NewReader(src))
169+
if err != nil {
170+
return nil
171+
}
172+
173+
changes := make([]*scm.Change, 0)
174+
for _, f := range files {
175+
changes = append(changes, &scm.Change{
176+
Path: f.NewName,
177+
PrevFilePath: f.OldName,
178+
Added: f.IsNew,
179+
Deleted: f.IsDelete,
180+
Renamed: f.IsRename,
181+
})
182+
}
183+
184+
return changes
185+
}
186+
175187
func convertCommitInfo(src *commitInfo) *scm.Commit {
176188
return &scm.Commit{
177189
Sha: src.Sha,

scm/driver/harness/git_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package harness
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
1011
"io/ioutil"
1112
"net/http"
1213
"testing"
@@ -207,3 +208,46 @@ func TestCreateBranch(t *testing.T) {
207208
}
208209

209210
}
211+
212+
func TestCompareChanges(t *testing.T) {
213+
source := "a24d87c887957954d6f872bac3676f12cb9f50a2"
214+
target := "5d1eb44a2aae537e5fa649dce3ff8c306af1527e"
215+
defer gock.Off()
216+
217+
gock.New(gockOrigin).
218+
Get(fmt.Sprintf("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/compare/%s...%s", source, target)).
219+
Reply(200).
220+
Type("application/json").
221+
File("testdata/gitdiff.json")
222+
223+
client, _ := New(gockOrigin, harnessOrg, harnessAccount, harnessProject)
224+
client.Client = &http.Client{
225+
Transport: &transport.Custom{
226+
Before: func(r *http.Request) {
227+
r.Header.Set("x-api-key", harnessPAT)
228+
},
229+
},
230+
}
231+
got, result, err := client.Git.CompareChanges(context.Background(), harnessRepo, source, target, scm.ListOptions{})
232+
if err != nil {
233+
t.Error(err)
234+
return
235+
}
236+
237+
if result.Status != 200 {
238+
t.Errorf("Unexpected Results")
239+
}
240+
241+
want := []*scm.Change{}
242+
raw, _ := ioutil.ReadFile("testdata/gitdiff.json.golden")
243+
wantErr := json.Unmarshal(raw, &want)
244+
if wantErr != nil {
245+
t.Error(wantErr)
246+
return
247+
}
248+
249+
if diff := cmp.Diff(got, want); diff != "" {
250+
t.Errorf("Unexpected Results")
251+
t.Log(diff)
252+
}
253+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
diff --git a/five b/five
2+
new file mode 100644
3+
index 0000000..54f9d6d
4+
--- /dev/null
5+
+++ b/five
6+
@@ -0,0 +1 @@
7+
+five
8+
diff --git a/two b/four
9+
similarity index 100%
10+
rename from two
11+
rename to four
12+
diff --git a/one b/one
13+
index 5626abf..9c0408b 100644
14+
--- a/one
15+
+++ b/one
16+
@@ -1 +1,2 @@
17+
one
18+
+modified to two
19+
diff --git a/three b/three
20+
deleted file mode 100644
21+
index 2bdf67a..0000000
22+
--- a/three
23+
+++ /dev/null
24+
@@ -1 +0,0 @@
25+
-three
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"Path": "five",
4+
"Added": true,
5+
"Renamed": false,
6+
"Deleted": false,
7+
"Sha": "",
8+
"BlobID": "",
9+
"PrevFilePath": ""
10+
},
11+
{
12+
"Path": "four",
13+
"Added": false,
14+
"Renamed": true,
15+
"Deleted": false,
16+
"Sha": "",
17+
"BlobID": "",
18+
"PrevFilePath": "two"
19+
},
20+
{
21+
"Path": "one",
22+
"Added": false,
23+
"Renamed": false,
24+
"Deleted": false,
25+
"Sha": "",
26+
"BlobID": "",
27+
"PrevFilePath": "one"
28+
},
29+
{
30+
"Path": "",
31+
"Added": false,
32+
"Renamed": false,
33+
"Deleted": true,
34+
"Sha": "",
35+
"BlobID": "",
36+
"PrevFilePath": "three"
37+
}
38+
]

0 commit comments

Comments
 (0)