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 functions for version request #231

Merged
merged 3 commits into from
Aug 3, 2022
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
105 changes: 105 additions & 0 deletions apis/meta/v1alpha1/buildmetadata_funcs.go
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 {
l-qing marked this conversation as resolved.
Show resolved Hide resolved
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
}
66 changes: 66 additions & 0 deletions apis/meta/v1alpha1/buildmetadata_funcs_test.go
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))
}
2 changes: 2 additions & 0 deletions apis/meta/v1alpha1/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
ValidationError = "ValidationError"
// ErrorReason some error occurred
ErrorReason = "Error"
// StartedReason resource started to be reconciled
StartedReason = "Started"
l-qing marked this conversation as resolved.
Show resolved Hide resolved
)

const (
Expand Down
22 changes: 22 additions & 0 deletions apis/meta/v1alpha1/testdata/gitbranch.golden.yaml
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
4 changes: 4 additions & 0 deletions apis/meta/v1alpha1/testdata/gitbranch.status.yaml
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
21 changes: 21 additions & 0 deletions apis/meta/v1alpha1/testdata/gitcommit.golden.yaml
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"

7 changes: 7 additions & 0 deletions apis/meta/v1alpha1/testdata/gitcommit.status.yaml
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"
29 changes: 29 additions & 0 deletions apis/meta/v1alpha1/testdata/gitpullrequest.golden.yaml
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
7 changes: 7 additions & 0 deletions apis/meta/v1alpha1/testdata/gitpullrequest.status.yaml
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
35 changes: 35 additions & 0 deletions apis/validation/regexp_validation.go
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 {
l-qing marked this conversation as resolved.
Show resolved Hide resolved
_, err := regexp.Compile(pattern)
if err == nil {
return nil
}
errs := field.ErrorList{
field.Invalid(fld, pattern, err.Error()),
}
return errs
}
76 changes: 76 additions & 0 deletions apis/validation/regexp_validation_test.go
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)
})
}
}