Skip to content

Fix diff api response conversion for harness compareChange #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module github.com/drone/go-scm

require (
github.com/bluekeyes/go-gitdiff v0.7.1
github.com/google/go-cmp v0.2.0
github.com/h2non/gock v1.0.9
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/bluekeyes/go-gitdiff v0.7.1 h1:graP4ElLRshr8ecu0UtqfNTCHrtSyZd3DABQm/DWesQ=
github.com/bluekeyes/go-gitdiff v0.7.1/go.mod h1:QpfYYO1E0fTVHVZAZKiRjtSGY9823iCdvGXBcEzHGbM=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
Expand Down
54 changes: 32 additions & 22 deletions scm/driver/harness/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package harness
import (
"context"
"fmt"
"strings"
"time"

"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/drone/go-scm/scm"
)

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

// native data structures
Expand Down Expand Up @@ -134,6 +132,20 @@ type (
Name string `json:"name"`
Sha string `json:"sha"`
}
fileDiff struct {
SHA string `json:"sha"`
OldSHA string `json:"old_sha,omitempty"`
Path string `json:"path"`
OldPath string `json:"old_path,omitempty"`
Status string `json:"status"`
Additions int64 `json:"additions"`
Deletions int64 `json:"deletions"`
Changes int64 `json:"changes"`
ContentURL string `json:"content_url"`
Patch []byte `json:"patch,omitempty"`
IsBinary bool `json:"is_binary"`
IsSubmodule bool `json:"is_submodule"`
}
)

//
Expand Down Expand Up @@ -164,24 +176,12 @@ func convertCommitList(src []*commitInfo) []*scm.Commit {
return dst
}

func convertCompareChanges(src string) []*scm.Change {
files, _, err := gitdiff.Parse(strings.NewReader(src))
if err != nil {
return nil
}

changes := make([]*scm.Change, 0)
for _, f := range files {
changes = append(changes, &scm.Change{
Path: f.NewName,
PrevFilePath: f.OldName,
Added: f.IsNew,
Deleted: f.IsDelete,
Renamed: f.IsRename,
})
func convertChangeList(src []*fileDiff) []*scm.Change {
dst := []*scm.Change{}
for _, v := range src {
dst = append(dst, convertChange(v))
}

return changes
return dst
}

func convertCommitInfo(src *commitInfo) *scm.Commit {
Expand All @@ -200,3 +200,13 @@ func convertCommitInfo(src *commitInfo) *scm.Commit {
},
}
}

func convertChange(src *fileDiff) *scm.Change {
return &scm.Change{
Path: src.Path,
PrevFilePath: src.OldPath,
Added: src.Status == "ADDED",
Renamed: src.Status == "RENAMED",
Deleted: src.Status == "DELETED",
}
}
4 changes: 2 additions & 2 deletions scm/driver/harness/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func TestCreateBranch(t *testing.T) {
}

func TestCompareChanges(t *testing.T) {
source := "a24d87c887957954d6f872bac3676f12cb9f50a2"
target := "5d1eb44a2aae537e5fa649dce3ff8c306af1527e"
source := "542ddabd47d7bfa79359b7b4e2af7f975354e35f"
target := "c7d0d4b21d5cfdf47475ff1f6281ef1a91883d"
defer gock.Off()

gock.New(gockOrigin).
Expand Down
78 changes: 53 additions & 25 deletions scm/driver/harness/testdata/gitdiff.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
diff --git a/five b/five
new file mode 100644
index 0000000..54f9d6d
--- /dev/null
+++ b/five
@@ -0,0 +1 @@
+five
diff --git a/two b/four
similarity index 100%
rename from two
rename to four
diff --git a/one b/one
index 5626abf..9c0408b 100644
--- a/one
+++ b/one
@@ -1 +1,2 @@
one
+modified to two
diff --git a/three b/three
deleted file mode 100644
index 2bdf67a..0000000
--- a/three
+++ /dev/null
@@ -1 +0,0 @@
-three
[
{
"sha": "c4e897c1fcd1cae04abf761f034ae4c5e210caad",
"old_sha": "0000000000000000000000000000000000000000",
"path": "hello.go",
"old_path": "hello.go",
"status": "ADDED",
"additions": 8,
"deletions": 0,
"changes": 8,
"content_url": "/api/v1/hello.go",
"is_binary": false,
"is_submodule": false
},
{
"sha": "0000000000000000000000000000000000000000",
"old_sha": "d7fcbf28651697b00add519d8b4402a5ab46ffc2",
"path": "null.go",
"old_path": "null.go",
"status": "DELETED",
"additions": 0,
"deletions": 118,
"changes": 118,
"content_url": "/api/v1/null.go",
"is_binary": false,
"is_submodule": false
},
{
"sha": "ce5a2cd34b697f7a8507192e7074b33737c6740c",
"old_sha": "7697802e4d16b255e7ea22a86071cfbe9af6aa39",
"path": "version4.go",
"old_path": "version4.go",
"status": "MODIFIED",
"additions": 8,
"deletions": 7,
"changes": 15,
"content_url": "/api/v1/version4.go",
"is_binary": false,
"is_submodule": false
},
{
"sha": "",
"path": "version_1.go",
"old_path": "version1.go",
"status": "RENAMED",
"additions": 0,
"deletions": 0,
"changes": 0,
"content_url": "/api/v1/version_1.go",
"is_binary": false,
"is_submodule": false
}
]
24 changes: 12 additions & 12 deletions scm/driver/harness/testdata/gitdiff.json.golden
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
[
{
"Path": "five",
"Path": "hello.go",
"Added": true,
"Renamed": false,
"Deleted": false,
"Sha": "",
"BlobID": "",
"PrevFilePath": ""
"PrevFilePath": "hello.go"
},
{
"Path": "four",
"Path": "null.go",
"Added": false,
"Renamed": true,
"Deleted": false,
"Renamed": false,
"Deleted": true,
"Sha": "",
"BlobID": "",
"PrevFilePath": "two"
"PrevFilePath": "null.go"
},
{
"Path": "one",
"Path": "version4.go",
"Added": false,
"Renamed": false,
"Deleted": false,
"Sha": "",
"BlobID": "",
"PrevFilePath": "one"
"PrevFilePath": "version4.go"
},
{
"Path": "",
"Path": "version_1.go",
"Added": false,
"Renamed": false,
"Deleted": true,
"Renamed": true,
"Deleted": false,
"Sha": "",
"BlobID": "",
"PrevFilePath": "three"
"PrevFilePath": "version1.go"
}
]