Skip to content

Commit 3139864

Browse files
Update example with Octokit Checks API methods (#3375)
1 parent f4b572f commit 3139864

File tree

1 file changed

+91
-127
lines changed

1 file changed

+91
-127
lines changed

content/developers/apps/creating-ci-tests-with-the-checks-api.md

Lines changed: 91 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -140,44 +140,39 @@ You'll add this new method as a [Sinatra helper](https://github.com/sinatra/sina
140140
``` ruby
141141
# Create a new check run with the status queued
142142
def create_check_run
143-
# # At the time of writing, Octokit does not support the Checks API yet, but
144-
# it does provide generic HTTP methods you can use:
145-
# /rest/reference/checks#create-a-check-run
146-
check_run = @installation_client.post(
147-
"repos/#{@payload['repository']['full_name']}/check-runs",
148-
{
149-
accept: 'application/vnd.github.v3+json',
150-
# The name of your check run.
151-
name: 'Octo RuboCop',
152-
# The payload structure differs depending on whether a check run or a check suite event occurred.
153-
head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha']
154-
}
143+
@installation_client.create_check_run(
144+
# [String, Integer, Hash, Octokit Repository object] A GitHub repository.
145+
@payload['repository']['full_name'],
146+
# [String] The name of your check run.
147+
'Octo RuboCop',
148+
# [String] The SHA of the commit to check
149+
# The payload structure differs depending on whether a check run or a check suite event occurred.
150+
@payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'],
151+
# [Hash] 'Accept' header option, to avoid a warning about the API not being ready for production use.
152+
accept: 'application/vnd.github.v3+json'
155153
)
156154
end
157155
```
158156
{% else %}
159157
``` ruby
160158
# Create a new check run with the status queued
161159
def create_check_run
162-
# # At the time of writing, Octokit does not support the Checks API yet, but
163-
# it does provide generic HTTP methods you can use:
164-
# /rest/reference/checks#create-a-check-run
165-
check_run = @installation_client.post(
166-
"repos/#{@payload['repository']['full_name']}/check-runs",
167-
{
168-
# This header allows for beta access to Checks API
169-
accept: 'application/vnd.github.antiope-preview+json',
170-
# The name of your check run.
171-
name: 'Octo RuboCop',
172-
# The payload structure differs depending on whether a check run or a check suite event occurred.
173-
head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha']
174-
}
160+
@installation_client.create_check_run(
161+
# [String, Integer, Hash, Octokit Repository object] A GitHub repository.
162+
@payload['repository']['full_name'],
163+
# [String] The name of your check run.
164+
'Octo RuboCop',
165+
# [String] The SHA of the commit to check
166+
# The payload structure differs depending on whether a check run or a check suite event occurred.
167+
@payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'],
168+
# [Hash] 'Accept' header option, to avoid a warning about the API not being ready for production use.
169+
accept: 'application/vnd.github.antiope-preview+json'
175170
)
176171
end
177172
```
178173
{% endif %}
179174

180-
This code calls the "[Create a check run](/rest/reference/checks#create-a-check-run)" endpoint using the generic [HTTP `POST` method](http://octokit.github.io/octokit.rb/Octokit/Connection.html#post-instance_method). This method takes two parameters: the URL of the endpoint and the input parameters to the method.
175+
This code calls the "[Create a check run](/rest/reference/checks#create-a-check-run)" endpoint using the [create_check_run method](https://rdoc.info/gems/octokit/Octokit%2FClient%2FChecks:create_check_run).
181176

182177
To create a check run, only two input parameters are required: `name` and `head_sha`. We will use [Rubocop](https://rubocop.readthedocs.io/en/latest/) to implement the CI test later in this quickstart, which is why the name "Octo Rubocop" is used here, but you can choose any name you'd like for the check run.
183178

@@ -240,31 +235,22 @@ def initiate_check_run
240235
# to 'in_progress' and run the CI process. When the CI finishes, you'll
241236
# update the check run status to 'completed' and add the CI results.
242237

243-
# Octokit doesn't yet support the Checks API, but it does provide generic
244-
# HTTP methods you can use:
245-
# /rest/reference/checks#update-a-check-run
246-
updated_check_run = @installation_client.patch(
247-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
248-
{
249-
accept: 'application/vnd.github.v3+json',
250-
name: 'Octo RuboCop',
251-
status: 'in_progress',
252-
started_at: Time.now.utc.iso8601
253-
}
238+
@installation_client.update_check_run(
239+
@payload['repository']['full_name'],
240+
@payload['check_run']['id'],
241+
status: 'in_progress',
242+
accept: 'application/vnd.github.v3+json'
254243
)
255244

256245
# ***** RUN A CI TEST *****
257246

258247
# Mark the check run as complete!
259-
updated_check_run = @installation_client.patch(
260-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
261-
{
262-
accept: 'application/vnd.github.v3+json',
263-
name: 'Octo RuboCop',
264-
status: 'completed',
265-
conclusion: 'success',
266-
completed_at: Time.now.utc.iso8601
267-
}
248+
@installation_client.update_check_run(
249+
@payload['repository']['full_name'],
250+
@payload['check_run']['id'],
251+
status: 'completed',
252+
conclusion: 'success',
253+
accept: 'application/vnd.github.v3+json'
268254
)
269255
end
270256
```
@@ -276,40 +262,30 @@ def initiate_check_run
276262
# to 'in_progress' and run the CI process. When the CI finishes, you'll
277263
# update the check run status to 'completed' and add the CI results.
278264

279-
# Octokit doesn't yet support the Checks API, but it does provide generic
280-
# HTTP methods you can use:
281-
# /rest/reference/checks#update-a-check-run
282-
updated_check_run = @installation_client.patch(
283-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
284-
{
285-
accept: 'application/vnd.github.antiope-preview+json', # This header is necessary for beta access to Checks API
286-
name: 'Octo RuboCop',
287-
status: 'in_progress',
288-
started_at: Time.now.utc.iso8601
289-
}
265+
@installation_client.update_check_run(
266+
@payload['repository']['full_name'],
267+
@payload['check_run']['id'],
268+
status: 'in_progress',
269+
accept: 'application/vnd.github.antiope-preview+json'
290270
)
291271

292272
# ***** RUN A CI TEST *****
293273

294274
# Mark the check run as complete!
295-
updated_check_run = @installation_client.patch(
296-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
297-
{
298-
# This header is necessary for beta access to Checks API
299-
accept: 'application/vnd.github.antiope-preview+json',
300-
name: 'Octo RuboCop',
301-
status: 'completed',
302-
conclusion: 'success',
303-
completed_at: Time.now.utc.iso8601
304-
}
275+
@installation_client.update_check_run(
276+
@payload['repository']['full_name'],
277+
@payload['check_run']['id'],
278+
status: 'completed',
279+
conclusion: 'success',
280+
accept: 'application/vnd.github.antiope-preview+json'
305281
)
306282
end
307283
```
308284
{% endif %}
309285

310-
The code above calls the "[Update a check run](/rest/reference/checks#update-a-check-run)" API endpoint using the generic [`patch` HTTP method](http://octokit.github.io/octokit.rb/Octokit/Connection.html#patch-instance_method) to update the check run that you already created.
286+
The code above calls the "[Update a check run](/rest/reference/checks#update-a-check-run)" API endpoint using the [`update_check_run` Octokit method](https://rdoc.info/gems/octokit/Octokit%2FClient%2FChecks:update_check_run) to update the check run that you already created.
311287

312-
Here's what this code is doing. First, it updates the check run's status to `in_progress` and sets the `started_at` time to the current time. In [Part 2](#part-2-creating-the-octo-rubocop-ci-test) of this quickstart, you'll add code that kicks off a real CI test under `***** RUN A CI TEST *****`. For now, you'll leave that section as a placeholder, so the code that follows it will just simulate that the CI process succeeds and all tests pass. Finally, the code updates the status of the check run again to `completed`.
288+
Here's what this code is doing. First, it updates the check run's status to `in_progress` and implicitly sets the `started_at` time to the current time. In [Part 2](#part-2-creating-the-octo-rubocop-ci-test) of this quickstart, you'll add code that kicks off a real CI test under `***** RUN A CI TEST *****`. For now, you'll leave that section as a placeholder, so the code that follows it will just simulate that the CI process succeeds and all tests pass. Finally, the code updates the status of the check run again to `completed`.
313289

314290
You'll notice in the "[Update a check run](/rest/reference/checks#update-a-check-run)" docs that when you provide a status of `completed`, the `conclusion` and `completed_at` parameters are required. The `conclusion` summarizes the outcome of a check run and can be `success`, `failure`, `neutral`, `cancelled`, `timed_out`, or `action_required`. You'll set the conclusion to `success`, the `completed_at` time to the current time, and the status to `completed`.
315291

@@ -613,29 +589,23 @@ Now you've got all the information you need to update your check run. In the [fi
613589
{% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "[email protected]" or currentVersion == "github-ae@latest" %}
614590
``` ruby
615591
# Mark the check run as complete!
616-
updated_check_run = @installation_client.patch(
617-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
618-
{
619-
accept: 'application/vnd.github.v3+json',
620-
name: 'Octo RuboCop',
621-
status: 'completed',
622-
conclusion: 'success',
623-
completed_at: Time.now.utc.iso8601
624-
}
592+
@installation_client.update_check_run(
593+
@payload['repository']['full_name'],
594+
@payload['check_run']['id'],
595+
status: 'completed',
596+
conclusion: 'success',
597+
accept: 'application/vnd.github.v3+json'
625598
)
626599
```
627600
{% else %}
628601
``` ruby
629602
# Mark the check run as complete!
630-
updated_check_run = @installation_client.patch(
631-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
632-
{
633-
accept: 'application/vnd.github.antiope-preview+json', # This header is necessary for beta access to Checks API
634-
name: 'Octo RuboCop',
635-
status: 'completed',
636-
conclusion: 'success',
637-
completed_at: Time.now.utc.iso8601
638-
}
603+
@installation_client.update_check_run(
604+
@payload['repository']['full_name'],
605+
@payload['check_run']['id'],
606+
status: 'completed',
607+
conclusion: 'success',
608+
accept: 'application/vnd.github.antiope-preview+json' # This header is necessary for beta access to Checks API
639609
)
640610
```
641611
{% endif %}
@@ -645,51 +615,45 @@ You'll need to update that code to use the `conclusion` variable you set based o
645615
{% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "[email protected]" or currentVersion == "github-ae@latest" %}
646616
``` ruby
647617
# Mark the check run as complete! And if there are warnings, share them.
648-
updated_check_run = @installation_client.patch(
649-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
650-
{
651-
accept: 'application/vnd.github.v3+json',
652-
name: 'Octo RuboCop',
653-
status: 'completed',
654-
conclusion: conclusion,
655-
completed_at: Time.now.utc.iso8601,
656-
output: {
657-
title: 'Octo RuboCop',
658-
summary: summary,
659-
text: text,
660-
annotations: annotations
661-
},
662-
actions: [{
663-
label: 'Fix this',
664-
description: 'Automatically fix all linter notices.',
665-
identifier: 'fix_rubocop_notices'
666-
}]
667-
}
618+
@installation_client.update_check_run(
619+
@payload['repository']['full_name'],
620+
@payload['check_run']['id'],
621+
status: 'completed',
622+
conclusion: conclusion,
623+
output: {
624+
title: 'Octo RuboCop',
625+
summary: summary,
626+
text: text,
627+
annotations: annotations
628+
},
629+
actions: [{
630+
label: 'Fix this',
631+
description: 'Automatically fix all linter notices.',
632+
identifier: 'fix_rubocop_notices'
633+
}],
634+
accept: 'application/vnd.github.v3+json'
668635
)
669636
```
670637
{% else %}
671638
``` ruby
672639
# Mark the check run as complete! And if there are warnings, share them.
673-
updated_check_run = @installation_client.patch(
674-
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
675-
{
676-
accept: 'application/vnd.github.antiope-preview+json',
677-
name: 'Octo RuboCop',
678-
status: 'completed',
679-
conclusion: conclusion,
680-
completed_at: Time.now.utc.iso8601,
681-
output: {
682-
title: 'Octo RuboCop',
683-
summary: summary,
684-
text: text,
685-
annotations: annotations
686-
},
687-
actions: [{
688-
label: 'Fix this',
689-
description: 'Automatically fix all linter notices.',
690-
identifier: 'fix_rubocop_notices'
691-
}]
692-
}
640+
@installation_client.update_check_run(
641+
@payload['repository']['full_name'],
642+
@payload['check_run']['id'],
643+
status: 'completed',
644+
conclusion: conclusion,
645+
output: {
646+
title: 'Octo RuboCop',
647+
summary: summary,
648+
text: text,
649+
annotations: annotations
650+
},
651+
actions: [{
652+
label: 'Fix this',
653+
description: 'Automatically fix all linter notices.',
654+
identifier: 'fix_rubocop_notices'
655+
}],
656+
accept: 'application/vnd.github.antiope-preview+json'
693657
)
694658
```
695659
{% endif %}

0 commit comments

Comments
 (0)