Skip to content

Commit 21ce6b4

Browse files
committed
remove non ending alphanumeric characters from labels
Fixes #1086 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent d6a7bda commit 21ce6b4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pkg/formatting/k8labels.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import "strings"
55
// K8LabelsCleanup k8s do not like slash in labels value and on push we have the
66
// full ref, we replace the "/" by "-". The tools probably need to be aware of
77
// it when querying.
8+
//
9+
// valid label must be an empty string or consist of alphanumeric characters,
10+
// '-', '_' or '.', and must start and end with an alphanumeric character
11+
// (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is
12+
// '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')
813
func K8LabelsCleanup(s string) string {
914
replasoeur := strings.NewReplacer("/", "-", " ", "_", "[", "__", "]", "__")
10-
return replasoeur.Replace(s)
15+
return replasoeur.Replace(strings.TrimRight(s, " -_[]"))
1116
}

pkg/formatting/k8labels_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ func TestK8LabelsCleanup(t *testing.T) {
2121
{
2222
name: "github bot name",
2323
str: "github-actions[bot]",
24-
want: "github-actions__bot__",
24+
want: "github-actions__bot",
25+
},
26+
{
27+
name: "trailing dash name removed",
28+
str: "MBAPPEvsMESSI--",
29+
want: "MBAPPEvsMESSI",
2530
},
2631
}
2732
for _, tt := range tests {

0 commit comments

Comments
 (0)