-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make harbor script more robust to error response returned by api #1094
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -44,6 +44,20 @@ local_DATAFED_HARBOR_URL="https://$local_DATAFED_HARBOR_REGISTRY" | |||||||||||||
URL="$local_DATAFED_HARBOR_URL/api/v2.0/projects/$local_DATAFED_HARBOR_PROJECT/repositories/$local_DATAFED_HARBOR_REPOSITORY/artifacts" | ||||||||||||||
echo "$URL" >> "$LOG_FILE" | ||||||||||||||
data=$(curl -u $local_DATAFED_HARBOR_USERNAME:$local_DATAFED_HARBOR_PASSWORD -s "${URL}?with_tag=$local_DATAFED_HARBOR_IMAGE_TAG" ) | ||||||||||||||
|
||||||||||||||
# In the case that an image has not yet been uploaded the server will return | ||||||||||||||
# a json object of the following format | ||||||||||||||
# | ||||||||||||||
# { | ||||||||||||||
# "errors": [ | ||||||||||||||
# { | ||||||||||||||
# "code": "NOT_FOUND", | ||||||||||||||
# "message": "path /api/v2.0/projects/datafed/repositories/datafed/gcs-1086-bug-python-client-pypi-version/artifacts was not found" | ||||||||||||||
# } | ||||||||||||||
# ] | ||||||||||||||
# } | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
error_code=$(echo $?) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Curl error handling could capture and log the actual error message Consider capturing and logging curl's error output when the command fails to aid in debugging
Suggested change
|
||||||||||||||
if [ "$error_code" != "0" ] | ||||||||||||||
then | ||||||||||||||
|
@@ -67,6 +81,26 @@ then | |||||||||||||
echo "$data" | jq >> "$LOG_FILE" | ||||||||||||||
fi | ||||||||||||||
number_of_artifacts=$(echo "$data" | jq ' . | length') | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): The error check should happen before getting the array length to avoid skipping error detection If the response contains an error, jq length will return 0, causing the error check to be skipped. Move the error check before the length calculation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite sure what is being proposed here? The error check is dependent on the response being correctly formatted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion is to ensure that any error in the API response is detected before attempting to calculate the length of the data. If the response contains an error, using To address this, you could first check if the response contains an error field or status before proceeding to calculate the length. This way, you can handle any errors appropriately before processing the data. For example, you might want to check for an error message or status code in the response JSON and handle it accordingly before using |
||||||||||||||
|
||||||||||||||
# Make sure the response doesn't contain an error code before assumptions are made | ||||||||||||||
if [ "$number_of_artifacts" != "0" ] | ||||||||||||||
then | ||||||||||||||
ERROR_FOUND=$(echo "$data" | jq -r 'if .errors and (.errors | length > 0) then "TRUE" else "FALSE" end') | ||||||||||||||
if [ "$ERROR_FOUND" == "TRUE" ] | ||||||||||||||
then | ||||||||||||||
ERROR_CODE=$(echo "$data" | jq -r '.errors[0].code') | ||||||||||||||
if [ "$ERROR_CODE" == "NOT_FOUND" ] | ||||||||||||||
then | ||||||||||||||
echo "0" | ||||||||||||||
exit 0 | ||||||||||||||
else | ||||||||||||||
echo "Aborting unhandled error $ERROR_CODE" >> "$LOG_FILE" | ||||||||||||||
exit 1 | ||||||||||||||
fi | ||||||||||||||
fi | ||||||||||||||
fi | ||||||||||||||
|
||||||||||||||
echo "Number of artifacts found: $number_of_artifacts" >> "$LOG_FILE" | ||||||||||||||
|
||||||||||||||
# Otherwise we assume the object contains the list of objects | ||||||||||||||
echo "$number_of_artifacts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Variables should be quoted to prevent word splitting issues
Use "$local_DATAFED_HARBOR_USERNAME" and "$local_DATAFED_HARBOR_PASSWORD" to handle cases where these values contain spaces