Skip to content
quarry Artifact [fcf7c04e31]
Login

Artifact [fcf7c04e31]

Artifact fcf7c04e311ec3fa7a304633fa55230e4d4ce2430b1882817f3948c04baad30b:


package ci

import (
	"fmt"
	"regexp"
	"strings"
)

// quarry_pool: NAME at the top of a ci.yml names the runner pool its
// jobs go to. The engine's worker group is the pipeline's task
// namespace, so a pool is the repository's group with the pool's name
// after a dash: <group>-<pool>, and its jobs are task type
// <group>-<pool>.job. A pipeline without the key stays on <group>.job.
var (
	yamlPool = regexp.MustCompile(`(?m)^quarry_pool[ \t]*:[ \t]*([^#\n]*?)[ \t]*(?:#[^\n]*)?$`)
	poolName = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`)
)

// PoolMax is the longest pool name.
const PoolMax = 32

// ValidPool reports whether name may name a pool.
func ValidPool(name string) bool {
	return len(name) <= PoolMax && poolName.MatchString(name)
}

// PoolGroup is the worker group a pool's jobs go to; the default pool,
// "", is the repository's own group.
func PoolGroup(group, pool string) string {
	if pool == "" {
		return group
	}
	return group + "-" + pool
}

// PoolTaskType is the task type a pool's jobs compile to.
func PoolTaskType(group, pool string) string {
	return PoolGroup(group, pool) + ".job"
}

// PoolOfTaskType reads a pool back from a job task type of group: the
// pool of "<group>-<pool>.job", and false for anything else.
func PoolOfTaskType(group, taskType string) (string, bool) {
	g, ok := strings.CutSuffix(taskType, ".job")
	if !ok {
		return "", false
	}
	pool, ok := strings.CutPrefix(g, group+"-")
	if !ok || !ValidPool(pool) {
		return "", false
	}
	return pool, true
}

// poolOf reads a ci.yml's quarry_pool: "" when it has none.
func poolOf(spec []byte) (string, error) {
	m := yamlPool.FindSubmatch(spec)
	if m == nil {
		return "", nil
	}
	v := strings.Trim(string(m[1]), `"'`)
	if !ValidPool(v) {
		return "", fmt.Errorf("quarry_pool is %q; a pool is named in lowercase letters, digits and -, at most %d characters, starting and ending with a letter or digit", v, PoolMax)
	}
	return v, nil
}

// withoutPool blanks the quarry_pool: line, keeping the line count so
// the compiler's line numbers still point at the author's lines.
func withoutPool(spec []byte) []byte {
	return yamlPool.ReplaceAll(spec, nil)
}

// namespaceOf is the task namespace a pipeline compiles under: the
// repository's group, or the group's pool.
func (e *Engine) namespaceOf(spec []byte, group string) (string, error) {
	pool, err := poolOf(spec)
	if err != nil {
		return "", err
	}
	ns := PoolGroup(group, pool)
	if pool != "" && e.groupTaken != nil && e.groupTaken(ns) {
		// A repository's group is its name, so the pool would put this
		// repository's jobs on that repository's runners.
		return "", fmt.Errorf("quarry_pool is %q; %s is also the worker group of a repository of that name, so its runners would take these jobs: name another pool", pool, ns)
	}
	return ns, nil
}

// SetGroupTaken tells the engine which worker groups belong to a
// repository, so a pool cannot be named into one.
func (e *Engine) SetGroupTaken(f func(group string) bool) { e.groupTaken = f }