Skip to content

Instantly share code, notes, and snippets.

@namnv609
Created November 10, 2022 02:57
Show Gist options
  • Save namnv609/0af7ee43e98d4a88633ff410b286fcd5 to your computer and use it in GitHub Desktop.
Save namnv609/0af7ee43e98d4a88633ff410b286fcd5 to your computer and use it in GitHub Desktop.
Generate PR desc for my project
#!/usr/bin/env ruby
require "optparse"
require "fileutils"
options = {}
current_work_dir = ::File.expand_path(::File.dirname(__FILE__))
current_date_time = Time.now.strftime("%Y%m%d-%H%M")
OptionParser.new do |opts|
opts.banner = "Usage: ./gen-pr-desc [options]"
opts.on("-tTICKET_ID", "--ticket-id TICKET_ID", "Ticket ID. Empty to default branch name") do |ticket_id|
options[:ticket_id] = ticket_id
end
opts.on("-mMILESTONE", "--milestone MILESTONE", "Milestone. Empty to current milestone") do |milestone|
options[:milestone] = milestone
end
end.parse!
unless options[:ticket_id]
current_branch = %x(git rev-parse --abbrev-ref HEAD).match(/(^MB\-[0-9]{1,})/)
options[:ticket_id] = current_branch[0] if current_branch
end
options[:milestone] = %x(git branch | cut -c 3- | grep -E "^milestone_").strip unless options[:milestone]
unless options[:milestone] && options[:ticket_id]
puts "Ticket ID and Milestone is required"
exit 1
end
pr_descs_dir = ::File.join(current_work_dir, "my_scripts", "pr-descs")
target_pr_desc_dir = ::File.join(pr_descs_dir, options[:milestone])
src_pr_desc_file = ::File.join(current_work_dir, "my_scripts", "merge_request_template.md")
dest_pr_desc_file = ::File.join(target_pr_desc_dir, "#{options[:ticket_id].downcase}-#{current_date_time}.md")
::FileUtils.mkdir_p(target_pr_desc_dir, verbose: true)
::FileUtils.cp(src_pr_desc_file, dest_pr_desc_file, verbose: true)
system("subl #{src_pr_desc_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment