1
0
mirror of synced 2025-12-23 03:44:00 -05:00

Update example with Octokit Checks API methods (#3375)

This commit is contained in:
Matias Albarello
2021-04-22 01:25:19 +02:00
committed by GitHub
parent f4b572fb4e
commit 3139864841

View File

@@ -140,18 +140,16 @@ You'll add this new method as a [Sinatra helper](https://github.com/sinatra/sina
``` ruby ``` ruby
# Create a new check run with the status queued # Create a new check run with the status queued
def create_check_run def create_check_run
# # At the time of writing, Octokit does not support the Checks API yet, but @installation_client.create_check_run(
# it does provide generic HTTP methods you can use: # [String, Integer, Hash, Octokit Repository object] A GitHub repository.
# /rest/reference/checks#create-a-check-run @payload['repository']['full_name'],
check_run = @installation_client.post( # [String] The name of your check run.
"repos/#{@payload['repository']['full_name']}/check-runs", 'Octo RuboCop',
{ # [String] The SHA of the commit to check
accept: 'application/vnd.github.v3+json',
# The name of your check run.
name: 'Octo RuboCop',
# The payload structure differs depending on whether a check run or a check suite event occurred. # The payload structure differs depending on whether a check run or a check suite event occurred.
head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'],
} # [Hash] 'Accept' header option, to avoid a warning about the API not being ready for production use.
accept: 'application/vnd.github.v3+json'
) )
end end
``` ```
@@ -159,25 +157,22 @@ end
``` ruby ``` ruby
# Create a new check run with the status queued # Create a new check run with the status queued
def create_check_run def create_check_run
# # At the time of writing, Octokit does not support the Checks API yet, but @installation_client.create_check_run(
# it does provide generic HTTP methods you can use: # [String, Integer, Hash, Octokit Repository object] A GitHub repository.
# /rest/reference/checks#create-a-check-run @payload['repository']['full_name'],
check_run = @installation_client.post( # [String] The name of your check run.
"repos/#{@payload['repository']['full_name']}/check-runs", 'Octo RuboCop',
{ # [String] The SHA of the commit to check
# This header allows for beta access to Checks API
accept: 'application/vnd.github.antiope-preview+json',
# The name of your check run.
name: 'Octo RuboCop',
# The payload structure differs depending on whether a check run or a check suite event occurred. # The payload structure differs depending on whether a check run or a check suite event occurred.
head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'] @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha'],
} # [Hash] 'Accept' header option, to avoid a warning about the API not being ready for production use.
accept: 'application/vnd.github.antiope-preview+json'
) )
end end
``` ```
{% endif %} {% endif %}
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. 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).
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. 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.
@@ -240,31 +235,22 @@ def initiate_check_run
# to 'in_progress' and run the CI process. When the CI finishes, you'll # to 'in_progress' and run the CI process. When the CI finishes, you'll
# update the check run status to 'completed' and add the CI results. # update the check run status to 'completed' and add the CI results.
# Octokit doesn't yet support the Checks API, but it does provide generic @installation_client.update_check_run(
# HTTP methods you can use: @payload['repository']['full_name'],
# /rest/reference/checks#update-a-check-run @payload['check_run']['id'],
updated_check_run = @installation_client.patch(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
{
accept: 'application/vnd.github.v3+json',
name: 'Octo RuboCop',
status: 'in_progress', status: 'in_progress',
started_at: Time.now.utc.iso8601 accept: 'application/vnd.github.v3+json'
}
) )
# ***** RUN A CI TEST ***** # ***** RUN A CI TEST *****
# Mark the check run as complete! # Mark the check run as complete!
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
accept: 'application/vnd.github.v3+json',
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: 'success', conclusion: 'success',
completed_at: Time.now.utc.iso8601 accept: 'application/vnd.github.v3+json'
}
) )
end end
``` ```
@@ -276,40 +262,30 @@ def initiate_check_run
# to 'in_progress' and run the CI process. When the CI finishes, you'll # to 'in_progress' and run the CI process. When the CI finishes, you'll
# update the check run status to 'completed' and add the CI results. # update the check run status to 'completed' and add the CI results.
# Octokit doesn't yet support the Checks API, but it does provide generic @installation_client.update_check_run(
# HTTP methods you can use: @payload['repository']['full_name'],
# /rest/reference/checks#update-a-check-run @payload['check_run']['id'],
updated_check_run = @installation_client.patch(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
{
accept: 'application/vnd.github.antiope-preview+json', # This header is necessary for beta access to Checks API
name: 'Octo RuboCop',
status: 'in_progress', status: 'in_progress',
started_at: Time.now.utc.iso8601 accept: 'application/vnd.github.antiope-preview+json'
}
) )
# ***** RUN A CI TEST ***** # ***** RUN A CI TEST *****
# Mark the check run as complete! # Mark the check run as complete!
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
# This header is necessary for beta access to Checks API
accept: 'application/vnd.github.antiope-preview+json',
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: 'success', conclusion: 'success',
completed_at: Time.now.utc.iso8601 accept: 'application/vnd.github.antiope-preview+json'
}
) )
end end
``` ```
{% endif %} {% endif %}
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. 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.
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`. 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`.
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`. 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`.
@@ -613,29 +589,23 @@ Now you've got all the information you need to update your check run. In the [fi
{% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" or currentVersion == "github-ae@latest" %} {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" or currentVersion == "github-ae@latest" %}
``` ruby ``` ruby
# Mark the check run as complete! # Mark the check run as complete!
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
accept: 'application/vnd.github.v3+json',
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: 'success', conclusion: 'success',
completed_at: Time.now.utc.iso8601 accept: 'application/vnd.github.v3+json'
}
) )
``` ```
{% else %} {% else %}
``` ruby ``` ruby
# Mark the check run as complete! # Mark the check run as complete!
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
accept: 'application/vnd.github.antiope-preview+json', # This header is necessary for beta access to Checks API
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: 'success', conclusion: 'success',
completed_at: Time.now.utc.iso8601 accept: 'application/vnd.github.antiope-preview+json' # This header is necessary for beta access to Checks API
}
) )
``` ```
{% endif %} {% endif %}
@@ -645,14 +615,11 @@ You'll need to update that code to use the `conclusion` variable you set based o
{% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" or currentVersion == "github-ae@latest" %} {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@2.22" or currentVersion == "github-ae@latest" %}
``` ruby ``` ruby
# Mark the check run as complete! And if there are warnings, share them. # Mark the check run as complete! And if there are warnings, share them.
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
accept: 'application/vnd.github.v3+json',
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: conclusion, conclusion: conclusion,
completed_at: Time.now.utc.iso8601,
output: { output: {
title: 'Octo RuboCop', title: 'Octo RuboCop',
summary: summary, summary: summary,
@@ -663,21 +630,18 @@ updated_check_run = @installation_client.patch(
label: 'Fix this', label: 'Fix this',
description: 'Automatically fix all linter notices.', description: 'Automatically fix all linter notices.',
identifier: 'fix_rubocop_notices' identifier: 'fix_rubocop_notices'
}] }],
} accept: 'application/vnd.github.v3+json'
) )
``` ```
{% else %} {% else %}
``` ruby ``` ruby
# Mark the check run as complete! And if there are warnings, share them. # Mark the check run as complete! And if there are warnings, share them.
updated_check_run = @installation_client.patch( @installation_client.update_check_run(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}", @payload['repository']['full_name'],
{ @payload['check_run']['id'],
accept: 'application/vnd.github.antiope-preview+json',
name: 'Octo RuboCop',
status: 'completed', status: 'completed',
conclusion: conclusion, conclusion: conclusion,
completed_at: Time.now.utc.iso8601,
output: { output: {
title: 'Octo RuboCop', title: 'Octo RuboCop',
summary: summary, summary: summary,
@@ -688,8 +652,8 @@ updated_check_run = @installation_client.patch(
label: 'Fix this', label: 'Fix this',
description: 'Automatically fix all linter notices.', description: 'Automatically fix all linter notices.',
identifier: 'fix_rubocop_notices' identifier: 'fix_rubocop_notices'
}] }],
} accept: 'application/vnd.github.antiope-preview+json'
) )
``` ```
{% endif %} {% endif %}