Skip to content

Commit

Permalink
feat: add common slice helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm committed Oct 18, 2023
1 parent 0e11bdc commit c18b7fc
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 17 deletions.
30 changes: 30 additions & 0 deletions apis/artifacts/v1alpha1/zz_generated.deepcopy.go

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

12 changes: 0 additions & 12 deletions apis/meta/v1alpha1/gitoption_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ type GitPullRequestOption struct {
Index int `json:"Index"`
}

// GitRepositoryTagOption option for repository tag
type GitRepositoryTagOption struct {
GitRepo
// Tag the name of the tag
Tag string `json:"tag"`
}

// GitRepositoryTagListOption option for list repository tag
type GitRepositoryTagListOption struct {
GitRepo
}

type PullRequestState string

const (
Expand Down
19 changes: 19 additions & 0 deletions apis/meta/v1alpha1/gitrepositories_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,22 @@ func GitRepositoryResourceAttributes(verb string) authv1.ResourceAttributes {
Verb: verb,
}
}

// GitRepositoryVisibility visibility of repository
type GitRepositoryVisibility string

const (
// GitRepositoryVisibilityPrivate private repository
GitRepositoryVisibilityPrivate GitRepositoryVisibility = "private"
// GitRepositoryVisibilityPublic public repository
GitRepositoryVisibilityPublic GitRepositoryVisibility = "public"
)

// CreateGitRepositoryPayload payload for create repository
type CreateGitRepositoryPayload struct {
GitRepo

DisplayName string `json:"displayName"`
Visibility GitRepositoryVisibility `json:"visibility"`
AutoInit bool `json:"autoInit"`
}
50 changes: 50 additions & 0 deletions apis/meta/v1alpha1/gittag_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2023 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 "errors"

// GitRepositoryTagOption option for repository tag
// Deprecated: use GitTag instead
type GitRepositoryTagOption struct {
GitRepo
// Tag the name of the tag
Tag string `json:"tag"`
}

// GitRepositoryTagListOption option for list repository tag
type GitRepositoryTagListOption struct {
GitRepo
}

// GitTag describe a unique tag
type GitTag struct {
GitRepo
// Tag the name of the tag
Tag string `json:"tag"`
}

// Validate validate the git repo
func (r *GitTag) Validate() error {
if err := r.GitRepo.Validate(); err != nil {
return err
}
if r.Tag == "" {
return errors.New("tag is empty")
}
return nil
}
83 changes: 83 additions & 0 deletions apis/meta/v1alpha1/gittag_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2023 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"

. "github.com/onsi/gomega"
)

func TestGitTag_Validate(t *testing.T) {
g := NewWithT(t)
testCases := map[string]struct {
gitTag GitTag
wantErr bool
wantErrMsg string
}{
"project is empty": {
gitTag: GitTag{
GitRepo: GitRepo{},
Tag: "",
},
wantErr: true,
wantErrMsg: "project is empty",
},
"repository is empty": {
gitTag: GitTag{
GitRepo: GitRepo{
Project: "abc",
},
Tag: "",
},
wantErr: true,
wantErrMsg: "repository is empty",
},
"tag is empty": {
gitTag: GitTag{
GitRepo: GitRepo{
Project: "abc",
Repository: "abc",
},
Tag: "",
},
wantErr: true,
wantErrMsg: "tag is empty",
},
"valid case": {
gitTag: GitTag{
GitRepo: GitRepo{
Project: "abc",
Repository: "abc",
},
Tag: "v0.0.1",
},
wantErr: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.wantErrMsg, func(t *testing.T) {
err := testCase.gitTag.Validate()
if !testCase.wantErr {
g.Expect(err).Should(Succeed())
} else {
g.Expect(err).Should(HaveOccurred())
g.Expect(err.Error()).Should(ContainSubstring(testCase.wantErrMsg))
}
})
}
}
107 changes: 102 additions & 5 deletions apis/meta/v1alpha1/zz_generated.deepcopy.go

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

12 changes: 12 additions & 0 deletions plugin/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,18 @@ type GitRepoFileCreator interface {
CreateGitRepoFile(ctx context.Context, payload metav1alpha1.CreateRepoFilePayload) (metav1alpha1.GitCommit, error)
}

// GitRepositoryCreator create a git repository
type GitRepositoryCreator interface {
Interface
CreateGitRepository(ctx context.Context, payload metav1alpha1.CreateGitRepositoryPayload) (metav1alpha1.GitRepository, error)
}

// GitRepositoryDeleter delete a git repository
type GitRepositoryDeleter interface {
Interface
DeleteGitRepository(ctx context.Context, gitRepo metav1alpha1.GitRepo) error
}

// GitRepositoryLister list git repository
type GitRepositoryLister interface {
Interface
Expand Down
Loading

0 comments on commit c18b7fc

Please sign in to comment.