Improvements to updating pull requests
This commit is contained in:
parent
066b4398c0
commit
98ee7d63d3
1 changed files with 34 additions and 19 deletions
|
@ -8,6 +8,7 @@ import sys
|
||||||
import time
|
import time
|
||||||
from git import Repo
|
from git import Repo
|
||||||
from github import Github
|
from github import Github
|
||||||
|
from github import GithubException
|
||||||
|
|
||||||
|
|
||||||
def get_github_event(github_event_path):
|
def get_github_event(github_event_path):
|
||||||
|
@ -81,7 +82,7 @@ def cs_string_to_list(str):
|
||||||
return list(filter(None, l))
|
return list(filter(None, l))
|
||||||
|
|
||||||
|
|
||||||
def process_event(github_token, github_repository, repo, branch, base, remote_exists):
|
def process_event(github_token, github_repository, repo, branch, base):
|
||||||
# Fetch optional environment variables with default values
|
# Fetch optional environment variables with default values
|
||||||
commit_message = os.getenv(
|
commit_message = os.getenv(
|
||||||
'COMMIT_MESSAGE',
|
'COMMIT_MESSAGE',
|
||||||
|
@ -104,21 +105,29 @@ def process_event(github_token, github_repository, repo, branch, base, remote_ex
|
||||||
push_result = push_changes(repo.git, branch, commit_message)
|
push_result = push_changes(repo.git, branch, commit_message)
|
||||||
print(push_result)
|
print(push_result)
|
||||||
|
|
||||||
# If the remote existed then we are using fixed branch strategy.
|
|
||||||
# A PR should already exist and we can finish here.
|
|
||||||
if remote_exists:
|
|
||||||
print("Updated pull request branch %s." % branch)
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
# Create the pull request
|
# Create the pull request
|
||||||
print("Creating a request to pull %s into %s." % (branch, base))
|
|
||||||
github_repo = Github(github_token).get_repo(github_repository)
|
github_repo = Github(github_token).get_repo(github_repository)
|
||||||
pull_request = github_repo.create_pull(
|
try:
|
||||||
title=title,
|
pull_request = github_repo.create_pull(
|
||||||
body=body,
|
title=title,
|
||||||
base=base,
|
body=body,
|
||||||
head=branch)
|
base=base,
|
||||||
print("Created pull request %d." % pull_request.number)
|
head=branch)
|
||||||
|
print("Created pull request #%d (%s => %s)" %
|
||||||
|
(pull_request.number, branch, base))
|
||||||
|
except GithubException as e:
|
||||||
|
if e.status == 422:
|
||||||
|
pull_request = github_repo.get_pulls(
|
||||||
|
state='open',
|
||||||
|
base=base,
|
||||||
|
head=branch)[1]
|
||||||
|
print("Updated pull request #%d (%s => %s)" %
|
||||||
|
(pull_request.number, branch, base))
|
||||||
|
else:
|
||||||
|
print(str(e))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Set the output variable
|
||||||
os.system(
|
os.system(
|
||||||
'echo ::set-env name=PULL_REQUEST_NUMBER::%d' %
|
'echo ::set-env name=PULL_REQUEST_NUMBER::%d' %
|
||||||
pull_request.number)
|
pull_request.number)
|
||||||
|
@ -135,11 +144,18 @@ def process_event(github_token, github_repository, repo, branch, base, remote_ex
|
||||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||||
pull_request.as_issue().edit(milestone=milestone)
|
pull_request.as_issue().edit(milestone=milestone)
|
||||||
|
|
||||||
# Set pull request reviewers and team reviewers
|
# Set pull request reviewers
|
||||||
if pull_request_reviewers is not None:
|
if pull_request_reviewers is not None:
|
||||||
print("Requesting reviewers")
|
print("Requesting reviewers")
|
||||||
pull_request.create_review_request(
|
try:
|
||||||
reviewers=cs_string_to_list(pull_request_reviewers))
|
pull_request.create_review_request(
|
||||||
|
reviewers=cs_string_to_list(pull_request_reviewers))
|
||||||
|
except GithubException as e:
|
||||||
|
# Likely caused by "Review cannot be requested from pull request author."
|
||||||
|
if e.status == 422:
|
||||||
|
print("Requesting reviewers failed - %s" % e.data["message"])
|
||||||
|
|
||||||
|
# Set pull request team reviewers
|
||||||
if pull_request_team_reviewers is not None:
|
if pull_request_team_reviewers is not None:
|
||||||
print("Requesting team reviewers")
|
print("Requesting team reviewers")
|
||||||
pull_request.create_review_request(
|
pull_request.create_review_request(
|
||||||
|
@ -238,7 +254,6 @@ if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||||
github_repository,
|
github_repository,
|
||||||
repo,
|
repo,
|
||||||
branch,
|
branch,
|
||||||
base,
|
base)
|
||||||
remote_exists)
|
|
||||||
else:
|
else:
|
||||||
print("Repository has no modified or untracked files. Skipping.")
|
print("Repository has no modified or untracked files. Skipping.")
|
||||||
|
|
Loading…
Reference in a new issue