Update/python client (#46)
* testing fixes for original query for pr * bugfix * need to loop through * Adding more verbosity * adding is_open * Adding GET request with headers if needed * Update pull-request.py
This commit is contained in:
@@ -45,15 +45,15 @@ Unlike standard actions, this action just uses variables from the environment.
|
||||
| PULL_REQUEST_ASSIGNEES | A list (string with spaces) of users to assign | false | unset |
|
||||
| PULL_REQUEST_REVIEWERS | A list (string with spaces) of users to assign review | false | unset |
|
||||
| PULL_REQUEST_TEAM_REVIEWERS | A list (string with spaces) of teams to assign review | false | unset |
|
||||
| FAIL_ON_ERROR | If any value is present this causes the action to error if PR creation fails for any reason | unset |
|
||||
| PASS_ON_ERROR | Instead of failing on an error response, pass | unset |
|
||||
|
||||
For `PULL_REQUEST_DRAFT`, `FAIL_ON_ERROR`, and `MAINTAINER_CANT_MODIFY`, these are
|
||||
For `PULL_REQUEST_DRAFT`, `PASS_ON_ERROR`, and `MAINTAINER_CANT_MODIFY`, these are
|
||||
treated as environment booleans. If they are defined in the environment, they trigger the
|
||||
"true" condition. E.g.,:
|
||||
|
||||
- Define `MAINTAINER_CANT_MODIFY` if you don't want the maintainer to be able to modify the pull request.
|
||||
- Define `PULL_REQUEST_DRAFT` if you want the PR to be a draft.
|
||||
- Define `FAIL_ON_ERROR` if you want the PR to exit given any non 200/201 response.
|
||||
- Define `PASS_ON_ERROR` if you want the PR to not exit given any non 200/201 response.
|
||||
|
||||
For `PULL_REQUEST_ASSIGNEES`, `PULL_REQUEST_REVIEWERS`, and `PULL_REQUEST_TEAM_REVIEWERS`
|
||||
you can provide a string of one or more GitHub usernames (or team names) to
|
||||
|
||||
@@ -26,10 +26,11 @@ def check_events_json():
|
||||
|
||||
|
||||
def abort_if_fail(reason):
|
||||
"""If FAIL_ON_ERROR, exit with an error and print some rationale"""
|
||||
if os.environ.get("FAIL_ON_ERROR"):
|
||||
"""If PASS_ON_ERROR, don't exit. Otherwise exit with an error and print the reason"""
|
||||
if os.environ.get("PASS_ON_ERROR"):
|
||||
print("Error, but PASS_ON_ERROR is set, continuing: %s" % reason)
|
||||
else:
|
||||
sys.exit(reason)
|
||||
print("Error, but FAIL_ON_ERROR is not set, continuing: %s" % reason)
|
||||
|
||||
|
||||
def parse_into_list(values):
|
||||
@@ -75,6 +76,10 @@ def create_pull_request(
|
||||
data = {"base": target, "head": source, "body": body}
|
||||
print("Data for checking if pull request exists: %s" % data)
|
||||
response = requests.get(PULLS_URL, json=data)
|
||||
|
||||
# Case 1: 404 might warrant needing a token
|
||||
if response.status_code == 404:
|
||||
response = requests.get(PULLS_URL, json=data, headers=HEADERS)
|
||||
if response.status_code != 200:
|
||||
abort_if_fail(
|
||||
"Unable to retrieve information about pull requests: %s: %s"
|
||||
@@ -82,18 +87,19 @@ def create_pull_request(
|
||||
)
|
||||
|
||||
response = response.json()
|
||||
print("::group::github pr response")
|
||||
print(response)
|
||||
print("::endgroup::github pr response")
|
||||
|
||||
# Option 1: The pull request is already open
|
||||
is_open = False
|
||||
if response:
|
||||
pull_request = response[0].get("head", {}).get("ref", "")
|
||||
if pull_request == source:
|
||||
print("Pull request from %s to %s is already open!" % (source, target))
|
||||
for entry in response:
|
||||
if entry.get("head", {}).get("ref", "") == source:
|
||||
print("Pull request from %s to %s is already open!" % (source, target))
|
||||
is_open = True
|
||||
|
||||
# Option 2: Open a new pull request
|
||||
else:
|
||||
if not is_open:
|
||||
print("%s does not have a pull request open, continuing!" % source)
|
||||
|
||||
# Post the pull request
|
||||
data = {
|
||||
"title": title,
|
||||
@@ -267,18 +273,18 @@ def main():
|
||||
print("PULL_REQUEST_TEAM_REVIEWERS is set, %s" % team_reviewers)
|
||||
|
||||
# The user is allowed to explicitly set the name of the branch
|
||||
branch = os.environ.get("PULL_REQUEST_FROM_BRANCH")
|
||||
if not branch:
|
||||
from_branch = os.environ.get("PULL_REQUEST_FROM_BRANCH")
|
||||
if not from_branch:
|
||||
print("PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.")
|
||||
with open(check_events_json(), "r") as fd:
|
||||
branch = json.loads(fd.read()).get("ref")
|
||||
branch = branch.replace("refs/heads/", "")
|
||||
from_branch = json.loads(fd.read()).get("ref")
|
||||
from_branch = from_branch.replace("refs/heads/", "").strip("/")
|
||||
else:
|
||||
print("PULL_REQUEST_FROM_BRANCH is set.")
|
||||
|
||||
# At this point, we must have a branch
|
||||
if branch:
|
||||
print("Found branch %s to open PR from" % branch)
|
||||
if from_branch:
|
||||
print("Found branch %s to open PR from" % from_branch)
|
||||
else:
|
||||
sys.exit(
|
||||
"No branch in payload, you are required to define PULL_REQUEST_FROM_BRANCH in the environment."
|
||||
@@ -286,30 +292,36 @@ def main():
|
||||
|
||||
# If it's to the target branch, ignore it
|
||||
|
||||
if branch == pull_request_branch:
|
||||
print("Target and current branch are identical (%s), skipping." % branch)
|
||||
if from_branch == pull_request_branch:
|
||||
print("Target and current branch are identical (%s), skipping." % from_branch)
|
||||
else:
|
||||
|
||||
# If the prefix for the branch matches
|
||||
if not branch_prefix or branch.startswith(branch_prefix):
|
||||
if not branch_prefix or from_branch.startswith(branch_prefix):
|
||||
|
||||
# Pull request body (optional)
|
||||
pull_request_body = os.environ.get(
|
||||
"PULL_REQUEST_BODY",
|
||||
"This is an automated pull request to update from branch %s" % branch,
|
||||
"This is an automated pull request to update from branch %s"
|
||||
% from_branch,
|
||||
)
|
||||
print("Pull request body is %s" % pull_request_body)
|
||||
|
||||
print("::group::pull request body")
|
||||
print(pull_request_body)
|
||||
print("::endgroup::pull request body")
|
||||
|
||||
# Pull request title (optional)
|
||||
pull_request_title = os.environ.get(
|
||||
"PULL_REQUEST_TITLE", "Update from %s" % branch
|
||||
"PULL_REQUEST_TITLE", "Update from %s" % from_branch
|
||||
)
|
||||
print("Pull request title is %s" % pull_request_title)
|
||||
print("::group::pull request title")
|
||||
print(pull_request_title)
|
||||
print("::endgroup::pull request title")
|
||||
|
||||
# Create the pull request
|
||||
create_pull_request(
|
||||
target=pull_request_branch,
|
||||
source=branch,
|
||||
source=from_branch,
|
||||
body=pull_request_body,
|
||||
title=pull_request_title,
|
||||
is_draft=pull_request_draft,
|
||||
|
||||
Reference in New Issue
Block a user