fix(schedule): validate variables before creating a schedule
What does this MR do and why?
glab schedule create creates a real pipeline schedule before validating
--variable values. An invalid value therefore makes the command return an
error after the schedule already exists, leaving an orphan schedule without
the requested variables.
The current code makes the side effect first:
schedule, _, err := client.PipelineSchedules.CreatePipelineSchedule(repo.FullName(), l)
// ...
for _, v := range variableList {
split := strings.SplitN(v, ":", 2)
if len(split) != 2 {
return fmt.Errorf("invalid format for --variable: %s", v)
}
}This MR parses every variable into CreatePipelineScheduleVariableOptions
before the first API call. If any value is invalid, no schedule or variable is
created. Only after the whole list validates does it create the schedule and
attach the parsed variables.
The touched client calls now also receive gitlab.WithContext(cmd.Context()),
matching the repository's API client convention and allowing cancellation to
propagate.
Test coverage
The existing invalid-variable case no longer expects
CreatePipelineSchedule. A second case verifies that a valid first variable
does not hide an invalid later value:
--variable foo:bar --variable invalidOn unmodified origin/main, both tests fail on the unexpected state-changing
API call:
Unexpected call to CreatePipelineSchedule(...) because:
there are no expected calls of the method "CreatePipelineSchedule"With the fix:
go test ./internal/commands/schedule/create -count=1
ok gitlab.com/gitlab-org/cli/internal/commands/schedule/creategit diff --check also passes.
Related issues
Closes #8418 (closed)