Skip to content

Update to use Ocktokit interface for the checks API #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'http://rubygems.org'

gem 'sinatra', '~> 2.0'
gem 'jwt', '~> 2.1'
gem 'octokit', '~> 4.0'
gem 'octokit', '~> 4.14'
gem 'rubocop'
gem 'dotenv'
gem 'git'
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ GEM
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
dotenv (2.5.0)
faraday (0.15.3)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
git (1.5.0)
jaro_winkler (1.5.1)
jwt (2.1.0)
multipart-post (2.0.0)
mustermann (1.0.3)
octokit (4.13.0)
octokit (4.14.0)
sawyer (~> 0.8.0, >= 0.5.3)
parallel (1.12.1)
parser (2.5.3.0)
Expand Down Expand Up @@ -50,7 +50,7 @@ DEPENDENCIES
dotenv
git
jwt (~> 2.1)
octokit (~> 4.0)
octokit (~> 4.14)
rubocop
sinatra (~> 2.0)

Expand Down
41 changes: 11 additions & 30 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,11 @@ class GHAapp < Sinatra::Application

# Create a new check run with the status queued
def create_check_run
# At the time of writing, Octokit does not support the Checks API, but
# it does provide generic HTTP methods you can use:
# https://developer.github.com/v3/checks/runs/#create-a-check-run
check_run = @installation_client.post(
"repos/#{@payload['repository']['full_name']}/check-runs",
{
# 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.
head_sha: @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha']
}
)
# 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.
head_sha = @payload['check_run'].nil? ? @payload['check_suite']['head_sha'] : @payload['check_run']['head_sha']
Octokit.create_check_run(@payload['repository']['full_name'], name, head_sha)

# You requested the creation of a check run from GitHub. Now, you'll wait
# to get confirmation from GitHub, in the form of a webhook, that it was
Expand All @@ -109,19 +100,8 @@ def initiate_check_run
# Once the check run is created, you'll update the status of the check run
# 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.

# At the time of writing, Octokit doesn't support the Checks API, but
# it does provide generic HTTP methods you can use:
# https://developer.github.com/v3/checks/runs/#update-a-check-run
updated_check_run = @installation_client.patch(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
{
accept: 'application/vnd.github.antiope-preview+json',
name: 'Octo RuboCop',
status: 'in_progress',
started_at: Time.now.utc.iso8601
}
)
Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'], name: 'Octo RuboCop',
status: 'in_progress', started_at: Time.now.utc.iso8601)

# ***** RUN A CI TEST *****
# Ideally this would be performed async, so you could return immediately.
Expand Down Expand Up @@ -192,8 +172,7 @@ def initiate_check_run
text = "Octo RuboCop version: #{@output['metadata']['rubocop_version']}"

# Mark the check run as complete! And if there are warnings, share them.
updated_check_run = @installation_client.patch(
"repos/#{@payload['repository']['full_name']}/check-runs/#{@payload['check_run']['id']}",
Octokit.update_check_run(@payload['repository']['full_name'], @payload['check_run']['id'],
{
accept: 'application/vnd.github.antiope-preview+json',
name: 'Octo RuboCop',
Expand Down Expand Up @@ -305,7 +284,9 @@ def authenticate_app
def authenticate_installation(payload)
@installation_id = payload['installation']['id']
@installation_token = @app_client.create_app_installation_access_token(@installation_id)[:token]
@installation_client = Octokit::Client.new(bearer_token: @installation_token)
Octokit.configure do |c|
c.bearer_token = @installation_token
end
end

# Check X-Hub-Signature to confirm that this webhook was generated by
Expand Down