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:
committed by
GitHub
parent
790b81a742
commit
1106c316c1
@@ -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_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 |
|
||||
| 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
|
||||
booleans. If they are defined in the environment, they trigger the "true" condition. E.g.,:
|
||||
For `PULL_REQUEST_DRAFT`, `FAIL_ON_ERROR`, and `MAINTAINER_CANT_MODIFY`, these are
|
||||
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 `PULL_REQUEST_DRAFT` if you want the PR to be a draft.
|
||||
|
||||
@@ -43,6 +43,7 @@ check_events_json() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
create_pull_request() {
|
||||
|
||||
# JSON strings
|
||||
@@ -56,9 +57,14 @@ create_pull_request() {
|
||||
REVIEWERS="$(echo -n "${8}" | 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
|
||||
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')
|
||||
printf "Response ref: ${PR}\n"
|
||||
|
||||
@@ -70,15 +76,13 @@ create_pull_request() {
|
||||
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"
|
||||
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL})
|
||||
RETVAL=$?
|
||||
printf "Pull request return code: ${RETVAL}\n"
|
||||
|
||||
# if we were successful to open, add assignees and reviewers
|
||||
if [[ "${RETVAL}" == "0" ]]; then
|
||||
|
||||
if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${PULLS_URL}); then
|
||||
RETVAL=$?
|
||||
abort_if_fail "Error ${RETVAL} creating PR: ${RESPONSE}"
|
||||
else
|
||||
echo "::group::github response"
|
||||
echo "${RESPONSE}"
|
||||
echo "::endgroup::github response"
|
||||
|
||||
NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number')
|
||||
printf "Number opened for PR is ${NUMBER}\n"
|
||||
@@ -105,12 +109,16 @@ create_pull_request() {
|
||||
DATA="{\"assignees\":[${ASSIGNEES}]}"
|
||||
echo "${DATA}"
|
||||
ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees"
|
||||
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${ASSIGNEES_URL}
|
||||
RETVAL=$?
|
||||
RETVAL=0
|
||||
if ! RESPONSE="$(curl_wrapper -X POST --data "${DATA}" ${ASSIGNEES_URL})"; then
|
||||
RETVAL=$?
|
||||
fi
|
||||
printf "Add assignees return code: ${RETVAL}\n"
|
||||
echo ::set-env 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
|
||||
|
||||
# Reviewers or team reviewers are defined
|
||||
@@ -130,14 +138,17 @@ create_pull_request() {
|
||||
# POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers
|
||||
REVIEWERS_URL="${PULLS_URL}/${NUMBER}/requested_reviewers"
|
||||
DATA="{\"reviewers\":[${REVIEWERS}], \"team_reviewers\":[${TEAM_REVIEWERS}]}"
|
||||
echo "${DATA}"
|
||||
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${REVIEWERS_URL}
|
||||
|
||||
RETVAL=$?
|
||||
printf "Add reviewers return code: ${RETVAL}\n"
|
||||
RETVAL=0
|
||||
if ! RESPONSE=$(curl_wrapper -X POST --data "${DATA}" ${REVIEWERS_URL}); then
|
||||
RETVAL=$?
|
||||
fi
|
||||
echo ::set-env 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
|
||||
@@ -205,7 +216,7 @@ main () {
|
||||
printf "PULL_REQUEST_TEAM_REVIEWERS is set, ${PULL_REQUEST_TEAM_REVIEWERS}\n"
|
||||
TEAM_REVIEWERS="${PULL_REQUEST_TEAM_REVIEWERS}"
|
||||
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"
|
||||
@@ -213,7 +224,7 @@ main () {
|
||||
BRANCH=$(echo "${BRANCH/refs\/heads\//}")
|
||||
else
|
||||
printf "PULL_REQUEST_FROM_BRANCH is set.\n"
|
||||
BRANCH="${PULL_REQUEST_FROM_BRANCH}"
|
||||
BRANCH="${PULL_REQUEST_FROM_BRANCH}"
|
||||
fi
|
||||
|
||||
# At this point, we must have a branch
|
||||
@@ -254,7 +265,24 @@ main () {
|
||||
"${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user