Skip to content

Commit

Permalink
feat: add versionPhase and nameAsTag for git branch status
Browse files Browse the repository at this point in the history
  • Loading branch information
l-qing committed Feb 6, 2023
1 parent 952bbec commit e13573c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 83 deletions.
22 changes: 22 additions & 0 deletions apis/meta/v1alpha1/buildmetadata_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"k8s.io/apimachinery/pkg/util/validation/field"

kregex "github.com/katanomi/pkg/regex"
ksubstitute "github.com/katanomi/pkg/substitution"
)

Expand Down Expand Up @@ -150,6 +151,8 @@ func (b *BuildRunGitStatus) GetValWithKey(ctx context.Context, path *field.Path)
//
stringReplacements[path.Child("version").String()] = b.Version
//
stringReplacements[path.Child("versionPhase").String()] = b.VersionPhase
//
variantsMap := map[string]string{}
for variant, version := range b.VersionVariants {
// the key is `version` not `versionVariants`, convenient for users.
Expand Down Expand Up @@ -198,8 +201,27 @@ func (b *BuildGitBranchStatus) GetValWithKey(ctx context.Context, path *field.Pa
}
stringVals := map[string]string{path.String(): b.Name}
stringVals[path.Child("name").String()] = b.Name
stringVals[path.Child("nameAsTag").String()] = nameAsTagReplaces.ReplaceAllString(b.Name)
stringVals[path.Child("protected").String()] = strconv.FormatBool(b.Protected)
stringVals[path.Child("default").String()] = strconv.FormatBool(b.Default)
stringVals[path.Child("webURL").String()] = b.WebURL
return stringVals
}

var (
// nameAsTagReplaces used to generate the tag name from the branch name.
// 1. replacing `/` and `_` to `-`
// 2. remove the ending non [0-9a-zA-Z] characters
// 3. maximum length limit is 30 (extra characters in the prefix will be removed.)
nameAsTagReplaces = kregex.Replaces{
// replacing `/` and `_` to `-`
{Regex: `[/_]`, Replacement: "-"},
// remove the ending non [0-9a-zA-Z] characters
{Regex: `[^0-9a-zA-Z]*$`, Replacement: ""},
// maximum length limit is 30 (extra characters in the prefix will be removed.)
// (?U) indicates that the regex is non-greedy regex.
{Regex: `^(?U)(.*)(.{0,30})$`, Replacement: "${2}"},
// remove the starting non [0-9a-zA-Z] characters
{Regex: `^[^0-9a-zA-Z]*`, Replacement: ""},
}
)
93 changes: 11 additions & 82 deletions apis/meta/v1alpha1/buildmetadata_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -78,6 +79,7 @@ var _ = Describe("Test.BuildRunGitStatus.GetValWithKey", func() {
ctx context.Context
gitStatus *BuildRunGitStatus
actual map[string]string
expected map[string]string
//
// log level. It can be debug, info, warn, error, dpanic, panic, fatal.
log, _ = logging.NewLogger("", "debug")
Expand All @@ -86,6 +88,7 @@ var _ = Describe("Test.BuildRunGitStatus.GetValWithKey", func() {
BeforeEach(func() {
ctx = context.TODO()
gitStatus = &BuildRunGitStatus{}
expected = map[string]string{}
Expect(ktesting.LoadYAML("testdata/gitstatus.golden.yaml", &gitStatus)).To(Succeed())
})

Expand All @@ -97,96 +100,22 @@ var _ = Describe("Test.BuildRunGitStatus.GetValWithKey", func() {
When("struct is empty", func() {
BeforeEach(func() {
gitStatus = &BuildRunGitStatus{}
ktesting.MustLoadYaml("testdata/gitstatus.emptymap.golden.yaml", &expected)
})

It("should have values", func() {
Expect(actual).To(Equal(map[string]string{
"git": "",
"git.url": "",
"git.version": "",
//
"git.revision": "",
"git.revision.raw": "",
"git.revision.id": "",
"git.revision.type": "",
//
"git.lastCommit": "",
"git.lastCommit.id": "",
"git.lastCommit.shortID": "",
"git.lastCommit.title": "",
"git.lastCommit.message": "",
"git.lastCommit.authorEmail": "",
"git.lastCommit.pushedAt": "",
"git.lastCommit.webURL": "",
//
"git.pullRequest": "",
"git.pullRequest.id": "",
"git.pullRequest.title": "",
"git.pullRequest.source": "",
"git.pullRequest.target": "",
"git.pullRequest.webURL": "",
"git.pullRequest.authorEmail": "",
"git.pullRequest.hasConflicts": "false",
// branch
"git.branch": "",
"git.branch.name": "",
"git.branch.protected": "false",
"git.branch.default": "false",
"git.branch.webURL": "",
// target
"git.target": "",
"git.target.name": "",
"git.target.protected": "false",
"git.target.default": "false",
"git.target.webURL": "",
}))
diff := cmp.Diff(actual, expected)
Expect(diff).To(BeEmpty())
})
})

When("struct is not empty", func() {
BeforeEach(func() {
ktesting.MustLoadYaml("testdata/gitstatus.map.golden.yaml", &expected)
})
It("should have values", func() {
Expect(actual).To(Equal(map[string]string{
"git": "",
"git.url": "https://github.com/katanomi/pkg",
"git.version": "v1.2.3",
"git.version.docker": "v1.2.3",
"git.version.custom": "v1.2.3-custom",
//
"git.revision": "refs/pulls/123/head",
"git.revision.raw": "refs/pulls/123/head",
"git.revision.id": "123",
"git.revision.type": "PullRequest",
//
"git.lastCommit": "abe83942",
"git.lastCommit.id": "abe83942450308432a12e9679519795f938b2bed",
"git.lastCommit.shortID": "abe83942",
"git.lastCommit.title": "Initial commit 406",
"git.lastCommit.message": "Initial commit 406\n",
"git.lastCommit.authorEmail": "[email protected]",
"git.lastCommit.pushedAt": "2020-01-01T01:02:03Z",
"git.lastCommit.webURL": "https://github.com",
//
"git.pullRequest": "1",
"git.pullRequest.id": "1",
"git.pullRequest.title": "test-build ==> master",
"git.pullRequest.source": "test-build",
"git.pullRequest.target": "master",
"git.pullRequest.webURL": "https://github.com/katanomi/pkg/merge_requests/1",
"git.pullRequest.hasConflicts": "true",
"git.pullRequest.authorEmail": "[email protected]",
// source in pr
"git.branch": "test-build",
"git.branch.name": "test-build",
"git.branch.protected": "true",
"git.branch.default": "true",
"git.branch.webURL": "https://github.com/katanomi/pkg/tree/test",
// target in pr
"git.target": "release",
"git.target.name": "release",
"git.target.protected": "true",
"git.target.default": "false",
"git.target.webURL": "https://github.com/katanomi/pkg/tree/release",
}))
diff := cmp.Diff(actual, expected)
Expect(diff).To(BeEmpty())
})
})

Expand Down
4 changes: 4 additions & 0 deletions apis/meta/v1alpha1/buildmetadata_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type BuildRunGitStatus struct {
// BaseGitStatus is the base git status
BaseGitStatus `json:",inline"`

// VersionPhase is the phase on the versionscheme that matches this git revision.
// +optional
VersionPhase string `json:"versionPhase,omitempty"`

// Version is the version generated for this git revision
// +optional
Version string `json:"version,omitempty"`
Expand Down
41 changes: 41 additions & 0 deletions apis/meta/v1alpha1/testdata/gitstatus.emptymap.golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"git": ""
"git.url": ""
"git.version": ""
"git.versionPhase": ""
#
"git.revision": ""
"git.revision.raw": ""
"git.revision.id": ""
"git.revision.type": ""
#
"git.lastCommit": ""
"git.lastCommit.id": ""
"git.lastCommit.shortID": ""
"git.lastCommit.title": ""
"git.lastCommit.message": ""
"git.lastCommit.authorEmail": ""
"git.lastCommit.pushedAt": ""
"git.lastCommit.webURL": ""
#
"git.pullRequest": ""
"git.pullRequest.id": ""
"git.pullRequest.title": ""
"git.pullRequest.source": ""
"git.pullRequest.target": ""
"git.pullRequest.webURL": ""
"git.pullRequest.authorEmail": ""
"git.pullRequest.hasConflicts": "false"
# branch
"git.branch": ""
"git.branch.name": ""
"git.branch.nameAsTag": ""
"git.branch.protected": "false"
"git.branch.default": "false"
"git.branch.webURL": ""
# target
"git.target": ""
"git.target.name": ""
"git.target.nameAsTag": ""
"git.target.protected": "false"
"git.target.default": "false"
"git.target.webURL": ""
43 changes: 43 additions & 0 deletions apis/meta/v1alpha1/testdata/gitstatus.map.golden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"git": ""
"git.url": "https://github.com/katanomi/pkg"
"git.versionPhase": ""
"git.version": "v1.2.3"
"git.version.docker": "v1.2.3"
"git.version.custom": "v1.2.3-custom"
#
"git.revision": "refs/pulls/123/head"
"git.revision.raw": "refs/pulls/123/head"
"git.revision.id": "123"
"git.revision.type": "PullRequest"
#
"git.lastCommit": "abe83942"
"git.lastCommit.id": "abe83942450308432a12e9679519795f938b2bed"
"git.lastCommit.shortID": "abe83942"
"git.lastCommit.title": "Initial commit 406"
"git.lastCommit.message": "Initial commit 406\n"
"git.lastCommit.authorEmail": "[email protected]"
"git.lastCommit.pushedAt": "2020-01-01T01:02:03Z"
"git.lastCommit.webURL": "https://github.com"
#
"git.pullRequest": "1"
"git.pullRequest.id": "1"
"git.pullRequest.title": "test-build ==> master"
"git.pullRequest.source": "test-build"
"git.pullRequest.target": "master"
"git.pullRequest.webURL": "https://github.com/katanomi/pkg/merge_requests/1"
"git.pullRequest.hasConflicts": "true"
"git.pullRequest.authorEmail": "[email protected]"
#
"git.branch": "test-build"
"git.branch.name": "test-build"
"git.branch.nameAsTag": "test-build"
"git.branch.protected": "true"
"git.branch.default": "true"
"git.branch.webURL": "https://github.com/katanomi/pkg/tree/test"
#
"git.target": "release"
"git.target.name": "release"
"git.target.nameAsTag": "release"
"git.target.protected": "true"
"git.target.default": "false"
"git.target.webURL": "https://github.com/katanomi/pkg/tree/release"
2 changes: 1 addition & 1 deletion regex/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package regex

import "regexp"

// Replace provide helper functions for replacing strings
// Replace provides helper functions for replacing strings
type Replace struct {
// Regex is a regular expression used to modify the original version to generate new variants
// +optional
Expand Down

0 comments on commit e13573c

Please sign in to comment.