Created
September 16, 2019 20:33
-
-
Save toomanyjoes/4bcc7283103dcdf7956d181ec7d03609 to your computer and use it in GitHub Desktop.
Create a pull request off current branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import github | |
import os | |
import git | |
import argparse | |
import pprint | |
def findRepository(): | |
git_path = os.getcwd() | |
while True: | |
try: | |
repo = git.Repo(git_path) | |
break | |
except: | |
if '/' == git_path: | |
raise Exception('Not in a Git repository') | |
git_path = os.path.abspath(os.path.join(git_path, os.pardir)) | |
return repo.active_branch, getGitHubRepository(os.path.basename(os.path.normpath(git_path))) | |
def getGitHubRepository(repo_name): | |
g = github.Github(os.environ['GITHUB_KEY']) | |
#org = g.get_organization('pdbradley') | |
u = g.get_user('pdbradley') | |
return u.get_repo(repo_name) | |
def checkForExistingPullRequest(repo, jira_id): | |
for pr in repo.get_pulls(): | |
if pr.title.find(jira_id) > -1: | |
return pr | |
return None | |
def main(assignees): | |
# First create a Github instance: | |
branch, repo = findRepository() | |
if 'master' == branch: | |
raise Exception("Can't create a pull request against master") | |
jira_id = '{}'.format(branch).split('_')[0] | |
if '' != jira_id: | |
pull_request = checkForExistingPullRequest(repo, jira_id) | |
if None == pull_request: | |
title = branch.commit.message.strip().replace('#comment', '') | |
pull_request = repo.create_pull(title, '', 'midcap-staging', branch.name) | |
issue = _get_issue(pull_request) | |
issue.edit(assignees=assignees) | |
_add_reviewers(pull_request, assignees) | |
return "Pull Request created: {} ({})".format(pull_request.title, pull_request.html_url) | |
def _get_issue(pull_request): | |
headers, data = pull_request._requester.requestJsonAndCheck( | |
"GET", | |
"/issues/".join(pull_request.url.rsplit("/pulls/", 1)) | |
) | |
return github.Issue.Issue(pull_request._requester, headers, data, completed=True) | |
def _add_reviewers(pull_request, reviewers): | |
_, data = pull_request._requester.requestJsonAndCheck( | |
"POST", | |
pull_request.url + "/requested_reviewers", | |
input={"reviewers": reviewers} | |
) | |
return data | |
def getDefaultAssignees(): | |
assignees = set([ | |
'toomanyjoes', | |
'pyowell', | |
'pdbradley', | |
]) | |
g = github.Github(os.environ['GITHUB_KEY']) | |
assignees.remove(g.get_user().login) | |
return list(assignees) | |
if __name__ == "__main__": | |
try: | |
parser = argparse.ArgumentParser(description='Choose a reviewer') | |
parser.add_argument('-r', dest='reviewer', type=str, help='identifier for reviewer') | |
args = parser.parse_args() | |
if None == args.reviewer: | |
assignees = getDefaultAssignees() | |
else: | |
assignees = [args.reviewer] | |
print(main(assignees)) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment