| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "regexp" |
| 7 | |
| 8 | "the-valley/flow/internal/run" |
| 9 | "the-valley/flow/internal/valley" |
| 10 | ) |
| 11 | |
| 12 | // exitNoWorker is launch's exit status, with --run-id, for a launch that |
| 13 | // failed before starting its worker. |
| 14 | const exitNoWorker = 3 |
| 15 | |
| 16 | // runIDPattern is the form run.GenID gives run IDs. |
| 17 | var runIDPattern = regexp.MustCompile(`^[0-9]{8}-[0-9]{4}-[0-9a-f]{8}$`) |
| 18 | |
| 19 | // validRunID reports whether id has the form of a run ID launch mints. |
| 20 | func validRunID(id string) bool { return runIDPattern.MatchString(id) } |
| 21 | |
| 22 | // changeExpectation is what a launch's --expect-* flags require of the |
| 23 | // change --change names, as the valley lists it: a dispatcher that checked |
| 24 | // the change before launching passes what it checked, and launch starts no |
| 25 | // worker unless the change still stands so. A zero field is not checked. |
| 26 | type changeExpectation struct { |
| 27 | Owner string // the change record's owner |
| 28 | Branch string // the active version's branch, and the launch's |
| 29 | Head string // the active version's head, and origin's tip of the branch |
| 30 | Draft bool // the change is a draft |
| 31 | } |
| 32 | |
| 33 | // set reports whether any expectation is set. |
| 34 | func (x changeExpectation) set() bool { |
| 35 | return x.Owner != "" || x.Branch != "" || x.Head != "" || x.Draft |
| 36 | } |
| 37 | |
| 38 | // validate rejects expectations launch can't check: they name the change by |
| 39 | // its full ID, with --change, and a head by its full SHA. |
| 40 | func (x changeExpectation) validate(changeRef string) error { |
| 41 | switch { |
| 42 | case !x.set(): |
| 43 | return nil |
| 44 | case !run.ValidChangeID(changeRef): |
| 45 | return errors.New("--expect-owner, --expect-branch, --expect-head and --expect-draft check the change --change names by its full change ID") |
| 46 | case x.Head != "" && !valley.IsOID(x.Head): |
| 47 | return fmt.Errorf("--expect-head %q isn't a full commit SHA", x.Head) |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // check reads the change t through list, the valley's listing of the |
| 53 | // target repo, and returns an error unless the change stands as expected: |
| 54 | // listed, by the ID launch resolved; owned, a draft, and with its active |
| 55 | // version on the branch and at the head expected; and with launch working |
| 56 | // on that branch, whose tip on origin, tip as the caller just read it, is |
| 57 | // that head. A valley that can't be asked fails the check. |
| 58 | func (x changeExpectation) check(t *changeTarget, tip string, list func() (*valley.Changes, error)) error { |
| 59 | if t == nil { |
| 60 | return errors.New("expectations need a change to check") |
| 61 | } |
| 62 | l, err := list() |
| 63 | if err != nil { |
| 64 | return fmt.Errorf("the valley couldn't be asked to confirm change %s: %w", t.Name, err) |
| 65 | } |
| 66 | _, ch, v, ok := activeVersionOf(l, t.ID) |
| 67 | switch { |
| 68 | case !ok: |
| 69 | return fmt.Errorf("the valley lists no active version of change %s", t.Name) |
| 70 | case x.Owner != "" && ch.Owner != x.Owner: |
| 71 | return fmt.Errorf("change %s's owner is %q, not %q", t.Name, ch.Owner, x.Owner) |
| 72 | case x.Draft && ch.State != valley.StateDraft: |
| 73 | return fmt.Errorf("change %s is %s, not a draft", t.Name, ch.State) |
| 74 | case x.Branch != "" && (v.Branch != x.Branch || t.Branch != x.Branch): |
| 75 | return fmt.Errorf("change %s's branch is %s, not %s", t.Name, v.Branch, x.Branch) |
| 76 | case x.Head != "" && v.Head != x.Head: |
| 77 | return fmt.Errorf("change %s's head is %s, not %s", t.Name, shortSHA(v.Head), shortSHA(x.Head)) |
| 78 | case x.Head != "" && tip != x.Head: |
| 79 | return fmt.Errorf("origin's %s is at %s, not change %s's expected head %s", t.Branch, shortSHA(tip), t.Name, shortSHA(x.Head)) |
| 80 | } |
| 81 | return nil |
| 82 | } |