Skip to content
quarry Artifact [d9e2c0488e]
Login

Artifact [d9e2c0488e]

Artifact d9e2c0488e63e085cfd46853d749fbd8cb65088b55e80ea16518a59543e3dcba:


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
	name := fmt.Sprintf("!%d", l.Number)
	if l.Number == 0 {
		name = l.Branch
	}
	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, name+" is "+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, name+" 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 := name + " waits for the landing below it in its stack."
		if below > 0 {
			say = fmt.Sprintf("%s waits for !%d below it in its stack.", name, 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, name+": "+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, name+": "+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, name+" was 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, name+": "+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: name + ": " + 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, name+" is armed by "+l.AutoBy+"; it lands itself when its checks are green.", nextstep.Next{Say: "Nothing to do."})
	}
	return w(waitNothing, name+" is 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
}