feat: add wildcard suport for repository#2734
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces glob pattern matching for repository URLs in MatchEventURLRepo through a new matchRepo helper function, along with comprehensive test coverage for various glob patterns. Feedback was provided to improve the robustness of the matching logic by prioritizing exact matches over wildcards, handling glob compilation errors gracefully, and employing more thorough URL normalization techniques like strings.TrimRight and strings.TrimSpace to handle multiple trailing slashes or whitespace.
| for _, repo := range repositories.Items { | ||
| repo.Spec.URL = strings.TrimSuffix(repo.Spec.URL, "/") | ||
| if repo.Spec.URL == event.URL { | ||
| match, err := matchRepo(event.URL, repo.Spec.URL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if match { | ||
| return &repo, nil | ||
| } | ||
| } |
There was a problem hiding this comment.
The current matching logic has several issues that should be addressed to ensure robustness and security:
- Priority: Exact matches should always take precedence over wildcard matches.
- Error Handling: Invalid glob patterns should be skipped instead of terminating the process.
- URL Normalization: When comparing repository URLs, use a robust normalization method like
strings.TrimRightandstrings.TrimSpaceto handle multiple trailing slashes or spaces, asstrings.TrimSuffixonly removes a single trailing character. - Loop Variable: Access the element by index to avoid issues with loop variables.
Suggested improvement using a two-pass approach to prioritize exact matches and handle errors gracefully:
for i := range repositories.Items {
repo := &repositories.Items[i]
if strings.TrimRight(strings.TrimSpace(repo.Spec.URL), "/") == event.URL {
return repo, nil
}
}
for i := range repositories.Items {
repo := &repositories.Items[i]
if match, err := matchRepo(event.URL, repo.Spec.URL); err == nil && match {
return repo, nil
}
}References
- When comparing repository URLs, use a robust normalization method like strings.TrimRight and strings.TrimSpace to handle multiple trailing slashes or spaces, as strings.TrimSuffix only removes a single trailing character.
📝 Description of the Change
This change introduce posibility to match repository over glob or regex pattern making able to setup a single repository cr to handle set of repositories
🔗 Linked GitHub Issue
Fixes #1246
🧪 Testing Strategy
🤖 AI Assistance
AI assistance can be used for various tasks, such as code generation,
documentation, or testing.
Please indicate whether you have used AI assistance
for this PR and provide details if applicable.
Important
Slop will be simply rejected, if you are using AI assistance you need to make sure you
understand the code generated and that it meets the project's standards. you
need at least know how to run the code and deploy it (if needed). See
startpaac to make it easy
to deploy and test your code changes.
If the majority of the code in this PR was generated by an AI, please add a
Co-authored-bytrailer to your commit message.For example:
Co-authored-by: Claude noreply@anthropic.com
✅ Submitter Checklist
fix:,feat:) matches the "Type of Change" I selected above.make testandmake lintlocally to check for and fix anyissues. For an efficient workflow, I have considered installing
pre-commit and running
pre-commit installtoautomate these checks.