Update CreateRepoOption struct (#445)

extend Validate() for CreateRepoOption

update CreateRepoOption struct

Add TrustModel enum

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/445
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Co-Authored-By: 6543 <6543@noreply.gitea.io>
Co-Committed-By: 6543 <6543@noreply.gitea.io>
This commit is contained in:
6543 2020-11-09 23:57:26 +08:00
parent 8c9d3985b4
commit 378e711f7a
2 changed files with 36 additions and 4 deletions

View file

@ -73,6 +73,20 @@ const (
RepoTypeMirror RepoType = "mirror"
)
// TrustModel represent how git signatures are handled in a repository
type TrustModel string
const (
// TrustModelDefault use TM set by global config
TrustModelDefault TrustModel = "default"
// TrustModelCollaborator gpg signature has to be owned by a repo collaborator
TrustModelCollaborator TrustModel = "collaborator"
// TrustModelCommitter gpg signature has to match committer
TrustModelCommitter TrustModel = "committer"
// TrustModelCollaboratorCommitter gpg signature has to match committer and owned by a repo collaborator
TrustModelCollaboratorCommitter TrustModel = "collaboratorcommitter"
)
// ListReposOptions options for listing repositories
type ListReposOptions struct {
ListOptions
@ -249,6 +263,8 @@ type CreateRepoOption struct {
IssueLabels string `json:"issue_labels"`
// Whether the repository should be auto-intialized?
AutoInit bool `json:"auto_init"`
// Whether the repository is template
Template bool `json:"template"`
// Gitignores to use
Gitignores string `json:"gitignores"`
// License to use
@ -257,19 +273,35 @@ type CreateRepoOption struct {
Readme string `json:"readme"`
// DefaultBranch of the repository (used when initializes and in template)
DefaultBranch string `json:"default_branch"`
// TrustModel of the repository
TrustModel TrustModel `json:"trust_model"`
}
// Validate the CreateRepoOption struct
func (opt CreateRepoOption) Validate() error {
func (opt CreateRepoOption) Validate(c *Client) error {
if len(strings.TrimSpace(opt.Name)) == 0 {
return fmt.Errorf("name is empty")
}
if len(opt.Name) > 100 {
return fmt.Errorf("name has more than 100 chars")
}
if len(opt.Description) > 255 {
return fmt.Errorf("name has more than 255 chars")
}
if len(opt.DefaultBranch) > 100 {
return fmt.Errorf("name has more than 100 chars")
}
if len(opt.TrustModel) != 0 {
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
return err
}
}
return nil
}
// CreateRepo creates a repository for authenticated user.
func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, *Response, error) {
if err := opt.Validate(); err != nil {
if err := opt.Validate(c); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)
@ -283,7 +315,7 @@ func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, *Response, error
// CreateOrgRepo creates an organization repository for authenticated user.
func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, *Response, error) {
if err := opt.Validate(); err != nil {
if err := opt.Validate(c); err != nil {
return nil, nil, err
}
body, err := json.Marshal(&opt)

View file

@ -66,7 +66,7 @@ func TestRepoMigrateAndLanguages(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, lang, 2)
assert.True(t, 217441 < lang["Go"])
assert.EqualValues(t, 3578, lang["Makefile"])
assert.EqualValues(t, 3614, lang["Makefile"])
}
func TestSearchRepo(t *testing.T) {