Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pkg/load/agents/configAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/load"
"github.com/openshift/ci-tools/pkg/prowgen"
)

type IndexDelta struct {
Expand Down Expand Up @@ -134,13 +135,17 @@ func (a *configAgent) GetMatchingConfig(metadata api.Metadata) (api.ReleaseBuild
}
var matchingConfigs []api.ReleaseBuildConfiguration
for _, config := range repoConfigs {
r, err := regexp.Compile(config.Metadata.Branch)
if err != nil {
return api.ReleaseBuildConfiguration{}, fmt.Errorf("could not compile regex for %s/%s@%s: %w", metadata.Org, metadata.Repo, config.Metadata.Branch, err)
}
if r.MatchString(metadata.Branch) && config.Metadata.Variant == metadata.Variant {
matchingConfigs = append(matchingConfigs, config)
for _, f := range []func(string) string{prowgen.ExactlyBranch, prowgen.FeatureBranch} {
r, err := regexp.Compile(f(config.Metadata.Branch))
if err != nil {
return api.ReleaseBuildConfiguration{}, fmt.Errorf("could not compile regex for %s/%s@%s: %w", metadata.Org, metadata.Repo, config.Metadata.Branch, err)
}
if r.MatchString(metadata.Branch) && config.Metadata.Variant == metadata.Variant {
matchingConfigs = append(matchingConfigs, config)
break
}
}

}
switch len(matchingConfigs) {
case 0:
Expand Down
54 changes: 41 additions & 13 deletions pkg/load/agents/configAgent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,32 +330,60 @@ func TestConfigAgent_GetMatchingConfig(t *testing.T) {
}}, {Metadata: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "branch-fo",
Branch: "branch-foo",
}}},
},
},
meta: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "branch-foo",
Branch: "branch-foo-bar",
},
expectedErr: true,
},
{
name: "no error on simple substring",
input: load.ByOrgRepo{
"org": map[string][]api.ReleaseBuildConfiguration{
"repo": {{Metadata: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "release-4.1",
}}, {Metadata: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "release-4.10",
}}},
},
},
meta: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "release-4.10",
},
expected: api.ReleaseBuildConfiguration{Metadata: api.Metadata{
Org: "org",
Repo: "repo",
Branch: "release-4.10",
}},
},
}

for _, testCase := range testCases {
agent := &configAgent{lock: &sync.RWMutex{}, configs: testCase.input}
actual, actualErr := agent.GetMatchingConfig(testCase.meta)
if testCase.expectedErr && actualErr == nil {
t.Errorf("%s: expected an error but got none", testCase.name)
}
if !testCase.expectedErr && actualErr != nil {
t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
}
t.Run(testCase.name, func(t *testing.T) {
agent := &configAgent{lock: &sync.RWMutex{}, configs: testCase.input}
actual, actualErr := agent.GetMatchingConfig(testCase.meta)
if testCase.expectedErr && actualErr == nil {
t.Errorf("%s: expected an error but got none", testCase.name)
}
if !testCase.expectedErr && actualErr != nil {
t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
}

if diff := cmp.Diff(actual, testCase.expected); diff != "" {
t.Errorf("%s: got incorrect config: %v", testCase.name, diff)
}
if diff := cmp.Diff(actual, testCase.expected); !testCase.expectedErr && diff != "" {
t.Errorf("%s: got incorrect config: %v", testCase.name, diff)
}
})
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/prowgen/prowgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func generatePresubmitForTest(name string, info *ProwgenInfo, podSpec *corev1.Po
return &prowconfig.Presubmit{
JobBase: base,
AlwaysRun: true,
Brancher: prowconfig.Brancher{Branches: sets.NewString(exactlyBranch(info.Branch), featureBranch(info.Branch)).List()},
Brancher: prowconfig.Brancher{Branches: sets.NewString(ExactlyBranch(info.Branch), FeatureBranch(info.Branch)).List()},
Reporter: prowconfig.Reporter{
Context: fmt.Sprintf("ci/prow/%s", shortName),
},
Expand All @@ -462,7 +462,7 @@ func generatePostsubmitForTest(name string, info *ProwgenInfo, podSpec *corev1.P
base := generateJobBase(name, jc.PostsubmitPrefix, info, podSpec, false, pathAlias, jobRelease, skipCloning)
return &prowconfig.Postsubmit{
JobBase: base,
Brancher: prowconfig.Brancher{Branches: []string{exactlyBranch(info.Branch)}},
Brancher: prowconfig.Brancher{Branches: []string{ExactlyBranch(info.Branch)}},
}
}

Expand Down Expand Up @@ -618,19 +618,19 @@ func generateJobBase(name, prefix string, info *ProwgenInfo, podSpec *corev1.Pod
return base
}

// exactlyBranch returns a regex string that matches exactly the given branch name: I.e. returns
// ExactlyBranch returns a regex string that matches exactly the given branch name: I.e. returns
// '^master$' for 'master'. If the given branch name already looks like a regex, return it unchanged.
func exactlyBranch(branch string) string {
func ExactlyBranch(branch string) string {
if !jc.SimpleBranchRegexp.MatchString(branch) {
return branch
}
return fmt.Sprintf("^%s$", regexp.QuoteMeta(branch))
}

// featureBranch returns a regex string that matches feature branch prefixes for the given branch name:
// FeatureBranch returns a regex string that matches feature branch prefixes for the given branch name:
// I.e. returns '^master-' for 'master'. If the given branch name already looks like a regex,
// return it unchanged.
func featureBranch(branch string) string {
func FeatureBranch(branch string) string {
if !jc.SimpleBranchRegexp.MatchString(branch) {
return branch
}
Expand Down