package api
import (
"fmt"
"strings"
"craftdesign.group/quarry/internal/nextstep"
)
// The values of landingWaiting.On (RFC 0031 "What a landing is waiting
// on"). Approval has no data behind it yet and is not produced.
const (
waitChecks = "checks"
waitConflict = "conflict"
waitStale = "stale"
waitNeverRan = "never_ran"
waitStack = "stack"
waitProtection = "protection"
waitNothing = "nothing"
)
// landingWaiting is the one thing a landing waits on, composed here from
// the fields landing.get already carries, so a client prints the
// server's words and holds no vocabulary of its own.
type landingWaiting struct {
On string `json:"on"`
// Say is one sentence, naming the landing.
Say string `json:"say"`
// Who names the people the wait is on, when it is on people.
Who string `json:"who"`
Next *nextstep.Next `json:"next"`
}
func landingWaitingOf(o landingGetOut) landingWaiting {
l := o.Landing
// Say never opens with the landing's own number: every consumer
// already shows it beside the sentence.
w := func(on, say string, next nextstep.Next) landingWaiting {
if next.Say == "" {
next.Say = say
}
return landingWaiting{On: on, Say: say, Next: &next}
}
first := func(names []string) string {
if len(names) == 0 {
return "a required check"
}
return "check " + fmt.Sprintf("%q", names[0])
}
switch {
case l.State != "open":
return w(waitNothing, "Already "+l.State+".", nextstep.Next{Say: "Nothing to do."})
case o.Mergeable == "conflict":
where := ""
if len(o.Conflicts) > 0 {
where = " in " + strings.Join(o.Conflicts, ", ")
}
return w(waitConflict, "Conflicts with "+l.Target+where+".",
nextstep.Next{Say: "Merge " + l.Target + " into the branch, resolve, commit and push.", Cmd: "zit merge " + l.Target})
case o.Mergeable == "stacked":
below := 0
for _, s := range o.Stack.Segments {
if s.State == "open" && s.Position < positionOf(o) && s.Number > 0 {
below = s.Number
}
}
say := "Waits for the landing below it in its stack."
if below > 0 {
say = fmt.Sprintf("Waits for !%d below it in its stack.", below)
}
return w(waitStack, say, nextstep.Next{Say: "Land the rung below first, or land the stack whole."})
case l.CI == "ci-fail":
return w(waitChecks, upper(first(o.Unmet))+" failed.",
nextstep.Next{Say: "Read the failed step, fix it and push; it is tested again.", Cmd: fmt.Sprintf("quarry status !%d --log", l.Number)})
case o.Untested && len(o.NeverRan) > 0:
return w(waitNeverRan, upper(first(o.NeverRan))+" has never run for this merge.",
nextstep.Next{Say: "Start the check, or push to start it."})
case o.Stale:
return w(waitStale, "Tested against "+l.Target+" before it moved.",
nextstep.Next{Say: "Update the branch from " + l.Target + " and push; it is tested again."})
case l.CI == "ci-waiting" || len(o.Unmet) > 0:
return w(waitChecks, upper(first(o.Unmet))+" is running.", nextstep.Next{Say: "Wait for it; nothing to do."})
case o.CanAccept && !o.MayLand && o.MayLandReason != "":
return landingWaiting{On: waitProtection, Say: upper(o.MayLandReason) + ".",
Who: strings.Join(o.Owners, ", "),
Next: &nextstep.Next{Say: "Ask an owner of " + l.Target + " to accept it."}}
case l.AutoBy != "":
return w(waitNothing, "Armed by "+l.AutoBy+"; it lands itself when its checks are green.", nextstep.Next{Say: "Nothing to do."})
}
return w(waitNothing, "Ready to land.", nextstep.Next{Say: "Arm it, so it lands when its checks are green.",
Cmd: fmt.Sprintf("quarry api POST /r/{repo}/landing/%d/auto -f on:=true", l.Number)})
}
func positionOf(o landingGetOut) int {
for _, s := range o.Stack.Segments {
if s.ID == o.Landing.ID && s.ID != "" {
return s.Position
}
}
return o.Landing.Position
}
func upper(s string) string {
if s == "" {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}