Files
pull-request-action/pull-request.sh

169 lines
6.2 KiB
Bash
Raw Normal View History

2019-01-31 18:56:56 -05:00
#!/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"
2019-01-31 18:56:56 -05:00
# URLs
REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}"
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"
2019-01-31 18:56:56 -05:00
exit 1
fi
}
check_events_json() {
if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then
printf "Cannot find Github events file at ${GITHUB_EVENT_PATH}\n";
2019-01-31 18:56:56 -05:00
exit 1;
fi
printf "Found ${GITHUB_EVENT_PATH}\n";
2019-01-31 18:56:56 -05:00
}
2019-02-01 17:42:55 -05:00
create_pull_request() {
2019-02-01 17:20:04 -05:00
# 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
2019-02-01 17:42:55 -05:00
# Check if the branch already has a pull request open
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
2019-02-01 17:20:04 -05:00
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X GET --data "${DATA}" ${PULLS_URL})
2019-02-01 17:39:21 -05:00
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
printf "Response ref: ${PR}\n"
2019-02-01 17:20:04 -05:00
2019-02-01 17:42:55 -05:00
# Option 1: The pull request is already open
if [[ "${PR}" == "${SOURCE}" ]]; then
printf "Pull request from ${SOURCE} to ${TARGET} is already open!\n"
2019-02-01 17:42:55 -05:00
# Option 2: Open a new pull request
2019-02-01 17:31:25 -05:00
else
# Post the pull request
DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"base\":${TARGET}, \"head\":${SOURCE}, \"draft\":${DRAFT}, \"maintainer_can_modify\":${MODIFY}}"
printf "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}\n"
2019-02-01 17:31:25 -05:00
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}
echo $?
2019-02-01 17:20:04 -05:00
fi
2019-02-01 13:31:55 -05:00
}
2019-01-31 18:56:56 -05:00
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"
2019-01-31 18:56:56 -05:00
BRANCH_PREFIX=""
printf "Branch prefix is $BRANCH_PREFIX\n"
2019-01-31 18:56:56 -05:00
fi
if [ -z "${PULL_REQUEST_BRANCH}" ]; then
PULL_REQUEST_BRANCH=master
fi
printf "Pull requests will go to ${PULL_REQUEST_BRANCH}\n"
2019-01-31 18:56:56 -05:00
# 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
# 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
2019-03-20 10:05:38 -04:00
2019-01-31 18:56:56 -05:00
# If it's to the target branch, ignore it
2019-01-31 19:03:56 -05:00
if [[ "${BRANCH}" == "${PULL_REQUEST_BRANCH}" ]]; then
printf "Target and current branch are identical (${BRANCH}), skipping.\n"
2019-01-31 18:56:56 -05:00
else
# If the prefix for the branch matches
if [[ $BRANCH == ${BRANCH_PREFIX}* ]]; then
# Ensure we have a GitHub token
check_credentials
2019-03-20 10:05:38 -04:00
# 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 the container collection ${BRANCH}"
fi
printf "Pull request body is ${PULL_REQUEST_BODY}\n"
2019-03-20 10:05:38 -04:00
# Pull request title (optional)
if [ -z "${PULL_REQUEST_TITLE}" ]; then
printf "No pull request title is set, will use default.\n"
2019-03-20 10:05:38 -04:00
PULL_REQUEST_TITLE="Update container ${BRANCH}"
fi
printf "Pull request title is ${PULL_REQUEST_TITLE}\n"
2019-03-20 10:05:38 -04:00
create_pull_request "${BRANCH}" "${PULL_REQUEST_BRANCH}" "${PULL_REQUEST_BODY}" \
"${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}" "${MODIFY}"
2019-01-31 18:56:56 -05:00
fi
fi
}
echo "==========================================================================
START: Running Pull Request on Branch Update Action!";
main;
echo "==========================================================================
END: Finished";