-
Notifications
You must be signed in to change notification settings - Fork 290
[WIP] - Add code to execute pre-submits by tag #4611
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
Open
kasturinarra
wants to merge
1
commit into
openshift:main
Choose a base branch
from
kasturinarra:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package rehearse | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
|
|
||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| // RehearsalTagConfig is the top-level structure for the tag configuration file. | ||
| type RehearsalTagConfig struct { | ||
| Tags []Tag `json:"tags"` | ||
| } | ||
|
|
||
| // Tag defines a single rehearsal tag and its selectors. | ||
| type Tag struct { | ||
| Name string `json:"name"` | ||
| Selectors []Selector `json:"selectors"` | ||
| } | ||
|
|
||
| // Selector defines the criteria for a job to be included in a tag. | ||
| // A job must match at least one selector in a tag's list. | ||
| type Selector struct { | ||
| JobNamePattern string `json:"job_name_pattern,omitempty"` | ||
| FilePathPattern string `json:"file_path_pattern,omitempty"` | ||
| ClusterProfile string `json:"cluster_profile,omitempty"` | ||
| JobName string `json:"job_name,omitempty"` | ||
| } | ||
|
|
||
| // LoadRehearsalTagConfig loads a rehearsal tag config from a file. | ||
| func LoadRehearsalTagConfig(path string) (*RehearsalTagConfig, error) { | ||
| data, err := ioutil.ReadFile(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not read file %s: %w", path, err) | ||
| } | ||
| var config RehearsalTagConfig | ||
| if err := yaml.Unmarshal(data, &config); err != nil { | ||
| return nil, fmt.Errorf("could not unmarshal config: %w", err) | ||
| } | ||
| return &config, nil | ||
| } | ||
|
|
||
| // HasTag returns true if the given tag name exists in the configuration. | ||
| func (c *RehearsalTagConfig) HasTag(tagName string) bool { | ||
| for _, tag := range c.Tags { | ||
| if tag.Name == tagName { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package rehearse | ||
|
|
||
| import ( | ||
| "regexp" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "sigs.k8s.io/prow/pkg/config" | ||
|
|
||
| ci_config "github.com/openshift/ci-tools/pkg/config" | ||
| ) | ||
|
|
||
| // FilterPresubmitsByTag filters the given presubmits based on the selectors for the requested tag. | ||
| func FilterPresubmitsByTag(presubmits ci_config.Presubmits, periodics []config.Periodic, tagConfig *RehearsalTagConfig, requestedTag string) ci_config.Presubmits { | ||
| filtered := make(ci_config.Presubmits) | ||
| var targetTag *Tag | ||
| for i := range tagConfig.Tags { | ||
| if tagConfig.Tags[i].Name == requestedTag { | ||
| targetTag = &tagConfig.Tags[i] | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if targetTag == nil { | ||
| return filtered | ||
| } | ||
|
|
||
| for repo, jobs := range presubmits { | ||
| for _, job := range jobs { | ||
| if jobMatchesTag(job.JobBase, targetTag, repo) { | ||
| if _, ok := filtered[repo]; !ok { | ||
| filtered[repo] = []config.Presubmit{} | ||
| } | ||
| filtered[repo] = append(filtered[repo], job) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, periodic := range periodics { | ||
| if jobMatchesTag(periodic.JobBase, targetTag, "") { | ||
| logrus.WithField("job", periodic.Name).Warn("Periodic jobs cannot be rehearsed by tag, skipping.") | ||
| } | ||
| } | ||
|
|
||
| return filtered | ||
| } | ||
|
|
||
| func jobMatchesTag(job config.JobBase, tag *Tag, repo string) bool { | ||
| for _, selector := range tag.Selectors { | ||
| // Check job name pattern | ||
| if selector.JobNamePattern != "" { | ||
| re, err := regexp.Compile(selector.JobNamePattern) | ||
| if err != nil { | ||
| logrus.WithError(err).Warnf("Invalid regex in rehearsal tag selector: %s", selector.JobNamePattern) | ||
| continue | ||
| } | ||
| if re.MatchString(job.Name) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| // Check exact job name match | ||
| if selector.JobName != "" { | ||
| if job.Name == selector.JobName { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| // Check cluster profile (stored in job labels) | ||
| if selector.ClusterProfile != "" { | ||
| if clusterProfile, ok := job.Labels["ci-operator.openshift.io/cloud-cluster-profile"]; ok { | ||
| if clusterProfile == selector.ClusterProfile { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check file path pattern (matches against the repository path) | ||
| if selector.FilePathPattern != "" && repo != "" { | ||
| re, err := regexp.Compile(selector.FilePathPattern) | ||
| if err != nil { | ||
| logrus.WithError(err).Warnf("Invalid regex in rehearsal tag selector: %s", selector.FilePathPattern) | ||
| continue | ||
| } | ||
| // The repo string is in format "org/repo-name", we can match against this | ||
| if re.MatchString(repo) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // RehearsalTagConfig contains the mapping of tags to jobs |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
math.MaxIntconcerns us