2 Commits

Author SHA1 Message Date
Vanessasaurus
c761be135f replacing set-env with new environment syntax (#48)
* replacing set-env with new environment syntax
* using direct append to file for set_env instead of os.system
Signed-off-by: vsoch <vsochat@stanford.edu>
2020-11-17 08:33:29 -07:00
Vanessasaurus
c39853dfde Add environment variable to pass if PR exists (#53)
* adding PASS_IF_EXISTS

Signed-off-by: vsoch <vsochat@stanford.edu>

* 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 <vsochat@gmail.com>

Co-authored-by: tscizzlebg <54290732+tscizzlebg@users.noreply.github.com>
2020-10-20 12:15:41 -06:00
4 changed files with 86 additions and 14 deletions

View File

@@ -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_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 |
| PASS_ON_ERROR | Instead of failing on an error response, pass | 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 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 `PASS_ON_ERROR` if you want the PR to not 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.
- 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` 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
@@ -91,7 +93,7 @@ Example workflows are provided in [examples](examples), and please contribute an
examples that you might have to help other users! We will walk through a basic examples that you might have to help other users! We will walk through a basic
example here for a niche case. Let's say that we are opening a pull request on the release event. This would mean example here for a niche case. Let's say that we are opening a pull request on the release event. This would mean
that the payload's branch variable would be null. We would need to define `PULL_REQUEST_FROM`. How would that the payload's branch variable would be null. We would need to define `PULL_REQUEST_FROM`. How would
we do that? We can [set environment variables](https://help.github.com/en/actions/reference/development-tools-for-github-actions#set-an-environment-variable-set-env) for next steps. Here is an example: we do that? We can [set environment variables](https://github.com/actions/toolkit/blob/main/docs/commands.md#environment-files) for next steps. Here is an example:
```yaml ```yaml
name: Pull Request on Branch Push name: Pull Request on Branch Push
@@ -107,7 +109,7 @@ jobs:
run: | run: |
# do custom parsing of your code / date to derive a branch from # do custom parsing of your code / date to derive a branch from
PR_BRANCH_FROM=release-v$(cat VERSION) PR_BRANCH_FROM=release-v$(cat VERSION)
::set-env name=PULL_REQUEST_FROM_BRANCH::${PR_BRANCH_FROM} export "PULL_REQUEST_FROM_BRANCH=${PR_BRANCH_FROM}" >> $GITHUB_ENV
- name: pull-request-action - name: pull-request-action
uses: vsoch/pull-request-action@1.0.6 uses: vsoch/pull-request-action@1.0.6
env: env:

View File

@@ -40,7 +40,7 @@ jobs:
git push origin "${BRANCH_FROM}" git push origin "${BRANCH_FROM}"
fi fi
# Here is where we are setting the environment variable! # Here is where we are setting the environment variable!
echo "::set-env name=PULL_REQUEST_FROM_BRANCH::${BRANCH_FROM}" echo "PULL_REQUEST_FROM_BRANCH=${BRANCH_FROM}" >> $GITHUB_ENV
- name: Open Pull Request - name: Open Pull Request
uses: vsoch/pull-request-action@1.0.6 uses: vsoch/pull-request-action@1.0.6

View File

@@ -0,0 +1,55 @@
name: Hotfix Branch Pull Request
on:
push:
branches-ignore:
- master
- production
# See https://github.com/vsoch/pull-request-action/issues/47#issuecomment-707109132
jobs:
auto-pull-request:
name: PullRequestAction
runs-on: ubuntu-latest
steps:
- name: Generate branch name
uses: actions/github-script@v3
id: set-branch-name
with:
script: |
const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
const emoji = context.payload.ref.startsWith("refs/heads/feature")
? "✨ "
: context.payload.ref.startsWith("refs/heads/hotfix")
? "🚑 "
: "";
return `${emoji}${capitalize(
context.payload.ref
.replace("refs/heads/", "")
.replace(/-/g, " ")
.replace("feature ", "")
.replace("hotfix ", "")
)}`;
result-encoding: string
- name: Set branch name
run: echo "PULL_REQUEST_TITLE=${{steps.set-branch-name.outputs.result}}" >> $GITHUB_ENV
- name: Generate PR body
uses: actions/github-script@v3
id: set-pr-body
with:
script: |
return `I'm opening this pull request for this branch, pushed by @${
context.payload.head_commit.author.username
} with ${context.payload.commits.length} commit${
context.payload.commits.length === 1 ? "" : "s"
}.`;
result-encoding: string
- name: Set PR body
run: echo "PULL_REQUEST_BODY=${{steps.set-pr-body.outputs.result}}" >> $GITHUB_ENV
- name: pull-request-action
uses: vsoch/pull-request-action@1.0.10
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_PREFIX: "hotfix-"
PULL_REQUEST_BRANCH: "production"
PULL_REQUEST_REVIEWERS: "AnandChowdhary"

View File

@@ -41,6 +41,14 @@ def parse_into_list(values):
return [x.strip() for x in values.split(" ")] return [x.strip() for x in values.split(" ")]
def set_env(name, value):
"""helper function to echo a key/value pair to the environement file"""
environment_file_path = os.environ.get("GITHUB_ENV")
with open(environment_file_path, "a") as environment_file:
environment_file.write("%s=%s" % (name, value))
################################################################################ ################################################################################
# Global Variables (we can't use GITHUB_ prefix) # Global Variables (we can't use GITHUB_ prefix)
################################################################################ ################################################################################
@@ -73,13 +81,14 @@ def create_pull_request(
): ):
# Check if the branch already has a pull request open # Check if the branch already has a pull request open
params = {"base": target, "head": source, "state": "open"}
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("Params for checking if pull request exists: %s" % params)
response = requests.get(PULLS_URL, json=data) response = requests.get(PULLS_URL, params=params)
# Case 1: 404 might warrant needing a token # Case 1: 404 might warrant needing a token
if response.status_code == 404: 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: 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"
@@ -96,9 +105,15 @@ def create_pull_request(
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 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 # Option 2: Open a new pull request
if not is_open: 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 # Post the pull request
data = { data = {
@@ -132,13 +147,13 @@ def create_pull_request(
number = response.get("number") number = response.get("number")
html_url = response.get("html_url") html_url = response.get("html_url")
print("Number opened for PR is %s" % number) print("Number opened for PR is %s" % number)
print("::set-env name=PULL_REQUEST_NUMBER::%s" % number) set_env("PULL_REQUEST_NUMBER", number)
print("::set-output name=pull_request_number::%s" % number) print("::set-output name=pull_request_number::%s" % number)
print("::set-env name=PULL_REQUEST_RETURN_CODE::%s" % pull_request_return_code) set_env("PULL_REQUEST_RETURN_CODE", pull_request_return_code)
print( print(
"::set-output name=pull_request_return_code::%s" % pull_request_return_code "::set-output name=pull_request_return_code::%s" % pull_request_return_code
) )
print("::set-env name=PULL_REQUEST_URL::%s" % html_url) set_env("PULL_REQUEST_URL", html_url)
print("::set-output name=pull_request_url::%s" % html_url) print("::set-output name=pull_request_url::%s" % html_url)
if assignees: if assignees:
@@ -168,7 +183,7 @@ def create_pull_request(
assignees_return_code = ( assignees_return_code = (
0 if response.status_code == 201 else response.status_code 0 if response.status_code == 201 else response.status_code
) )
print("::set-env name=ASSIGNEES_RETURN_CODE::%s" % assignees_return_code) set_env("ASSIGNEES_RETURN_CODE", assignees_return_code)
print("::set-output name=assignees_return_code::%s" % assignees_return_code) print("::set-output name=assignees_return_code::%s" % assignees_return_code)
if reviewers or team_reviewers: if reviewers or team_reviewers:
@@ -206,7 +221,7 @@ def create_pull_request(
print("::group::github reviewers response") print("::group::github reviewers response")
print(response) print(response)
print("::endgroup::github reviewers response") print("::endgroup::github reviewers response")
print("::set-env name=REVIEWERS_RETURN_CODE::%s" % reviewers_return_code) set_env("REVIEWERS_RETURN_CODE", reviewers_return_code)
print("::set-output name=reviewers_return_code::%s" % reviewers_return_code) print("::set-output name=reviewers_return_code::%s" % reviewers_return_code)
print("Add reviewers return code: %s" % reviewers_return_code) print("Add reviewers return code: %s" % reviewers_return_code)