mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-01 01:18:29 +02:00
feat: add validation for object-format flag in repo create command
This commit is contained in:
@ -87,6 +87,10 @@ var CmdRepoCreate = cli.Command{
|
|||||||
Name: "trustmodel",
|
Name: "trustmodel",
|
||||||
Usage: "select trust model (committer,collaborator,collaborator+committer)",
|
Usage: "select trust model (committer,collaborator,collaborator+committer)",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "object-format",
|
||||||
|
Usage: "select git object format (sha1,sha256)",
|
||||||
|
},
|
||||||
}, flags.LoginOutputFlags...),
|
}, flags.LoginOutputFlags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,18 +116,26 @@ func runRepoCreate(cmd *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ctx.IsSet("object-format") {
|
||||||
|
format := ctx.String("object-format")
|
||||||
|
if format != "sha1" && format != "sha256" {
|
||||||
|
return fmt.Errorf("invalid object format '%s', must be either 'sha1' or 'sha256'", format)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
opts := gitea.CreateRepoOption{
|
opts := gitea.CreateRepoOption{
|
||||||
Name: ctx.String("name"),
|
Name: ctx.String("name"),
|
||||||
Description: ctx.String("description"),
|
Description: ctx.String("description"),
|
||||||
Private: ctx.Bool("private"),
|
Private: ctx.Bool("private"),
|
||||||
AutoInit: ctx.Bool("init"),
|
AutoInit: ctx.Bool("init"),
|
||||||
IssueLabels: ctx.String("labels"),
|
IssueLabels: ctx.String("labels"),
|
||||||
Gitignores: ctx.String("gitignores"),
|
Gitignores: ctx.String("gitignores"),
|
||||||
License: ctx.String("license"),
|
License: ctx.String("license"),
|
||||||
Readme: ctx.String("readme"),
|
Readme: ctx.String("readme"),
|
||||||
DefaultBranch: ctx.String("branch"),
|
DefaultBranch: ctx.String("branch"),
|
||||||
Template: ctx.Bool("template"),
|
Template: ctx.Bool("template"),
|
||||||
TrustModel: trustmodel,
|
TrustModel: trustmodel,
|
||||||
|
ObjectFormatName: ctx.String("object-format"),
|
||||||
}
|
}
|
||||||
if len(ctx.String("owner")) != 0 {
|
if len(ctx.String("owner")) != 0 {
|
||||||
repo, _, err = client.CreateOrgRepo(ctx.String("owner"), opts)
|
repo, _, err = client.CreateOrgRepo(ctx.String("owner"), opts)
|
||||||
|
68
cmd/repos/create_test.go
Normal file
68
cmd/repos/create_test.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package repos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRepoObjectFormat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
wantOpts gitea.CreateRepoOption
|
||||||
|
wantErr bool
|
||||||
|
errContains string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "create repo with sha1 object format",
|
||||||
|
args: []string{"--name", "test-sha1", "--object-format", "sha1"},
|
||||||
|
wantOpts: gitea.CreateRepoOption{
|
||||||
|
Name: "test-sha1",
|
||||||
|
ObjectFormatName: "sha1",
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "create repo with sha256 object format",
|
||||||
|
args: []string{"--name", "test-sha256", "--object-format", "sha256"},
|
||||||
|
wantOpts: gitea.CreateRepoOption{
|
||||||
|
Name: "test-sha256",
|
||||||
|
ObjectFormatName: "sha256",
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "create repo with invalid object format",
|
||||||
|
args: []string{"--name", "test-invalid", "--object-format", "invalid"},
|
||||||
|
wantErr: true,
|
||||||
|
errContains: "invalid object format",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
app := cli.NewApp()
|
||||||
|
app.Commands = []*cli.Command{&CmdRepoCreate}
|
||||||
|
args := append([]string{"tea", "repo", "create"}, tt.args...)
|
||||||
|
|
||||||
|
err := app.Run(args)
|
||||||
|
if tt.wantErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
if tt.errContains != "" {
|
||||||
|
assert.Contains(t, err.Error(), tt.errContains)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
// Note: We can't directly test the CreateRepoOption since it's created inside runRepoCreate
|
||||||
|
// In a real test environment, we would mock the client and verify the options passed to it
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user