Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'optparse'
options = {
line: false,
job_name: 'build',
branch: nil,
vcs_type: :github
}
OptionParser.new do |opts|
opts.on('-n', '--build_num VALUE', Integer, 'CircleCI Build number(default: nil)') { |v| options[:build_num] = v }
opts.on('-l', '--[no-]line', 'output lines(default: false)') { |v| options[:line] = v }
opts.on('-j', '--job_name VALUE', 'job name(default: build)') { |v| options[:job_name] = v }
opts.on('-b', '--branch VALUE', 'git branch name(default: current_branch)') { |v| options[:branch] = v }
opts.on('-t', '--vcs_type VALUE', [:github, :bitbucket], 'vcs_type(default: github)') { |v| options[:vcs_type] = v }
opts.on('-h', '--help', 'Prints this help') do
puts opts
exit
end
end.parse!
user_name = ENV['CIRCLE_PROJECT_USERNAME']
repo_name = ENV['CIRCLE_PROJECT_REPONAME']
branch = options[:branch] || ENV.fetch('CIRCLE_BRANCH') do
require 'git'
Git.open(Rails.root).current_branch
end
vcs_type = options[:vcs_type]
build_num = options.fetch(:build_num) do
project = CircleCi::Project.new(user_name, repo_name, vcs_type)
builds = project.recent_builds_branch(branch, filter: 'completed').body
test_job_builds = if builds.any? { |build| build['workflows'] }
# 特定のワークフローの特定のJOBの結果だけを抽出する
builds.select do |build|
build.dig('workflows', 'workflow_name') == 'build' && build.dig('workflows', 'job_name') == options[:job_name]
end
else
builds
end
failed_builds, successful_builds = test_job_builds.partition { |build| build['failed'] }
successful_build = successful_builds.first
failed_build = failed_builds.first
return nil if failed_build.nil?
return nil if successful_build && (successful_build['build_num'] > failed_build['build_num'])
failed_build['build_num']
end
if build_num
build = CircleCi::Build.new(user_name, repo_name, vcs_type, build_num)
tests = build.tests.body['tests']
separator = options[:line] ? "\n" : ' '
print tests.select { |test| test['result'] == 'failure' }.pluck('file').uniq.join(separator)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment