Skip to content

Instantly share code, notes, and snippets.

@fmasuhr
Created December 14, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmasuhr/8b7dee8a706f3d431b22 to your computer and use it in GitHub Desktop.
Save fmasuhr/8b7dee8a706f3d431b22 to your computer and use it in GitHub Desktop.
Create GitHub Pull Request for local changes
require 'bundler/cli'
require 'git'
require 'octokit'
BRANCH_NAME = 'branch'
COMMIT_MESSAGE = 'Commit Message'
# ASSIGNEE = 'pengwynn' # update to assing pull request
git = Git.open(Dir.pwd)
github = Octokit::Client.new(access_token: ENV['GITHUB_ACCESS_TOKEN'])
repository = git.remote(:origin).url.split(':')[1].chomp('.git')
default_branch = github.repository(repository).default_branch
# Checkout new branch and get up to date
git.branch(BRANCH_NAME).checkout
git.pull(:origin, default_branch)
# Commit changes
`git add -u` # git.add does not support to only add updated files
git.commit(COMMIT_MESSAGE)
git.push(:origin, BRANCH_NAME)
# Create Github Pull request
pull = github.create_pull_request(repository, default_branch, BRANCH_NAME, COMMIT_MESSAGE)
# Add assignee
github.update_issue(repository, pull.number, assignee: ASSIGNEE) if defined?(ASSIGNEE) && ASSIGNEE
# Cleanup branch
git.branch('master').checkout
git.branch(BRANCH_NAME).delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment