From 378e711f7a881e46870033b5fc15144e570a45d1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Mon, 9 Nov 2020 23:57:26 +0800 Subject: [PATCH] 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 Reviewed-by: Norwin Co-Authored-By: 6543 <6543@noreply.gitea.io> Co-Committed-By: 6543 <6543@noreply.gitea.io> --- gitea/repo.go | 38 +++++++++++++++++++++++++++++++++++--- gitea/repo_test.go | 2 +- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/gitea/repo.go b/gitea/repo.go index c432724..9aa1378 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -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) diff --git a/gitea/repo_test.go b/gitea/repo_test.go index c2a1f72..0131505 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -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) {