Skip to content

Instantly share code, notes, and snippets.

@lee-dohm
Created March 22, 2017 16:18
Show Gist options
  • Save lee-dohm/483f9e9b66230bbf0ce2c508d100cdf0 to your computer and use it in GitHub Desktop.
Save lee-dohm/483f9e9b66230bbf0ce2c508d100cdf0 to your computer and use it in GitHub Desktop.
Script to create a templates PR for a repo
#!/usr/bin/env ruby
require 'fileutils'
# Use: atom-templates [repo_name]
#
# 1. Clone the repository if it doesn't already exist; fetch `master` if it does
# 2. Clean up old `template-update` branch if it exists
# 3. Check out new branch `template-update` based on `origin/master`
# 4. Copy template files over
# 5. If there have been changes commit them, push the branch and open the repo in a browser window
BRANCH_NAME = 'template-update'
REPO_HOME = File.expand_path(ENV['ATOM_REPOS_HOME']) || File.expand_path('../..', File.dirname(__FILE__))
TRIAGE_HOME = File.expand_path('..', File.dirname(__FILE__))
def clone(name)
git("clone git@github.com:atom/#{name}.git #{File.join(REPO_HOME, name)}")
end
def current_branch
`git rev-parse --abbrev-ref HEAD`.strip
end
def git(command, options = {})
puts "git #{command}"
result = system("git #{command}")
unless result || options[:ignore_failures]
raise StandardError, "Git command 'git #{command}' failed with error code #{$?}"
end
end
def repo_exists?(name)
repo_dir = File.join(REPO_HOME, name)
git_dir = File.join(repo_dir, '.git')
Dir.exist?(repo_dir) && Dir.exist?(git_dir)
end
def working_tree_clean?
system('git diff-index --quiet HEAD --')
end
repo_name = ARGV[0]
repo_dir = File.join(REPO_HOME, repo_name)
if repo_exists?(repo_name)
Dir.chdir(repo_dir) do
git('fetch origin master')
end
else
clone(repo_name)
end
Dir.chdir(repo_dir) do
git('checkout master') if current_branch == BRANCH_NAME
git("branch -df #{BRANCH_NAME}", ignore_failures: true)
git("checkout -b #{BRANCH_NAME} origin/master")
FileUtils.cp(File.join(TRIAGE_HOME, 'issue-template.md'), File.join(repo_dir, 'ISSUE_TEMPLATE.md'))
if repo_name == 'atom'
FileUtils.cp(File.join(TRIAGE_HOME, 'pull-request-template-atom.md'), File.join(repo_dir, 'PULL_REQUEST_TEMPLATE.md'))
else
FileUtils.cp(File.join(TRIAGE_HOME, 'pull-request-template.md'), File.join(repo_dir, 'PULL_REQUEST_TEMPLATE.md'))
end
if working_tree_clean?
puts
puts('No changes')
else
git('add ISSUE_TEMPLATE.md PULL_REQUEST_TEMPLATE.md')
git('commit -m ":memo: Update issue and PR templates"')
git("push -u origin #{BRANCH_NAME}")
system("open https://github.com/atom/#{repo_name}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment