From c1d64dc4e0a1328107c05bb952133c0ca70b6e1c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Apr 2018 00:13:25 +0200 Subject: update_redmine_status(): Print error if Redmine request fails We want the program to error if the update fails, because that's kind of the whole point. Thanks to 'pvandenberk' for the explanation of how to get the HTTP status code from Curl: https://superuser.com/questions/272265/getting-curl-to-output-http-status-code/442395#442395 $ curl -s -o /dev/null -w "%{http_code}" http://www.example.org/ $ curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/ The error information isn't really enough (we only print the status code, not a real error message from Redmine), but I'm letting it slide for laziness. Now that we're exiting from the program on a Redmine error, it means the cache won't get updated with successful Redmine updated PRs. We need a way to only store successfully updated issue PRs to the cache file at the end of the run. --- redprine | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/redprine b/redprine index 912d825..e664027 100755 --- a/redprine +++ b/redprine @@ -146,18 +146,21 @@ function update_redmine_status () { } }" - curl \ - --verbose \ - --header 'Content-Type: application/json' \ - --header "X-Redmine-API-Key: $REDMINE_TOKEN" \ - --request PUT \ - --data "$json" \ - --location "${REDMINE_BASE_URL}/issues/${issue_id}.json" - - # Output an error message if the request fails - # https://superuser.com/questions/272265/getting-curl-to-output-http-status-code/442395#442395 - # curl -s -o /dev/null -w "%{http_code}" http://www.example.org/ - # curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/ + local status_code=$( + curl \ + --output /dev/null \ + --head \ + --write-out '%{http_code}' \ + --header 'Content-Type: application/json' \ + --header "X-Redmine-API-Key: $REDMINE_TOKEN" \ + --request PUT \ + --data "$json" \ + --location "${REDMINE_BASE_URL}/issues/${issue_id}.json" + ) + + if [ "$status_code" != '200' ]; then + exit_with_error "Redmine API error, status code $status_code" + fi } # Updates the cache file appending new pull requests in our JSON format. -- cgit v1.2.3