Skip to content

Commit

Permalink
GitHub Status API Integration
Browse files Browse the repository at this point in the history
Commits are marked with [success/error] state depending on offence detection. If there are any offences in the code, the commit is marked with error state otherwise with success state. It is reflected at the end of PR in the check conclusion part.
  • Loading branch information
europ committed Mar 12, 2018
1 parent 8bbabd9 commit 42e8f34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ def rubocop_comments

def replace_rubocop_comments
logger.info("Updating PR #{pr_number} with rubocop comment.")
GithubService.replace_comments(fq_repo_name, pr_number, rubocop_comments) do |old_comment|

commit = {
"status" => results["files"].select { |f| f["offenses"].any? }.empty?,
"sha" => commits.last
}

GithubService.replace_comments(fq_repo_name, pr_number, rubocop_comments, commit) do |old_comment|
rubocop_comment?(old_comment)
end
end
Expand Down
25 changes: 22 additions & 3 deletions lib/github_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ module GithubService
#
class << self
def add_comments(fq_repo_name, issue_number, comments)
Array(comments).each do |comment|
Array(comments).map do |comment|
add_comment(fq_repo_name, issue_number, comment)
end
end

# https://octokit.github.io/octokit.rb/Octokit/Client/Statuses.html#create_status-instance_method
def add_status(fq_repo_name, commit_sha, comment_url, commit_state)
repo = fq_repo_name
sha = commit_sha
state = commit_state ? "success" : "error"
options = {
"context" => Settings.github_credentials.username,
"target_url" => comment_url,
"description" => commit_state ? "Everything looks fine." : "Something went wrong."
}

create_status(repo, sha, state, options)
end

def delete_comments(fq_repo_name, comment_ids)
Array(comment_ids).each do |comment_id|
delete_comment(fq_repo_name, comment_id)
Expand All @@ -25,12 +39,17 @@ def delete_comments(fq_repo_name, comment_ids)

# Deletes the issue comments found by the provided block, then creates new
# issue comments from those provided.
def replace_comments(fq_repo_name, issue_number, new_comments)
def replace_comments(fq_repo_name, issue_number, new_comments, commit = nil)
raise "no block given" unless block_given?

to_delete = issue_comments(fq_repo_name, issue_number).select { |c| yield c }
delete_comments(fq_repo_name, to_delete.map(&:id))
add_comments(fq_repo_name, issue_number, new_comments)
first_comment = add_comments(fq_repo_name, issue_number, new_comments).first

# add_status creates a commit status pointing to the first comment URL
unless first_comment.nil? && commit.nil?
add_status(fq_repo_name, commit["sha"], first_comment["html_url"], commit["status"])
end
end

def issue(*args)
Expand Down

0 comments on commit 42e8f34

Please sign in to comment.