Add ability to create draft PRs with env var (#5)

* initial effort to add draft PRs

Document how to use draft PR feature

* make PRs draft anytime env var is explicitly declared

* change draft PR env var name

Signed-off-by: Ben Cooper <bencooper222@gmail.com>

* document new draft env var name

* consolidate draft PR logging block

* actually remove draft pr logging
This commit is contained in:
Benjamin Cooper
2019-05-17 17:33:39 -07:00
committed by Vanessasaurus
parent 820608e5d2
commit 1e2916c992
2 changed files with 13 additions and 3 deletions

View File

@@ -32,6 +32,7 @@ Environment variables include:
- **PULL_REQUEST_BRANCH**: the branch to issue the pull request to. Defaults to master. - **PULL_REQUEST_BRANCH**: the branch to issue the pull request to. Defaults to master.
- **PULL_REQUEST_BODY**: the body for the pull request (optional) - **PULL_REQUEST_BODY**: the body for the pull request (optional)
- **PULL_REQUEST_TITLE**: the title for the pull request (optional) - **PULL_REQUEST_TITLE**: the title for the pull request (optional)
- **PULL_REQUEST_DRAFT**: should the pull request be a draft PR? (optional; unset defaults to `false`)
## Example use Case: Update Registry ## Example use Case: Update Registry

View File

@@ -12,7 +12,7 @@ API_VERSION=v3
BASE=https://api.github.com BASE=https://api.github.com
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
HEADER="Accept: application/vnd.github.${API_VERSION}+json" HEADER="Accept: application/vnd.github.${API_VERSION}+json"
HEADER="${HEADER}; application/vnd.github.antiope-preview+json" HEADER="${HEADER}; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
# URLs # URLs
REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}" REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}"
@@ -48,6 +48,7 @@ create_pull_request() {
TARGET=${2} # pull request TO this target TARGET=${2} # pull request TO this target
BODY=${3} # this is the content of the message BODY=${3} # this is the content of the message
TITLE=${4} # pull request title TITLE=${4} # pull request title
DRAFT=${5} # if PRs are draft
# Check if the branch already has a pull request open # Check if the branch already has a pull request open
@@ -63,7 +64,7 @@ create_pull_request() {
# Option 2: Open a new pull request # Option 2: Open a new pull request
else else
# Post the pull request # Post the pull request
DATA="{\"title\":\"${TITLE}\", \"base\":\"${TARGET}\", \"head\":\"${SOURCE}\"}" DATA="{\"title\":\"${TITLE}\", \"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"draft\":\"${DRAFT}\"}"
echo "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}" echo "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL} curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}
echo $? echo $?
@@ -90,6 +91,14 @@ main () {
fi fi
echo "Pull requests will go to ${PULL_REQUEST_BRANCH}" echo "Pull requests will go to ${PULL_REQUEST_BRANCH}"
if [ -z "${PULL_REQUEST_DRAFT}" ]; then
echo "No explicit preference for draft PR: created PRs will be normal PRs."
PULL_REQUEST_DRAFT="false"
else
echo "Environment variable PULL_REQUEST_DRAFT set to a value: created PRs will be draft PRs."
PULL_REQUEST_DRAFT="true"
fi
# Get the name of the action that was triggered # Get the name of the action that was triggered
BRANCH=$(jq --raw-output .ref "${GITHUB_EVENT_PATH}"); BRANCH=$(jq --raw-output .ref "${GITHUB_EVENT_PATH}");
BRANCH=$(echo "${BRANCH/refs\/heads\//}") BRANCH=$(echo "${BRANCH/refs\/heads\//}")
@@ -120,7 +129,7 @@ main () {
echo "Pull request title is ${PULL_REQUEST_TITLE}" echo "Pull request title is ${PULL_REQUEST_TITLE}"
fi fi
create_pull_request $BRANCH $PULL_REQUEST_BRANCH $PULL_REQUEST_BODY $PULL_REQUEST_TITLE create_pull_request $BRANCH $PULL_REQUEST_BRANCH $PULL_REQUEST_BODY $PULL_REQUEST_TITLE $PULL_REQUEST_DRAFT
fi fi