|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:github/github.dart'; |
| 4 | +import 'package:github/src/util.dart'; |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | + |
| 7 | +const _previewHeader = 'application/vnd.github.antiope-preview+json'; |
| 8 | + |
| 9 | +/// Contains methods to interact with the Checks API. |
| 10 | +/// |
| 11 | +/// API docs: https://developer.github.com/v3/checks/ |
| 12 | +class ChecksService extends Service { |
| 13 | + /// Methods to interact with Check Runs. |
| 14 | + /// |
| 15 | + /// API docs: https://developer.github.com/v3/checks/runs/ |
| 16 | + final CheckRunsService checkRuns; |
| 17 | + ChecksService(GitHub github) |
| 18 | + : checkRuns = CheckRunsService._(github), |
| 19 | + super(github); |
| 20 | + |
| 21 | + // TODO: implement Check Suites methods https://developer.github.com/v3/checks/suites/ |
| 22 | +} |
| 23 | + |
| 24 | +class CheckRunsService extends Service { |
| 25 | + CheckRunsService._(GitHub github) : super(github); |
| 26 | + |
| 27 | + /// Creates a new check run for a specific commit in a repository. |
| 28 | + /// Your GitHub App must have the `checks:write` permission to create check runs. |
| 29 | + /// * [name]: The name of the check. For example, "code-coverage". |
| 30 | + /// * [headSha]: The SHA of the commit. |
| 31 | + /// * [detailsUrl]: The URL of the integrator's site that has the full details of the check. |
| 32 | + /// * [externalId]: A reference for the run on the integrator's system. |
| 33 | + /// * [status]: The current status. Can be one of queued, in_progress, or completed. Default: queued. |
| 34 | + /// * [startedAt]: The time that the check run began. |
| 35 | + /// * [conclusion]: **Required if you provide completed_at or a status of completed.** The final conclusion of the check. |
| 36 | + /// When the conclusion is action_required, additional details should be provided on the site specified by details_url. **Note**: Providing conclusion will automatically set the status parameter to completed. |
| 37 | + /// * [completedAt]: The time the check completed. |
| 38 | + /// * [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. |
| 39 | + /// * [actions]: Displays a button on GitHub that can be clicked to alert your app to do additional tasks. |
| 40 | + /// For example, a code linting app can display a button that automatically fixes detected errors. |
| 41 | + /// The button created in this object is displayed after the check run completes. |
| 42 | + /// When a user clicks the button, GitHub sends the check_run.requested_action webhook to your app. |
| 43 | + /// A maximum of three actions are accepted. |
| 44 | + /// |
| 45 | + /// API docs: https://developer.github.com/v3/checks/runs/#create-a-check-run |
| 46 | + Future<CheckRun> createCheckRun( |
| 47 | + RepositorySlug slug, { |
| 48 | + @required String name, |
| 49 | + @required String headSha, |
| 50 | + String detailsUrl, |
| 51 | + String externalId, |
| 52 | + CheckRunStatus status = CheckRunStatus.queued, |
| 53 | + DateTime startedAt, |
| 54 | + CheckRunConclusion conclusion, |
| 55 | + DateTime completedAt, |
| 56 | + CheckRunOutput output, |
| 57 | + List<CheckRunAction> actions, |
| 58 | + }) async { |
| 59 | + assert(conclusion != null || |
| 60 | + (completedAt == null && status != CheckRunStatus.completed)); |
| 61 | + assert(actions == null || actions.length <= 3); |
| 62 | + return github.postJSON<Map<String, dynamic>, CheckRun>( |
| 63 | + '/repos/${slug.fullName}/check-runs', |
| 64 | + statusCode: StatusCodes.CREATED, |
| 65 | + preview: _previewHeader, |
| 66 | + body: jsonEncode(createNonNullMap(<String, dynamic>{ |
| 67 | + 'name': name, |
| 68 | + 'head_sha': headSha, |
| 69 | + 'details_url': detailsUrl, |
| 70 | + 'external_id': externalId, |
| 71 | + 'status': status, |
| 72 | + 'started_at': dateToGitHubIso8601(startedAt), |
| 73 | + 'conclusion': conclusion, |
| 74 | + 'completed_at': dateToGitHubIso8601(completedAt), |
| 75 | + 'output': output, |
| 76 | + 'actions': actions, |
| 77 | + })), |
| 78 | + convert: (i) => CheckRun.fromJson(i), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /// Updates a check run for a specific commit in a repository. |
| 83 | + /// Your GitHub App must have the `checks:write` permission to edit check runs. |
| 84 | + /// |
| 85 | + /// * [name]: The name of the check. For example, "code-coverage". |
| 86 | + /// * [detailsUrl]: The URL of the integrator's site that has the full details of the check. |
| 87 | + /// * [externalId]: A reference for the run on the integrator's system. |
| 88 | + /// * [status]: The current status. Can be one of queued, in_progress, or completed. Default: queued. |
| 89 | + /// * [startedAt]: The time that the check run began. |
| 90 | + /// * [conclusion]: **Required if you provide completed_at or a status of completed.** The final conclusion of the check. |
| 91 | + /// When the conclusion is action_required, additional details should be provided on the site specified by details_url. **Note**: Providing conclusion will automatically set the status parameter to completed. |
| 92 | + /// * [completedAt]: The time the check completed. |
| 93 | + /// * [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. |
| 94 | + /// * [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. |
| 95 | + /// |
| 96 | + /// API docs: https://developer.github.com/v3/checks/runs/#update-a-check-run |
| 97 | + Future<CheckRun> updateCheckRun( |
| 98 | + RepositorySlug slug, |
| 99 | + CheckRun checkRunToUpdate, { |
| 100 | + String name, |
| 101 | + String detailsUrl, |
| 102 | + String externalId, |
| 103 | + DateTime startedAt, |
| 104 | + CheckRunStatus status = CheckRunStatus.queued, |
| 105 | + CheckRunConclusion conclusion, |
| 106 | + DateTime completedAt, |
| 107 | + CheckRunOutput output, |
| 108 | + List<CheckRunAction> actions, |
| 109 | + }) async { |
| 110 | + assert(conclusion != null || |
| 111 | + (completedAt == null && status != CheckRunStatus.completed)); |
| 112 | + assert(actions == null || actions.length <= 3); |
| 113 | + return github.requestJson<Map<String, dynamic>, CheckRun>( |
| 114 | + 'PATCH', |
| 115 | + '/repos/${slug.fullName}/check-runs/${checkRunToUpdate.id}', |
| 116 | + statusCode: StatusCodes.OK, |
| 117 | + preview: _previewHeader, |
| 118 | + body: jsonEncode(createNonNullMap(<String, dynamic>{ |
| 119 | + 'name': name, |
| 120 | + 'details_url': detailsUrl, |
| 121 | + 'external_id': externalId, |
| 122 | + 'started_at': dateToGitHubIso8601(startedAt), |
| 123 | + 'status': status, |
| 124 | + 'conclusion': conclusion, |
| 125 | + 'completed_at': dateToGitHubIso8601(completedAt), |
| 126 | + 'output': output, |
| 127 | + 'actions': actions, |
| 128 | + })), |
| 129 | + convert: (i) => CheckRun.fromJson(i), |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + /// Lists check runs for a commit [ref]. |
| 134 | + /// The `[ref]` can be a SHA, branch name, or a tag name. |
| 135 | + /// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check runs. |
| 136 | + /// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository. |
| 137 | + /// * [checkName]: returns check runs with the specified name. |
| 138 | + /// * [status]: returns check runs with the specified status. |
| 139 | + /// * [filter]: filters check runs by their completed_at timestamp. Can be one of latest (returning the most recent check runs) or all. Default: latest. |
| 140 | + /// |
| 141 | + /// API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref |
| 142 | + Stream<CheckRun> listCheckRunsForRef( |
| 143 | + RepositorySlug slug, { |
| 144 | + @required String ref, |
| 145 | + String checkName, |
| 146 | + CheckRunStatus status, |
| 147 | + CheckRunFilter filter, |
| 148 | + }) { |
| 149 | + ArgumentError.checkNotNull(ref); |
| 150 | + return PaginationHelper(github).objects<Map<String, dynamic>, CheckRun>( |
| 151 | + 'GET', |
| 152 | + 'repos/$slug/commits/$ref/check-runs', |
| 153 | + (input) => CheckRun.fromJson(input), |
| 154 | + statusCode: StatusCodes.OK, |
| 155 | + preview: _previewHeader, |
| 156 | + params: createNonNullMap({ |
| 157 | + 'check_name': checkName, |
| 158 | + 'filter': filter, |
| 159 | + 'status': status, |
| 160 | + }), |
| 161 | + arrayKey: 'check_runs', |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + /// Lists check runs for a check suite using its [checkSuiteId]. |
| 166 | + /// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check runs. |
| 167 | + /// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository. |
| 168 | + /// * [checkName]: returns check runs with the specified name. |
| 169 | + /// * [status]: returns check runs with the specified status. |
| 170 | + /// * [filter]: filters check runs by their completed_at timestamp. Can be one of latest (returning the most recent check runs) or all. Default: latest. |
| 171 | + /// |
| 172 | + /// API docs: https://developer.github.com/v3/checks/runs/#list-check-runs-in-a-check-suite |
| 173 | + Stream<CheckRun> listCheckRunsInSuite( |
| 174 | + RepositorySlug slug, { |
| 175 | + @required int checkSuiteId, |
| 176 | + String checkName, |
| 177 | + CheckRunStatus status, |
| 178 | + CheckRunFilter filter, |
| 179 | + }) { |
| 180 | + ArgumentError.checkNotNull(checkSuiteId); |
| 181 | + return PaginationHelper(github).objects<Map<String, dynamic>, CheckRun>( |
| 182 | + 'GET', |
| 183 | + 'repos/$slug/check-suites/$checkSuiteId/check-runs', |
| 184 | + (input) => CheckRun.fromJson(input), |
| 185 | + statusCode: StatusCodes.OK, |
| 186 | + preview: _previewHeader, |
| 187 | + params: createNonNullMap({ |
| 188 | + 'check_name': checkName, |
| 189 | + 'status': status, |
| 190 | + 'filter': filter, |
| 191 | + }), |
| 192 | + arrayKey: 'check_runs', |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + /// Gets a single check run using its [checkRunId]. |
| 197 | + /// GitHub Apps must have the `checks:read` permission on a private repository or pull access to a public repository to get check runs. |
| 198 | + /// OAuth Apps and authenticated users must have the `repo` scope to get check runs in a private repository. |
| 199 | + /// |
| 200 | + /// API docs: https://developer.github.com/v3/checks/runs/#get-a-single-check-run |
| 201 | + Future<CheckRun> getCheckRun( |
| 202 | + RepositorySlug slug, { |
| 203 | + @required int checkRunId, |
| 204 | + }) { |
| 205 | + ArgumentError.checkNotNull(checkRunId); |
| 206 | + return github.getJSON<Map<String, dynamic>, CheckRun>( |
| 207 | + 'repos/${slug.fullName}/check-runs/$checkRunId', |
| 208 | + preview: _previewHeader, |
| 209 | + statusCode: StatusCodes.OK, |
| 210 | + convert: (i) => CheckRun.fromJson(i), |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + /// Lists annotations for a check run. |
| 215 | + /// 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. |
| 216 | + /// OAuth Apps and authenticated users must have the `repo` scope to get annotations for a check run in a private repository. |
| 217 | + /// |
| 218 | + /// API docs: https://developer.github.com/v3/checks/runs/#list-annotations-for-a-check-run |
| 219 | + Stream<CheckRunAnnotation> listAnnotationsInCheckRun( |
| 220 | + RepositorySlug slug, { |
| 221 | + @required CheckRun checkRun, |
| 222 | + }) { |
| 223 | + return PaginationHelper(github) |
| 224 | + .objects<Map<String, dynamic>, CheckRunAnnotation>( |
| 225 | + 'GET', |
| 226 | + '/repos/${slug.fullName}/check-runs/${checkRun.id}/annotations', |
| 227 | + (i) => CheckRunAnnotation.fromJSON(i), |
| 228 | + statusCode: StatusCodes.OK, |
| 229 | + preview: _previewHeader, |
| 230 | + ); |
| 231 | + } |
| 232 | +} |
0 commit comments