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, a double underscore
// and the pool's name: <group>__<pool>, and its jobs are task type
// <group>__<pool>.job. A pipeline without the key stays on <group>.job.
//
// The double underscore is the one separator no repository name can
// hold (oltp.RepoName refuses it) and no pool name can hold, and the
// engine allows nothing but [A-Za-z0-9_-] in a group, so one
// repository's pool group can never be another repository's group or
// pool group.
var (
yamlPool = regexp.MustCompile(`(?m)^quarry_pool[ \t]*:[ \t]*([^#\n]*?)[ \t]*(?:#[^\n]*)?$`)
poolName = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
)
// PoolMax is the longest pool name.
const PoolMax = 32
// PoolSep joins a repository's group to a pool's name.
const PoolSep = "__"
// 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. A group that already holds the
// separator is refused, so it could not be read as another's pool.
func PoolGroup(group, pool string) (string, error) {
if pool == "" {
return group, nil
}
if !ValidPool(pool) {
return "", fmt.Errorf("pool %q is not a pool name", pool)
}
if strings.Contains(group, PoolSep) {
return "", fmt.Errorf("worker group %q holds %q, which is reserved for pools", group, PoolSep)
}
return group + PoolSep + pool, nil
}
// PoolTaskType is the task type a pool's jobs compile to.
func PoolTaskType(group, pool string) (string, error) {
g, err := PoolGroup(group, pool)
return g + ".job", err
}
// PoolOfTaskType reads a pool back from a task type of group: the pool
// of "<group>__<pool>.<anything>", and false for any other.
func PoolOfTaskType(group, taskType string) (string, bool) {
ns, _, _ := strings.Cut(taskType, ".")
return PoolOfGroup(group, ns)
}
// PoolOfGroup reads a pool back from a worker group of group.
func PoolOfGroup(group, g string) (string, bool) {
pool, ok := strings.CutPrefix(g, group+PoolSep)
if !ok || strings.Contains(group, PoolSep) || !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 and digits, joined by single dashes, at most %d characters", 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 namespaceOf(spec []byte, group string) (string, error) {
pool, err := poolOf(spec)
if err != nil {
return "", err
}
ns, err := PoolGroup(group, pool)
if err != nil {
return "", fmt.Errorf("quarry_pool: %v", err)
}
return ns, nil
}