Skip to content
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

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

JoshuaSBrown
Copy link
Collaborator

@JoshuaSBrown JoshuaSBrown commented Nov 14, 2024

Summary by Sourcery

Enhancements:

  • Improve error handling in the Harbor artifact count script by checking for error responses in the API response.

Copy link

sourcery-ai bot commented Nov 14, 2024

Reviewer's Guide by Sourcery

The changes improve error handling in a shell script that interacts with Harbor's API to count artifacts. The implementation adds specific handling for the 'NOT_FOUND' error case when an image hasn't been uploaded yet, returning 0 instead of failing. The script now properly parses the error response JSON before making assumptions about the response structure.

Sequence diagram for improved error handling in Harbor script

sequenceDiagram
    participant Script
    participant HarborAPI
    Script->>HarborAPI: GET /api/v2.0/projects/{project}/repositories/{repository}/artifacts
    alt Image not uploaded
        HarborAPI-->>Script: { "errors": [{ "code": "NOT_FOUND", "message": "..." }] }
        Script->>Script: Check error code
        Script->>Script: Return 0
    else Image uploaded
        HarborAPI-->>Script: List of artifacts
        Script->>Script: Count artifacts
        Script->>Script: Log number of artifacts
    end
Loading

File-Level Changes

Change Details Files
Added robust error handling for Harbor API responses
  • Added documentation for the error response JSON structure
  • Added parsing of error response using jq to check for error objects
  • Added specific handling for 'NOT_FOUND' error code to return 0
  • Added fallback error handling for unhandled error codes
  • Added clarifying comment about response structure assumptions
scripts/ci_harbor_artifact_count.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@JoshuaSBrown JoshuaSBrown added the Type: Bug Something isn't working label Nov 14, 2024
@JoshuaSBrown JoshuaSBrown linked an issue Nov 14, 2024 that may be closed by this pull request
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @JoshuaSBrown - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -67,6 +81,26 @@ then
echo "$data" | jq >> "$LOG_FILE"
fi
number_of_artifacts=$(echo "$data" | jq ' . | length')
Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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 jq ' . | length' will return 0, which might incorrectly suggest that there are no artifacts, rather than indicating an error.

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 jq to get the length of the artifacts array.

@@ -44,6 +44,20 @@
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" )
Copy link

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

# ]
# }


error_code=$(echo $?)
Copy link

Choose a reason for hiding this comment

The 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
error_code=$(echo $?)
error_message=$(curl -s -f "$url" 2>&1 || true)
error_code=$?
if [ $error_code -ne 0 ]; then
echo "Curl failed with error: $error_message"
fi

@JoshuaSBrown JoshuaSBrown self-assigned this Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CI] - Bug harbor check is not accurate
1 participant