diff --git a/go.mod b/go.mod index 61703b6e..1a7e7c5e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index bce3cf42..6d69376b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/scm/driver/harness/git.go b/scm/driver/harness/git.go index bde9965e..309a125f 100644 --- a/scm/driver/harness/git.go +++ b/scm/driver/harness/git.go @@ -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", + } +} diff --git a/scm/driver/harness/git_test.go b/scm/driver/harness/git_test.go index 79dae39e..51570506 100644 --- a/scm/driver/harness/git_test.go +++ b/scm/driver/harness/git_test.go @@ -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). diff --git a/scm/driver/harness/testdata/gitdiff.json b/scm/driver/harness/testdata/gitdiff.json index d44e597b..c4dd5768 100644 --- a/scm/driver/harness/testdata/gitdiff.json +++ b/scm/driver/harness/testdata/gitdiff.json @@ -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 \ No newline at end of file +[ + { + "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 + } +] \ No newline at end of file diff --git a/scm/driver/harness/testdata/gitdiff.json.golden b/scm/driver/harness/testdata/gitdiff.json.golden index 6439c0c0..c3258c31 100644 --- a/scm/driver/harness/testdata/gitdiff.json.golden +++ b/scm/driver/harness/testdata/gitdiff.json.golden @@ -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" } ] \ No newline at end of file