Skip to content

Added CompareChanges function to GitService. #38

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 1 commit into from
Oct 4, 2019
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
8 changes: 8 additions & 0 deletions scm/driver/bitbucket/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm
return convertDiffstats(out), res, err
}

func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/diffstat/%s..%s?%s", repo, source, target, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
return convertDiffstats(out), res, err
}

type branch struct {
Type string `json:"type"`
Name string `json:"name"`
Expand Down
26 changes: 26 additions & 0 deletions scm/driver/bitbucket/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,29 @@ func TestGitListChanges(t *testing.T) {
t.Log(diff)
}
}
func TestGitCompareChanges(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/atlaskit/diffstat/dec26e0fe887167743c2b7e36531dedfeb6cd478..425863f9dbe56d70c8dcdbf2e4e0805e85591fcc").
MatchParam("page", "1").
MatchParam("pagelen", "30").
Reply(200).
Type("application/json").
File("testdata/diffstat.json")

client, _ := New("https://api.bitbucket.org")
got, _, err := client.Git.CompareChanges(context.Background(), "atlassian/atlaskit", "dec26e0fe887167743c2b7e36531dedfeb6cd478", "425863f9dbe56d70c8dcdbf2e4e0805e85591fcc", scm.ListOptions{Page: 1, Size: 30})
if err != nil {
t.Error(err)
}

want := []*scm.Change{}
raw, _ := ioutil.ReadFile("testdata/diffstat.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
4 changes: 4 additions & 0 deletions scm/driver/gitea/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
return nil, nil, scm.ErrNotSupported
}

func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

//
// native data structures
//
Expand Down
11 changes: 11 additions & 0 deletions scm/driver/github/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
return convertChangeList(out.Files), res, err
}

func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/compare/%s...%s", repo, source, target)
out := new(compare)
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertChangeList(out.Files), res, err
}

type branch struct {
Name string `json:"name"`
Commit commit `json:"commit"`
Expand Down Expand Up @@ -95,6 +102,10 @@ type commit struct {
Files []*file `json:"files"`
}

type compare struct {
Files []*file `json:"files"`
}

func convertCommitList(from []*commit) []*scm.Commit {
to := []*scm.Commit{}
for _, v := range from {
Expand Down
30 changes: 30 additions & 0 deletions scm/driver/github/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,33 @@ func TestGitListChanges(t *testing.T) {
t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestGitCompareChanges(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Get("/repos/octocat/hello-world/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/compare.json")

client := NewDefault()
got, res, err := client.Git.CompareChanges(context.Background(), "octocat/hello-world", "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e", "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", scm.ListOptions{})
if err != nil {
t.Error(err)
return
}

want := []*scm.Change{}
raw, _ := ioutil.ReadFile("testdata/compare.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}
260 changes: 260 additions & 0 deletions scm/driver/github/testdata/compare.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"url": "https://api.github.com/repos/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"html_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"permalink_url": "https://github.com/octocat/Hello-World/compare/octocat:553c207...octocat:7fd1a60",
"diff_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.diff",
"patch_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.patch",
"base_commit": {
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo1NTNjMjA3N2YwZWRjM2Q1ZGM1ZDE3MjYyZjZhYTQ5OGU2OWQ2Zjhl",
"commit": {
"author": {
"name": "cameronmcefee",
"email": "[email protected]",
"date": "2011-01-26T19:06:08Z"
},
"committer": {
"name": "cameronmcefee",
"email": "[email protected]",
"date": "2011-01-26T19:06:08Z"
},
"message": "first commit",
"tree": {
"sha": "fcf4a9bba6857422971d67147517eb5edfdbf48d",
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/fcf4a9bba6857422971d67147517eb5edfdbf48d"
},
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"comment_count": 51,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e/comments",
"author": null,
"committer": null,
"parents": []
},
"merge_base_commit": {
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo1NTNjMjA3N2YwZWRjM2Q1ZGM1ZDE3MjYyZjZhYTQ5OGU2OWQ2Zjhl",
"commit": {
"author": {
"name": "cameronmcefee",
"email": "[email protected]",
"date": "2011-01-26T19:06:08Z"
},
"committer": {
"name": "cameronmcefee",
"email": "[email protected]",
"date": "2011-01-26T19:06:08Z"
},
"message": "first commit",
"tree": {
"sha": "fcf4a9bba6857422971d67147517eb5edfdbf48d",
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/fcf4a9bba6857422971d67147517eb5edfdbf48d"
},
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"comment_count": 51,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e/comments",
"author": null,
"committer": null,
"parents": []
},
"status": "ahead",
"ahead_by": 2,
"behind_by": 0,
"total_commits": 2,
"commits": [
{
"sha": "762941318ee16e59dabbacb1b4049eec22f0d303",
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo3NjI5NDEzMThlZTE2ZTU5ZGFiYmFjYjFiNDA0OWVlYzIyZjBkMzAz",
"commit": {
"author": {
"name": "Johnneylee Jack Rollins",
"email": "[email protected]",
"date": "2011-09-14T04:42:41Z"
},
"committer": {
"name": "Johnneylee Jack Rollins",
"email": "[email protected]",
"date": "2011-09-14T04:42:41Z"
},
"message": "New line at end of file. --Signed off by Spaceghost",
"tree": {
"sha": "b4eecafa9be2f2006ce1b709d6857b07069b4608",
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/b4eecafa9be2f2006ce1b709d6857b07069b4608"
},
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
"comment_count": 213,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
"html_url": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303/comments",
"author": {
"login": "Spaceghost",
"id": 251370,
"node_id": "MDQ6VXNlcjI1MTM3MA==",
"avatar_url": "https://avatars2.githubusercontent.com/u/251370?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Spaceghost",
"html_url": "https://github.com/Spaceghost",
"followers_url": "https://api.github.com/users/Spaceghost/followers",
"following_url": "https://api.github.com/users/Spaceghost/following{/other_user}",
"gists_url": "https://api.github.com/users/Spaceghost/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Spaceghost/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Spaceghost/subscriptions",
"organizations_url": "https://api.github.com/users/Spaceghost/orgs",
"repos_url": "https://api.github.com/users/Spaceghost/repos",
"events_url": "https://api.github.com/users/Spaceghost/events{/privacy}",
"received_events_url": "https://api.github.com/users/Spaceghost/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "Spaceghost",
"id": 251370,
"node_id": "MDQ6VXNlcjI1MTM3MA==",
"avatar_url": "https://avatars2.githubusercontent.com/u/251370?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Spaceghost",
"html_url": "https://github.com/Spaceghost",
"followers_url": "https://api.github.com/users/Spaceghost/followers",
"following_url": "https://api.github.com/users/Spaceghost/following{/other_user}",
"gists_url": "https://api.github.com/users/Spaceghost/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Spaceghost/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Spaceghost/subscriptions",
"organizations_url": "https://api.github.com/users/Spaceghost/orgs",
"repos_url": "https://api.github.com/users/Spaceghost/repos",
"events_url": "https://api.github.com/users/Spaceghost/events{/privacy}",
"received_events_url": "https://api.github.com/users/Spaceghost/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e"
}
]
},
{
"sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo3ZmQxYTYwYjAxZjkxYjMxNGY1OTk1NWE0ZTRkNGU4MGQ4ZWRmMTFk",
"commit": {
"author": {
"name": "The Octocat",
"email": "[email protected]",
"date": "2012-03-06T23:06:50Z"
},
"committer": {
"name": "The Octocat",
"email": "[email protected]",
"date": "2012-03-06T23:06:50Z"
},
"message": "Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file.",
"tree": {
"sha": "b4eecafa9be2f2006ce1b709d6857b07069b4608",
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/b4eecafa9be2f2006ce1b709d6857b07069b4608"
},
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"comment_count": 61,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"html_url": "https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/comments",
"author": {
"login": "octocat",
"id": 583231,
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
"avatar_url": "https://avatars3.githubusercontent.com/u/583231?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "octocat",
"id": 583231,
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
"avatar_url": "https://avatars3.githubusercontent.com/u/583231?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e"
},
{
"sha": "762941318ee16e59dabbacb1b4049eec22f0d303",
"url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
"html_url": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303"
}
]
}
],
"files": [
{
"sha": "980a0d5f19a64b4b30a87d4206aade58726b60e3",
"filename": "README",
"status": "modified",
"additions": 1,
"deletions": 1,
"changes": 2,
"blob_url": "https://github.com/octocat/Hello-World/blob/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
"raw_url": "https://github.com/octocat/Hello-World/raw/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/README?ref=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
"patch": "@@ -1 +1 @@\n-Hello World!\n\\ No newline at end of file\n+Hello World!"
}
]
}
8 changes: 8 additions & 0 deletions scm/driver/github/testdata/compare.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"Path": "README",
"Added": false,
"Renamed": false,
"Deleted": false
}
]
Loading