Fix diff api response conversion for harness compareChange (#269)

* Fix diff api response conversion
pull/117/merge
Shubham Agrawal 9 months ago committed by GitHub
parent 5098db8648
commit 8764ac057e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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=

@ -7,10 +7,8 @@ package harness
import (
"context"
"fmt"
"strings"
"time"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/drone/go-scm/scm"
)
@ -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
@ -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"`
}
)
//
@ -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 {
@ -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",
}
}

@ -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).

@ -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
}
]

@ -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"
}
]
Loading…
Cancel
Save