Skip to content
quarry Artifact [040e39ec1d]
Login

Artifact [040e39ec1d]

Artifact 040e39ec1d06fc65898196746ba0d314827f264c0e5e8bd1a719683725bd41a9:


package api

import (
	"strings"
	"sync"
	"testing"
)

// mergeChecks requires "ci" on every branch.
type mergeChecks struct{}

func (mergeChecks) RequiredChecks(string, string) []string { return []string{"ci"} }

// mergeRuns is pairRuns that also answers the newest pair per tip.
type mergeRuns struct {
	*pairRuns
	mu    sync.Mutex
	order []string
}

func (m *mergeRuns) green(branchTip, targetTip string) {
	m.pairRuns.green(branchTip, targetTip)
	m.mu.Lock()
	m.order = append(m.order, "pair:"+branchTip+":"+targetTip)
	m.mu.Unlock()
}

func (m *mergeRuns) LatestPairFor(_, tip string) (string, error) {
	m.mu.Lock()
	defer m.mu.Unlock()
	for i := len(m.order) - 1; i >= 0; i-- {
		if strings.HasPrefix(m.order[i], "pair:"+tip+":") {
			return m.order[i], nil
		}
	}
	return "", nil
}

// With no merge tested yet the accept waits, as a pull request waits
// for its required checks (a moved target: stalemerge_test.go).
func TestAnAcceptWaitsForTheFirstMergeVerdict(t *testing.T) {
	_, ids, ix, pipeSync := premergeStore(t)
	d := Deps{Version: "v", Checks: mergeChecks{}, CI: &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}},
		Repos: NewRepos(map[string]*Repo{"test": {Index: ix, Sync: pipeSync}})}
	_, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[0]}, "accept")
	if err == nil || !strings.Contains(err.Error(), "required checks not green") {
		t.Fatalf("FAIL: an accept with no merge tested answered %v, want the required-checks refusal", err)
	}
	t.Log("ok: no merge verdict, no accept")
}