Skip to content

Commit 4986ac1

Browse files
Casey Hillersrobbecker-wf
andauthored
Add optional filter params on Repositories.listCommits (#368)
* Add optional filter params on Repositories.listCommits * dart format --------- Co-authored-by: Rob Becker <[email protected]>
1 parent 87babe4 commit 4986ac1

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed

lib/src/common/repos_service.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,41 @@ class RepositoriesService extends Service {
448448

449449
/// Lists the commits of the provided repository [slug].
450450
///
451+
/// [sha] is the SHA or branch to start listing commits from. Default: the
452+
/// repository’s default branch (usually main).
453+
///
454+
/// [path] will only show commits that changed that file path.
455+
///
456+
/// [author] and [committer] are the GitHub username to filter commits for.
457+
///
458+
/// [since] shows commit after this time, and [until] shows commits before
459+
/// this time.
460+
///
451461
/// API docs: https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository
452-
Stream<RepositoryCommit> listCommits(RepositorySlug slug) {
462+
Stream<RepositoryCommit> listCommits(
463+
RepositorySlug slug, {
464+
String? sha,
465+
String? path,
466+
String? author,
467+
String? committer,
468+
DateTime? since,
469+
DateTime? until,
470+
}) {
453471
ArgumentError.checkNotNull(slug);
472+
final params = <String, dynamic>{
473+
if (author != null) 'author': author,
474+
if (committer != null) 'committer': committer,
475+
if (sha != null) 'sha': sha,
476+
if (path != null) 'path': path,
477+
if (since != null) 'since': since.toIso8601String(),
478+
if (until != null) 'until': until.toIso8601String(),
479+
};
454480
return PaginationHelper(github)
455481
.objects<Map<String, dynamic>, RepositoryCommit>(
456482
'GET',
457483
'/repos/${slug.fullName}/commits',
458484
RepositoryCommit.fromJson,
485+
params: params,
459486
);
460487
}
461488

test/common/data/repos_json.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const String listCommits = '''
2+
[
3+
{
4+
"sha": "3771b3c9ab1912d32a0d0baffaf489d561caf558",
5+
"node_id": "C_kwDOAVT0d9oAKDM3NzFiM2M5YWIxOTEyZDMyYTBkMGJhZmZhZjQ4OWQ1NjFjYWY1NTg",
6+
"commit": {
7+
"author": {
8+
"name": "Rob Becker",
9+
"email": "[email protected]",
10+
"date": "2023-04-17T14:55:14Z"
11+
},
12+
"committer": {
13+
"name": "Rob Becker",
14+
"email": "[email protected]",
15+
"date": "2023-04-17T14:55:14Z"
16+
},
17+
"message": "prep 9.12.0",
18+
"tree": {
19+
"sha": "282532b41e8fead81ec6d68e7e603139e7dd7581",
20+
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/git/trees/282532b41e8fead81ec6d68e7e603139e7dd7581"
21+
},
22+
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/git/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558",
23+
"comment_count": 0,
24+
"verification": {
25+
"verified": true,
26+
"reason": "valid"
27+
}
28+
},
29+
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558",
30+
"html_url": "https://github.com/SpinlockLabs/github.dart/commit/3771b3c9ab1912d32a0d0baffaf489d561caf558",
31+
"comments_url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558/comments",
32+
"author": {
33+
"login": "robbecker-wf",
34+
"id": 6053699,
35+
"node_id": "MDQ6VXNlcjYwNTM2OTk=",
36+
"avatar_url": "https://avatars.githubusercontent.com/u/6053699?v=4",
37+
"gravatar_id": "",
38+
"url": "https://api.github.com/users/robbecker-wf",
39+
"html_url": "https://github.com/robbecker-wf",
40+
"followers_url": "https://api.github.com/users/robbecker-wf/followers",
41+
"following_url": "https://api.github.com/users/robbecker-wf/following{/other_user}",
42+
"gists_url": "https://api.github.com/users/robbecker-wf/gists{/gist_id}",
43+
"starred_url": "https://api.github.com/users/robbecker-wf/starred{/owner}{/repo}",
44+
"subscriptions_url": "https://api.github.com/users/robbecker-wf/subscriptions",
45+
"organizations_url": "https://api.github.com/users/robbecker-wf/orgs",
46+
"repos_url": "https://api.github.com/users/robbecker-wf/repos",
47+
"events_url": "https://api.github.com/users/robbecker-wf/events{/privacy}",
48+
"received_events_url": "https://api.github.com/users/robbecker-wf/received_events",
49+
"type": "User",
50+
"site_admin": false
51+
},
52+
"committer": {
53+
"login": "robbecker-wf",
54+
"id": 6053699,
55+
"node_id": "MDQ6VXNlcjYwNTM2OTk=",
56+
"avatar_url": "https://avatars.githubusercontent.com/u/6053699?v=4",
57+
"gravatar_id": "",
58+
"url": "https://api.github.com/users/robbecker-wf",
59+
"html_url": "https://github.com/robbecker-wf",
60+
"followers_url": "https://api.github.com/users/robbecker-wf/followers",
61+
"following_url": "https://api.github.com/users/robbecker-wf/following{/other_user}",
62+
"gists_url": "https://api.github.com/users/robbecker-wf/gists{/gist_id}",
63+
"starred_url": "https://api.github.com/users/robbecker-wf/starred{/owner}{/repo}",
64+
"subscriptions_url": "https://api.github.com/users/robbecker-wf/subscriptions",
65+
"organizations_url": "https://api.github.com/users/robbecker-wf/orgs",
66+
"repos_url": "https://api.github.com/users/robbecker-wf/repos",
67+
"events_url": "https://api.github.com/users/robbecker-wf/events{/privacy}",
68+
"received_events_url": "https://api.github.com/users/robbecker-wf/received_events",
69+
"type": "User",
70+
"site_admin": false
71+
},
72+
"parents": [
73+
{
74+
"sha": "a3081681da68383d9a5f18ef3502f47f7144e7d8",
75+
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/a3081681da68383d9a5f18ef3502f47f7144e7d8",
76+
"html_url": "https://github.com/SpinlockLabs/github.dart/commit/a3081681da68383d9a5f18ef3502f47f7144e7d8"
77+
}
78+
]
79+
}
80+
]
81+
''';

test/common/repos_service_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:github/src/common.dart';
2+
import 'package:http/http.dart';
3+
import 'package:http/testing.dart';
4+
import 'package:test/test.dart';
5+
6+
import 'data/repos_json.dart';
7+
8+
void main() {
9+
final slug = RepositorySlug('SpinlockLabs', 'github.dart');
10+
RepositoriesService create(Future<Response> Function(Request) f) {
11+
final client = MockClient(f);
12+
final github = GitHub(client: client);
13+
return RepositoriesService(github);
14+
}
15+
16+
test('listCommits', () async {
17+
final repositories = create((request) async {
18+
expect(request.url.path, '/repos/${slug.fullName}/commits');
19+
expect(request.url.query, 'page=1');
20+
21+
return Response(listCommits, StatusCodes.OK);
22+
});
23+
final commits = await repositories.listCommits(slug).toList();
24+
expect(commits, hasLength(1));
25+
});
26+
27+
test('listCommits with query params', () async {
28+
final repositories = create((request) async {
29+
expect(request.url.path, '/repos/${slug.fullName}/commits');
30+
expect(
31+
request.url.query,
32+
'author=octocat&committer=octodog&sha=abc&path=%2Fpath&since=2022-02-22T00%3A00%3A00.000&until=2023-02-22T00%3A00%3A00.000&page=1',
33+
);
34+
return Response(listCommits, StatusCodes.OK);
35+
});
36+
final commits = await repositories
37+
.listCommits(
38+
slug,
39+
sha: 'abc',
40+
path: '/path',
41+
author: 'octocat',
42+
committer: 'octodog',
43+
since: DateTime(2022, 2, 22),
44+
until: DateTime(2023, 2, 22),
45+
)
46+
.toList();
47+
expect(commits, hasLength(1));
48+
});
49+
}

0 commit comments

Comments
 (0)