From faf4687f172d5d780fe993016cab97fd9bde9215 Mon Sep 17 00:00:00 2001 From: Zoran Regvart Date: Mon, 4 Jan 2021 10:54:51 +0100 Subject: [PATCH] refactor: better HTTP POST error handling --- backport.sh | 38 +++++++++++++++++------------- spec/backport_spec.sh | 55 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/backport.sh b/backport.sh index 30dceda..3898631 100755 --- a/backport.sh +++ b/backport.sh @@ -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}" ] @@ -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 } @@ -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() { @@ -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}' \ diff --git a/spec/backport_spec.sh b/spec/backport_spec.sh index 9184d4d..c3a924a 100755 --- a/spec/backport_spec.sh +++ b/spec/backport_spec.sh @@ -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<"${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
Error
error
"}' + The status should equal 1 + End + End + Describe 'cherry_pick' Describe 'Cherry pick succeeds' @@ -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() { @@ -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
Error
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 ' or 'git rm '\\nhint: and commit the result with 'git commit'
\"} 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 ' or 'git rm ' +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
Error
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 ' or 'git rm '\\nhint: and commit the result with 'git commit'
\"}" The status should equal 1 End End @@ -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 @@ -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 @@ -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