From c39853dfded408dc718ecccf8d1a6568c38496cc Mon Sep 17 00:00:00 2001 From: Vanessasaurus Date: Tue, 20 Oct 2020 12:15:41 -0600 Subject: [PATCH] Add environment variable to pass if PR exists (#53) * adding PASS_IF_EXISTS Signed-off-by: vsoch * PASS_IF_EXISTS option, to not error out when a matching PR already is open (#52) * Pass filters to request for PRs, and move PASS_IF_EXISTS exit in different spot * Changed a printout to say the params used for a req * break when looping through branches and find the matching one Co-authored-by: Vanessasaurus Co-authored-by: tscizzlebg <54290732+tscizzlebg@users.noreply.github.com> --- README.md | 4 +++- pull-request.py | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d3dada5..2faef03 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,16 @@ Unlike standard actions, this action just uses variables from the environment. | 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 | | PASS_ON_ERROR | Instead of failing on an error response, pass | unset | +| PASS_IF_EXISTS | Instead of failing if the pull request already exists, pass | unset | -For `PULL_REQUEST_DRAFT`, `PASS_ON_ERROR`, and `MAINTAINER_CANT_MODIFY`, these are +For `PULL_REQUEST_DRAFT`, `PASS_ON_ERROR`, `PASS_IF_EXISTS`, 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 `PASS_ON_ERROR` if you want the PR to not exit given any non 200/201 response. + - Define `PASS_IF_EXISTS` if you want the PR to not exit given the pull request is already open. 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 diff --git a/pull-request.py b/pull-request.py index a01d2e2..22a5bd0 100755 --- a/pull-request.py +++ b/pull-request.py @@ -73,13 +73,14 @@ def create_pull_request( ): # Check if the branch already has a pull request open + params = {"base": target, "head": source, "state": "open"} data = {"base": target, "head": source, "body": body} - print("Data for checking if pull request exists: %s" % data) - response = requests.get(PULLS_URL, json=data) - + print("Params for checking if pull request exists: %s" % params) + response = requests.get(PULLS_URL, params=params) + # Case 1: 404 might warrant needing a token if response.status_code == 404: - response = requests.get(PULLS_URL, json=data, headers=HEADERS) + response = requests.get(PULLS_URL, params=params, headers=HEADERS) if response.status_code != 200: abort_if_fail( "Unable to retrieve information about pull requests: %s: %s" @@ -96,9 +97,15 @@ def create_pull_request( print("Pull request from %s to %s is already open!" % (source, target)) is_open = True + # Does the user want to pass if the pull request exists? + if os.environ.get("PASS_IF_EXISTS"): + print("PASS_IF_EXISTS is set, exiting with success status.") + sys.exit(0) + break + # Option 2: Open a new pull request if not is_open: - print("%s does not have a pull request open, continuing!" % source) + print("No pull request from %s to %s is open, continuing!" % (source, target)) # Post the pull request data = {