Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add replaces used to replace string #376

Merged
merged 5 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/applications/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions apis/codequality/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/data/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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{
l-qing marked this conversation as resolved.
Show resolved Hide resolved
// 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}"},
l-qing marked this conversation as resolved.
Show resolved Hide resolved
// 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())
l-qing marked this conversation as resolved.
Show resolved Hide resolved
})
})

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: 0 additions & 2 deletions apis/security/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/selection/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions apis/storage/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading