Skip to content

Commit 8764ac0

Browse files
authored
Fix diff api response conversion for harness compareChange (#269)
* Fix diff api response conversion
1 parent 5098db8 commit 8764ac0

File tree

6 files changed

+99
-64
lines changed

6 files changed

+99
-64
lines changed

go.mod

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

33
require (
4-
github.com/bluekeyes/go-gitdiff v0.7.1
54
github.com/google/go-cmp v0.2.0
65
github.com/h2non/gock v1.0.9
76
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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=
31
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
42
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
53
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=

scm/driver/harness/git.go

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

13-
"github.com/bluekeyes/go-gitdiff/gitdiff"
1412
"github.com/drone/go-scm/scm"
1513
)
1614

@@ -81,9 +79,9 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
8179
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
8280
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
8381
path := fmt.Sprintf("api/v1/repos/%s/diff/%s...%s", harnessURI, source, target)
84-
buf := new(strings.Builder)
85-
res, err := s.client.do(ctx, "GET", path, nil, buf)
86-
return convertCompareChanges(buf.String()), res, err
82+
out := []*fileDiff{}
83+
res, err := s.client.do(ctx, "GET", path, nil, &out)
84+
return convertChangeList(out), res, err
8785
}
8886

8987
// native data structures
@@ -134,6 +132,20 @@ type (
134132
Name string `json:"name"`
135133
Sha string `json:"sha"`
136134
}
135+
fileDiff struct {
136+
SHA string `json:"sha"`
137+
OldSHA string `json:"old_sha,omitempty"`
138+
Path string `json:"path"`
139+
OldPath string `json:"old_path,omitempty"`
140+
Status string `json:"status"`
141+
Additions int64 `json:"additions"`
142+
Deletions int64 `json:"deletions"`
143+
Changes int64 `json:"changes"`
144+
ContentURL string `json:"content_url"`
145+
Patch []byte `json:"patch,omitempty"`
146+
IsBinary bool `json:"is_binary"`
147+
IsSubmodule bool `json:"is_submodule"`
148+
}
137149
)
138150

139151
//
@@ -164,24 +176,12 @@ func convertCommitList(src []*commitInfo) []*scm.Commit {
164176
return dst
165177
}
166178

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-
})
179+
func convertChangeList(src []*fileDiff) []*scm.Change {
180+
dst := []*scm.Change{}
181+
for _, v := range src {
182+
dst = append(dst, convertChange(v))
182183
}
183-
184-
return changes
184+
return dst
185185
}
186186

187187
func convertCommitInfo(src *commitInfo) *scm.Commit {
@@ -200,3 +200,13 @@ func convertCommitInfo(src *commitInfo) *scm.Commit {
200200
},
201201
}
202202
}
203+
204+
func convertChange(src *fileDiff) *scm.Change {
205+
return &scm.Change{
206+
Path: src.Path,
207+
PrevFilePath: src.OldPath,
208+
Added: src.Status == "ADDED",
209+
Renamed: src.Status == "RENAMED",
210+
Deleted: src.Status == "DELETED",
211+
}
212+
}

scm/driver/harness/git_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ func TestCreateBranch(t *testing.T) {
210210
}
211211

212212
func TestCompareChanges(t *testing.T) {
213-
source := "a24d87c887957954d6f872bac3676f12cb9f50a2"
214-
target := "5d1eb44a2aae537e5fa649dce3ff8c306af1527e"
213+
source := "542ddabd47d7bfa79359b7b4e2af7f975354e35f"
214+
target := "c7d0d4b21d5cfdf47475ff1f6281ef1a91883d"
215215
defer gock.Off()
216216

217217
gock.New(gockOrigin).
Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
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
1+
[
2+
{
3+
"sha": "c4e897c1fcd1cae04abf761f034ae4c5e210caad",
4+
"old_sha": "0000000000000000000000000000000000000000",
5+
"path": "hello.go",
6+
"old_path": "hello.go",
7+
"status": "ADDED",
8+
"additions": 8,
9+
"deletions": 0,
10+
"changes": 8,
11+
"content_url": "/api/v1/hello.go",
12+
"is_binary": false,
13+
"is_submodule": false
14+
},
15+
{
16+
"sha": "0000000000000000000000000000000000000000",
17+
"old_sha": "d7fcbf28651697b00add519d8b4402a5ab46ffc2",
18+
"path": "null.go",
19+
"old_path": "null.go",
20+
"status": "DELETED",
21+
"additions": 0,
22+
"deletions": 118,
23+
"changes": 118,
24+
"content_url": "/api/v1/null.go",
25+
"is_binary": false,
26+
"is_submodule": false
27+
},
28+
{
29+
"sha": "ce5a2cd34b697f7a8507192e7074b33737c6740c",
30+
"old_sha": "7697802e4d16b255e7ea22a86071cfbe9af6aa39",
31+
"path": "version4.go",
32+
"old_path": "version4.go",
33+
"status": "MODIFIED",
34+
"additions": 8,
35+
"deletions": 7,
36+
"changes": 15,
37+
"content_url": "/api/v1/version4.go",
38+
"is_binary": false,
39+
"is_submodule": false
40+
},
41+
{
42+
"sha": "",
43+
"path": "version_1.go",
44+
"old_path": "version1.go",
45+
"status": "RENAMED",
46+
"additions": 0,
47+
"deletions": 0,
48+
"changes": 0,
49+
"content_url": "/api/v1/version_1.go",
50+
"is_binary": false,
51+
"is_submodule": false
52+
}
53+
]
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
[
22
{
3-
"Path": "five",
3+
"Path": "hello.go",
44
"Added": true,
55
"Renamed": false,
66
"Deleted": false,
77
"Sha": "",
88
"BlobID": "",
9-
"PrevFilePath": ""
9+
"PrevFilePath": "hello.go"
1010
},
1111
{
12-
"Path": "four",
12+
"Path": "null.go",
1313
"Added": false,
14-
"Renamed": true,
15-
"Deleted": false,
14+
"Renamed": false,
15+
"Deleted": true,
1616
"Sha": "",
1717
"BlobID": "",
18-
"PrevFilePath": "two"
18+
"PrevFilePath": "null.go"
1919
},
2020
{
21-
"Path": "one",
21+
"Path": "version4.go",
2222
"Added": false,
2323
"Renamed": false,
2424
"Deleted": false,
2525
"Sha": "",
2626
"BlobID": "",
27-
"PrevFilePath": "one"
27+
"PrevFilePath": "version4.go"
2828
},
2929
{
30-
"Path": "",
30+
"Path": "version_1.go",
3131
"Added": false,
32-
"Renamed": false,
33-
"Deleted": true,
32+
"Renamed": true,
33+
"Deleted": false,
3434
"Sha": "",
3535
"BlobID": "",
36-
"PrevFilePath": "three"
36+
"PrevFilePath": "version1.go"
3737
}
3838
]

0 commit comments

Comments
 (0)