Skip to content

Commit

Permalink
Add optional filter params on Repositories.listCommits (#368)
Browse files Browse the repository at this point in the history
* Add optional filter params on Repositories.listCommits

* dart format

---------

Co-authored-by: Rob Becker <[email protected]>
  • Loading branch information
Casey Hillers and robbecker-wf authored May 11, 2023
1 parent 87babe4 commit 4986ac1
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/src/common/repos_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,41 @@ class RepositoriesService extends Service {

/// Lists the commits of the provided repository [slug].
///
/// [sha] is the SHA or branch to start listing commits from. Default: the
/// repository’s default branch (usually main).
///
/// [path] will only show commits that changed that file path.
///
/// [author] and [committer] are the GitHub username to filter commits for.
///
/// [since] shows commit after this time, and [until] shows commits before
/// this time.
///
/// API docs: https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository
Stream<RepositoryCommit> listCommits(RepositorySlug slug) {
Stream<RepositoryCommit> listCommits(
RepositorySlug slug, {
String? sha,
String? path,
String? author,
String? committer,
DateTime? since,
DateTime? until,
}) {
ArgumentError.checkNotNull(slug);
final params = <String, dynamic>{
if (author != null) 'author': author,
if (committer != null) 'committer': committer,
if (sha != null) 'sha': sha,
if (path != null) 'path': path,
if (since != null) 'since': since.toIso8601String(),
if (until != null) 'until': until.toIso8601String(),
};
return PaginationHelper(github)
.objects<Map<String, dynamic>, RepositoryCommit>(
'GET',
'/repos/${slug.fullName}/commits',
RepositoryCommit.fromJson,
params: params,
);
}

Expand Down
81 changes: 81 additions & 0 deletions test/common/data/repos_json.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const String listCommits = '''
[
{
"sha": "3771b3c9ab1912d32a0d0baffaf489d561caf558",
"node_id": "C_kwDOAVT0d9oAKDM3NzFiM2M5YWIxOTEyZDMyYTBkMGJhZmZhZjQ4OWQ1NjFjYWY1NTg",
"commit": {
"author": {
"name": "Rob Becker",
"email": "[email protected]",
"date": "2023-04-17T14:55:14Z"
},
"committer": {
"name": "Rob Becker",
"email": "[email protected]",
"date": "2023-04-17T14:55:14Z"
},
"message": "prep 9.12.0",
"tree": {
"sha": "282532b41e8fead81ec6d68e7e603139e7dd7581",
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/git/trees/282532b41e8fead81ec6d68e7e603139e7dd7581"
},
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/git/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558",
"comment_count": 0,
"verification": {
"verified": true,
"reason": "valid"
}
},
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558",
"html_url": "https://github.com/SpinlockLabs/github.dart/commit/3771b3c9ab1912d32a0d0baffaf489d561caf558",
"comments_url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/3771b3c9ab1912d32a0d0baffaf489d561caf558/comments",
"author": {
"login": "robbecker-wf",
"id": 6053699,
"node_id": "MDQ6VXNlcjYwNTM2OTk=",
"avatar_url": "https://avatars.githubusercontent.com/u/6053699?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/robbecker-wf",
"html_url": "https://github.com/robbecker-wf",
"followers_url": "https://api.github.com/users/robbecker-wf/followers",
"following_url": "https://api.github.com/users/robbecker-wf/following{/other_user}",
"gists_url": "https://api.github.com/users/robbecker-wf/gists{/gist_id}",
"starred_url": "https://api.github.com/users/robbecker-wf/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/robbecker-wf/subscriptions",
"organizations_url": "https://api.github.com/users/robbecker-wf/orgs",
"repos_url": "https://api.github.com/users/robbecker-wf/repos",
"events_url": "https://api.github.com/users/robbecker-wf/events{/privacy}",
"received_events_url": "https://api.github.com/users/robbecker-wf/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "robbecker-wf",
"id": 6053699,
"node_id": "MDQ6VXNlcjYwNTM2OTk=",
"avatar_url": "https://avatars.githubusercontent.com/u/6053699?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/robbecker-wf",
"html_url": "https://github.com/robbecker-wf",
"followers_url": "https://api.github.com/users/robbecker-wf/followers",
"following_url": "https://api.github.com/users/robbecker-wf/following{/other_user}",
"gists_url": "https://api.github.com/users/robbecker-wf/gists{/gist_id}",
"starred_url": "https://api.github.com/users/robbecker-wf/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/robbecker-wf/subscriptions",
"organizations_url": "https://api.github.com/users/robbecker-wf/orgs",
"repos_url": "https://api.github.com/users/robbecker-wf/repos",
"events_url": "https://api.github.com/users/robbecker-wf/events{/privacy}",
"received_events_url": "https://api.github.com/users/robbecker-wf/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "a3081681da68383d9a5f18ef3502f47f7144e7d8",
"url": "https://api.github.com/repos/SpinlockLabs/github.dart/commits/a3081681da68383d9a5f18ef3502f47f7144e7d8",
"html_url": "https://github.com/SpinlockLabs/github.dart/commit/a3081681da68383d9a5f18ef3502f47f7144e7d8"
}
]
}
]
''';
49 changes: 49 additions & 0 deletions test/common/repos_service_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:github/src/common.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:test/test.dart';

import 'data/repos_json.dart';

void main() {
final slug = RepositorySlug('SpinlockLabs', 'github.dart');
RepositoriesService create(Future<Response> Function(Request) f) {
final client = MockClient(f);
final github = GitHub(client: client);
return RepositoriesService(github);
}

test('listCommits', () async {
final repositories = create((request) async {
expect(request.url.path, '/repos/${slug.fullName}/commits');
expect(request.url.query, 'page=1');

return Response(listCommits, StatusCodes.OK);
});
final commits = await repositories.listCommits(slug).toList();
expect(commits, hasLength(1));
});

test('listCommits with query params', () async {
final repositories = create((request) async {
expect(request.url.path, '/repos/${slug.fullName}/commits');
expect(
request.url.query,
'author=octocat&committer=octodog&sha=abc&path=%2Fpath&since=2022-02-22T00%3A00%3A00.000&until=2023-02-22T00%3A00%3A00.000&page=1',
);
return Response(listCommits, StatusCodes.OK);
});
final commits = await repositories
.listCommits(
slug,
sha: 'abc',
path: '/path',
author: 'octocat',
committer: 'octodog',
since: DateTime(2022, 2, 22),
until: DateTime(2023, 2, 22),
)
.toList();
expect(commits, hasLength(1));
});
}

0 comments on commit 4986ac1

Please sign in to comment.