Skip to content

Commit

Permalink
Implement Check Suites methods
Browse files Browse the repository at this point in the history
  • Loading branch information
axel-op committed Mar 9, 2020
1 parent 79e584c commit 632f35f
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 6 deletions.
133 changes: 127 additions & 6 deletions lib/src/common/checks_service.dart
Expand Up @@ -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.
Expand Down Expand Up @@ -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<CheckSuite> 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<CheckSuite> listCheckSuitesForRef(
RepositorySlug slug, {
@required String ref,
int appId,
String checkName,
}) {
ArgumentError.checkNotNull(ref);
return PaginationHelper(github).objects<Map<String, dynamic>, 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<List<AutoTriggerChecks>> setPreferencesForCheckSuites(
RepositorySlug slug, {
@required List<AutoTriggerChecks> autoTriggerChecks,
}) {
ArgumentError.checkNotNull(autoTriggerChecks);
return github.requestJson<Map<String, dynamic>, List<AutoTriggerChecks>>(
'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<CheckSuite> createCheckSuite(
RepositorySlug slug, {
@required String headSha,
}) {
ArgumentError.checkNotNull(headSha);
return github.requestJson<Map<String, dynamic>, 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<void> reRequestCheckSuite(
RepositorySlug slug, {
@required int checkSuiteId,
}) {
ArgumentError.checkNotNull(checkSuiteId);
return github.request(
'POST',
'repos/$slug/check-suites/$checkSuiteId/rerequest',
statusCode: StatusCodes.CREATED,
headers: _headersBase,
);
}
}
68 changes: 68 additions & 0 deletions lib/src/common/model/checks.dart
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String, dynamic> 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<String, dynamic> input) {
if (input == null) {
return null;
}
return AutoTriggerChecks(
appId: input['app_id'],
setting: input['setting'],
);
}

Map<String, dynamic> toJson() => {'app_id': appId, 'setting': setting};
}

0 comments on commit 632f35f

Please sign in to comment.