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

status update checks #105

Merged
merged 4 commits into from
Apr 4, 2024
Merged

status update checks #105

merged 4 commits into from
Apr 4, 2024

Conversation

amirmalka
Copy link
Contributor

@amirmalka amirmalka commented Apr 3, 2024

User description


Type

enhancement, bug_fix


Description

  • Introduced validation and rejection logic for status updates in both ApplicationProfile and NetworkNeighbors to prevent invalid status transitions.
  • Added utility functions in pkg/utils/validations.go for validating completion and status annotations with predefined valid values.

Changes walkthrough

Relevant files
Enhancement
strategy.go
Enhance ApplicationProfile Status Update Validation           

pkg/registry/softwarecomposition/applicationprofile/strategy.go

  • Added validation and rejection logic for status updates in
    ApplicationProfile.
  • Prevents status transition from 'complete' to 'partial'.
  • Validates completion and status annotations during creation and
    update.
  • +37/-2   
    strategy.go
    Implement NetworkNeighbors Status Update Validation           

    pkg/registry/softwarecomposition/networkneighbors/strategy.go

  • Implemented validation and rejection logic for status updates in
    NetworkNeighbors.
  • Blocks status transition from 'complete' to 'partial'.
  • Validates completion and status annotations during creation and
    update.
  • +38/-3   
    validations.go
    Add Utility Functions for Annotations Validation                 

    pkg/utils/validations.go

  • Introduced utility functions for validating completion and status
    annotations.
  • Supports a set of predefined valid values for each annotation.
  • +32/-0   

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

    Signed-off-by: Amir Malka <[email protected]>
    @codiumai-pr-agent-free codiumai-pr-agent-free bot added enhancement New feature or request bug_fix labels Apr 3, 2024
    Copy link

    PR Description updated to latest commit (9c8ab02)

    Copy link

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are mostly focused on adding validation logic and annotations handling, which are straightforward to review. The logic is not deeply complex but requires understanding of the existing annotation and status management system.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The validation logic in both PrepareForUpdate methods (for ApplicationProfile and NetworkNeighbors) directly modifies the annotations of the new object to match the old object if a transition from 'complete' to 'partial' is attempted. This approach might not be thread-safe and could lead to race conditions in a concurrent environment. Consider using a more robust method to handle this validation and rejection logic.

    Inconsistency: The validation functions ValidateCompletionAnnotation and ValidateStatusAnnotation in pkg/utils/validations.go return a single error. In scenarios where multiple annotations are invalid, it would only report the first encountered error. This might not provide full visibility into all issues with the annotations to the caller. Consider aggregating all errors before returning.

    🔒 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                                                                                                                                                       
    Maintainability
    Refactor annotation checks and settings into a utility function for better code reuse.

    Instead of manually checking and setting the annotations for completion and status in
    PrepareForUpdate, consider creating a utility function that encapsulates this logic. This
    will improve code reuse and maintainability.

    pkg/registry/softwarecomposition/applicationprofile/strategy.go [67-69]

    -if oldAP.Annotations[helpers.CompletionMetadataKey] == helpers.Complete && newAP.Annotations[helpers.CompletionMetadataKey] == helpers.Partial {
    -    newAP.Annotations[helpers.CompletionMetadataKey] = helpers.Complete
    -    newAP.Annotations[helpers.StatusMetadataKey] = oldAP.Annotations[helpers.StatusMetadataKey]
    -}
    +utils.EnforceCompletionAndStatusAnnotations(oldAP.Annotations, newAP.Annotations)
     
    Use a loop to append validation errors for improved readability.

    To improve code readability and maintainability, consider using a loop to append errors
    from validation functions in Validate and ValidateUpdate methods, similar to the previous
    suggestion.

    pkg/registry/softwarecomposition/networkneighbors/strategy.go [75-80]

    -if err := utils.ValidateCompletionAnnotation(nn.Annotations); err != nil {
    -    allErrors = append(allErrors, err)
    +validationFuncs := []func(map[string]string) *field.Error{
    +    utils.ValidateCompletionAnnotation,
    +    utils.ValidateStatusAnnotation,
     }
    +for _, validate := range validationFuncs {
    +    allErrors = append(allErrors, validate(nn.Annotations))
    +}
    +// Filter out nil errors before returning
    +var validErrors field.ErrorList
    +for _, err := range allErrors {
    +    if err != nil {
    +        validErrors = append(validErrors, err)
    +    }
    +}
    +return validErrors
     
    Bug
    Add nil check for annotations map to prevent potential nil pointer dereferences.

    Consider adding error handling for the case where the annotations map is nil to prevent
    potential nil pointer dereferences in ValidateCompletionAnnotation and
    ValidateStatusAnnotation.

    pkg/utils/validations.go [9]

    +if annotations == nil {
    +    return field.Required(field.NewPath("metadata").Child("annotations"), "annotations cannot be nil")
    +}
     if v, ok := annotations[helpers.CompletionMetadataKey]; ok {
     
    Enhancement
    Aggregate validation errors for comprehensive feedback.

    The error handling in Validate and ValidateUpdate methods could be enhanced by aggregating
    errors instead of returning immediately upon encountering the first error. This provides a
    more comprehensive validation feedback.

    pkg/registry/softwarecomposition/applicationprofile/strategy.go [78-83]

    -if err := utils.ValidateCompletionAnnotation(ap.Annotations); err != nil {
    -    allErrors = append(allErrors, err)
    +allErrors = append(allErrors, utils.ValidateCompletionAnnotation(ap.Annotations))
    +allErrors = append(allErrors, utils.ValidateStatusAnnotation(ap.Annotations))
    +// Filter out nil errors before returning
    +var validErrors field.ErrorList
    +for _, err := range allErrors {
    +    if err != nil {
    +        validErrors = append(validErrors, err)
    +    }
     }
    +return validErrors
     

    ✨ 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 3, 2024

    Summary:

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

    @amirmalka amirmalka requested review from matthyx and dwertent April 3, 2024 16:07
    @matthyx
    Copy link
    Contributor

    matthyx commented Apr 3, 2024

    @amirmalka can you check you test failures?

    Signed-off-by: Amir Malka <[email protected]>
    Copy link

    github-actions bot commented Apr 3, 2024

    Summary:

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

    Copy link
    Contributor

    @matthyx matthyx left a comment

    Choose a reason for hiding this comment

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

    some nits in tests

    Signed-off-by: Amir Malka <[email protected]>
    Copy link
    Contributor

    @matthyx matthyx left a comment

    Choose a reason for hiding this comment

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

    thanks!

    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

    @dwertent dwertent merged commit c8186a0 into main Apr 4, 2024
    6 checks passed
    @matthyx matthyx deleted the status-update-checks branch October 1, 2024 10:02
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants