Skip to content

Commit a4f2ff9

Browse files
committed
Updates after review: unification of isVersion fcns and error handling
Signed-off-by: Jakub Guzik <jguzik@redhat.com>
1 parent e6ba944 commit a4f2ff9

File tree

5 files changed

+41
-89
lines changed

5 files changed

+41
-89
lines changed

cmd/branchingconfigmanagers/frequency-reducer/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ func updateIntervalFieldsForMatchedSteps(
7979
if err != nil {
8080
return
8181
}
82+
83+
pastVersion, err := version.GetPastVersion()
84+
if err != nil {
85+
logrus.Warningf("Can't get past version for %s: %v", version.GetVersion(), err)
86+
pastVersion = ""
87+
}
88+
pastPastVersion, err := version.GetPastPastVersion()
89+
if err != nil {
90+
logrus.Debugf("Can't get past-past version for %s: %v", version.GetVersion(), err)
91+
pastPastVersion = ""
92+
}
93+
8294
if configuration.Info.Metadata.Org == "openshift" || configuration.Info.Metadata.Org == "openshift-priv" {
8395
for _, test := range configuration.Configuration.Tests {
8496
if !strings.Contains(test.As, "mirror-nightly-image") && !strings.Contains(test.As, "promote-") {
@@ -93,7 +105,7 @@ func updateIntervalFieldsForMatchedSteps(
93105
if !correctCron {
94106
*test.Cron = generateMonthlyCron()
95107
}
96-
} else if testVersion.GetVersion() == version.GetPastPastVersion() {
108+
} else if pastPastVersion != "" && testVersion.GetVersion() == pastPastVersion {
97109
correctCron, err := isExecutedAtMostXTimesAMonth(*test.Cron, 2)
98110
if err != nil {
99111
logrus.Warningf("Can't parse cron string %s", *test.Cron)
@@ -102,7 +114,7 @@ func updateIntervalFieldsForMatchedSteps(
102114
if !correctCron {
103115
*test.Cron = generateBiWeeklyCron()
104116
}
105-
} else if testVersion.GetVersion() == version.GetPastVersion() {
117+
} else if pastVersion != "" && testVersion.GetVersion() == pastVersion {
106118
correctCron, err := isExecutedAtMostXTimesAMonth(*test.Cron, 4)
107119
if err != nil {
108120
logrus.Warningf("Can't parse cron string %s", *test.Cron)
@@ -125,7 +137,7 @@ func updateIntervalFieldsForMatchedSteps(
125137
test.Cron = &cronExpr
126138
test.Interval = nil
127139
}
128-
} else if testVersion.GetVersion() == version.GetPastPastVersion() {
140+
} else if pastPastVersion != "" && testVersion.GetVersion() == pastPastVersion {
129141
duration, err := time.ParseDuration(*test.Interval)
130142
if err != nil {
131143
logrus.Warningf("Can't parse interval string %s", *test.Cron)
@@ -136,7 +148,7 @@ func updateIntervalFieldsForMatchedSteps(
136148
test.Cron = &cronExpr
137149
test.Interval = nil
138150
}
139-
} else if testVersion.GetVersion() == version.GetPastVersion() {
151+
} else if pastVersion != "" && testVersion.GetVersion() == pastVersion {
140152
duration, err := time.ParseDuration(*test.Interval)
141153
if err != nil {
142154
logrus.Warningf("Can't parse interval string %s", *test.Cron)

cmd/branchingconfigmanagers/tide-config-manager/main.go

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -491,63 +491,14 @@ func isVersionedBranch(branch string) bool {
491491
for _, prefix := range prefixes {
492492
if strings.HasPrefix(branch, prefix) {
493493
versionPart := strings.TrimPrefix(branch, prefix)
494-
if isValidVersion(versionPart) {
494+
if api.IsValidOCPVersion(versionPart) {
495495
return true
496496
}
497497
}
498498
}
499499
return false
500500
}
501501

502-
// isValidVersion checks if a string represents a valid version like "4.15", "5.0", etc.
503-
func isValidVersion(version string) bool {
504-
parts := strings.Split(version, ".")
505-
if len(parts) != 2 {
506-
return false
507-
}
508-
509-
// Validate major version (must be 4 or higher)
510-
major := parts[0]
511-
if major == "" {
512-
return false
513-
}
514-
for _, char := range major {
515-
if char < '0' || char > '9' {
516-
return false
517-
}
518-
}
519-
majorNum := 0
520-
for _, char := range major {
521-
majorNum = majorNum*10 + int(char-'0')
522-
}
523-
if majorNum < 4 {
524-
return false
525-
}
526-
527-
// Validate minor version
528-
minor := parts[1]
529-
if !isValidMinorVersion(minor) {
530-
return false
531-
}
532-
533-
return true
534-
}
535-
536-
// isValidMinorVersion checks if a string represents a valid minor version (e.g., "0", "9", "10", "15")
537-
func isValidMinorVersion(version string) bool {
538-
if version == "" {
539-
return false
540-
}
541-
542-
for _, char := range version {
543-
if char < '0' || char > '9' {
544-
return false
545-
}
546-
}
547-
548-
return true
549-
}
550-
551502
func (ve *verifiedEvent) GetDataFromProwConfig(*prowconfig.ProwConfig) {
552503
}
553504

@@ -631,9 +582,13 @@ func reconcile(currentOCPVersion, lifecyclePhase string, config *prowconfig.Prow
631582
)
632583
}
633584
if lifecyclePhase == GeneralAvailability {
585+
pastVersion, err := currentVersion.GetPastVersion()
586+
if err != nil {
587+
return fmt.Errorf("failed to get past version for %s: %w", currentVersion.GetVersion(), err)
588+
}
634589
_, err = shardprowconfig.ShardProwConfig(config, target,
635590
newGeneralAvailabilityEvent(
636-
currentVersion.GetPastVersion(),
591+
pastVersion,
637592
currentVersion.GetVersion(),
638593
currentVersion.GetFutureVersion(),
639594
repos),

cmd/cvp-trigger/main.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"path/filepath"
1010
"sort"
11-
"strconv"
1211
"strings"
1312
"time"
1413

@@ -154,8 +153,7 @@ func (o options) validateOptions() error {
154153
if o.ocpVersion == "" {
155154
return fmt.Errorf("required parameter %s was not provided", ocpVersionOption)
156155
}
157-
// Validate ocp-version format (must be X.Y where X >= 4)
158-
if !isValidOCPVersion(o.ocpVersion) {
156+
if !api.IsValidOCPVersion(o.ocpVersion) {
159157
return fmt.Errorf("ocp-version must be in format X.Y where X >= 4 (e.g., 4.15, 5.0)")
160158
}
161159

@@ -181,26 +179,6 @@ func (o options) validateOptions() error {
181179
return nil
182180
}
183181

184-
// isValidOCPVersion validates that the version is in format X.Y where X >= 4
185-
func isValidOCPVersion(version string) bool {
186-
parts := strings.Split(version, ".")
187-
if len(parts) != 2 {
188-
return false
189-
}
190-
191-
major, err := strconv.Atoi(parts[0])
192-
if err != nil {
193-
return false
194-
}
195-
196-
_, err = strconv.Atoi(parts[1])
197-
if err != nil {
198-
return false
199-
}
200-
201-
return major >= 4
202-
}
203-
204182
// getPeriodicJob returns a Prow Job or an error if the provided
205183
// periodic job name is not found
206184
func getPeriodicJob(jobName string, config *prowconfig.Config) (*pjapi.ProwJob, error) {

pkg/api/ocplifecycle/ocplifecycle.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,22 @@ func (m MajorMinor) Less(other MajorMinor) bool {
211211
}
212212

213213
// GetPastVersion returns the previous version using VersionTransitionOverrides.
214-
func (m MajorMinor) GetPastVersion() string {
214+
// Returns an error if no previous version can be determined (e.g., missing override for X.0).
215+
func (m MajorMinor) GetPastVersion() (string, error) {
215216
current := m.GetVersion()
216-
prev, err := api.GetPreviousVersionSimple(current)
217-
if err != nil {
218-
return ""
219-
}
220-
return prev
217+
return api.GetPreviousVersionSimple(current)
221218
}
222219

223220
// GetPastPastVersion returns the version before the previous version.
224-
func (m MajorMinor) GetPastPastVersion() string {
225-
pastVersion := m.GetPastVersion()
226-
if pastVersion == "" {
227-
return ""
221+
// Returns an error if any step in the chain fails.
222+
func (m MajorMinor) GetPastPastVersion() (string, error) {
223+
pastVersion, err := m.GetPastVersion()
224+
if err != nil {
225+
return "", fmt.Errorf("failed to get past version: %w", err)
228226
}
229227
pastMM, err := ParseMajorMinor(pastVersion)
230228
if err != nil {
231-
return ""
229+
return "", fmt.Errorf("failed to parse past version %s: %w", pastVersion, err)
232230
}
233231
return pastMM.GetPastVersion()
234232
}

pkg/api/version.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func ParseVersion(version string) (ParsedVersion, error) {
4242
return ParsedVersion{Major: major, Minor: minor}, nil
4343
}
4444

45+
// IsValidOCPVersion validates that the version is in format X.Y where X >= 4.
46+
func IsValidOCPVersion(version string) bool {
47+
parsed, err := ParseVersion(version)
48+
if err != nil {
49+
return false
50+
}
51+
return parsed.Major >= 4
52+
}
53+
4554
// GetPreviousVersion returns the primary previous version. For X.0 without override,
4655
// it finds the highest (X-1).* from availableVersions.
4756
func GetPreviousVersion(current string, availableVersions []string) (string, error) {

0 commit comments

Comments
 (0)