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

solved locked routine when image has more than 1 tag #218

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Conversation

matthyx
Copy link
Contributor

@matthyx matthyx commented Apr 4, 2024

Type

bug_fix, enhancement


Description

  • Corrected spelling mistakes in error messages and comments.
  • Changed slice initialization for clarity and consistency.
  • Updated createTriggerRequestConfigMap and setCronJobTemplate functions to remove unnecessary parameters.
  • Fixed incorrect logging message during image scanning.
  • Improved error handling and logging for the process of scanning image tags.

Changes walkthrough

Relevant files
Enhancement
imageregistryhandler.go
Refactoring and Bug Fixes in Image Registry Handler           

mainhandler/imageregistryhandler.go

  • Fixed typos in error messages and comments.
  • Simplified slice initialization.
  • Removed unnecessary parameters in createTriggerRequestConfigMap and
    setCronJobTemplate functions.
  • Corrected log message from "repoes" to "repos".
  • Enhanced error handling and logging for image tag scanning.
  • +16/-21 
    imageregistryhandlerhelper.go
    Update ConfigMap Creation Call to Match New Signature       

    mainhandler/imageregistryhandlerhelper.go

  • Updated createTriggerRequestConfigMap call to match its new signature.
  • +1/-1     

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @matthyx matthyx requested a review from dwertent April 4, 2024 12:03
    @matthyx matthyx added the release Create release label Apr 4, 2024
    @codiumai-pr-agent-free codiumai-pr-agent-free bot added enhancement New feature or request bug_fix labels Apr 4, 2024
    Copy link

    PR Description updated to latest commit (0c64b62)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are mostly straightforward, involving spelling corrections, refactoring for clarity, and parameter adjustments. The logic seems to remain largely unchanged, making it easier to review.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The change from []string{} to var tags []string inside loops might unintentionally shadow the outer tags variable, especially in the setImageToTagsMap function. This could lead to logical errors where the intended operations on the outer tags variable are instead performed on a new, shadowed tags variable within the loop's scope.

    Refactoring Concern: The removal of parameters in the createTriggerRequestConfigMap and setCronJobTemplate functions simplifies the interface but requires careful validation to ensure that all calls to these functions throughout the codebase have been correctly updated to match the new signatures.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Handle errors returned by function calls instead of ignoring them.

    Consider handling the error returned by regCommon.GetRegistryKind instead of ignoring it.
    This can help in identifying issues with fetching the registry kind early on.

    mainhandler/imageregistryhandler.go [213]

    -reg.Kind, _ = regCommon.GetRegistryKind(strings.Split(reg.Registry, "/")[0])
    +var err error
    +reg.Kind, err = regCommon.GetRegistryKind(strings.Split(reg.Registry, "/")[0])
    +if err != nil {
    +    return err
    +}
     
    Initialize maps before assignment to avoid panics.

    It's a good practice to check if the map configMap.Data is initialized before assigning
    values to it to avoid potential nil map assignment panics.

    mainhandler/imageregistryhandler.go [301]

    +if configMap.Data == nil {
    +    configMap.Data = make(map[string]string)
    +}
     configMap.Data[requestBodyFile] = command
     
    Use fmt.Errorf for creating error messages with dynamic content.

    Use fmt.Errorf to include dynamic values within error messages instead of
    errorWithDocumentationRef for consistency and to leverage Go's standard error handling.

    mainhandler/imageregistryhandler.go [370-372]

     errMsg := fmt.Sprintf("image %s has %d tags. scanning only first %d tags - %s", repo, tagsForDigestLen, *tagsDepth, strings.Join(tagsForDigest[:*tagsDepth], ","))
     if sender != nil {
    -    err := errorWithDocumentationRef(errMsg)
    +    err := fmt.Errorf(errMsg)
     
    Maintainability
    Avoid variable shadowing within the same function.

    Instead of redeclaring var tags []string inside the if block, consider using the tags
    variable declared at the start of the function to avoid shadowing and potential confusion.

    mainhandler/imageregistryhandler.go [351-357]

     var tags []string
     if latestTags, err := iRegistry.GetLatestTags(repo, *tagsDepth, options...); err == nil {
    -    var tags []string
     
    Readability
    Use descriptive variable names for better readability.

    Consider using a more descriptive variable name than c for the range variable in the loop.
    This enhances code readability.

    mainhandler/imageregistryhandler.go [767-768]

    -for _, c := range registriesConfigs {
    -    if c.Registry == registryInfo.RegistryName {
    +for _, config := range registriesConfigs {
    +    if config.Registry == registryInfo.RegistryName {
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    Copy link

    github-actions bot commented Apr 4, 2024

    Summary:

    • License scan: failure
    • Credentials scan: failure
    • Vulnerabilities scan: failure
    • Unit test: success
    • Go linting: success

    err := errorWithDocumentationRef(errMsg)
    sender.SendWarning(err.Error(), registryScan.sendReport, true)
    if err := <-errChan; err != nil {
    Copy link

    Choose a reason for hiding this comment

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

    Was this the issue?

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    yes

    @matthyx matthyx merged commit 58f4a1c into main Apr 4, 2024
    8 checks passed
    @matthyx matthyx deleted the nolock branch April 4, 2024 15:05
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants