Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
refactor: better HTTP POST error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zregvart committed Jan 4, 2021
1 parent 545fbc0 commit faf4687
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
38 changes: 22 additions & 16 deletions backport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
set -e
set -o pipefail

http_post() {
local url=$1
local json=$2

if ! result=$(curl -XPOST -fsL \
--output /dev/null \
-w '{"http_code":%{http_code},"url_effective":"%{url_effective}"}' \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: Bearer ${INPUT_TOKEN}" \
-H "Content-Type: application/json" \
-d "${json}" \
"${url}"); then
local message
message=$(echo "${result}"| jq -r -s 'add | (.http_code|tostring) + ": " + .message + " effective url: " + .url_effective')
echo "::error::Error in HTTP POST to ${url} of \`${json}\`: ${message}"
fi
}

fail() {
local message=$1
local error=$2

echo "::error::${message}"
echo "::error::${message} (${error})"

local comment="${message}"
if [ -n "${error}" ]
Expand All @@ -19,13 +37,7 @@ fail() {
local comments_url
comments_url=$(jq --raw-output .pull_request._links.comments.href "${GITHUB_EVENT_PATH}")

curl -XPOST -fsSL \
--output /dev/null \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: Bearer ${INPUT_TOKEN}" \
-H "Content-Type: application/json" \
-d "${comment_json}" \
"${comments_url}"
http_post "${comments_url}" "${comment_json}"

exit 1
}
Expand Down Expand Up @@ -101,13 +113,7 @@ create_pull_request() {
\"base\": \"${branch}\" \
}"

curl -XPOST -fsSL \
--output /dev/null \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: Bearer ${INPUT_TOKEN}" \
-H "Content-Type: application/json" \
-d "${pull_request}" \
"${pulls_url}"
http_post "${pulls_url}" "${pull_request}"
}

backport() {
Expand Down Expand Up @@ -143,7 +149,7 @@ delete_branch() {
refs_url=$(tmp=$(jq --raw-output .pull_request.head.repo.git_refs_url "${GITHUB_EVENT_PATH}"); echo "${tmp%{*}")

local status
status=$(curl -XDELETE -fsSL \
status=$(curl -XDELETE -fsL \
--fail \
--output /dev/null \
-w '%{http_code}' \
Expand Down
55 changes: 44 additions & 11 deletions spec/backport_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ Describe 'Backport action'
The output should equal 'eC1hY2Nlc3MtdG9rZW46Z2l0aHViLXRva2Vu'
End

Describe 'fail'
# mock http_post
http_post() {
echo "http_post invoked with: $*"
}
setup_event_file() {
cat<<EOF>"${GITHUB_EVENT_PATH}"
{
"pull_request": {
"_links": {
"comments": {
"href": "comments-url"
}
}
}
}
EOF
}
Before 'setup_event_file'
It 'Handles reports failures'
When run fail message error
The output should equal '::error::message (error)
http_post invoked with: comments-url {"body":"message\n\n<details><summary>Error</summary><pre>error</pre></details>"}'
The status should equal 1
End
End
Describe 'cherry_pick'
Describe 'Cherry pick succeeds'
Expand Down Expand Up @@ -54,9 +82,9 @@ Describe 'Backport action'
End
Describe 'Merge conflict'
# mock curl
curl() {
echo "CURL invoked with:$*"
# mock http_post
http_post() {
echo "http_post invoked with:$*"
}
setup_repo() {
Expand Down Expand Up @@ -117,8 +145,13 @@ EOF
It 'Fails due to merge conflict'
When run cherry_pick 'branch' "${git_repository}" 'backport-branch' "${merge_commit_sha}"
The value "$(cd "${GITHUB_WORKSPACE}" && git show backport-branch:file)" should equal "conflict"
The output should match pattern "::error::Unable to cherry-pick commit ${merge_commit_sha} on top of branch \`branch\`.\n\nThis pull request needs to be backported manually.
CURL invoked with:-XPOST -fsSL --output /dev/null -H Accept: application/vnd.github.v3+json -H Authorization: Bearer github-token -H Content-Type: application/json -d {\"body\":\"Unable to cherry-pick commit * on top of branch \`branch\`.\\n\\nThis pull request needs to be backported manually.\\n\\n<details><summary>Error</summary><pre>Auto-merging file\\nCONFLICT (content): Merge conflict in file\\nerror: could not apply *... Merge commit '*'\\nhint: after resolving the conflicts, mark the corrected paths\\nhint: with 'git add <paths>' or 'git rm <paths>'\\nhint: and commit the result with 'git commit'</pre></details>\"} comments-url"
The output should match pattern "::error::Unable to cherry-pick commit ${merge_commit_sha} on top of branch \`branch\`.\n\nThis pull request needs to be backported manually. (Auto-merging file
CONFLICT (content): Merge conflict in file
error: could not apply *... Merge commit '*'
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit')
http_post invoked with:comments-url {\"body\":\"Unable to cherry-pick commit * on top of branch \`branch\`.\\n\\nThis pull request needs to be backported manually.\\n\\n<details><summary>Error</summary><pre>Auto-merging file\\nCONFLICT (content): Merge conflict in file\\nerror: could not apply *... Merge commit '*'\\nhint: after resolving the conflicts, mark the corrected paths\\nhint: with 'git add <paths>' or 'git rm <paths>'\\nhint: and commit the result with 'git commit'</pre></details>\"}"
The status should equal 1
End
End
Expand Down Expand Up @@ -152,14 +185,14 @@ CURL invoked with:-XPOST -fsSL --output /dev/null -H Accept: application/vnd.git
End
Describe 'create_pull_request'
# mock curl
curl() {
export curl_args="$*"
# mock http_post
http_post() {
export http_post_args="$*"
}
It 'Creates pull requests'
When call create_pull_request branch backport-branch title 123 url
The variable 'curl_args' should equal "-XPOST -fsSL --output /dev/null -H Accept: application/vnd.github.v3+json -H Authorization: Bearer ${INPUT_TOKEN} -H Content-Type: application/json -d { \"title\": \"[Backport branch] title\", \"body\": \"Backport of #123\", \"head\": \"backport-branch\", \"base\": \"branch\" } url"
The variable 'http_post_args' should equal "url { \"title\": \"[Backport branch] title\", \"body\": \"Backport of #123\", \"head\": \"backport-branch\", \"base\": \"branch\" }"
End
End
Expand Down Expand Up @@ -230,7 +263,7 @@ EOF
It 'Deletes branches'
When call delete_branch backport/123-to-branch
The value "$(cat "${curl_args}")" should equal "-XDELETE -fsSL --fail --output /dev/null -w %{http_code} -H Accept: application/vnd.github.v3+json -H Authorization: Bearer ${INPUT_TOKEN} git-refs-url/heads/backport/123-to-branch"
The value "$(cat "${curl_args}")" should equal "-XDELETE -fsL --fail --output /dev/null -w %{http_code} -H Accept: application/vnd.github.v3+json -H Authorization: Bearer ${INPUT_TOKEN} git-refs-url/heads/backport/123-to-branch"
End
End
Expand All @@ -246,7 +279,7 @@ EOF
It 'Doesn''t fail on deleted branches'
When call delete_branch backport/123-to-branch
The value "$(cat "${curl_args}")" should equal "-XDELETE -fsSL --fail --output /dev/null -w %{http_code} -H Accept: application/vnd.github.v3+json -H Authorization: Bearer ${INPUT_TOKEN} git-refs-url/heads/backport/123-to-branch"
The value "$(cat "${curl_args}")" should equal "-XDELETE -fsL --fail --output /dev/null -w %{http_code} -H Accept: application/vnd.github.v3+json -H Authorization: Bearer ${INPUT_TOKEN} git-refs-url/heads/backport/123-to-branch"
End
End
Expand Down

0 comments on commit faf4687

Please sign in to comment.