Skip to content

Commit

Permalink
fix: Coerce build- and source-ref to valid prerelease tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
rradczewski committed May 29, 2024
1 parent 49482c2 commit cf4dd11
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
29 changes: 26 additions & 3 deletions internal/trunkver/generate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package trunkver

import "time"
import (
"regexp"
"time"
)

var sourceRefFilterRegex *regexp.Regexp = regexp.MustCompile(`[^a-zA-Z0-9]`)

func coerceSourceRef(sourceRef string) string {
return sourceRefFilterRegex.ReplaceAllString(sourceRef, "")
}

var buildRefFilterRegex *regexp.Regexp = regexp.MustCompile(`[^a-zA-Z0-9-]`)

func coerceBuildRef(buildRef string) string {
return buildRefFilterRegex.ReplaceAllString(buildRef, "-")
}

func GenerateMajorTrunkver(ts time.Time, sourceRef, buildRef string) string {
return ts.UTC().Format("20060102150405") + ".0.0-" + sourceRef + "-" + buildRef
trunkVer := ts.UTC().Format("20060102150405") + ".0.0-" + coerceSourceRef(sourceRef) + "-" + coerceBuildRef(buildRef)
if _, err := ParseTrunkVer(trunkVer); err != nil {
panic(err)
}
return trunkVer
}

func GeneratePrereleaseTrunkver(ts time.Time, sourceRef, buildRef string) string {
return ts.UTC().Format("20060102150405") + "-" + sourceRef + "-" + buildRef
trunkVer := ts.UTC().Format("20060102150405") + "-" + coerceSourceRef(sourceRef) + "-" + coerceBuildRef(buildRef)
if _, err := ParseTrunkVer(trunkVer); err != nil {
panic(err)
}
return trunkVer
}
12 changes: 12 additions & 0 deletions smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ tests:
stdout: |
20240522142548/g1234567/R4242
- name: generate/slugifies-illegal-characters
args:
- generate
- --timestamp
- "2024-05-22T16:25:48+02:00"
- --source-ref
- "g1234567$_-+"
- --build-ref
- "R4242$_hy$phen-is-okay-in-build-ref+"
stdout: |
20240522142548.0.0-g1234567-R4242--hy-phen-is-okay-in-build-ref-
- name: generate/simple
args:
- generate
Expand Down

0 comments on commit cf4dd11

Please sign in to comment.