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

@@ -44,15 +44,22 @@ check_events_json() {
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
# JSON boolean
if [[ "${5}" == "true" ]]; then # if PRs are draft
DRAFT="true";
else
DRAFT="false";
fi
# Check if the branch already has a pull request open # Check if the branch already has a pull request open
DATA="{\"base\":\"${TARGET}\", \"head\":\"${SOURCE}\", \"body\":\"${BODY}\"}" 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 $?