mirror of
https://gitea.com/gitea/tea.git
synced 2026-09-14 17:36:33 -04:00
Comma-separated flags (`--assignees`, `--labels`, `--add/remove-labels`,
`--add/remove-reviewers`, `--events`, login `--scopes`) silently accepted
stray commas and whitespace, producing values like `[""]` for empty input
or `["alice", "", "bob"]` for `--assignees=alice,,bob`. Those blanks
were then forwarded to the Gitea SDK, where they either triggered obscure
server errors or silently mutated state in unexpected ways.
Root cause: a mix of `strings.Split(s, ",")` and ad-hoc trim loops, none
of which rejected empty segments.
Introduce `modules/utils.SplitCSV` as the single parser for all CSV flag
values. It:
- returns `nil` for an empty or whitespace-only input (so callers can use
`len(x) == 0` to mean "not set", and `cmd.IsSet("...")` keeps
working),
- drops empty and whitespace-only segments anywhere in the input,
- otherwise trims surrounding whitespace from each remaining segment.
`cmd/flags.SplitCSV` is re-exported from `cmd/flags/csvflag.go` so
existing call sites read `flags.SplitCSV` without depending on a leaf
package that `modules/task` already imports them.
Convert the remaining raw `strings.Split(..., ",")` sites:
- `cmd/flags/issue_pr.go` (issue/pr --assignees, --labels and the
set/add/remove variants)
- `cmd/pulls/edit.go` (`--add-reviewers`, `--remove-reviewers`)
- `cmd/webhooks/create.go`, `cmd/webhooks/update.go` (`--events`)
- `modules/task/login_create.go` (login `--scopes`)
Drop the now-unused `"strings"` imports from the three cmd/* files.
Tests:
- `modules/utils/splitcsv_test.go` — table-driven coverage for empty,
whitespace-only, leading/trailing commas, internal empty segments,
surrounding whitespace, and re-imported identity with `flags.SplitCSV`.
- `cmd/flags/csvflag_test.go` — `GetValues` happy path, error path,
empty entry drop, and AvailableFields variants.
- `cmd/flags/issue_pr_test.go` — regression test for
`GetIssuePREditFlags` proving `SetAssignees/AddAssignees/...`
produce `nil` (not `[""]`) for empty input and trimmed slices for
mixed inputs.
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package flags
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
|
|
"gitea.dev/tea/modules/utils"
|
|
)
|
|
|
|
// CsvFlag is a wrapper around cli.StringFlag, with an added GetValues() method
|
|
// to retrieve comma separated string values as a slice.
|
|
type CsvFlag struct {
|
|
cli.StringFlag
|
|
AvailableFields []string
|
|
}
|
|
|
|
// NewCsvFlag creates a CsvFlag, while setting its usage string and default values
|
|
func NewCsvFlag(name, usage string, aliases, availableValues, defaults []string) *CsvFlag {
|
|
var availableDesc string
|
|
if len(availableValues) != 0 {
|
|
availableDesc = " Available values:"
|
|
}
|
|
return &CsvFlag{
|
|
AvailableFields: availableValues,
|
|
StringFlag: cli.StringFlag{
|
|
Name: name,
|
|
Aliases: aliases,
|
|
Value: strings.Join(defaults, ","),
|
|
Usage: fmt.Sprintf(`Comma-separated list of %s.%s
|
|
%s
|
|
`, usage, availableDesc, strings.Join(availableValues, ",")),
|
|
},
|
|
}
|
|
}
|
|
|
|
// SplitCSV is re-exported here for convenience; the canonical
|
|
// implementation lives in gitea.dev/tea/modules/utils so it can be used
|
|
// from both cmd/flags and modules/task without an import cycle.
|
|
var SplitCSV = utils.SplitCSV
|
|
|
|
// GetValues returns the value of the flag, parsed as a comma-separated list.
|
|
// Empty entries (from e.g. "alice,,bob" or a trailing comma) are dropped so
|
|
// callers never send blank identifiers to the API.
|
|
func (f CsvFlag) GetValues(cmd *cli.Command) ([]string, error) {
|
|
val := cmd.String(f.Name)
|
|
selection := utils.SplitCSV(val)
|
|
if f.AvailableFields != nil && len(selection) != 0 {
|
|
for _, field := range selection {
|
|
if !utils.Contains(f.AvailableFields, field) {
|
|
return nil, fmt.Errorf("invalid field '%s'", field)
|
|
}
|
|
}
|
|
}
|
|
return selection, nil
|
|
}
|