Files
openproject/script/github_pr_errors
T
Christophe Bliard 25ad65d88e Enclose failing spec in single quotes
So that when copy pasting it, zsh won't complain with "zsh: no matches found" error due to presence of unescaped `[]` in the command.

[skip ci]
2022-08-23 15:48:50 +02:00

85 lines
2.1 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :development)
require 'pathname'
require 'json'
require 'rest-client'
require 'pry'
# current branch
branch_name = `git rev-parse --abbrev-ref HEAD`.strip
if !ENV['GITHUB_USERNAME']
raise "Missing GITHUB_USERNAME env"
elsif !ENV['GITHUB_TOKEN']
raise "Missing GITHUB_TOKEN env, go to https://github.com/settings/tokens and create one with 'repo' access"
end
def get_http(path)
url =
if path.start_with?('http')
path
else
"https://api.github.com/repos/opf/openproject/#{path}"
end
response = RestClient::Request.new(
method: :get,
url:,
user: ENV.fetch('GITHUB_USERNAME'),
password: ENV.fetch('GITHUB_TOKEN')
).execute
response.to_str
rescue StandardError => e
warn "Failed to perform API request #{url}: #{e} #{e.message}"
exit 1
end
def get_json(path)
JSON.parse(get_http(path))
end
def commit_message(workflow_run)
get_json("commits/#{workflow_run['head_sha']}?per_page=1")
.then { |commit_response| commit_response["commit"]["message"] }
.then { |message| message.split("\n", 2).first }
end
warn "Looking for the last 'Test suite' workflow run in branch #{branch_name}"
response = get_json "actions/runs?branch=#{CGI.escape(branch_name)}"
last_test_action =
response['workflow_runs']
.select { |entry| entry['name'] == 'Test suite' }
.reject { |entry| entry['status'] == 'in_progress' }
.max_by { |entry| entry['run_number'] }
raise "No action run found for branch #{branch_name}" unless last_test_action
warn " Commit SHA: #{last_test_action['head_sha']}"
warn " Commit message: #{commit_message(last_test_action)}"
errors = []
get_json(last_test_action['jobs_url'])
.then { |jobs_response| jobs_response['jobs'] }
.select { _1['conclusion'] == 'failure' }
.sort_by { _1['name'] }
.each { warn " #{_1['name']}: #{_1['conclusion']}" }
.each do |job|
log = get_http "actions/jobs/#{job['id']}/logs"
log.scan(/^\S+ rspec (\S+) #.+$/) do |match|
errors << match
end
end
if errors.empty?
warn "No rspec errors found :-/"
else
puts errors.flatten.uniq.map { "'#{_1}'" }.join(" ")
end