Testing assign request again (#43)
* Update pull-request.sh * making reviewer failure more verbose * More debugging * updating pull request script to be more verbose * need to pipe curl_wrapper into stderr so it doesnt return * final tweaks to remove extra printing * Testing assign request again * updating to be more verbose * testing with python entrypoint Signed-off-by: vsoch <vsochat@stanford.edu>
This commit is contained in:
@@ -7,8 +7,9 @@ LABEL "com.github.actions.description"="Create a pull request when a branch is c
|
||||
LABEL "com.github.actions.icon"="activity"
|
||||
LABEL "com.github.actions.color"="yellow"
|
||||
|
||||
RUN apk --no-cache add curl wget git bash jq
|
||||
COPY pull-request.sh /pull-request.sh
|
||||
RUN apk --no-cache add python3 py3-pip git bash && \
|
||||
pip3 install requests
|
||||
COPY pull-request.py /pull-request.py
|
||||
|
||||
RUN chmod u+x /pull-request.sh
|
||||
ENTRYPOINT ["/pull-request.sh"]
|
||||
RUN chmod u+x /pull-request.py
|
||||
ENTRYPOINT ["python3", "/pull-request.py"]
|
||||
|
||||
@@ -53,6 +53,7 @@ treated as environment booleans. If they are defined in the environment, they tr
|
||||
|
||||
- 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.
|
||||
|
||||
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
|
||||
|
||||
328
pull-request.py
Executable file
328
pull-request.py
Executable file
@@ -0,0 +1,328 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
|
||||
################################################################################
|
||||
# Helper Functions
|
||||
################################################################################
|
||||
|
||||
|
||||
def get_envar(name):
|
||||
value = os.environ.get(name)
|
||||
if not value:
|
||||
sys.exit("%s is required for vsoch/pull-request-action" % name)
|
||||
return value
|
||||
|
||||
|
||||
def check_events_json():
|
||||
events = get_envar("GITHUB_EVENT_PATH")
|
||||
if not os.path.exists(events):
|
||||
sys.exit("Cannot find Github events file at ${GITHUB_EVENT_PATH}")
|
||||
print("Found ${GITHUB_EVENT_PATH} at %s" % events)
|
||||
return events
|
||||
|
||||
|
||||
def abort_if_fail(reason):
|
||||
"""If FAIL_ON_ERROR, exit with an error and print some rationale"""
|
||||
if os.environ.get("FAIL_ON_ERROR"):
|
||||
sys.exit(reason)
|
||||
print("Error, but FAIL_ON_ERROR is not set, continuing: %s" % reason)
|
||||
|
||||
|
||||
def parse_into_list(values):
|
||||
if values:
|
||||
values = values.replace('"', "").replace("'", "")
|
||||
if not values:
|
||||
return []
|
||||
return [x.strip() for x in values.split(" ")]
|
||||
|
||||
|
||||
################################################################################
|
||||
# Global Variables (we can't use GITHUB_ prefix)
|
||||
################################################################################
|
||||
|
||||
API_VERSION = "v3"
|
||||
BASE = "https://api.github.com"
|
||||
|
||||
HEADERS = {
|
||||
"Authorization": "token %s" % get_envar("GITHUB_TOKEN"),
|
||||
"Accept": "application/vnd.github.%s+json;application/vnd.github.antiope-preview+json;application/vnd.github.shadow-cat-preview+json"
|
||||
% API_VERSION,
|
||||
}
|
||||
|
||||
# URLs
|
||||
REPO_URL = "%s/repos/%s" % (BASE, get_envar("GITHUB_REPOSITORY"))
|
||||
ISSUE_URL = "%s/issues" % REPO_URL
|
||||
PULLS_URL = "%s/pulls" % REPO_URL
|
||||
|
||||
|
||||
def create_pull_request(
|
||||
source,
|
||||
target,
|
||||
body,
|
||||
title,
|
||||
assignees,
|
||||
reviewers,
|
||||
team_reviewers,
|
||||
is_draft=False,
|
||||
can_modify=True,
|
||||
):
|
||||
|
||||
# Check if the branch already has a pull request 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)
|
||||
if response.status_code != 200:
|
||||
abort_if_fail(
|
||||
"Unable to retrieve information about pull requests: %s: %s"
|
||||
% (response.status_code, response.reason)
|
||||
)
|
||||
|
||||
response = response.json()
|
||||
print("::group::github pr response")
|
||||
print(response)
|
||||
print("::endgroup::github pr response")
|
||||
|
||||
# Option 1: The pull request is already open
|
||||
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))
|
||||
|
||||
# Option 2: Open a new pull request
|
||||
else:
|
||||
# Post the pull request
|
||||
data = {
|
||||
"title": title,
|
||||
"body": body,
|
||||
"base": target,
|
||||
"head": source,
|
||||
"draft": is_draft,
|
||||
"maintainer_can_modify": can_modify,
|
||||
}
|
||||
print("Data for opening pull request: %s" % data)
|
||||
response = requests.post(PULLS_URL, json=data, headers=HEADERS)
|
||||
if response.status_code != 201:
|
||||
abort_if_fail(
|
||||
"Unable to create pull request: %s: %s, %s"
|
||||
% (
|
||||
response.status_code,
|
||||
response.reason,
|
||||
response.json().get("message", ""),
|
||||
)
|
||||
)
|
||||
|
||||
# Expected return codes are 0 for success
|
||||
pull_request_return_code = (
|
||||
0 if response.status_code == 201 else response.status_code
|
||||
)
|
||||
response = response.json()
|
||||
print("::group::github response")
|
||||
print(response)
|
||||
print("::endgroup::github response")
|
||||
number = response.get("number")
|
||||
html_url = response.get("html_url")
|
||||
print("Number opened for PR is %s" % number)
|
||||
print("::set-env 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)
|
||||
print(
|
||||
"::set-output name=pull_request_return_code::%s" % pull_request_return_code
|
||||
)
|
||||
print("::set-env name=PULL_REQUEST_URL::%s" % html_url)
|
||||
print("::set-output name=pull_request_url::%s" % html_url)
|
||||
|
||||
if assignees:
|
||||
|
||||
# Remove leading and trailing quotes
|
||||
assignees = parse_into_list(assignees)
|
||||
|
||||
print(
|
||||
"Attempting to assign %s to pull request with number %s"
|
||||
% (assignees, number)
|
||||
)
|
||||
|
||||
# POST /repos/:owner/:repo/issues/:issue_number/assignees
|
||||
data = {"assignees": assignees}
|
||||
ASSIGNEES_URL = "%s/%s/assignees" % (ISSUE_URL, number)
|
||||
response = requests.post(ASSIGNEES_URL, json=data, headers=HEADERS)
|
||||
if response.status_code != 201:
|
||||
abort_if_fail(
|
||||
"Unable to create assignees: %s: %s, %s"
|
||||
% (
|
||||
response.status_code,
|
||||
response.reason,
|
||||
response.json().get("message", ""),
|
||||
)
|
||||
)
|
||||
|
||||
assignees_return_code = (
|
||||
0 if response.status_code == 201 else response.status_code
|
||||
)
|
||||
print("::set-env name=ASSIGNEES_RETURN_CODE::%s" % assignees_return_code)
|
||||
print("::set-output name=assignees_return_code::%s" % assignees_return_code)
|
||||
|
||||
if reviewers or team_reviewers:
|
||||
|
||||
print(
|
||||
"Found reviewers: %s and team reviewers: %s"
|
||||
% (reviewers, team_reviewers)
|
||||
)
|
||||
team_reviewers = parse_into_list(team_reviewers)
|
||||
reviewers = parse_into_list(reviewers)
|
||||
print(
|
||||
"Parsed reviewers: %s and team reviewers: %s"
|
||||
% (reviewers, team_reviewers)
|
||||
)
|
||||
|
||||
# POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers
|
||||
REVIEWERS_URL = "%s/%s/requested_reviewers" % (PULLS_URL, number)
|
||||
|
||||
data = {"reviewers": reviewers, "team_reviewers": team_reviewers}
|
||||
response = requests.post(REVIEWERS_URL, json=data, headers=HEADERS)
|
||||
if response.status_code != 201:
|
||||
abort_if_fail(
|
||||
"Unable to assign reviewers: %s: %s, %s"
|
||||
% (
|
||||
response.status_code,
|
||||
response.reason,
|
||||
response.json().get("message", ""),
|
||||
)
|
||||
)
|
||||
|
||||
reviewers_return_code = (
|
||||
0 if response.status_code == 201 else response.status_code
|
||||
)
|
||||
response = response.json()
|
||||
print("::group::github reviewers response")
|
||||
print(response)
|
||||
print("::endgroup::github reviewers response")
|
||||
print("::set-env 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)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
# path to file that contains the POST response of the event
|
||||
# Example: https://github.com/actions/bin/tree/master/debug
|
||||
# Value: /github/workflow/event.json
|
||||
check_events_json()
|
||||
|
||||
branch_prefix = os.environ.get("BRANCH_PREFIX", "")
|
||||
print("Branch prefix is %s" % branch_prefix)
|
||||
if not branch_prefix:
|
||||
print("No branch prefix is set, all branches will be used.")
|
||||
|
||||
# Default to master to support older, will eventually change to main
|
||||
pull_request_branch = os.environ.get("PULL_REQUEST_BRANCH", "master")
|
||||
print("Pull requests will go to %s" % pull_request_branch)
|
||||
|
||||
# Pull request draft
|
||||
pull_request_draft = os.environ.get("PULL_REQUEST_DRAFT")
|
||||
if not pull_request_draft:
|
||||
print("No explicit preference for draft PR: created PRs will be normal PRs.")
|
||||
pull_request_draft = False
|
||||
else:
|
||||
print(
|
||||
"Environment variable PULL_REQUEST_DRAFT set to a value: created PRs will be draft PRs."
|
||||
)
|
||||
pull_request_draft = True
|
||||
|
||||
# Maintainer can modify, defaults to CAN, unless user sets MAINTAINER_CANT_MODIFY
|
||||
maintainer_can_modify = os.environ.get("MAINTAINER_CANT_MODIFY")
|
||||
if not maintainer_can_modify:
|
||||
print(
|
||||
"No explicit preference for maintainer being able to modify: default is true."
|
||||
)
|
||||
maintainer_can_modify = True
|
||||
else:
|
||||
print(
|
||||
"Environment variable MAINTAINER_CANT_MODIFY set to a value: maintainer will not be able to modify."
|
||||
)
|
||||
maintainer_can_modify = False
|
||||
|
||||
# Assignees
|
||||
assignees = os.environ.get("PULL_REQUEST_ASSIGNEES")
|
||||
if not assignees:
|
||||
print("PULL_REQUEST_ASSIGNEES is not set, no assignees.")
|
||||
else:
|
||||
print("PULL_REQUEST_ASSIGNEES is set, %s" % assignees)
|
||||
|
||||
# Reviewers (individual and team)
|
||||
|
||||
reviewers = os.environ.get("PULL_REQUEST_REVIEWERS")
|
||||
team_reviewers = os.environ.get("PULL_REQUEST_TEAM_REVIEWERS")
|
||||
if not reviewers:
|
||||
print("PULL_REQUEST_REVIEWERS is not set, no reviewers.")
|
||||
else:
|
||||
print("PULL_REQUEST_REVIEWERS is set, %s" % reviewers)
|
||||
|
||||
if not team_reviewers:
|
||||
print("PULL_REQUEST_TEAM_REVIEWERS is not set, no team reviewers.")
|
||||
else:
|
||||
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:
|
||||
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/", "")
|
||||
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)
|
||||
else:
|
||||
sys.exit(
|
||||
"No branch in payload, you are required to define PULL_REQUEST_FROM_BRANCH in the environment."
|
||||
)
|
||||
|
||||
# If it's to the target branch, ignore it
|
||||
|
||||
if branch == pull_request_branch:
|
||||
print("Target and current branch are identical (%s), skipping." % branch)
|
||||
else:
|
||||
|
||||
# If the prefix for the branch matches
|
||||
if not branch_prefix or 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,
|
||||
)
|
||||
print("Pull request body is %s" % pull_request_body)
|
||||
|
||||
# Pull request title (optional)
|
||||
pull_request_title = os.environ.get(
|
||||
"PULL_REQUEST_TITLE", "Update from %s" % branch
|
||||
)
|
||||
print("Pull request title is %s" % pull_request_title)
|
||||
|
||||
# Create the pull request
|
||||
create_pull_request(
|
||||
target=pull_request_branch,
|
||||
source=branch,
|
||||
body=pull_request_body,
|
||||
title=pull_request_title,
|
||||
is_draft=pull_request_draft,
|
||||
can_modify=maintainer_can_modify,
|
||||
assignees=assignees,
|
||||
reviewers=reviewers,
|
||||
team_reviewers=team_reviewers,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("==========================================================================")
|
||||
print("START: Running Pull Request on Branch Update Action!")
|
||||
main()
|
||||
print("==========================================================================")
|
||||
print("END: Finished")
|
||||
300
pull-request.sh
300
pull-request.sh
@@ -1,300 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Suggested by Github actions to be strict
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
################################################################################
|
||||
# Global Variables (we can't use GITHUB_ prefix)
|
||||
################################################################################
|
||||
|
||||
API_VERSION=v3
|
||||
BASE=https://api.github.com
|
||||
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
|
||||
HEADER="Accept: application/vnd.github.${API_VERSION}+json"
|
||||
HEADER="${HEADER}; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
|
||||
|
||||
# URLs
|
||||
REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}"
|
||||
ISSUE_URL=${REPO_URL}/issues
|
||||
PULLS_URL=$REPO_URL/pulls
|
||||
|
||||
################################################################################
|
||||
# Helper Functions
|
||||
################################################################################
|
||||
|
||||
|
||||
check_credentials() {
|
||||
|
||||
if [[ -z "${GITHUB_TOKEN}" ]]; then
|
||||
printf "You must include the GITHUB_TOKEN as an environment variable.\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
check_events_json() {
|
||||
|
||||
if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then
|
||||
printf "Cannot find Github events file at ${GITHUB_EVENT_PATH}\n";
|
||||
exit 1
|
||||
fi
|
||||
printf "Found ${GITHUB_EVENT_PATH}\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
create_pull_request() {
|
||||
|
||||
# JSON strings
|
||||
SOURCE="$(echo -n "${1}" | jq --raw-input --slurp ".")" # from this branch
|
||||
TARGET="$(echo -n "${2}" | jq --raw-input --slurp ".")" # pull request TO this target
|
||||
BODY="$(echo -n "${3}" | jq --raw-input --slurp ".")" # this is the content of the message
|
||||
TITLE="$(echo -n "${4}" | jq --raw-input --slurp ".")" # pull request title
|
||||
DRAFT="${5}" # pull request draft?
|
||||
MODIFY="${6}" # maintainer can modify
|
||||
ASSIGNEES="$(echo -n "${7}" | jq --raw-input --slurp ".")"
|
||||
REVIEWERS="$(echo -n "${8}" | jq --raw-input --slurp ".")"
|
||||
TEAM_REVIEWERS="$(echo -n "${9}" | jq --raw-input --slurp ".")"
|
||||
|
||||
RETVAL=0
|
||||
|
||||
# Check if the branch already has a pull request open
|
||||
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
|
||||
if ! RESPONSE=$(curl_wrapper -X GET --data "${DATA}" ${PULLS_URL}); then
|
||||
RETVAL=$?
|
||||
abort_if_fail "Error ${RETVAL} getting open PRs: ${RESPONSE}\n"
|
||||
fi
|
||||
echo "::group::github pr response"
|
||||
echo "${RESPONSE}"
|
||||
echo "::endgroup::github pr response"
|
||||
|
||||
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
|
||||
printf "Response ref: ${PR}\n"
|
||||
|
||||
# Option 1: The pull request is already open
|
||||
if [[ "${PR}" == "${SOURCE}" ]]; then
|
||||
printf "Pull request from ${SOURCE} to ${TARGET} is already open!\n"
|
||||
|
||||
# Option 2: Open a new pull request
|
||||
else
|
||||
# Post the pull request
|
||||
DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"base\":${TARGET}, \"head\":${SOURCE}, \"draft\":${DRAFT}, \"maintainer_can_modify\":${MODIFY}}"
|
||||
if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${PULLS_URL}); then
|
||||
RETVAL=$?
|
||||
abort_if_fail "Error ${RETVAL} creating PR: ${RESPONSE}"
|
||||
else
|
||||
echo "::group::github response"
|
||||
echo "${RESPONSE}"
|
||||
echo "::endgroup::github response"
|
||||
|
||||
NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number')
|
||||
printf "Number opened for PR is ${NUMBER}\n"
|
||||
HTML_URL=$(echo "${RESPONSE}" | jq --raw-output '.html_url')
|
||||
|
||||
echo ::set-env name=PULL_REQUEST_NUMBER::${NUMBER}
|
||||
echo ::set-output name=pull_request_number::${NUMBER}
|
||||
echo ::set-env name=PULL_REQUEST_RETURN_CODE::${RETVAL}
|
||||
echo ::set-output name=pull_request_return_code::${RETVAL}
|
||||
echo ::set-env name=PULL_REQUEST_URL::${HTML_URL}
|
||||
echo ::set-output name=pull_request_url::${HTML_URL}
|
||||
|
||||
# Assignees are defined
|
||||
if [[ "$ASSIGNEES" != '""' ]]; then
|
||||
|
||||
# Remove leading and trailing quotes
|
||||
ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//')
|
||||
|
||||
# Parse assignees into a list
|
||||
ASSIGNEES=$(echo $ASSIGNEES | printf '"%s"\n' $ASSIGNEES|paste -sd, -)
|
||||
printf "Attempting to assign ${ASSIGNEES} to ${PR} with number ${NUMBER}\n"
|
||||
|
||||
# POST /repos/:owner/:repo/issues/:issue_number/assignees
|
||||
DATA="{\"assignees\":[${ASSIGNEES}]}"
|
||||
echo "${DATA}"
|
||||
ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees"
|
||||
RETVAL=0
|
||||
if ! RESPONSE="$(curl_wrapper -X POST --data "${DATA}" ${ASSIGNEES_URL})"; then
|
||||
RETVAL=$?
|
||||
fi
|
||||
printf "Add assignees return code: ${RETVAL}\n"
|
||||
echo ::set-env name=ASSIGNEES_RETURN_CODE::${RETVAL}
|
||||
echo ::set-output name=assignees_return_code::${RETVAL}
|
||||
if [[ "${RETVAL}" != 0 ]]; then
|
||||
abort_if_fail "Error ${RETVAL} adding assignees: $RESPONSE"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Reviewers or team reviewers are defined
|
||||
if [[ "$REVIEWERS" != '""' ]] || [[ "$TEAM_REVIEWERS" != '""' ]]; then
|
||||
|
||||
printf "Found reviewers: ${REVIEWERS} and team reviewers: ${TEAM_REVIEWERS}\n"
|
||||
|
||||
REVIEWERS=$(echo "$REVIEWERS" | sed -e 's/^"//' -e 's/"$//')
|
||||
REVIEWERS=$(echo $REVIEWERS | printf '"%s"\n' $REVIEWERS|paste -sd, -)
|
||||
TEAM_REVIEWERS=$(echo "$TEAM_REVIEWERS" | sed -e 's/^"//' -e 's/"$//')
|
||||
TEAM_REVIEWERS=$(echo $TEAM_REVIEWERS | printf '"%s"\n' $TEAM_REVIEWERS|paste -sd, -)
|
||||
|
||||
# If either is empty, don't include emty string (provide empty list)
|
||||
if [[ "$REVIEWERS" == '""' ]]; then REVIEWERS=; fi
|
||||
if [[ "$TEAM_REVIEWERS" == '""' ]]; then TEAM_REVIEWERS=; fi
|
||||
|
||||
# POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers
|
||||
REVIEWERS_URL="${PULLS_URL}/${NUMBER}/requested_reviewers"
|
||||
DATA="{\"reviewers\":[${REVIEWERS}], \"team_reviewers\":[${TEAM_REVIEWERS}]}"
|
||||
RETVAL=0
|
||||
if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${REVIEWERS_URL}); then
|
||||
RETVAL=$?
|
||||
printf "Return value of ${RETVAL} for ${REVIEWERS_URL}\n"
|
||||
else
|
||||
echo "::group::github reviewers response"
|
||||
echo "${RESPONSE}"
|
||||
echo "::endgroup::github reviewers response"
|
||||
fi
|
||||
echo ::set-env name=REVIEWERS_RETURN_CODE::${RETVAL}
|
||||
echo ::set-output name=reviewers_return_code::${RETVAL}
|
||||
printf "Add reviewers return code: ${RETVAL}\n"
|
||||
|
||||
if [[ "${RETVAL}" != 0 ]]; then
|
||||
abort_if_fail "Error ${RETVAL} setting reviewers: ${RESPONSE}\n"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
main () {
|
||||
|
||||
# path to file that contains the POST response of the event
|
||||
# Example: https://github.com/actions/bin/tree/master/debug
|
||||
# Value: /github/workflow/event.json
|
||||
check_events_json;
|
||||
|
||||
# User specified branch to PR to, and check
|
||||
if [ -z "${BRANCH_PREFIX}" ]; then
|
||||
printf "No branch prefix is set, all branches will be used.\n"
|
||||
BRANCH_PREFIX=""
|
||||
printf "Branch prefix is $BRANCH_PREFIX\n"
|
||||
fi
|
||||
|
||||
if [ -z "${PULL_REQUEST_BRANCH}" ]; then
|
||||
PULL_REQUEST_BRANCH=master
|
||||
fi
|
||||
printf "Pull requests will go to ${PULL_REQUEST_BRANCH}\n"
|
||||
|
||||
# Pull request draft
|
||||
if [ -z "${PULL_REQUEST_DRAFT}" ]; then
|
||||
printf "No explicit preference for draft PR: created PRs will be normal PRs.\n"
|
||||
PULL_REQUEST_DRAFT="false"
|
||||
else
|
||||
printf "Environment variable PULL_REQUEST_DRAFT set to a value: created PRs will be draft PRs.\n"
|
||||
PULL_REQUEST_DRAFT="true"
|
||||
fi
|
||||
|
||||
# Maintainer can modify, defaults to CAN, unless user sets MAINTAINER_CANT_MODIFY
|
||||
if [ -z "${MAINTAINER_CANT_MODIFY}" ]; then
|
||||
printf "No explicit preference for maintainer being able to modify: default is true.\n"
|
||||
MODIFY="true"
|
||||
else
|
||||
printf "Environment variable MAINTAINER_CANT_MODIFY set to a value: maintainer will not be able to modify.\n"
|
||||
MODIFY="false"
|
||||
fi
|
||||
|
||||
# Assignees
|
||||
ASSIGNEES=""
|
||||
if [ -z "${PULL_REQUEST_ASSIGNEES}" ]; then
|
||||
printf "PULL_REQUEST_ASSIGNEES is not set, no assignees.\n"
|
||||
else
|
||||
printf "PULL_REQUEST_ASSIGNEES is set, ${PULL_REQUEST_ASSIGNEES}\n"
|
||||
ASSIGNEES="${PULL_REQUEST_ASSIGNEES}"
|
||||
fi
|
||||
|
||||
# Reviewers (individual and team)
|
||||
TEAM_REVIEWERS=""
|
||||
REVIEWERS=""
|
||||
if [ -z "${PULL_REQUEST_REVIEWERS}" ]; then
|
||||
printf "PULL_REQUEST_REVIEWERS is not set, no reviewers.\n"
|
||||
else
|
||||
printf "PULL_REQUEST_REVIEWERS is set, ${PULL_REQUEST_REVIEWERS}\n"
|
||||
REVIEWERS="${PULL_REQUEST_REVIEWERS}"
|
||||
fi
|
||||
if [ -z "${PULL_REQUEST_TEAM_REVIEWERS}" ]; then
|
||||
printf "PULL_REQUEST_TEAM_REVIEWERS is not set, no team reviewers.\n"
|
||||
else
|
||||
printf "PULL_REQUEST_TEAM_REVIEWERS is set, ${PULL_REQUEST_TEAM_REVIEWERS}\n"
|
||||
TEAM_REVIEWERS="${PULL_REQUEST_TEAM_REVIEWERS}"
|
||||
fi
|
||||
|
||||
# The user is allowed to explicitly set the name of the branch
|
||||
if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then
|
||||
printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n"
|
||||
BRANCH=$(jq --raw-output .ref "${GITHUB_EVENT_PATH}");
|
||||
BRANCH=$(echo "${BRANCH/refs\/heads\//}")
|
||||
else
|
||||
printf "PULL_REQUEST_FROM_BRANCH is set.\n"
|
||||
BRANCH="${PULL_REQUEST_FROM_BRANCH}"
|
||||
fi
|
||||
|
||||
# At this point, we must have a branch
|
||||
if [[ "$BRANCH" != "null" ]]; then
|
||||
printf "Found branch $BRANCH to open PR from\n"
|
||||
else
|
||||
printf "No branch in payload, you are required to define PULL_REQUEST_FROM_BRANCH in the environment.\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# If it's to the target branch, ignore it
|
||||
if [[ "${BRANCH}" == "${PULL_REQUEST_BRANCH}" ]]; then
|
||||
printf "Target and current branch are identical (${BRANCH}), skipping.\n"
|
||||
else
|
||||
|
||||
# If the prefix for the branch matches
|
||||
if [[ $BRANCH == ${BRANCH_PREFIX}* ]]; then
|
||||
|
||||
# Ensure we have a GitHub token
|
||||
check_credentials
|
||||
|
||||
# Pull request body (optional)
|
||||
if [ -z "${PULL_REQUEST_BODY}" ]; then
|
||||
echo "No pull request body is set, will use default."
|
||||
PULL_REQUEST_BODY="This is an automated pull request to update from branch ${BRANCH}"
|
||||
fi
|
||||
printf "Pull request body is ${PULL_REQUEST_BODY}\n"
|
||||
|
||||
# Pull request title (optional)
|
||||
if [ -z "${PULL_REQUEST_TITLE}" ]; then
|
||||
printf "No pull request title is set, will use default.\n"
|
||||
PULL_REQUEST_TITLE="Update from ${BRANCH}"
|
||||
fi
|
||||
printf "Pull request title is ${PULL_REQUEST_TITLE}\n"
|
||||
|
||||
create_pull_request "${BRANCH}" "${PULL_REQUEST_BRANCH}" "${PULL_REQUEST_BODY}" \
|
||||
"${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}" "${MODIFY}" \
|
||||
"${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}"
|
||||
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Run curl with default values
|
||||
curl_wrapper() {
|
||||
printf "curl -fsSL -H 'AUTH...' %s\n" "$*" >&2
|
||||
curl -fsSL -H "${AUTH_HEADER}" -H "${HEADER}" "$@"
|
||||
ret=$?
|
||||
return $ret
|
||||
}
|
||||
|
||||
# Print the response and, if FAIL_ON_ERROR, exit with an error
|
||||
abort_if_fail() {
|
||||
printf "%s\n" "$*"
|
||||
if [[ -n "${FAIL_ON_ERROR}" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
echo "==========================================================================
|
||||
START: Running Pull Request on Branch Update Action!";
|
||||
main;
|
||||
echo "==========================================================================
|
||||
END: Finished";
|
||||
Reference in New Issue
Block a user