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 7, 2018
1 parent 8bbabd9 commit 0f24fbc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.15.4
1.16.1
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ def process_branch
end

def rubocop_comments
MessageBuilder.new(results, branch).comments
message_builder = MessageBuilder.new(results, branch)
@status = message_builder.commit_status
message_builder.comments
end

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|
GithubService.replace_comments(fq_repo_name, pr_number, rubocop_comments, @status) do |old_comment|
rubocop_comment?(old_comment)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ def comments
message_builder.comments
end

# requirements to "create_status(repo, sha, state, options)"
def commit_status
{
"repo" => fq_repo_name,
"sha" => commits.last,
"state" => files.empty? ? "success" : "error",
"options" => {
"context" => "miq-bot", # TODO: it should be user name variable
"target_url" => nil,
"description" => files.empty? ? "Everything looks fine." : "Something went wrong."
}
}
end

private

attr_reader :results, :message_builder
Expand Down
18 changes: 14 additions & 4 deletions lib/github_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ module GithubService
# may already be well handled by Octokit itself.
#
class << self
def add_comments(fq_repo_name, issue_number, comments)
def add_comments(fq_repo_name, issue_number, comments, commit_status = nil)
Array(comments).each do |comment|
add_comment(fq_repo_name, issue_number, comment)
retval = add_comment(fq_repo_name, issue_number, comment)

if !commit_status.nil?
commit_status["options"]["target_url"] = retval["html_url"]
add_status(commit_status)
end
end
end

# https://octokit.github.io/octokit.rb/Octokit/Client/Statuses.html#create_status-instance_method
def add_status(comstat)
create_status(comstat["repo"], comstat["sha"], comstat["state"], comstat["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 +35,12 @@ 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_status = 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)
add_comments(fq_repo_name, issue_number, new_comments, commit_status)
end

def issue(*args)
Expand Down

0 comments on commit 0f24fbc

Please sign in to comment.