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:
Vanessasaurus
2020-09-25 18:31:06 -06:00
committed by GitHub
parent 727bec37b0
commit 0d662a3b50
2 changed files with 39 additions and 27 deletions

View File

@@ -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_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_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 | | 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 treated as environment booleans. If they are defined in the environment, they trigger the
"true" condition. E.g.,: "true" condition. E.g.,:
- Define `MAINTAINER_CANT_MODIFY` if you don't want the maintainer to be able to modify the pull request. - 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 `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` 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 you can provide a string of one or more GitHub usernames (or team names) to

View File

@@ -26,10 +26,11 @@ def check_events_json():
def abort_if_fail(reason): def abort_if_fail(reason):
"""If FAIL_ON_ERROR, exit with an error and print some rationale""" """If PASS_ON_ERROR, don't exit. Otherwise exit with an error and print the reason"""
if os.environ.get("FAIL_ON_ERROR"): if os.environ.get("PASS_ON_ERROR"):
print("Error, but PASS_ON_ERROR is set, continuing: %s" % reason)
else:
sys.exit(reason) sys.exit(reason)
print("Error, but FAIL_ON_ERROR is not set, continuing: %s" % reason)
def parse_into_list(values): def parse_into_list(values):
@@ -75,6 +76,10 @@ def create_pull_request(
data = {"base": target, "head": source, "body": body} data = {"base": target, "head": source, "body": body}
print("Data for checking if pull request exists: %s" % data) print("Data for checking if pull request exists: %s" % data)
response = requests.get(PULLS_URL, json=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: if response.status_code != 200:
abort_if_fail( abort_if_fail(
"Unable to retrieve information about pull requests: %s: %s" "Unable to retrieve information about pull requests: %s: %s"
@@ -82,18 +87,19 @@ def create_pull_request(
) )
response = response.json() response = response.json()
print("::group::github pr response")
print(response)
print("::endgroup::github pr response")
# Option 1: The pull request is already open # Option 1: The pull request is already open
is_open = False
if response: if response:
pull_request = response[0].get("head", {}).get("ref", "") for entry in response:
if pull_request == source: if entry.get("head", {}).get("ref", "") == source:
print("Pull request from %s to %s is already open!" % (source, target)) print("Pull request from %s to %s is already open!" % (source, target))
is_open = True
# Option 2: Open a new pull request # 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 # Post the pull request
data = { data = {
"title": title, "title": title,
@@ -267,18 +273,18 @@ def main():
print("PULL_REQUEST_TEAM_REVIEWERS is set, %s" % team_reviewers) print("PULL_REQUEST_TEAM_REVIEWERS is set, %s" % team_reviewers)
# The user is allowed to explicitly set the name of the branch # The user is allowed to explicitly set the name of the branch
branch = os.environ.get("PULL_REQUEST_FROM_BRANCH") from_branch = os.environ.get("PULL_REQUEST_FROM_BRANCH")
if not branch: if not from_branch:
print("PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.") print("PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.")
with open(check_events_json(), "r") as fd: with open(check_events_json(), "r") as fd:
branch = json.loads(fd.read()).get("ref") from_branch = json.loads(fd.read()).get("ref")
branch = branch.replace("refs/heads/", "") from_branch = from_branch.replace("refs/heads/", "").strip("/")
else: else:
print("PULL_REQUEST_FROM_BRANCH is set.") print("PULL_REQUEST_FROM_BRANCH is set.")
# At this point, we must have a branch # At this point, we must have a branch
if branch: if from_branch:
print("Found branch %s to open PR from" % branch) print("Found branch %s to open PR from" % from_branch)
else: else:
sys.exit( sys.exit(
"No branch in payload, you are required to define PULL_REQUEST_FROM_BRANCH in the environment." "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 it's to the target branch, ignore it
if branch == pull_request_branch: if from_branch == pull_request_branch:
print("Target and current branch are identical (%s), skipping." % branch) print("Target and current branch are identical (%s), skipping." % from_branch)
else: else:
# If the prefix for the branch matches # 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 (optional)
pull_request_body = os.environ.get( pull_request_body = os.environ.get(
"PULL_REQUEST_BODY", "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 (optional)
pull_request_title = os.environ.get( 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 the pull request
create_pull_request( create_pull_request(
target=pull_request_branch, target=pull_request_branch,
source=branch, source=from_branch,
body=pull_request_body, body=pull_request_body,
title=pull_request_title, title=pull_request_title,
is_draft=pull_request_draft, is_draft=pull_request_draft,