mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-11-04 03:05:26 +01:00 
			
		
		
		
	feat: add validation for object-format flag in repo create command (#741)
This PR adds validation for the `--object-format` flag in the `repo create` command. The flag now accepts only `sha1` or `sha256` as valid values, and returns an error for any other value. Changes: - Added validation in `runRepoCreate` to check for valid object format values - Added unit tests to verify the validation logic - Fixed the field name from `ObjectFormat` to `ObjectFormatName` to match the SDK The changes ensure that users get clear error messages when using invalid object format values, improving the user experience. Fix #727 Fix #660 Fix #767 Co-authored-by: techknowlogick <techknowlogick@noreply.gitea.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/741 Reviewed-by: TheFox0x7 <thefox0x7@noreply.gitea.com>
This commit is contained in:
		@@ -88,6 +88,17 @@ var CmdRepoCreate = cli.Command{
 | 
			
		||||
			Name:  "trustmodel",
 | 
			
		||||
			Usage: "select trust model (committer,collaborator,collaborator+committer)",
 | 
			
		||||
		},
 | 
			
		||||
		&cli.StringFlag{
 | 
			
		||||
			Name:     "object-format",
 | 
			
		||||
			Required: false,
 | 
			
		||||
			Usage:    "select git object format (sha1,sha256)",
 | 
			
		||||
			Validator: func(v string) error {
 | 
			
		||||
				if v != "sha1" && v != "sha256" {
 | 
			
		||||
					return fmt.Errorf("invalid object format '%s', must be either 'sha1' or 'sha256'", v)
 | 
			
		||||
				}
 | 
			
		||||
				return nil
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}, flags.LoginOutputFlags...),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -114,17 +125,18 @@ func runRepoCreate(_ stdctx.Context, cmd *cli.Command) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										88
									
								
								cmd/repos/create_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								cmd/repos/create_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
			
		||||
// Copyright 2025 The Gitea Authors. All rights reserved.
 | 
			
		||||
// SPDX-License-Identifier: MIT
 | 
			
		||||
 | 
			
		||||
package repos
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/sdk/gitea"
 | 
			
		||||
	"code.gitea.io/tea/modules/task"
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"github.com/urfave/cli/v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestCreateRepoObjectFormat(t *testing.T) {
 | 
			
		||||
	giteaURL := os.Getenv("GITEA_TEA_TEST_URL")
 | 
			
		||||
	if giteaURL == "" {
 | 
			
		||||
		t.Skip("GITEA_TEA_TEST_URL is not set, skipping test")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	timestamp := time.Now().Unix()
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name        string
 | 
			
		||||
		args        []string
 | 
			
		||||
		wantOpts    gitea.CreateRepoOption
 | 
			
		||||
		wantErr     bool
 | 
			
		||||
		errContains string
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name: "create repo with sha1 object format",
 | 
			
		||||
			args: []string{"--name", fmt.Sprintf("test-sha1-%d", timestamp), "--object-format", "sha1"},
 | 
			
		||||
			wantOpts: gitea.CreateRepoOption{
 | 
			
		||||
				Name:             fmt.Sprintf("test-sha1-%d", timestamp),
 | 
			
		||||
				ObjectFormatName: "sha1",
 | 
			
		||||
			},
 | 
			
		||||
			wantErr: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "create repo with sha256 object format",
 | 
			
		||||
			args: []string{"--name", fmt.Sprintf("test-sha256-%d", timestamp), "--object-format", "sha256"},
 | 
			
		||||
			wantOpts: gitea.CreateRepoOption{
 | 
			
		||||
				Name:             fmt.Sprintf("test-sha256-%d", timestamp),
 | 
			
		||||
				ObjectFormatName: "sha256",
 | 
			
		||||
			},
 | 
			
		||||
			wantErr: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:        "create repo with invalid object format",
 | 
			
		||||
			args:        []string{"--name", fmt.Sprintf("test-invalid-%d", timestamp), "--object-format", "invalid"},
 | 
			
		||||
			wantErr:     true,
 | 
			
		||||
			errContains: "invalid object format",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	giteaUserName := os.Getenv("GITEA_TEA_TEST_USERNAME")
 | 
			
		||||
	giteaUserPasword := os.Getenv("GITEA_TEA_TEST_PASSWORD")
 | 
			
		||||
 | 
			
		||||
	err := task.CreateLogin("test", "", giteaUserName, giteaUserPasword, "", "", "", giteaURL, "", "", true, false, false, false)
 | 
			
		||||
	if err != nil && err.Error() != "login name 'test' has already been used" {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			reposCmd := &cli.Command{
 | 
			
		||||
				Name:     "repos",
 | 
			
		||||
				Commands: []*cli.Command{&CmdRepoCreate},
 | 
			
		||||
			}
 | 
			
		||||
			tt.args = append(tt.args, "--login", "test")
 | 
			
		||||
			args := append([]string{"repos", "create"}, tt.args...)
 | 
			
		||||
 | 
			
		||||
			err := reposCmd.Run(context.Background(), args)
 | 
			
		||||
			if tt.wantErr {
 | 
			
		||||
				assert.Error(t, err)
 | 
			
		||||
				if tt.errContains != "" {
 | 
			
		||||
					assert.Contains(t, err.Error(), tt.errContains)
 | 
			
		||||
				}
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			assert.NoError(t, err)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user