adding PR_BRANCH_FROM to support null use case

Signed-off-by: vsoch <vsochat@stanford.edu>
This commit is contained in:
vsoch
2020-03-17 10:45:52 -06:00
parent a3c90d58a7
commit d0c1b9ddb0
2 changed files with 84 additions and 32 deletions

View File

@@ -26,7 +26,7 @@ PULLS_URL=$REPO_URL/pulls
check_credentials() {
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "You must include the GITHUB_TOKEN as an environment variable."
printf "You must include the GITHUB_TOKEN as an environment variable.\n"
exit 1
fi
@@ -35,10 +35,10 @@ check_credentials() {
check_events_json() {
if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then
echo "Cannot find Github events file at ${GITHUB_EVENT_PATH}";
printf "Cannot find Github events file at ${GITHUB_EVENT_PATH}\n";
exit 1;
fi
echo "Found ${GITHUB_EVENT_PATH}";
printf "Found ${GITHUB_EVENT_PATH}\n";
}
@@ -52,9 +52,9 @@ create_pull_request() {
# JSON boolean
if [[ "${5}" == "true" ]]; then # if PRs are draft
DRAFT="true";
DRAFT="true";
else
DRAFT="false";
DRAFT="false";
fi
# Check if the branch already has a pull request open
@@ -62,17 +62,17 @@ create_pull_request() {
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X GET --data "${DATA}" ${PULLS_URL})
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
echo "Response ref: ${PR}"
printf "Response ref: ${PR}\n"
# Option 1: The pull request is already open
if [[ "${PR}" == "${SOURCE}" ]]; then
echo "Pull request from ${SOURCE} to ${TARGET} is already open!"
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}}"
echo "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}"
printf "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}\n"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}
echo $?
fi
@@ -88,38 +88,45 @@ main () {
# User specified branch to PR to, and check
if [ -z "${BRANCH_PREFIX}" ]; then
echo "No branch prefix is set, all branches will be used."
printf "No branch prefix is set, all branches will be used.\n"
BRANCH_PREFIX=""
echo "Branch prefix is $BRANCH_PREFIX"
printf "Branch prefix is $BRANCH_PREFIX\n"
fi
if [ -z "${PULL_REQUEST_BRANCH}" ]; then
PULL_REQUEST_BRANCH=master
fi
echo "Pull requests will go to ${PULL_REQUEST_BRANCH}"
printf "Pull requests will go to ${PULL_REQUEST_BRANCH}\n"
if [ -z "${PULL_REQUEST_DRAFT}" ]; then
echo "No explicit preference for draft PR: created PRs will be normal PRs."
printf "No explicit preference for draft PR: created PRs will be normal PRs.\n"
PULL_REQUEST_DRAFT="false"
else
echo "Environment variable PULL_REQUEST_DRAFT set to a value: created PRs will be draft PRs."
printf "Environment variable PULL_REQUEST_DRAFT set to a value: created PRs will be draft PRs.\n"
PULL_REQUEST_DRAFT="true"
fi
# Get the name of the action that was triggered
BRANCH=$(jq --raw-output .ref "${GITHUB_EVENT_PATH}");
BRANCH=$(echo "${BRANCH/refs\/heads\//}")
if [[ "$BRANCH" != "null" ]]; then
echo "Found branch $BRANCH"
# 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
echo "No trigger branch found in event data. Using '$BRANCH_PREFIX' as BRANCH instead."
BRANCH="$BRANCH_PREFIX"
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
echo "Target and current branch are identical (${BRANCH}), skipping."
printf "Target and current branch are identical (${BRANCH}), skipping.\n"
else
# If the prefix for the branch matches
@@ -133,14 +140,14 @@ main () {
echo "No pull request body is set, will use default."
PULL_REQUEST_BODY="This is an automated pull request to update the container collection ${BRANCH}"
fi
echo "Pull request body is ${PULL_REQUEST_BODY}"
printf "Pull request body is ${PULL_REQUEST_BODY}\n"
# Pull request title (optional)
if [ -z "${PULL_REQUEST_TITLE}" ]; then
echo "No pull request title is set, will use default."
printf "No pull request title is set, will use default.\n"
PULL_REQUEST_TITLE="Update container ${BRANCH}"
fi
echo "Pull request title is ${PULL_REQUEST_TITLE}"
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}"