Tell curl to set an exit code if a request fails (#39)

* Tell curl to set an exit code if the request fails
* Remove the `--user GITHUB_ACTOR` from curl calls
* fixup! Tell curl to set an exit code if the request fails
This commit is contained in:
Brandon W Maister
2020-08-07 15:09:22 -04:00
committed by GitHub
parent 790b81a742
commit 1106c316c1
2 changed files with 51 additions and 21 deletions

View File

@@ -45,9 +45,11 @@ Unlike standard actions, this action just uses variables from the environment.
| PULL_REQUEST_ASSIGNEES | A list (string with spaces) of users to assign | false | unset | | PULL_REQUEST_ASSIGNEES | A list (string with spaces) of users to assign | false | unset |
| PULL_REQUEST_REVIEWERS | A list (string with spaces) of users to assign review | false | unset | | PULL_REQUEST_REVIEWERS | A list (string with spaces) of users to assign review | false | unset |
| PULL_REQUEST_TEAM_REVIEWERS | A list (string with spaces) of teams to assign review | false | unset | | PULL_REQUEST_TEAM_REVIEWERS | A list (string with spaces) of teams to assign review | false | unset |
| FAIL_ON_ERROR | If any value is present this causes the action to error if PR creation fails for any reason | unset |
For `PULL_REQUEST_DRAFT` and `MAINTAINER_CANT_MODIFY`, these are treated as environment For `PULL_REQUEST_DRAFT`, `FAIL_ON_ERROR`, and `MAINTAINER_CANT_MODIFY`, these are
booleans. If they are defined in the environment, they trigger the "true" condition. E.g.,: treated as environment booleans. If they are defined in the environment, they trigger the
"true" condition. E.g.,:
- Define `MAINTAINER_CANT_MODIFY` if you don't want the maintainer to be able to modify the pull request. - Define `MAINTAINER_CANT_MODIFY` if you don't want the maintainer to be able to modify the pull request.
- Define `PULL_REQUEST_DRAFT` if you want the PR to be a draft. - Define `PULL_REQUEST_DRAFT` if you want the PR to be a draft.

View File

@@ -43,6 +43,7 @@ check_events_json() {
} }
create_pull_request() { create_pull_request() {
# JSON strings # JSON strings
@@ -56,9 +57,14 @@ create_pull_request() {
REVIEWERS="$(echo -n "${8}" | jq --raw-input --slurp ".")" REVIEWERS="$(echo -n "${8}" | jq --raw-input --slurp ".")"
TEAM_REVIEWERS="$(echo -n "${9}" | jq --raw-input --slurp ".")" TEAM_REVIEWERS="$(echo -n "${9}" | jq --raw-input --slurp ".")"
RETVAL=0
# 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}) if ! RESPONSE=$(curl_wrapper -X GET --data "${DATA}" ${PULLS_URL}); then
RETVAL=$?
abort_if_fail "Error ${RETVAL} getting open PRs: ${RESPONSE}"
fi
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref') PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
printf "Response ref: ${PR}\n" printf "Response ref: ${PR}\n"
@@ -70,15 +76,13 @@ create_pull_request() {
else else
# Post the pull request # Post the pull request
DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"base\":${TARGET}, \"head\":${SOURCE}, \"draft\":${DRAFT}, \"maintainer_can_modify\":${MODIFY}}" 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" if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${PULLS_URL}); then
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}) RETVAL=$?
RETVAL=$? abort_if_fail "Error ${RETVAL} creating PR: ${RESPONSE}"
printf "Pull request return code: ${RETVAL}\n" else
echo "::group::github response"
# if we were successful to open, add assignees and reviewers
if [[ "${RETVAL}" == "0" ]]; then
echo "${RESPONSE}" echo "${RESPONSE}"
echo "::endgroup::github response"
NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number') NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number')
printf "Number opened for PR is ${NUMBER}\n" printf "Number opened for PR is ${NUMBER}\n"
@@ -105,12 +109,16 @@ create_pull_request() {
DATA="{\"assignees\":[${ASSIGNEES}]}" DATA="{\"assignees\":[${ASSIGNEES}]}"
echo "${DATA}" echo "${DATA}"
ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees" ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${ASSIGNEES_URL} RETVAL=0
RETVAL=$? if ! RESPONSE="$(curl_wrapper -X POST --data "${DATA}" ${ASSIGNEES_URL})"; then
RETVAL=$?
fi
printf "Add assignees return code: ${RETVAL}\n" printf "Add assignees return code: ${RETVAL}\n"
echo ::set-env name=ASSIGNEES_RETURN_CODE::${RETVAL} echo ::set-env name=ASSIGNEES_RETURN_CODE::${RETVAL}
echo ::set-output name=assignees_return_code::${RETVAL} echo ::set-output name=assignees_return_code::${RETVAL}
if [[ "${RETVAL}" != 0 ]]; then
abort_if_fail "Error ${RETVAL} adding assignees: $RESPONSE"
fi
fi fi
# Reviewers or team reviewers are defined # Reviewers or team reviewers are defined
@@ -130,14 +138,17 @@ create_pull_request() {
# POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers # POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers
REVIEWERS_URL="${PULLS_URL}/${NUMBER}/requested_reviewers" REVIEWERS_URL="${PULLS_URL}/${NUMBER}/requested_reviewers"
DATA="{\"reviewers\":[${REVIEWERS}], \"team_reviewers\":[${TEAM_REVIEWERS}]}" DATA="{\"reviewers\":[${REVIEWERS}], \"team_reviewers\":[${TEAM_REVIEWERS}]}"
echo "${DATA}" RETVAL=0
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${REVIEWERS_URL} if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${REVIEWERS_URL}); then
RETVAL=$?
RETVAL=$? fi
printf "Add reviewers return code: ${RETVAL}\n"
echo ::set-env name=REVIEWERS_RETURN_CODE::${RETVAL} echo ::set-env name=REVIEWERS_RETURN_CODE::${RETVAL}
echo ::set-output name=reviewers_return_code::${RETVAL} echo ::set-output name=reviewers_return_code::${RETVAL}
printf "Add reviewers return code: ${RETVAL}\n"
if [[ "${RETVAL}" != 0 ]]; then
abort_if_fail "Error ${RETVAL} setting reviewers: ${RESPONSE}"
fi
fi fi
fi fi
fi fi
@@ -205,7 +216,7 @@ main () {
printf "PULL_REQUEST_TEAM_REVIEWERS is set, ${PULL_REQUEST_TEAM_REVIEWERS}\n" printf "PULL_REQUEST_TEAM_REVIEWERS is set, ${PULL_REQUEST_TEAM_REVIEWERS}\n"
TEAM_REVIEWERS="${PULL_REQUEST_TEAM_REVIEWERS}" TEAM_REVIEWERS="${PULL_REQUEST_TEAM_REVIEWERS}"
fi fi
# The user is allowed to explicitly set the name of the branch # The user is allowed to explicitly set the name of the branch
if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then
printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n" printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n"
@@ -213,7 +224,7 @@ main () {
BRANCH=$(echo "${BRANCH/refs\/heads\//}") BRANCH=$(echo "${BRANCH/refs\/heads\//}")
else else
printf "PULL_REQUEST_FROM_BRANCH is set.\n" printf "PULL_REQUEST_FROM_BRANCH is set.\n"
BRANCH="${PULL_REQUEST_FROM_BRANCH}" BRANCH="${PULL_REQUEST_FROM_BRANCH}"
fi fi
# At this point, we must have a branch # At this point, we must have a branch
@@ -254,7 +265,24 @@ main () {
"${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}" "${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}"
fi fi
fi
}
# Run curl with default values
curl_wrapper() {
printf "curl -fsSL -H 'AUTH...' %s\n" "$*" >&2
set +e
curl -fsSL -H "${AUTH_HEADER}" -H "${HEADER}" "$@"
ret=$?
set -e
return $ret
}
# Print the response and, if FAIL_ON_ERROR, exit with an error
abort_if_fail() {
printf "%s\n" "$*"
if [[ -n "${FAIL_ON_ERROR}" ]]; then
exit 1
fi fi
} }