Last active
October 26, 2022 01:05
-
-
Save bluerabbit/085feb474622575abf1c5dd8aae2d54e to your computer and use it in GitHub Desktop.
CircleCIにてrspecでテストが落ちてたらGitHubのPull RequestのDescriptionに落ちたテストファイルを列挙する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "octokit" | |
require "json" | |
class GithubPullRequest | |
def initialize(access_token: ENV["GITHUB_TOKEN"], repository:) | |
@client = Octokit::Client.new(access_token: access_token) | |
@repository = repository | |
end | |
def update_description!(rspec_examples:, pull_request_number:, ci_build_url:) | |
errors = rspec_examples.select { |e| e["status"] == "failed" }.map { |e| "#{e["file_path"]}:#{e["line_number"]}" } | |
time = Time.now.strftime("%Y/%m/%d %H:%M") | |
header = "[:cry: #{time}](#{ci_build_url}) #{rspec_examples.size} tests with #{errors.size} failures" | |
fail_message = errors.map { |v| "- [ ] `#{v}`" }.join("\n") | |
if errors.size > 5 | |
fail_message = "<details><summary>#{errors.size} failures</summary>\n\n#{fail_message}\n\n</details>" | |
end | |
body = "#{fetch_body(pull_request_number)}\n## #{header}\n\n#{fail_message}\n" | |
@client.update_pull_request(@repository, pull_request_number, {body: body}) | |
end | |
private | |
def fetch_body(pull_request_number) | |
@client.pull_request(@repository, pull_request_number)[:body] | |
end | |
end | |
unless ENV["CI_PULL_REQUEST"].nil? | |
rspec_result_json_file_path = ENV["RSPEC_JSON_PATH"] | |
if File.exist?(rspec_result_json_file_path) | |
github_pr = GithubPullRequest.new(repository: "#{ENV["CIRCLE_PROJECT_USERNAME"]}/#{ENV["CIRCLE_PROJECT_REPONAME"]}") | |
github_pr.update_description!(rspec_examples: JSON.parse(File.read(rspec_result_json_file_path))["examples"], | |
pull_request_number: ENV["CI_PULL_REQUEST"].split("/").last.to_i, | |
ci_build_url: ENV["CIRCLE_BUILD_URL"]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
personal-access-tokenで必要な権限
Octokitで確認