Skip to content

Instantly share code, notes, and snippets.

@ryangreenberg
Created August 6, 2019 15:34
Show Gist options
  • Save ryangreenberg/ca240600494f1d36df429eefc7da1e0e to your computer and use it in GitHub Desktop.
Save ryangreenberg/ca240600494f1d36df429eefc7da1e0e to your computer and use it in GitHub Desktop.
prn.rb - Pull request notes
#!/usr/bin/env ruby
# prn = pull request notes
# TODO: Better name
# PRN is an abbreviation for the Latin term, "pro re nata" which loosely translates to "as needed."
#
# This tool opens notes for the current pull request in a text file named for the current branch.
require 'fileutils'
DEFAULT_LOCATION = File.expand_path('~/Desktop')
# Use Desktop by default
notes_dir_path = if ENV['PR_NOTES_DIRECTORY']
File.expand_path(ENV['PR_NOTES_DIRECTORY'])
else
warn "No value set for PR_NOTES_DIRECTORY, using default location #{DEFAULT_LOCATION}"
DEFAULT_LOCATION
end
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
notes_path = File.join(notes_dir_path, "#{current_branch}.md")
unless File.exists?(notes_path)
# Create new file using a template
content = [
"# #{current_branch}",
'',
]
# Extract the JIRA from the branch name and create a
# `Issue: []()` link in the body
jira = current_branch.match(/([A-Z]+-[0-9]+)_/)
if jira
issue = jira[1]
content << "Issue: [#{issue}](https://jira.example.com/browse/#{issue})"
end
File.open(notes_path, 'a') do |file|
file.puts content.join("\n")
end
end
warn "Opening #{notes_path}..."
system("open '#{notes_path}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment