From 4986ac130fd006bfaabe57c0ba1887ccd16628ec Mon Sep 17 00:00:00 2001 From: Casey Hillers Date: Thu, 11 May 2023 09:21:12 -0700 Subject: [PATCH] Add optional filter params on Repositories.listCommits (#368) * Add optional filter params on Repositories.listCommits * dart format --------- Co-authored-by: Rob Becker --- lib/src/common/repos_service.dart | 29 ++++++++++- test/common/data/repos_json.dart | 81 +++++++++++++++++++++++++++++ test/common/repos_service_test.dart | 49 +++++++++++++++++ 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 test/common/data/repos_json.dart create mode 100644 test/common/repos_service_test.dart diff --git a/lib/src/common/repos_service.dart b/lib/src/common/repos_service.dart index 535e1060..a274710e 100644 --- a/lib/src/common/repos_service.dart +++ b/lib/src/common/repos_service.dart @@ -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 listCommits(RepositorySlug slug) { + Stream listCommits( + RepositorySlug slug, { + String? sha, + String? path, + String? author, + String? committer, + DateTime? since, + DateTime? until, + }) { ArgumentError.checkNotNull(slug); + final params = { + 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, RepositoryCommit>( 'GET', '/repos/${slug.fullName}/commits', RepositoryCommit.fromJson, + params: params, ); } diff --git a/test/common/data/repos_json.dart b/test/common/data/repos_json.dart new file mode 100644 index 00000000..f5087cf7 --- /dev/null +++ b/test/common/data/repos_json.dart @@ -0,0 +1,81 @@ +const String listCommits = ''' +[ + { + "sha": "3771b3c9ab1912d32a0d0baffaf489d561caf558", + "node_id": "C_kwDOAVT0d9oAKDM3NzFiM2M5YWIxOTEyZDMyYTBkMGJhZmZhZjQ4OWQ1NjFjYWY1NTg", + "commit": { + "author": { + "name": "Rob Becker", + "email": "rob.becker@workiva.com", + "date": "2023-04-17T14:55:14Z" + }, + "committer": { + "name": "Rob Becker", + "email": "rob.becker@workiva.com", + "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" + } + ] + } +] +'''; diff --git a/test/common/repos_service_test.dart b/test/common/repos_service_test.dart new file mode 100644 index 00000000..feebe05c --- /dev/null +++ b/test/common/repos_service_test.dart @@ -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 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)); + }); +}