feat: add validation for object-format flag in repo create command

This commit is contained in:
Lunny Xiao 2025-04-03 22:23:45 -07:00
parent d633e3018d
commit f0b07c7614
2 changed files with 91 additions and 11 deletions

View File

@ -87,6 +87,10 @@ var CmdRepoCreate = cli.Command{
Name: "trustmodel",
Usage: "select trust model (committer,collaborator,collaborator+committer)",
},
&cli.StringFlag{
Name: "object-format",
Usage: "select git object format (sha1,sha256)",
},
}, 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{
Name: ctx.String("name"),
Description: ctx.String("description"),
Private: ctx.Bool("private"),
AutoInit: ctx.Bool("init"),
IssueLabels: ctx.String("labels"),
Gitignores: ctx.String("gitignores"),
License: ctx.String("license"),
Readme: ctx.String("readme"),
DefaultBranch: ctx.String("branch"),
Template: ctx.Bool("template"),
TrustModel: trustmodel,
Name: ctx.String("name"),
Description: ctx.String("description"),
Private: ctx.Bool("private"),
AutoInit: ctx.Bool("init"),
IssueLabels: ctx.String("labels"),
Gitignores: ctx.String("gitignores"),
License: ctx.String("license"),
Readme: ctx.String("readme"),
DefaultBranch: ctx.String("branch"),
Template: ctx.Bool("template"),
TrustModel: trustmodel,
ObjectFormatName: ctx.String("object-format"),
}
if len(ctx.String("owner")) != 0 {
repo, _, err = client.CreateOrgRepo(ctx.String("owner"), opts)

68
cmd/repos/create_test.go Normal file
View 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
})
}
}