#!/usr/bin/env ruby -wU #******************************************************************************* # # CONFIGURATION OPTIONS # #******************************************************************************* CONFIG_KEYS = [:REPOSITORY_PATH, :STARTED_ISSUES_FILE].freeze REPOSITORY_PATH = ENV.fetch('REPOSITORY_PATH') { File.expand_path(File.join(File.dirname(__FILE__), '../')) } STARTED_ISSUES_FILE = ENV.fetch('STARTED_ISSUES_FILE', '.started-issues') #******************************************************************************* # # HELP OPTIONS # #******************************************************************************* if ARGV.grep(/-h/i).size == 1 $stdout.puts "" $stdout.puts "$ ./#{File.basename(__FILE__)} 123" $stdout.puts "" $stdout.puts "This script will close the current issue branch." $stdout.puts "" $stdout.puts "* Remove the started issue from the #{STARTED_ISSUES_FILE}" $stdout.puts "* Make a commit closing the issue" $stdout.puts "" $stdout.puts "Note: There are steps to insure you have a clean working directory." $stdout.puts "Note: If you have spaces in your configuration all bets are off!" $stdout.puts "" $stdout.puts "Current Configuration:" CONFIG_KEYS.each do |key| $stdout.puts "\t#{key}='#{Object.const_get(key)}'" end $stdout.puts "" $stdout.puts "You can override the configuration option by adding the corresponding" $stdout.puts "ENV variable." $stdout.puts "" $stdout.puts "Example:" $stdout.puts "$ REPOSITORY_PATH=.. ./scripts/#{File.basename(__FILE__)}" exit(0) end #******************************************************************************* # # GUARD # #******************************************************************************* # Guard that directories exist [:REPOSITORY_PATH].each do |key| repository_path = Object.const_get(key) unless File.directory?(repository_path) $stderr.puts "Expected directory for #{key} @ #{repository_path} to exist.\n\n" $stderr.puts "See help for details on specifying #{key}.\n\n" $stderr.puts "$ ./#{File.basename(__FILE__)} -h" exit!(1) end end # Guard that I know what the issue number is current_branch = `cd #{REPOSITORY_PATH} && git branch | grep '^[*]'`.sub(/^\*\s*/, '').strip issue_source = :unknown if current_branch =~ /^(\w+-\d+)\D/ # jira issue ISSUE_NUMBER = $1 issue_source = :jira elsif current_branch =~ /^(\d+)\D/ # github issue ISSUE_NUMBER = $1 issue_source = :github else $stderr.puts "Expected to be able to determine issue number from branch #{current_branch} name @ #{REPOSITORY_PATH}.\n\n" $stderr.puts "See help for more information.\n\n" $stderr.puts "$ ./#{File.basename(__FILE__)} -h" exit!(2) end # Guard that we have a clean working directory if `cd #{REPOSITORY_PATH} && git status --porcelain`.strip.size > 0 $stderr.puts "Repository @ #{REPOSITORY_PATH} did not have a clean working directory" exit!(3) end # Guard that the tests all pass unless system("cd #{REPOSITORY_PATH} && bundle exec rake") $stderr.puts "Your tests failed. Please review the above output." exit!(4) end #******************************************************************************* # # DO STUFF # #******************************************************************************* started_issues_file_lines = File.read(File.join(REPOSITORY_PATH, STARTED_ISSUES_FILE)).split("\n") File.open(STARTED_ISSUES_FILE, 'w+') do |file| started_issues_file_lines.each do |line| file.puts line unless line.strip.downcase == ISSUE_NUMBER.downcase end end `cd #{REPOSITORY_PATH}; git add #{STARTED_ISSUES_FILE}` path_to_commit_message = File.expand_path(File.join(REPOSITORY_PATH, '../COMMIT.msg')) begin File.open(path_to_commit_message, 'w+') do |file| file.puts "Closing issue #{ISSUE_NUMBER}" file.puts "" if issue_source == :github file.puts "Closes ##{ISSUE_NUMBER}" file.puts "" end message = "$ ./script/#{File.basename(__FILE__)} #{ISSUE_NUMBER}" CONFIG_KEYS.each_with_object(message) do |key, mem| if ENV.key?(key.to_s) mem = "#{key}=\"#{ENV[key.to_s].to_s}\" #{mem}" end mem end file.puts message end $stdout.puts `cd #{REPOSITORY_PATH} && git commit -F #{path_to_commit_message}` ensure File.unlink(path_to_commit_message) rescue true end # update jira and mark this issue as finished if issue_source == :jira system("jira transition qa #{ISSUE_NUMBER} --noedit") exit!(5) unless $?.success? end