Skip to content

Commit

Permalink
Implemented Check Runs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
axel-op committed Jan 20, 2020
1 parent 7564bd2 commit f6ac352
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 59 deletions.
97 changes: 91 additions & 6 deletions lib/src/common/checks_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import 'package:meta/meta.dart';
class ChecksService extends Service {
ChecksService(GitHub gitHub) : super(gitHub);

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

/// 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.
/// * [name]: The name of the check. For example, "code-coverage".
Expand Down Expand Up @@ -48,9 +52,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 +69,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 +82,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 +104,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 +119,84 @@ 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);
// TODO: help wanted to implement this method
throw UnimplementedError();
}

/// 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);
// TODO: help wanted to implement this method
throw UnimplementedError();
}

/// 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,
);
}

// TODO: implement Check Suites methods https://developer.github.com/v3/checks/suites/
}
73 changes: 21 additions & 52 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,84 +80,53 @@ class GitHub {
int _rateLimitReset, _rateLimitLimit, _rateLimitRemaining;

/// Service for activity related methods of the GitHub API.
ActivityService get activity {
_activity ??= ActivityService(this);
return _activity;
}
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 {
_authorizations ??= AuthorizationsService(this);
return _authorizations;
}
AuthorizationsService get authorizations =>
_authorizations ??= AuthorizationsService(this);

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

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

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

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

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

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

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

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

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

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

ChecksService get checks {
_checks ??= ChecksService(this);
return _checks;
}
/// 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
37 changes: 37 additions & 0 deletions lib/src/common/model/checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,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;
if (this == notice) return other != notice;
Expand Down Expand Up @@ -52,6 +67,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 @@ -198,6 +219,22 @@ class CheckRunAnnotation {
'Annotations only support start_column and end_column on the same line.'),
assert(title.length <= 255);

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 f6ac352

Please sign in to comment.