Skip to content

Commit 71b214b

Browse files
Merge pull request #38 from chhsia0/git-compare-changes
Added `CompareChanges` function to `GitService`.
2 parents d8fff5c + 838ea43 commit 71b214b

File tree

17 files changed

+649
-1
lines changed

17 files changed

+649
-1
lines changed

scm/driver/bitbucket/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm
6969
return convertDiffstats(out), res, err
7070
}
7171

72+
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
73+
path := fmt.Sprintf("2.0/repositories/%s/diffstat/%s..%s?%s", repo, source, target, encodeListOptions(opts))
74+
out := new(diffstats)
75+
res, err := s.client.do(ctx, "GET", path, nil, &out)
76+
copyPagination(out.pagination, res)
77+
return convertDiffstats(out), res, err
78+
}
79+
7280
type branch struct {
7381
Type string `json:"type"`
7482
Name string `json:"name"`

scm/driver/bitbucket/git_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,29 @@ func TestGitListChanges(t *testing.T) {
204204
t.Log(diff)
205205
}
206206
}
207+
func TestGitCompareChanges(t *testing.T) {
208+
defer gock.Off()
209+
210+
gock.New("https://api.bitbucket.org").
211+
Get("/2.0/repositories/atlassian/atlaskit/diffstat/dec26e0fe887167743c2b7e36531dedfeb6cd478..425863f9dbe56d70c8dcdbf2e4e0805e85591fcc").
212+
MatchParam("page", "1").
213+
MatchParam("pagelen", "30").
214+
Reply(200).
215+
Type("application/json").
216+
File("testdata/diffstat.json")
217+
218+
client, _ := New("https://api.bitbucket.org")
219+
got, _, err := client.Git.CompareChanges(context.Background(), "atlassian/atlaskit", "dec26e0fe887167743c2b7e36531dedfeb6cd478", "425863f9dbe56d70c8dcdbf2e4e0805e85591fcc", scm.ListOptions{Page: 1, Size: 30})
220+
if err != nil {
221+
t.Error(err)
222+
}
223+
224+
want := []*scm.Change{}
225+
raw, _ := ioutil.ReadFile("testdata/diffstat.json.golden")
226+
json.Unmarshal(raw, &want)
227+
228+
if diff := cmp.Diff(got, want); diff != "" {
229+
t.Errorf("Unexpected Results")
230+
t.Log(diff)
231+
}
232+
}

scm/driver/gitea/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
5353
return nil, nil, scm.ErrNotSupported
5454
}
5555

56+
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
57+
return nil, nil, scm.ErrNotSupported
58+
}
59+
5660
//
5761
// native data structures
5862
//

scm/driver/github/git.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.Li
6262
return convertChangeList(out.Files), res, err
6363
}
6464

65+
func (s *gitService) CompareChanges(ctx context.Context, repo, source, target string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
66+
path := fmt.Sprintf("repos/%s/compare/%s...%s", repo, source, target)
67+
out := new(compare)
68+
res, err := s.client.do(ctx, "GET", path, nil, &out)
69+
return convertChangeList(out.Files), res, err
70+
}
71+
6572
type branch struct {
6673
Name string `json:"name"`
6774
Commit commit `json:"commit"`
@@ -95,6 +102,10 @@ type commit struct {
95102
Files []*file `json:"files"`
96103
}
97104

105+
type compare struct {
106+
Files []*file `json:"files"`
107+
}
108+
98109
func convertCommitList(from []*commit) []*scm.Commit {
99110
to := []*scm.Commit{}
100111
for _, v := range from {

scm/driver/github/git_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,33 @@ func TestGitListChanges(t *testing.T) {
216216
t.Run("Request", testRequest(res))
217217
t.Run("Rate", testRate(res))
218218
}
219+
220+
func TestGitCompareChanges(t *testing.T) {
221+
defer gock.Off()
222+
223+
gock.New("https://api.github.com").
224+
Get("/repos/octocat/hello-world/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d").
225+
Reply(200).
226+
Type("application/json").
227+
SetHeaders(mockHeaders).
228+
File("testdata/compare.json")
229+
230+
client := NewDefault()
231+
got, res, err := client.Git.CompareChanges(context.Background(), "octocat/hello-world", "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e", "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", scm.ListOptions{})
232+
if err != nil {
233+
t.Error(err)
234+
return
235+
}
236+
237+
want := []*scm.Change{}
238+
raw, _ := ioutil.ReadFile("testdata/compare.json.golden")
239+
json.Unmarshal(raw, &want)
240+
241+
if diff := cmp.Diff(got, want); diff != "" {
242+
t.Errorf("Unexpected Results")
243+
t.Log(diff)
244+
}
245+
246+
t.Run("Request", testRequest(res))
247+
t.Run("Rate", testRate(res))
248+
}
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"url": "https://api.github.com/repos/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
3+
"html_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
4+
"permalink_url": "https://github.com/octocat/Hello-World/compare/octocat:553c207...octocat:7fd1a60",
5+
"diff_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.diff",
6+
"patch_url": "https://github.com/octocat/Hello-World/compare/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e...7fd1a60b01f91b314f59955a4e4d4e80d8edf11d.patch",
7+
"base_commit": {
8+
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
9+
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo1NTNjMjA3N2YwZWRjM2Q1ZGM1ZDE3MjYyZjZhYTQ5OGU2OWQ2Zjhl",
10+
"commit": {
11+
"author": {
12+
"name": "cameronmcefee",
13+
"email": "[email protected]",
14+
"date": "2011-01-26T19:06:08Z"
15+
},
16+
"committer": {
17+
"name": "cameronmcefee",
18+
"email": "[email protected]",
19+
"date": "2011-01-26T19:06:08Z"
20+
},
21+
"message": "first commit",
22+
"tree": {
23+
"sha": "fcf4a9bba6857422971d67147517eb5edfdbf48d",
24+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/fcf4a9bba6857422971d67147517eb5edfdbf48d"
25+
},
26+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
27+
"comment_count": 51,
28+
"verification": {
29+
"verified": false,
30+
"reason": "unsigned",
31+
"signature": null,
32+
"payload": null
33+
}
34+
},
35+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
36+
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
37+
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e/comments",
38+
"author": null,
39+
"committer": null,
40+
"parents": []
41+
},
42+
"merge_base_commit": {
43+
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
44+
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo1NTNjMjA3N2YwZWRjM2Q1ZGM1ZDE3MjYyZjZhYTQ5OGU2OWQ2Zjhl",
45+
"commit": {
46+
"author": {
47+
"name": "cameronmcefee",
48+
"email": "[email protected]",
49+
"date": "2011-01-26T19:06:08Z"
50+
},
51+
"committer": {
52+
"name": "cameronmcefee",
53+
"email": "[email protected]",
54+
"date": "2011-01-26T19:06:08Z"
55+
},
56+
"message": "first commit",
57+
"tree": {
58+
"sha": "fcf4a9bba6857422971d67147517eb5edfdbf48d",
59+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/fcf4a9bba6857422971d67147517eb5edfdbf48d"
60+
},
61+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
62+
"comment_count": 51,
63+
"verification": {
64+
"verified": false,
65+
"reason": "unsigned",
66+
"signature": null,
67+
"payload": null
68+
}
69+
},
70+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
71+
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
72+
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e/comments",
73+
"author": null,
74+
"committer": null,
75+
"parents": []
76+
},
77+
"status": "ahead",
78+
"ahead_by": 2,
79+
"behind_by": 0,
80+
"total_commits": 2,
81+
"commits": [
82+
{
83+
"sha": "762941318ee16e59dabbacb1b4049eec22f0d303",
84+
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo3NjI5NDEzMThlZTE2ZTU5ZGFiYmFjYjFiNDA0OWVlYzIyZjBkMzAz",
85+
"commit": {
86+
"author": {
87+
"name": "Johnneylee Jack Rollins",
88+
"email": "[email protected]",
89+
"date": "2011-09-14T04:42:41Z"
90+
},
91+
"committer": {
92+
"name": "Johnneylee Jack Rollins",
93+
"email": "[email protected]",
94+
"date": "2011-09-14T04:42:41Z"
95+
},
96+
"message": "New line at end of file. --Signed off by Spaceghost",
97+
"tree": {
98+
"sha": "b4eecafa9be2f2006ce1b709d6857b07069b4608",
99+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/b4eecafa9be2f2006ce1b709d6857b07069b4608"
100+
},
101+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
102+
"comment_count": 213,
103+
"verification": {
104+
"verified": false,
105+
"reason": "unsigned",
106+
"signature": null,
107+
"payload": null
108+
}
109+
},
110+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
111+
"html_url": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303",
112+
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303/comments",
113+
"author": {
114+
"login": "Spaceghost",
115+
"id": 251370,
116+
"node_id": "MDQ6VXNlcjI1MTM3MA==",
117+
"avatar_url": "https://avatars2.githubusercontent.com/u/251370?v=4",
118+
"gravatar_id": "",
119+
"url": "https://api.github.com/users/Spaceghost",
120+
"html_url": "https://github.com/Spaceghost",
121+
"followers_url": "https://api.github.com/users/Spaceghost/followers",
122+
"following_url": "https://api.github.com/users/Spaceghost/following{/other_user}",
123+
"gists_url": "https://api.github.com/users/Spaceghost/gists{/gist_id}",
124+
"starred_url": "https://api.github.com/users/Spaceghost/starred{/owner}{/repo}",
125+
"subscriptions_url": "https://api.github.com/users/Spaceghost/subscriptions",
126+
"organizations_url": "https://api.github.com/users/Spaceghost/orgs",
127+
"repos_url": "https://api.github.com/users/Spaceghost/repos",
128+
"events_url": "https://api.github.com/users/Spaceghost/events{/privacy}",
129+
"received_events_url": "https://api.github.com/users/Spaceghost/received_events",
130+
"type": "User",
131+
"site_admin": false
132+
},
133+
"committer": {
134+
"login": "Spaceghost",
135+
"id": 251370,
136+
"node_id": "MDQ6VXNlcjI1MTM3MA==",
137+
"avatar_url": "https://avatars2.githubusercontent.com/u/251370?v=4",
138+
"gravatar_id": "",
139+
"url": "https://api.github.com/users/Spaceghost",
140+
"html_url": "https://github.com/Spaceghost",
141+
"followers_url": "https://api.github.com/users/Spaceghost/followers",
142+
"following_url": "https://api.github.com/users/Spaceghost/following{/other_user}",
143+
"gists_url": "https://api.github.com/users/Spaceghost/gists{/gist_id}",
144+
"starred_url": "https://api.github.com/users/Spaceghost/starred{/owner}{/repo}",
145+
"subscriptions_url": "https://api.github.com/users/Spaceghost/subscriptions",
146+
"organizations_url": "https://api.github.com/users/Spaceghost/orgs",
147+
"repos_url": "https://api.github.com/users/Spaceghost/repos",
148+
"events_url": "https://api.github.com/users/Spaceghost/events{/privacy}",
149+
"received_events_url": "https://api.github.com/users/Spaceghost/received_events",
150+
"type": "User",
151+
"site_admin": false
152+
},
153+
"parents": [
154+
{
155+
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
156+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
157+
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e"
158+
}
159+
]
160+
},
161+
{
162+
"sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
163+
"node_id": "MDY6Q29tbWl0MTI5NjI2OTo3ZmQxYTYwYjAxZjkxYjMxNGY1OTk1NWE0ZTRkNGU4MGQ4ZWRmMTFk",
164+
"commit": {
165+
"author": {
166+
"name": "The Octocat",
167+
"email": "[email protected]",
168+
"date": "2012-03-06T23:06:50Z"
169+
},
170+
"committer": {
171+
"name": "The Octocat",
172+
"email": "[email protected]",
173+
"date": "2012-03-06T23:06:50Z"
174+
},
175+
"message": "Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file.",
176+
"tree": {
177+
"sha": "b4eecafa9be2f2006ce1b709d6857b07069b4608",
178+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/b4eecafa9be2f2006ce1b709d6857b07069b4608"
179+
},
180+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
181+
"comment_count": 61,
182+
"verification": {
183+
"verified": false,
184+
"reason": "unsigned",
185+
"signature": null,
186+
"payload": null
187+
}
188+
},
189+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
190+
"html_url": "https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
191+
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/comments",
192+
"author": {
193+
"login": "octocat",
194+
"id": 583231,
195+
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
196+
"avatar_url": "https://avatars3.githubusercontent.com/u/583231?v=4",
197+
"gravatar_id": "",
198+
"url": "https://api.github.com/users/octocat",
199+
"html_url": "https://github.com/octocat",
200+
"followers_url": "https://api.github.com/users/octocat/followers",
201+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
202+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
203+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
204+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
205+
"organizations_url": "https://api.github.com/users/octocat/orgs",
206+
"repos_url": "https://api.github.com/users/octocat/repos",
207+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
208+
"received_events_url": "https://api.github.com/users/octocat/received_events",
209+
"type": "User",
210+
"site_admin": false
211+
},
212+
"committer": {
213+
"login": "octocat",
214+
"id": 583231,
215+
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
216+
"avatar_url": "https://avatars3.githubusercontent.com/u/583231?v=4",
217+
"gravatar_id": "",
218+
"url": "https://api.github.com/users/octocat",
219+
"html_url": "https://github.com/octocat",
220+
"followers_url": "https://api.github.com/users/octocat/followers",
221+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
222+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
223+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
224+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
225+
"organizations_url": "https://api.github.com/users/octocat/orgs",
226+
"repos_url": "https://api.github.com/users/octocat/repos",
227+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
228+
"received_events_url": "https://api.github.com/users/octocat/received_events",
229+
"type": "User",
230+
"site_admin": false
231+
},
232+
"parents": [
233+
{
234+
"sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
235+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
236+
"html_url": "https://github.com/octocat/Hello-World/commit/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e"
237+
},
238+
{
239+
"sha": "762941318ee16e59dabbacb1b4049eec22f0d303",
240+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303",
241+
"html_url": "https://github.com/octocat/Hello-World/commit/762941318ee16e59dabbacb1b4049eec22f0d303"
242+
}
243+
]
244+
}
245+
],
246+
"files": [
247+
{
248+
"sha": "980a0d5f19a64b4b30a87d4206aade58726b60e3",
249+
"filename": "README",
250+
"status": "modified",
251+
"additions": 1,
252+
"deletions": 1,
253+
"changes": 2,
254+
"blob_url": "https://github.com/octocat/Hello-World/blob/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
255+
"raw_url": "https://github.com/octocat/Hello-World/raw/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
256+
"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/README?ref=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
257+
"patch": "@@ -1 +1 @@\n-Hello World!\n\\ No newline at end of file\n+Hello World!"
258+
}
259+
]
260+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"Path": "README",
4+
"Added": false,
5+
"Renamed": false,
6+
"Deleted": false
7+
}
8+
]

0 commit comments

Comments
 (0)