Skip to content

Commit

Permalink
Implement Check Runs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
axel-op committed Mar 9, 2020
1 parent 3f8848a commit 79e584c
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 70 deletions.
133 changes: 125 additions & 8 deletions lib/src/common/checks_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ import 'package:github/github.dart';
import 'package:github/src/util.dart';
import 'package:meta/meta.dart';

Map<String, String> get _headersBase => Map<String, String>.from(const {
'Accept': 'application/vnd.github.antiope-preview+json',
});

/// Contains methods to interact with the Checks API.
///
/// API docs: https://developer.github.com/v3/checks/
class ChecksService extends Service {
ChecksService(GitHub gitHub) : super(gitHub);
/// Methods to interact with Check Runs.
///
/// API docs: https://developer.github.com/v3/checks/runs/
final CheckRunsService checkRuns;
ChecksService(GitHub github)
: checkRuns = CheckRunsService._(github),
super(github);

// TODO: implement Check Suites methods https://developer.github.com/v3/checks/suites/
}

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.
/// Your GitHub App must have the `checks:write` permission to create check runs.
/// * [name]: The name of the check. For example, "code-coverage".
/// * [headSha]: The SHA of the commit.
/// * [detailsUrl]: The URL of the integrator's site that has the full details of the check.
Expand Down Expand Up @@ -48,9 +64,7 @@ class ChecksService extends Service {
return github.postJSON<Map<String, dynamic>, CheckRun>(
'/repos/${slug.fullName}/check-runs',
statusCode: StatusCodes.CREATED,
headers: <String, String>{
'Accept': 'application/vnd.github.antiope-preview+json'
},
headers: _headersBase,
body: jsonEncode(createNonNullMap(<String, dynamic>{
'name': name,
'head_sha': headSha,
Expand All @@ -67,6 +81,9 @@ class ChecksService extends Service {
);
}

/// Updates a check run for a specific commit in a repository.
/// Your GitHub App must have the `checks:write` permission to edit check runs.
///
/// * [name]: The name of the check. For example, "code-coverage".
/// * [detailsUrl]: The URL of the integrator's site that has the full details of the check.
/// * [externalId]: A reference for the run on the integrator's system.
Expand All @@ -77,6 +94,8 @@ class ChecksService extends Service {
/// * [completedAt]: The time the check completed.
/// * [output]: Check runs can accept a variety of data in the output object, including a title and summary and can optionally provide descriptive details about the run.
/// * [actions]: Possible further actions the integrator can perform, which a user may trigger. Each action includes a label, identifier and description. A maximum of three actions are accepted.
///
/// API docs: https://developer.github.com/v3/checks/runs/#update-a-check-run
Future<CheckRun> updateCheckRun(
RepositorySlug slug,
CheckRun checkRunToUpdate, {
Expand All @@ -97,9 +116,7 @@ class ChecksService extends Service {
'PATCH',
'/repos/${slug.fullName}/check-runs/${checkRunToUpdate.id}',
statusCode: StatusCodes.OK,
headers: <String, String>{
'Accept': 'application/vnd.github.antiope-preview+json'
},
headers: _headersBase,
body: jsonEncode(createNonNullMap(<String, dynamic>{
'name': name,
'details_url': detailsUrl,
Expand All @@ -114,4 +131,104 @@ class ChecksService extends Service {
convert: (i) => CheckRun.fromJson(i),
);
}

/// Lists check runs 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 get check runs.
/// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository.
/// * [checkName]: returns check runs with the specified name.
/// * [status]: returns check runs with the specified status.
/// * [filter]: filters check runs by their completed_at timestamp. Can be one of latest (returning the most recent check runs) or all. Default: latest.
///
/// API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref
Stream<CheckRun> listCheckRunsForRef(
RepositorySlug slug, {
@required String ref,
String checkName,
CheckRunStatus status,
CheckRunFilter filter,
}) {
ArgumentError.checkNotNull(ref);
return PaginationHelper(github).objects<Map<String, dynamic>, CheckRun>(
'GET',
'repos/$slug/commits/$ref/check-runs',
(input) => CheckRun.fromJson(input),
statusCode: StatusCodes.OK,
headers: _headersBase,
params: createNonNullMap({
'check_name': checkName,
'filter': filter,
'status': status,
}),
arrayKey: 'check_runs',
);
}

/// Lists check runs for a check suite using its [checkSuiteId].
/// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check runs.
/// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository.
/// * [checkName]: returns check runs with the specified name.
/// * [status]: returns check runs with the specified status.
/// * [filter]: filters check runs by their completed_at timestamp. Can be one of latest (returning the most recent check runs) or all. Default: latest.
///
/// API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-in-a-check-suite
Stream<CheckRun> listCheckRunsInSuite(
RepositorySlug slug, {
@required int checkSuiteId,
String checkName,
CheckRunStatus status,
CheckRunFilter filter,
}) {
ArgumentError.checkNotNull(checkSuiteId);
return PaginationHelper(github).objects<Map<String, dynamic>, CheckRun>(
'GET',
'repos/$slug/check-suites/$checkSuiteId/check-runs',
(input) => CheckRun.fromJson(input),
statusCode: StatusCodes.OK,
headers: _headersBase,
params: createNonNullMap({
'check_name': checkName,
'status': status,
'filter': filter,
}),
arrayKey: 'check_runs',
);
}

/// Gets a single check run using its [checkRunId].
/// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check runs.
/// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository.
///
/// API docs: https://developer.github.com/v3/checks/runs/#get-a-single-check-run
Future<CheckRun> getCheckRun(
RepositorySlug slug, {
@required int checkRunId,
}) {
ArgumentError.checkNotNull(checkRunId);
return github.getJSON<Map<String, dynamic>, CheckRun>(
'repos/${slug.fullName}/check-runs/$checkRunId',
headers: _headersBase,
statusCode: StatusCodes.OK,
convert: (i) => CheckRun.fromJson(i),
);
}

/// Lists annotations for a check run.
/// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get annotations for a check run.
/// OAuth Apps and authenticated users must have the `repo` scope to get annotations for a check run in a private repository.
///
/// API docs: https://developer.github.com/v3/checks/runs/#list-annotations-for-a-check-run
Stream<CheckRunAnnotation> listAnnotationsInCheckRun(
RepositorySlug slug, {
@required CheckRun checkRun,
}) {
return PaginationHelper(github)
.objects<Map<String, dynamic>, CheckRunAnnotation>(
'GET',
'/repos/${slug.fullName}/check-runs/${checkRun.id}/annotations',
(i) => CheckRunAnnotation.fromJSON(i),
statusCode: StatusCodes.OK,
headers: _headersBase,
);
}
}
60 changes: 21 additions & 39 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,71 +80,53 @@ class GitHub {
int _rateLimitReset, _rateLimitLimit, _rateLimitRemaining;

/// Service for activity related methods of the GitHub API.
ActivityService get activity {
return _activity ??= ActivityService(this);
}
ActivityService get activity => _activity ??= ActivityService(this);

/// Service for autorizations related methods of the GitHub API.
///
/// Note: You can only access this API via Basic Authentication using your
/// username and password, not tokens.
AuthorizationsService get authorizations {
return _authorizations ??= AuthorizationsService(this);
}
AuthorizationsService get authorizations =>
_authorizations ??= AuthorizationsService(this);

/// Service for gist related methods of the GitHub API.
GistsService get gists {
return _gists ??= GistsService(this);
}
GistsService get gists => _gists ??= GistsService(this);

/// Service for git data related methods of the GitHub API.
GitService get git {
return _git ??= GitService(this);
}
GitService get git => _git ??= GitService(this);

/// Service for issues related methods of the GitHub API.
IssuesService get issues {
return _issues ??= IssuesService(this);
}
IssuesService get issues => _issues ??= IssuesService(this);

/// Service for misc related methods of the GitHub API.
MiscService get misc {
return _misc ??= MiscService(this);
}
MiscService get misc => _misc ??= MiscService(this);

/// Service for organization related methods of the GitHub API.
OrganizationsService get organizations {
return _organizations ??= OrganizationsService(this);
}
OrganizationsService get organizations =>
_organizations ??= OrganizationsService(this);

/// Service for pull requests related methods of the GitHub API.
PullRequestsService get pullRequests {
return _pullRequests ??= PullRequestsService(this);
}
PullRequestsService get pullRequests =>
_pullRequests ??= PullRequestsService(this);

/// Service for repository related methods of the GitHub API.
RepositoriesService get repositories {
return _repositories ??= RepositoriesService(this);
}
RepositoriesService get repositories =>
_repositories ??= RepositoriesService(this);

/// Service for search related methods of the GitHub API.
SearchService get search {
return _search ??= SearchService(this);
}
SearchService get search => _search ??= SearchService(this);

/// Service to provide a handy method to access GitHub's url shortener.
UrlShortenerService get urlShortener {
return _urlShortener ??= UrlShortenerService(this);
}
UrlShortenerService get urlShortener =>
_urlShortener ??= UrlShortenerService(this);

/// Service for user related methods of the GitHub API.
UsersService get users {
return _users ??= UsersService(this);
}
UsersService get users => _users ??= UsersService(this);

ChecksService get checks {
return _checks ??= ChecksService(this);
}
/// Service containing methods to interact with the Checks API.
///
/// See https://developer.github.com/v3/checks/
ChecksService get checks => _checks ??= ChecksService(this);

/// Handles Get Requests that respond with JSON
/// [path] can either be a path like '/repos' or a full url.
Expand Down
39 changes: 39 additions & 0 deletions lib/src/common/model/checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@ class CheckRunAnnotationLevel extends _EnumWithValue {
static const notice = CheckRunAnnotationLevel._('notice');
static const warning = CheckRunAnnotationLevel._('warning');
static const failure = CheckRunAnnotationLevel._('failure');

const CheckRunAnnotationLevel._(String value) : super._(value);

factory CheckRunAnnotationLevel._fromValue(String value) {
switch (value) {
case 'notice':
return notice;
case 'warning':
return warning;
case 'failure':
return failure;
default:
throw Exception(
'This level of check run annotation is unimplemented: $value.');
}
}

bool operator <(CheckRunAnnotationLevel other) {
if (this == failure) {
return false;
Expand Down Expand Up @@ -58,6 +73,12 @@ class CheckRunStatus extends _EnumWithValue {
const CheckRunStatus._(String value) : super._(value);
}

class CheckRunFilter extends _EnumWithValue {
static const all = CheckRunFilter._('all');
static const latest = CheckRunFilter._('latest');
const CheckRunFilter._(String value) : super._(value);
}

@immutable
class CheckRun {
final String name;
Expand Down Expand Up @@ -223,6 +244,24 @@ class CheckRunAnnotation {
@override
int get hashCode => path.hashCode;

factory CheckRunAnnotation.fromJSON(Map<String, dynamic> input) {
if (input == null) {
return null;
}
return CheckRunAnnotation(
path: input['path'],
startLine: input['start_line'],
endLine: input['end_line'],
startColumn: input['start_column'],
endColumn: input['end_column'],
annotationLevel:
CheckRunAnnotationLevel._fromValue(input['annotation_level']),
title: input['title'],
message: input['message'],
rawDetails: input['raw_details'],
);
}

Map<String, dynamic> _toJson() {
return createNonNullMap(<String, dynamic>{
'path': path,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/repos_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class RepositoriesService extends Service {
/// API docs: https://developer.github.com/v3/repos/collaborators/#list
Stream<Collaborator> listCollaborators(RepositorySlug slug) {
ArgumentError.checkNotNull(slug);
return PaginationHelper(github).objects(
return PaginationHelper(github).objects<Map<String, dynamic>, Collaborator>(
'GET',
'/repos/${slug.fullName}/collaborators',
(json) => Collaborator.fromJson(json),
Expand Down

0 comments on commit 79e584c

Please sign in to comment.