Skip to content

Commit ccab29f

Browse files
authored
Merge pull request #4975 from vitorfloriano/alpha-update-validation
🌱 (alpha update) add validation for equal versions
2 parents c4b1c5c + 0c6ce99 commit ccab29f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/cli/alpha/internal/update/validate.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package update
1919
import (
2020
"fmt"
2121
"net/http"
22+
"os"
2223
"os/exec"
2324
"strings"
2425

@@ -28,6 +29,9 @@ import (
2829

2930
// Validate checks the input info provided for the update and populates the cliVersion
3031
func (opts *Update) Validate() error {
32+
if err := opts.validateEqualVersions(); err != nil {
33+
return fmt.Errorf("failed to validate equal versions: %w", err)
34+
}
3135
if err := opts.validateGitRepo(); err != nil {
3236
return fmt.Errorf("failed to validate git repository: %w", err)
3337
}
@@ -117,3 +121,23 @@ func validateReleaseAvailability(version string) error {
117121
resp.StatusCode, version)
118122
}
119123
}
124+
125+
// validateEqualVersions checks if from-version and to-version are the same.
126+
// If they are equal, logs an appropriate message and exits successfully.
127+
func (opts *Update) validateEqualVersions() error {
128+
if opts.FromVersion == opts.ToVersion {
129+
// Check if this is the latest version to provide appropriate message
130+
latestVersion, err := fetchLatestRelease()
131+
if err != nil {
132+
return fmt.Errorf("failed to fetch latest release for messaging: %w", err)
133+
}
134+
135+
if opts.ToVersion == latestVersion {
136+
log.Infof("Your project already uses the latest version (%s). No action taken.", opts.FromVersion)
137+
} else {
138+
log.Infof("Your project already uses the specified version (%s). No action taken.", opts.FromVersion)
139+
}
140+
os.Exit(0)
141+
}
142+
return nil
143+
}

test/e2e/alphaupdate/update_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ var _ = Describe("kubebuilder", func() {
161161
By("validating merge stopped in conflict state for manual resolution")
162162
validateConflictState(mockProjectDir)
163163
})
164+
165+
It("should succeed with no action when from-version and to-version are the same", func() {
166+
By("creating mock project with kubebuilder v4.5.2")
167+
createMockProject(mockProjectDir, pathBinFromVersion)
168+
169+
By("initializing git repository and committing mock project")
170+
initializeGitRepo(mockProjectDir)
171+
172+
By("running alpha update with same versions (v4.5.2 to v4.5.2)")
173+
cmd := exec.Command(kbc.BinaryName, "alpha", "update",
174+
"--from-version", "v4.5.2", "--to-version", "v4.5.2", "--from-branch", "main")
175+
cmd.Dir = mockProjectDir
176+
output, err := cmd.CombinedOutput()
177+
178+
Expect(err).NotTo(HaveOccurred(), "Expected command to succeed when from-version equals to-version")
179+
Expect(string(output)).To(ContainSubstring("already uses the specified version"))
180+
Expect(string(output)).To(ContainSubstring("No action taken"))
181+
})
164182
})
165183
})
166184

0 commit comments

Comments
 (0)