1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

fix: automatically trim label lists around the commas to remove whitespace

This commit is contained in:
Mathieu Fenniak 2025-08-08 20:52:51 -06:00
parent e331b4380f
commit 1b08fe340e
2 changed files with 54 additions and 2 deletions

View file

@ -172,7 +172,7 @@ func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *co
case StageInputLabels: case StageInputLabels:
r.Labels = defaultLabels r.Labels = defaultLabels
if value != "" { if value != "" {
r.Labels = strings.Split(value, ",") r.Labels = commaSplit(value)
} }
if validateLabels(r.Labels) != nil { if validateLabels(r.Labels) != nil {
@ -260,7 +260,7 @@ func registerNoInteractive(ctx context.Context, configFile string, regArgs *regi
regArgs.Labels = strings.TrimSpace(regArgs.Labels) regArgs.Labels = strings.TrimSpace(regArgs.Labels)
// command line flag. // command line flag.
if regArgs.Labels != "" { if regArgs.Labels != "" {
inputs.Labels = strings.Split(regArgs.Labels, ",") inputs.Labels = commaSplit(regArgs.Labels)
} }
// specify labels in config file. // specify labels in config file.
if len(cfg.Runner.Labels) > 0 { if len(cfg.Runner.Labels) > 0 {
@ -353,3 +353,24 @@ func doRegister(ctx context.Context, cfg *config.Config, inputs *registerInputs)
} }
return nil return nil
} }
// Splits a string by commas, but trims whitespace around the commas. Used for label parsing to avoid a rogue whitespace
// like "docker:docker://node:22-bookworm, self-hosted:host, lxc:lxc://debian:bookworm" becoming labeled with
// " self-hosted" and " lxc".
func commaSplit(s string) []string {
if s == "" {
return make([]string, 0)
}
parts := strings.Split(s, ",")
result := make([]string, 0, len(parts))
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if trimmed != "" {
result = append(result, trimmed)
}
}
return result
}

View file

@ -0,0 +1,31 @@
// Copyright 2025 The Forgejo Authors
// SPDX-License-Identifier: MIT
package cmd
import (
"slices"
"testing"
)
func TestCommaSplit(t *testing.T) {
tests := []struct {
input string
expected []string
}{
{"", []string{}},
{"abc", []string{"abc"}},
{"abc,def", []string{"abc", "def"}},
{"abc, def", []string{"abc", "def"}},
{" abc , def ", []string{"abc", "def"}},
{"abc, ,def", []string{"abc", "def"}},
{" , , ", []string{}},
}
for _, test := range tests {
result := commaSplit(test.input)
if !slices.Equal(result, test.expected) {
t.Errorf("commaSplit(%q) = %v, want %v", test.input, result, test.expected)
}
}
}