From a3fe01219bae89c8ddedd46e5e4a3fc640bf467b Mon Sep 17 00:00:00 2001 From: qingliu Date: Fri, 29 Jul 2022 23:33:24 +0800 Subject: [PATCH 1/3] feat: add function to validate regular expression --- apis/meta/v1alpha1/constant.go | 2 + apis/validation/regexp_validation.go | 35 +++++++++++ apis/validation/regexp_validation_test.go | 76 +++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 apis/validation/regexp_validation.go create mode 100644 apis/validation/regexp_validation_test.go diff --git a/apis/meta/v1alpha1/constant.go b/apis/meta/v1alpha1/constant.go index f0ff0f75..792af629 100644 --- a/apis/meta/v1alpha1/constant.go +++ b/apis/meta/v1alpha1/constant.go @@ -48,6 +48,8 @@ const ( ValidationError = "ValidationError" // ErrorReason some error occurred ErrorReason = "Error" + // StartedReason resource started to be reconciled + StartedReason = "Started" ) const ( diff --git a/apis/validation/regexp_validation.go b/apis/validation/regexp_validation.go new file mode 100644 index 00000000..d523ded5 --- /dev/null +++ b/apis/validation/regexp_validation.go @@ -0,0 +1,35 @@ +/* +Copyright 2022 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package validation + +import ( + "regexp" + + "k8s.io/apimachinery/pkg/util/validation/field" +) + +// ValidateRegExp validates that the string is a valid regular expression. +func ValidateRegExp(pattern string, fld *field.Path) field.ErrorList { + _, err := regexp.Compile(pattern) + if err == nil { + return nil + } + errs := field.ErrorList{ + field.Invalid(fld, pattern, err.Error()), + } + return errs +} diff --git a/apis/validation/regexp_validation_test.go b/apis/validation/regexp_validation_test.go new file mode 100644 index 00000000..87c12667 --- /dev/null +++ b/apis/validation/regexp_validation_test.go @@ -0,0 +1,76 @@ +/* +Copyright 2022 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package validation + +import ( + "testing" + + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateRegExp(t *testing.T) { + table := map[string]struct { + Pattern string + FieldPath *field.Path + Evaluation func(g *WithT, errs field.ErrorList) + }{ + "empty string": { + ``, + field.NewPath("abc"), + func(g *WithT, errs field.ErrorList) { + g.Expect(errs).To(HaveLen(0)) + }, + }, + "normal string": { + `abc`, + field.NewPath("abc"), + func(g *WithT, errs field.ErrorList) { + g.Expect(errs).To(HaveLen(0)) + }, + }, + "correct regular expressions": { + `^abc.*z$`, + field.NewPath("abc"), + func(g *WithT, errs field.ErrorList) { + g.Expect(errs).To(HaveLen(0)) + }, + }, + "complex regular expressions": { + `v(?P\d+)\.(?P\d+).*(?P\d+)*`, + field.NewPath("abc"), + func(g *WithT, errs field.ErrorList) { + g.Expect(errs).To(HaveLen(0)) + }, + }, + "incorrect regular expressions": { + `^(1234\$`, + field.NewPath("abc"), + func(g *WithT, errs field.ErrorList) { + g.Expect(errs).To(HaveLen(1)) + }, + }, + } + + for i, test := range table { + t.Run(i, func(t *testing.T) { + g := NewGomegaWithT(t) + errs := ValidateRegExp(test.Pattern, test.FieldPath) + test.Evaluation(g, errs) + }) + } +} From 388e379ea6bdd13c1255f24b338da7ddbfce7733 Mon Sep 17 00:00:00 2001 From: qingliu Date: Thu, 21 Jul 2022 13:36:47 +0800 Subject: [PATCH 2/3] feat: add functions to convert git status --- apis/meta/v1alpha1/buildmetadata_funcs.go | 105 ++++++++++++++++++ .../meta/v1alpha1/buildmetadata_funcs_test.go | 66 +++++++++++ .../v1alpha1/testdata/gitbranch.golden.yaml | 22 ++++ .../v1alpha1/testdata/gitbranch.status.yaml | 4 + .../v1alpha1/testdata/gitcommit.golden.yaml | 21 ++++ .../v1alpha1/testdata/gitcommit.status.yaml | 7 ++ .../testdata/gitpullrequest.golden.yaml | 29 +++++ .../testdata/gitpullrequest.status.yaml | 7 ++ 8 files changed, 261 insertions(+) create mode 100644 apis/meta/v1alpha1/buildmetadata_funcs.go create mode 100644 apis/meta/v1alpha1/buildmetadata_funcs_test.go create mode 100644 apis/meta/v1alpha1/testdata/gitbranch.golden.yaml create mode 100644 apis/meta/v1alpha1/testdata/gitbranch.status.yaml create mode 100644 apis/meta/v1alpha1/testdata/gitcommit.golden.yaml create mode 100644 apis/meta/v1alpha1/testdata/gitcommit.status.yaml create mode 100644 apis/meta/v1alpha1/testdata/gitpullrequest.golden.yaml create mode 100644 apis/meta/v1alpha1/testdata/gitpullrequest.status.yaml diff --git a/apis/meta/v1alpha1/buildmetadata_funcs.go b/apis/meta/v1alpha1/buildmetadata_funcs.go new file mode 100644 index 00000000..2ece2431 --- /dev/null +++ b/apis/meta/v1alpha1/buildmetadata_funcs.go @@ -0,0 +1,105 @@ +/* +Copyright 2021 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "encoding/json" + "strconv" +) + +// AssignByGitBranch is used to assign the infomation from GitBranch +func (b *BuildGitBranchStatus) AssignByGitBranch(gitBranch *GitBranch) *BuildGitBranchStatus { + if gitBranch == nil { + return b + } + if b == nil { + b = &BuildGitBranchStatus{} + } + b.Name = gitBranch.Name + b.Default = *gitBranch.Spec.Default + b.Protected = *gitBranch.Spec.Protected + if gitBranch.Spec.Properties.Raw != nil { + var content map[string]string + json.Unmarshal(gitBranch.Spec.Properties.Raw, &content) + b.WebURL = content["webURL"] + } + return b +} + +// CommitProperties commit properties info +// +kubebuilder:object:generate=false +type CommitProperties struct { + // ShortID commit short id + ShortID string `json:"shortID"` + // Title commit title + Title string `json:"title"` +} + +// AssignByGitCommit is used to assign the infomation from GitCommit +func (b *BuildGitCommitStatus) AssignByGitCommit(gitCommit *GitCommit) *BuildGitCommitStatus { + if gitCommit == nil { + return b + } + if b == nil { + b = &BuildGitCommitStatus{} + } + if gitCommit.Spec.SHA != nil { + b.ID = *gitCommit.Spec.SHA + } + if gitCommit.Spec.Message != nil { + b.Message = *gitCommit.Spec.Message + } + if gitCommit.Spec.Author != nil { + b.AuthorEmail = gitCommit.Spec.Author.Email + } + if gitCommit.Spec.Address != nil && gitCommit.Spec.Address.URL != nil { + b.WebURL = gitCommit.Spec.Address.URL.String() + } + + if gitCommit.Spec.Properties.Raw != nil { + propertiesInfo := &CommitProperties{} + if err := json.Unmarshal(gitCommit.Spec.Properties.Raw, propertiesInfo); err == nil { + b.ShortID = propertiesInfo.ShortID + b.Title = propertiesInfo.Title + } + } + + return b +} + +// AssignByGitPullRequest is used to assign the infomation from GitPullRequest +func (b *BuildGitPullRequestStatus) AssignByGitPullRequest(gitPullRequest *GitPullRequest) *BuildGitPullRequestStatus { + if gitPullRequest == nil { + return b + } + if b == nil { + b = &BuildGitPullRequestStatus{} + } + b.ID = strconv.FormatInt(gitPullRequest.Spec.Number, 10) + b.Title = gitPullRequest.Spec.Title + b.HasConflicts = (gitPullRequest.Spec.MergeStatus == MergeStatusCannotBeMerged) + + b.Target = gitPullRequest.Spec.Target.Name + b.Source = gitPullRequest.Spec.Source.Name + b.AuthorEmail = gitPullRequest.Spec.Author.Email + if gitPullRequest.Spec.Properties.Raw != nil { + var content map[string]string + json.Unmarshal(gitPullRequest.Spec.Properties.Raw, &content) + b.WebURL = content["webURL"] + } + return b +} diff --git a/apis/meta/v1alpha1/buildmetadata_funcs_test.go b/apis/meta/v1alpha1/buildmetadata_funcs_test.go new file mode 100644 index 00000000..89fdcb9f --- /dev/null +++ b/apis/meta/v1alpha1/buildmetadata_funcs_test.go @@ -0,0 +1,66 @@ +/* +Copyright 2022 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "testing" + + // ktesting "github.com/katanomi/pkg/testing" + . "github.com/onsi/gomega" + // corev1 "k8s.io/api/core/v1" + + ktesting "github.com/katanomi/pkg/testing" +) + +func TestAssignByGitBranch(t *testing.T) { + g := NewGomegaWithT(t) + + gitBranch := GitBranch{} + gitBranchStatus := BuildGitBranchStatus{} + g.Expect(ktesting.LoadYAML("testdata/gitbranch.golden.yaml", &gitBranch)).To(Succeed()) + g.Expect(ktesting.LoadYAML("testdata/gitbranch.status.yaml", &gitBranchStatus)).To(Succeed()) + + expectStatus := gitBranchStatus + actualGitBranchStatus := gitBranchStatus.AssignByGitBranch(&gitBranch) + g.Expect(*actualGitBranchStatus).To(Equal(expectStatus)) +} + +func TestAssignByGitCommit(t *testing.T) { + g := NewGomegaWithT(t) + + gitCommit := GitCommit{} + gitCommitStatus := BuildGitCommitStatus{} + g.Expect(ktesting.LoadYAML("testdata/gitcommit.golden.yaml", &gitCommit)).To(Succeed()) + g.Expect(ktesting.LoadYAML("testdata/gitcommit.status.yaml", &gitCommitStatus)).To(Succeed()) + + expectStatus := gitCommitStatus + actualGitCommitStatus := gitCommitStatus.AssignByGitCommit(&gitCommit) + g.Expect(*actualGitCommitStatus).To(Equal(expectStatus)) +} + +func TestAssignByGitPullRequest(t *testing.T) { + g := NewGomegaWithT(t) + + gitPullRequest := GitPullRequest{} + gitPullRequestStatus := BuildGitPullRequestStatus{} + g.Expect(ktesting.LoadYAML("testdata/gitpullrequest.golden.yaml", &gitPullRequest)).To(Succeed()) + g.Expect(ktesting.LoadYAML("testdata/gitpullrequest.status.yaml", &gitPullRequestStatus)).To(Succeed()) + + expectStatus := gitPullRequestStatus + actualGitPullRequestStatus := gitPullRequestStatus.AssignByGitPullRequest(&gitPullRequest) + g.Expect(*actualGitPullRequestStatus).To(Equal(expectStatus)) +} diff --git a/apis/meta/v1alpha1/testdata/gitbranch.golden.yaml b/apis/meta/v1alpha1/testdata/gitbranch.golden.yaml new file mode 100644 index 00000000..5eec57c5 --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitbranch.golden.yaml @@ -0,0 +1,22 @@ +kind: GitBranch +apiVersion: v1alpha1 +metadata: + name: release-1.7 + creationTimestamp: null +spec: + project: kubernetes + repository: kubernetes + name: release-1.7 + protected: false + default: false + commit: + sha: 347f652944cc2348d59b1c815007e21e062fe597 + createdAt: null + webURL: https://github.com/kubernetes/kubernetes/tree/release-1.7 + downloadURL: + zip: https://github.com/kubernetes/kubernetes/archive/refs/heads/release-1.7.zip + tar.gz: https://github.com/kubernetes/kubernetes/archive/refs/heads/release-1.7.tar.gz + tar.ba2: null + tar: null + properties: + webURL: https://github.com/kubernetes/kubernetes/tree/release-1.7 diff --git a/apis/meta/v1alpha1/testdata/gitbranch.status.yaml b/apis/meta/v1alpha1/testdata/gitbranch.status.yaml new file mode 100644 index 00000000..5361f463 --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitbranch.status.yaml @@ -0,0 +1,4 @@ +name: release-1.7 +protected: false +default: false +webURL: https://github.com/kubernetes/kubernetes/tree/release-1.7 diff --git a/apis/meta/v1alpha1/testdata/gitcommit.golden.yaml b/apis/meta/v1alpha1/testdata/gitcommit.golden.yaml new file mode 100644 index 00000000..07591efa --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitcommit.golden.yaml @@ -0,0 +1,21 @@ +kind: "GitCommit" +apiVersion: "v1alpha1" +metadata: + name: "9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" + creationTimestamp: "2022-07-26T15:01:21Z" +spec: + sha: "9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" + createdAt: "2022-07-26T15:01:21Z" + address: + url: "https://github.com/kubernetes/kubernetes/commit/9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" + author: + name: "Kubernetes Prow Robot" + email: "k8s-ci-robot@users.noreply.github.com" + committer: + name: "GitHub" + email: "noreply@github.com" + message: "Merge pull request #111306 from stlaz/restricted_scheduling_test\n\nMake scheduling e2e tests run PSa-restricted pods" + properties: + shortID: "952520" + title: "Merge pull request #111306 from stlaz/restricted_scheduling_test\n\nMake scheduling e2e tests run PSa-restricted pods" + diff --git a/apis/meta/v1alpha1/testdata/gitcommit.status.yaml b/apis/meta/v1alpha1/testdata/gitcommit.status.yaml new file mode 100644 index 00000000..5b49b8b9 --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitcommit.status.yaml @@ -0,0 +1,7 @@ +shortID: "952520" +id: "9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" +title: "Merge pull request #111306 from stlaz/restricted_scheduling_test\n\nMake scheduling e2e tests run PSa-restricted pods" +message: "Merge pull request #111306 from stlaz/restricted_scheduling_test\n\nMake scheduling e2e tests run PSa-restricted pods" +authorEmail: "k8s-ci-robot@users.noreply.github.com" +pushedAT: "2022-07-26T15:01:21Z" +webURL: "https://github.com/kubernetes/kubernetes/commit/9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" diff --git a/apis/meta/v1alpha1/testdata/gitpullrequest.golden.yaml b/apis/meta/v1alpha1/testdata/gitpullrequest.golden.yaml new file mode 100644 index 00000000..1654ede9 --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitpullrequest.golden.yaml @@ -0,0 +1,29 @@ +kind: GitPullRequest +apiVersion: v1alpha1 +metadata: + name: PR_kwDODnYaK848fkOR + creationTimestamp: null +spec: + project: kubernetes + repository: kubernetes + id: 1014907793 + num: 1 + title: Release 1.18 + state: open + createdAt: "2022-08-02T10:08:24Z" + updateAt: "2022-08-02T10:08:24Z" + target: + project: kubernetes + repository: kubernetes + name: master + source: + project: kubernetes + repository: kubernetes + name: release-1.18 + author: + name: kubernetes + email: "k8s-ci-robot@users.noreply.github.com" + properties: + revision: refs/pull/1/head + webURL: https://github.com/kubernetes/kubernetes/pull/1 + hasConflicts: false diff --git a/apis/meta/v1alpha1/testdata/gitpullrequest.status.yaml b/apis/meta/v1alpha1/testdata/gitpullrequest.status.yaml new file mode 100644 index 00000000..4d1f2c46 --- /dev/null +++ b/apis/meta/v1alpha1/testdata/gitpullrequest.status.yaml @@ -0,0 +1,7 @@ +id: 1 +title: "Release 1.18" +source: release-1.18 +target: master +authorEmail: "k8s-ci-robot@users.noreply.github.com" +webURL: https://github.com/kubernetes/kubernetes/pull/1 +hasConflicts: false From 1b78693a8e67e9d0dd373830788a3c57bf819f55 Mon Sep 17 00:00:00 2001 From: qingliu Date: Tue, 2 Aug 2022 22:39:44 +0800 Subject: [PATCH 3/3] fix: typo --- apis/meta/v1alpha1/buildmetadata_funcs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apis/meta/v1alpha1/buildmetadata_funcs.go b/apis/meta/v1alpha1/buildmetadata_funcs.go index 2ece2431..44b2d649 100644 --- a/apis/meta/v1alpha1/buildmetadata_funcs.go +++ b/apis/meta/v1alpha1/buildmetadata_funcs.go @@ -21,7 +21,7 @@ import ( "strconv" ) -// AssignByGitBranch is used to assign the infomation from GitBranch +// AssignByGitBranch is used to assign the information from GitBranch func (b *BuildGitBranchStatus) AssignByGitBranch(gitBranch *GitBranch) *BuildGitBranchStatus { if gitBranch == nil { return b @@ -49,7 +49,7 @@ type CommitProperties struct { Title string `json:"title"` } -// AssignByGitCommit is used to assign the infomation from GitCommit +// AssignByGitCommit is used to assign the information from GitCommit func (b *BuildGitCommitStatus) AssignByGitCommit(gitCommit *GitCommit) *BuildGitCommitStatus { if gitCommit == nil { return b @@ -81,7 +81,7 @@ func (b *BuildGitCommitStatus) AssignByGitCommit(gitCommit *GitCommit) *BuildGit return b } -// AssignByGitPullRequest is used to assign the infomation from GitPullRequest +// AssignByGitPullRequest is used to assign the information from GitPullRequest func (b *BuildGitPullRequestStatus) AssignByGitPullRequest(gitPullRequest *GitPullRequest) *BuildGitPullRequestStatus { if gitPullRequest == nil { return b