From 632f35fdbbc983359ab14f179fb0f66f49fe1ec7 Mon Sep 17 00:00:00 2001 From: Axel Ogereau-Peltier <49279289+axel-op@users.noreply.github.com> Date: Mon, 9 Mar 2020 03:33:33 +0100 Subject: [PATCH] Implement Check Suites methods --- lib/src/common/checks_service.dart | 133 +++++++++++++++++++++++++++-- lib/src/common/model/checks.dart | 68 +++++++++++++++ 2 files changed, 195 insertions(+), 6 deletions(-) diff --git a/lib/src/common/checks_service.dart b/lib/src/common/checks_service.dart index d08b2a51..7ef808a9 100644 --- a/lib/src/common/checks_service.dart +++ b/lib/src/common/checks_service.dart @@ -15,16 +15,21 @@ class ChecksService extends Service { /// Methods to interact with Check Runs. /// /// API docs: https://developer.github.com/v3/checks/runs/ - final CheckRunsService checkRuns; + final _CheckRunsService checkRuns; + + /// Methods to interact with Check Suites. + /// + /// API docs: https://developer.github.com/v3/checks/suites/ + final _CheckSuitesService checkSuites; + ChecksService(GitHub github) - : checkRuns = CheckRunsService._(github), + : checkRuns = _CheckRunsService._(github), + checkSuites = _CheckSuitesService._(github), super(github); - - // TODO: implement Check Suites methods https://developer.github.com/v3/checks/suites/ } -class CheckRunsService extends Service { - CheckRunsService._(GitHub github) : super(github); +class _CheckRunsService extends Service { + _CheckRunsService._(GitHub github) : super(github); /// Creates a new check run for a specific commit in a repository. /// Your GitHub App must have the `checks:write` permission to create check runs. @@ -232,3 +237,119 @@ class CheckRunsService extends Service { ); } } + +class _CheckSuitesService extends Service { + _CheckSuitesService._(GitHub github) : super(github); + + /// Gets a single check suite using its `id`. + /// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check suites. + /// OAuth Apps and authenticated users must have the `repo` scope to get check suites in a private repository. + /// + /// API docs: https://developer.github.com/v3/checks/suites/#get-a-single-check-suite + Future getCheckSuite( + RepositorySlug slug, { + @required int checkSuiteId, + }) async { + ArgumentError.checkNotNull(checkSuiteId); + return github.requestJson( + 'GET', + 'repos/$slug/check-suites/$checkSuiteId', + convert: (input) => CheckSuite.fromJson(input), + headers: _headersBase, + statusCode: StatusCodes.OK, + ); + } + + /// Lists check suites for a commit `[ref]`. + /// The `[ref]` can be a SHA, branch name, or a tag name. + /// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to list check suites. + /// OAuth Apps and authenticated users must have the `repo` scope to get check suites in a private repository. + /// * [appId]: Filters check suites by GitHub App id. + /// * [checkName]: Filters checks suites by the name of the check run. + /// + /// API docs: https://developer.github.com/v3/checks/suites/#list-check-suites-for-a-specific-ref + Stream listCheckSuitesForRef( + RepositorySlug slug, { + @required String ref, + int appId, + String checkName, + }) { + ArgumentError.checkNotNull(ref); + return PaginationHelper(github).objects, CheckSuite>( + 'GET', + 'repos/$slug/commits/$ref/check-suites', + (input) => CheckSuite.fromJson(input), + headers: _headersBase, + params: createNonNullMap({ + 'app_id': appId, + 'check_name': checkName, + }), + statusCode: StatusCodes.OK, + arrayKey: 'check_suites', + ); + } + + /// Changes the default automatic flow when creating check suites. + /// By default, the CheckSuiteEvent is automatically created each time code is pushed to a repository. + /// When you disable the automatic creation of check suites, you can manually [Create a check suite](https://developer.github.com/v3/checks/suites/#create-a-check-suite). + /// You must have admin permissions in the repository to set preferences for check suites. + /// * [autoTriggerChecks]: Enables or disables automatic creation of CheckSuite events upon pushes to the repository. Enabled by default. + /// + /// API docs: https://developer.github.com/v3/checks/suites/#set-preferences-for-check-suites-on-a-repository + Future> setPreferencesForCheckSuites( + RepositorySlug slug, { + @required List autoTriggerChecks, + }) { + ArgumentError.checkNotNull(autoTriggerChecks); + return github.requestJson, List>( + 'PATCH', + 'repos/$slug/check-suites/preferences', + statusCode: StatusCodes.OK, + headers: _headersBase, + params: {'auto_trigger_checks': jsonEncode(autoTriggerChecks)}, + convert: (input) => (input['preferences']['auto_trigger_checks'] as List) + .map((e) => AutoTriggerChecks.fromJson(e)) + .toList(), + ); + } + + /// By default, check suites are automatically created when you create a [check run](https://developer.github.com/v3/checks/runs/). + /// You only need to use this endpoint for manually creating check suites when you've disabled automatic creation using "[Set preferences for check suites on a repository](https://developer.github.com/v3/checks/suites/#set-preferences-for-check-suites-on-a-repository)". + /// Your GitHub App must have the `checks:write` permission to create check suites. + /// * [headSha]: The sha of the head commit. + /// + /// API docs: https://developer.github.com/v3/checks/suites/#create-a-check-suite + Future createCheckSuite( + RepositorySlug slug, { + @required String headSha, + }) { + ArgumentError.checkNotNull(headSha); + return github.requestJson, CheckSuite>( + 'POST', + 'repos/$slug/check-suites', + statusCode: StatusCodes.CREATED, + headers: _headersBase, + params: {'head_sha': headSha}, + convert: (input) => CheckSuite.fromJson(input), + ); + } + + /// Triggers GitHub to rerequest an existing check suite, without pushing new code to a repository. + /// This endpoint will trigger the [`check_suite` webhook](https://developer.github.com/v3/activity/events/types/#checksuiteevent) event with the action rerequested. + /// When a check suite is `rerequested`, its `status` is reset to `queued` and the `conclusion` is cleared. + /// To rerequest a check suite, your GitHub App must have the `checks:read` permission on a private repository or pull access to a public repository. + /// + /// API docs: https://developer.github.com/v3/checks/suites/#rerequest-check-suite + Future reRequestCheckSuite( + RepositorySlug slug, { + @required int checkSuiteId, + }) { + ArgumentError.checkNotNull(checkSuiteId); + return github.request( + 'POST', + 'repos/$slug/check-suites/$checkSuiteId/rerequest', + statusCode: StatusCodes.CREATED, + headers: _headersBase, + ); + } +} diff --git a/lib/src/common/model/checks.dart b/lib/src/common/model/checks.dart index e0230263..81568e8f 100644 --- a/lib/src/common/model/checks.dart +++ b/lib/src/common/model/checks.dart @@ -63,7 +63,25 @@ class CheckRunConclusion extends _EnumWithValue { static const cancelled = CheckRunConclusion._('cancelled'); static const timedOut = CheckRunConclusion._('timed_out'); static const actionRequired = CheckRunConclusion._('action_required'); + const CheckRunConclusion._(String value) : super._(value); + + factory CheckRunConclusion._fromValue(String value) { + for (final level in const [ + success, + failure, + neutral, + cancelled, + timedOut, + actionRequired + ]) { + if (level._jsonValue == value) { + return level; + } + } + throw Exception( + 'This level of check run conclusion is unimplemented: $value.'); + } } class CheckRunStatus extends _EnumWithValue { @@ -337,3 +355,53 @@ class CheckRunAction { }); } } + +@immutable +class CheckSuite { + final int id; + final String headSha; + final CheckRunConclusion conclusion; + + const CheckSuite({ + @required this.conclusion, + @required this.headSha, + @required this.id, + }); + + factory CheckSuite.fromJson(Map input) { + if (input == null) { + return null; + } + return CheckSuite( + conclusion: CheckRunConclusion._fromValue(input['conclusion']), + headSha: input['head_sha'], + id: input['id'], + ); + } +} + +@immutable +class AutoTriggerChecks { + /// The id of the GitHub App. + final int appId; + + /// Set to true to enable automatic creation of CheckSuite events upon pushes to the repository, or false to disable them. + final bool setting; + + const AutoTriggerChecks({ + @required this.appId, + this.setting = true, + }) : assert(appId != null); + + factory AutoTriggerChecks.fromJson(Map input) { + if (input == null) { + return null; + } + return AutoTriggerChecks( + appId: input['app_id'], + setting: input['setting'], + ); + } + + Map toJson() => {'app_id': appId, 'setting': setting}; +}