mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-04 14:35:32 +00:00
Removes dead code and redundant logic and consolidates tests, so there is less to read and maintain. No behavior change. 1. **act/runner**: inline single-use wrappers, compile regexes once instead of per call, embed the trampoline as a string, drop the unused legacy result of `parseActionCommand` 2. **act/container**: `sanitizeConfig` and `mergeContainerConfigs` stop returning the configs they already mutate in place, drop never-set option fields and the unused `user` parameter of `ExecWithCmdLine` 3. **act/common and cache server**: drop the unused `Trace`/`Tracef` loggers and the short SHA of `FindGitRevision`, `RegisterJob` releases through `RevokeJob` 4. **daemon, poller and reporter**: one shutdown wait for both poll modes, drop an unreachable fetch timeout branch, guard the reporter's last report time with its existing lock instead of an atomic 5. **build**: drop unused Makefile and release workflow variables, collapse the goreleaser ignore list to the same build targets 6. **tests**: fold near-duplicate tests into tables with shared setup Throughout, hand-rolled logic gives way to `cmp.Or`, `strings.Cut`, `slices` and `min`/`max`. Reviewed-on: https://gitea.com/gitea/runner/pulls/1335 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package labels
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
SchemeHost = "host"
|
|
SchemeDocker = "docker"
|
|
SchemeKubernetes = "kubernetes"
|
|
|
|
// SelfHostedPlatform is the platform marker act treats as "run on the host".
|
|
SelfHostedPlatform = "-self-hosted"
|
|
)
|
|
|
|
type Label struct {
|
|
Name string
|
|
Schema string
|
|
Arg string
|
|
// Opaque marks a label whose name contains a colon but no supported schema,
|
|
// like "pool:e57e18d4-...". It is kept verbatim and behaves like a host label.
|
|
Opaque bool
|
|
}
|
|
|
|
func Parse(str string) (*Label, error) {
|
|
if str == "" {
|
|
return nil, errors.New("empty label")
|
|
}
|
|
|
|
name, remainder, hasSchema := strings.Cut(str, ":")
|
|
label := &Label{Name: name, Schema: SchemeHost}
|
|
if hasSchema {
|
|
label.Schema, label.Arg, _ = strings.Cut(remainder, ":")
|
|
}
|
|
kubernetes := label.Schema == SchemeKubernetes && strings.HasPrefix(label.Arg, "//") && len(label.Arg) > 2
|
|
if label.Schema != SchemeHost && label.Schema != SchemeDocker && !kubernetes {
|
|
// Not a schema we know, or kubernetes without an image: the colon belongs to the label name itself.
|
|
return &Label{
|
|
Name: str,
|
|
Schema: SchemeHost,
|
|
Opaque: true,
|
|
}, nil
|
|
}
|
|
return label, nil
|
|
}
|
|
|
|
type Labels []*Label
|
|
|
|
func (l Labels) RequireDocker() bool {
|
|
return slices.ContainsFunc(l, func(label *Label) bool { return label.Schema == SchemeDocker })
|
|
}
|
|
|
|
// PickPlatform returns the platform of the first runs-on entry this runner has a label for, or "".
|
|
func (l Labels) PickPlatform(runsOn []string) string {
|
|
platforms := make(map[string]string, len(l))
|
|
for _, label := range l {
|
|
switch label.Schema {
|
|
case SchemeDocker:
|
|
// "//" will be ignored
|
|
platforms[label.Name] = strings.TrimPrefix(label.Arg, "//")
|
|
case SchemeKubernetes:
|
|
platforms[label.Name] = SchemeKubernetes + ":" + label.Arg
|
|
case SchemeHost:
|
|
platforms[label.Name] = SelfHostedPlatform
|
|
default:
|
|
continue
|
|
}
|
|
}
|
|
for _, v := range runsOn {
|
|
if v, ok := platforms[v]; ok {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (l Labels) Names() []string {
|
|
names := make([]string, 0, len(l))
|
|
for _, label := range l {
|
|
names = append(names, label.Name)
|
|
}
|
|
return names
|
|
}
|
|
|
|
func (l Labels) ToStrings() []string {
|
|
ls := make([]string, 0, len(l))
|
|
for _, label := range l {
|
|
lbl := label.Name
|
|
if !label.Opaque && label.Schema != "" {
|
|
lbl += ":" + label.Schema
|
|
if label.Arg != "" {
|
|
lbl += ":" + label.Arg
|
|
}
|
|
}
|
|
ls = append(ls, lbl)
|
|
}
|
|
return ls
|
|
}
|