From d59d9037bc05a272ee431c4498ceb05e86dd7d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20T=C3=B3th?= Date: Wed, 7 Mar 2018 17:18:14 +0100 Subject: [PATCH] GitHub Status API Integration 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. --- .../commit_range/rubocop_checker.rb | 8 +++----- .../rubocop_checker/message_builder.rb | 14 ++++++++++++++ lib/github_service.rb | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/workers/commit_monitor_handlers/commit_range/rubocop_checker.rb b/app/workers/commit_monitor_handlers/commit_range/rubocop_checker.rb index 23fdd432..6924fbf0 100644 --- a/app/workers/commit_monitor_handlers/commit_range/rubocop_checker.rb +++ b/app/workers/commit_monitor_handlers/commit_range/rubocop_checker.rb @@ -33,13 +33,11 @@ def process_branch nil # Avoid working on unmergeable PRs end - def rubocop_comments - MessageBuilder.new(results, branch).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| + message_builder = MessageBuilder.new(results, branch) + + GithubService.replace_comments(fq_repo_name, pr_number, message_builder.comments, message_builder.commit_status) do |old_comment| rubocop_comment?(old_comment) end end diff --git a/app/workers/commit_monitor_handlers/commit_range/rubocop_checker/message_builder.rb b/app/workers/commit_monitor_handlers/commit_range/rubocop_checker/message_builder.rb index 3e9d043b..ca4b5c37 100644 --- a/app/workers/commit_monitor_handlers/commit_range/rubocop_checker/message_builder.rb +++ b/app/workers/commit_monitor_handlers/commit_range/rubocop_checker/message_builder.rb @@ -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 diff --git a/lib/github_service.rb b/lib/github_service.rb index 133b8d6c..016a6ce8 100644 --- a/lib/github_service.rb +++ b/lib/github_service.rb @@ -12,9 +12,16 @@ module GithubService # class << self def add_comments(fq_repo_name, issue_number, comments) - Array(comments).each do |comment| + # add_status requires first comment due to its URL + Array(comments).map do |comment| add_comment(fq_repo_name, issue_number, comment) - end + end.first + end + + # https://octokit.github.io/octokit.rb/Octokit/Client/Statuses.html#create_status-instance_method + def add_status(comstat, comment) + comstat["options"]["target_url"] = comment["html_url"] + create_status(comstat["repo"], comstat["sha"], comstat["state"], comstat["options"]) end def delete_comments(fq_repo_name, comment_ids) @@ -25,12 +32,15 @@ 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) + first_comment = add_comments(fq_repo_name, issue_number, new_comments) + + # add_status creates a commit status pointing to the first comment URL + add_status(commit_status, first_comment) unless first_comment.nil? end def issue(*args)