Quote JSON Strings (#8)

The previous approach causes problems when there are e.g. newlines in the pull
request body. Also, the `draft` parameter was previously passed as a JSON
string (`"draft": "true"` and `"draft": "false"`, as opposed to `"draft": true`
and `"draft": false`), which I suspect is just a bug.
This commit is contained in:
Godfrey Chan
2019-08-23 08:03:18 -07:00
committed by Vanessasaurus
parent 0b6c3279d5
commit e54bcbb815

View File

@@ -39,20 +39,27 @@ check_events_json() {
exit 1; exit 1;
fi fi
echo "Found ${GITHUB_EVENT_PATH}"; echo "Found ${GITHUB_EVENT_PATH}";
} }
create_pull_request() { create_pull_request() {
SOURCE="${1}" # from this branch # JSON strings
TARGET="${2}" # pull request TO this target SOURCE="$(echo -n "${1}" | jq --raw-input --slurp ".")" # from this branch
BODY="${3}" # this is the content of the message TARGET="$(echo -n "${2}" | jq --raw-input --slurp ".")" # pull request TO this target
TITLE="${4}" # pull request title BODY="$(echo -n "${3}" | jq --raw-input --slurp ".")" # this is the content of the message
DRAFT="${5}" # if PRs are draft TITLE="$(echo -n "${4}" | jq --raw-input --slurp ".")" # pull request title
# Check if the branch already has a pull request open # JSON boolean
if [[ "${5}" == "true" ]]; then # if PRs are draft
DRAFT="true";
else
DRAFT="false";
fi
DATA="{\"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"body\":\"${BODY}\"}" # Check if the branch already has a pull request open
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X GET --data "${DATA}" ${PULLS_URL}) 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') PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
echo "Response ref: ${PR}" echo "Response ref: ${PR}"
@@ -64,7 +71,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}\", \"body\":\"${BODY}\", \"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"draft\":\"${DRAFT}\"}" DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"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 $?
@@ -121,7 +128,7 @@ main () {
PULL_REQUEST_BODY="This is an automated pull request to update the container collection ${BRANCH}" PULL_REQUEST_BODY="This is an automated pull request to update the container collection ${BRANCH}"
fi fi
echo "Pull request body is ${PULL_REQUEST_BODY}" echo "Pull request body is ${PULL_REQUEST_BODY}"
# Pull request title (optional) # Pull request title (optional)
if [ -z "${PULL_REQUEST_TITLE}" ]; then if [ -z "${PULL_REQUEST_TITLE}" ]; then
echo "No pull request title is set, will use default." echo "No pull request title is set, will use default."