-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions for version request (#231)
* feat: add function to validate regular expression * feat: add functions to convert git status Co-authored-by: qingliu <[email protected]>
- Loading branch information
Showing
11 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 information 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 information 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 information 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name: release-1.7 | ||
protected: false | ||
default: false | ||
webURL: https://github.com/kubernetes/kubernetes/tree/release-1.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: "[email protected]" | ||
committer: | ||
name: "GitHub" | ||
email: "[email protected]" | ||
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: "[email protected]" | ||
pushedAT: "2022-07-26T15:01:21Z" | ||
webURL: "https://github.com/kubernetes/kubernetes/commit/9525206bcb1b51e3cb11e5efc19f274fc9cfc6fd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: "[email protected]" | ||
properties: | ||
revision: refs/pull/1/head | ||
webURL: https://github.com/kubernetes/kubernetes/pull/1 | ||
hasConflicts: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
id: 1 | ||
title: "Release 1.18" | ||
source: release-1.18 | ||
target: master | ||
authorEmail: "[email protected]" | ||
webURL: https://github.com/kubernetes/kubernetes/pull/1 | ||
hasConflicts: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<major>\d+)\.(?P<minor>\d+).*(?P<patch>\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) | ||
}) | ||
} | ||
} |