1
1
mirror of https://github.com/go-gitea/gitea synced 2025-12-06 21:08:25 +00:00

update golangci-lint to v2.7.0 (#36079)

- Update and autofix most issues
- Corrected variable names to `cutOk`
- Impossible condition in `services/migrations/onedev_test.go` removed
- `modules/setting/config_env.go:128:3` looks like a false-positive,
added nolint
This commit is contained in:
silverwind
2025-12-04 10:06:44 +01:00
committed by GitHub
parent ee6e371e44
commit b49dd8e32f
14 changed files with 53 additions and 56 deletions

View File

@@ -32,7 +32,7 @@ XGO_VERSION := go-1.25.x
AIR_PACKAGE ?= github.com/air-verse/air@v1 AIR_PACKAGE ?= github.com/air-verse/air@v1
EDITORCONFIG_CHECKER_PACKAGE ?= github.com/editorconfig-checker/editorconfig-checker/v3/cmd/editorconfig-checker@v3 EDITORCONFIG_CHECKER_PACKAGE ?= github.com/editorconfig-checker/editorconfig-checker/v3/cmd/editorconfig-checker@v3
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2 GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.0
GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.15 GXZ_PACKAGE ?= github.com/ulikunitz/xz/cmd/gxz@v0.5.15
MISSPELL_PACKAGE ?= github.com/golangci/misspell/cmd/misspell@v0.7.0 MISSPELL_PACKAGE ?= github.com/golangci/misspell/cmd/misspell@v0.7.0
SWAGGER_PACKAGE ?= github.com/go-swagger/go-swagger/cmd/swagger@v0.33.1 SWAGGER_PACKAGE ?= github.com/go-swagger/go-swagger/cmd/swagger@v0.33.1

View File

@@ -96,8 +96,8 @@ func (attrs *Attributes) GetGitlabLanguage() optional.Option[string] {
// gitlab-language may have additional parameters after the language // gitlab-language may have additional parameters after the language
// ignore them and just use the main language // ignore them and just use the main language
// https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type // https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type
if idx := strings.IndexByte(raw, '?'); idx >= 0 { if before, _, ok := strings.Cut(raw, "?"); ok {
return optional.Some(raw[:idx]) return optional.Some(before)
} }
} }
return attrStr return attrStr

View File

@@ -113,10 +113,10 @@ func (p *Parser) parseRef(refBlock string) (map[string]string, error) {
var fieldKey string var fieldKey string
var fieldVal string var fieldVal string
firstSpace := strings.Index(field, " ") before, after, ok := strings.Cut(field, " ")
if firstSpace > 0 { if ok {
fieldKey = field[:firstSpace] fieldKey = before
fieldVal = field[firstSpace+1:] fieldVal = after
} else { } else {
// could be the case if the requested field had no value // could be the case if the requested field had no value
fieldKey = field fieldKey = field

View File

@@ -27,15 +27,15 @@ func parseLsTreeLine(line []byte) (*LsTreeEntry, error) {
// <mode> <type> <sha>\t<filename> // <mode> <type> <sha>\t<filename>
var err error var err error
posTab := bytes.IndexByte(line, '\t') before, after, ok := bytes.Cut(line, []byte{'\t'})
if posTab == -1 { if !ok {
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line) return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
} }
entry := new(LsTreeEntry) entry := new(LsTreeEntry)
entryAttrs := line[:posTab] entryAttrs := before
entryName := line[posTab+1:] entryName := after
entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace) entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
_ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type _ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type

View File

@@ -77,8 +77,8 @@ func Code(fileName, language, code string) (output template.HTML, lexerName stri
if lexer == nil { if lexer == nil {
// Attempt stripping off the '?' // Attempt stripping off the '?'
if idx := strings.IndexByte(language, '?'); idx > 0 { if before, _, ok := strings.Cut(language, "?"); ok {
lexer = lexers.Get(language[:idx]) lexer = lexers.Get(before)
} }
} }
} }

View File

@@ -17,20 +17,20 @@ func FilenameIndexerID(repoID int64, filename string) string {
} }
func ParseIndexerID(indexerID string) (int64, string) { func ParseIndexerID(indexerID string) (int64, string) {
index := strings.IndexByte(indexerID, '_') before, after, ok := strings.Cut(indexerID, "_")
if index == -1 { if !ok {
log.Error("Unexpected ID in repo indexer: %s", indexerID) log.Error("Unexpected ID in repo indexer: %s", indexerID)
} }
repoID, _ := internal.ParseBase36(indexerID[:index]) repoID, _ := internal.ParseBase36(before)
return repoID, indexerID[index+1:] return repoID, after
} }
func FilenameOfIndexerID(indexerID string) string { func FilenameOfIndexerID(indexerID string) string {
index := strings.IndexByte(indexerID, '_') _, after, ok := strings.Cut(indexerID, "_")
if index == -1 { if !ok {
log.Error("Unexpected ID in repo indexer: %s", indexerID) log.Error("Unexpected ID in repo indexer: %s", indexerID)
} }
return indexerID[index+1:] return after
} }
// FilenameMatchIndexPos returns the boundaries of its first seven lines. // FilenameMatchIndexPos returns the boundaries of its first seven lines.

View File

@@ -33,7 +33,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
// Of text and link contents // Of text and link contents
sl := strings.SplitSeq(content, "|") sl := strings.SplitSeq(content, "|")
for v := range sl { for v := range sl {
if equalPos := strings.IndexByte(v, '='); equalPos == -1 { if found := strings.Contains(v, "="); !found {
// There is no equal in this argument; this is a mandatory arg // There is no equal in this argument; this is a mandatory arg
if props["name"] == "" { if props["name"] == "" {
if IsFullURLString(v) { if IsFullURLString(v) {
@@ -55,8 +55,8 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
} else { } else {
// There is an equal; optional argument. // There is an equal; optional argument.
sep := strings.IndexByte(v, '=') before, after, _ := strings.Cut(v, "=")
key, val := v[:sep], html.UnescapeString(v[sep+1:]) key, val := before, html.UnescapeString(after)
// When parsing HTML, x/net/html will change all quotes which are // When parsing HTML, x/net/html will change all quotes which are
// not used for syntax into UTF-8 quotes. So checking val[0] won't // not used for syntax into UTF-8 quotes. So checking val[0] won't

View File

@@ -51,10 +51,10 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
for _, unescapeIdx := range escapeStringIndices { for _, unescapeIdx := range escapeStringIndices {
preceding := encoded[last:unescapeIdx[0]] preceding := encoded[last:unescapeIdx[0]]
if !inKey { if !inKey {
if splitter := strings.Index(preceding, "__"); splitter > -1 { if before, after, cutOk := strings.Cut(preceding, "__"); cutOk {
section += preceding[:splitter] section += before
inKey = true inKey = true
key += preceding[splitter+2:] key += after
} else { } else {
section += preceding section += preceding
} }
@@ -77,9 +77,9 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
} }
remaining := encoded[last:] remaining := encoded[last:]
if !inKey { if !inKey {
if splitter := strings.Index(remaining, "__"); splitter > -1 { if before, after, cutOk := strings.Cut(remaining, "__"); cutOk {
section += remaining[:splitter] section += before
key += remaining[splitter+2:] key += after
} else { } else {
section += remaining section += remaining
} }
@@ -111,21 +111,21 @@ func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, sect
func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) { func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
for _, kv := range envs { for _, kv := range envs {
idx := strings.IndexByte(kv, '=') before, after, ok := strings.Cut(kv, "=")
if idx < 0 { if !ok {
continue continue
} }
// parse the environment variable to config section name and key name // parse the environment variable to config section name and key name
envKey := kv[:idx] envKey := before
envValue := kv[idx+1:] envValue := after
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(EnvConfigKeyPrefixGitea, EnvConfigKeySuffixFile, envKey) ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(EnvConfigKeyPrefixGitea, EnvConfigKeySuffixFile, envKey)
if !ok { if !ok {
continue continue
} }
// use environment value as config value, or read the file content as value if the key indicates a file // use environment value as config value, or read the file content as value if the key indicates a file
keyValue := envValue keyValue := envValue //nolint:staticcheck // false positive
if useFileValue { if useFileValue {
fileContent, err := os.ReadFile(envValue) fileContent, err := os.ReadFile(envValue)
if err != nil { if err != nil {

View File

@@ -215,8 +215,8 @@ func addValidGroupTeamMapRule() {
} }
func portOnly(hostport string) string { func portOnly(hostport string) string {
colon := strings.IndexByte(hostport, ':') _, after, ok := strings.Cut(hostport, ":")
if colon == -1 { if !ok {
return "" return ""
} }
if i := strings.Index(hostport, "]:"); i != -1 { if i := strings.Index(hostport, "]:"); i != -1 {
@@ -225,7 +225,7 @@ func portOnly(hostport string) string {
if strings.Contains(hostport, "]") { if strings.Contains(hostport, "]") {
return "" return ""
} }
return hostport[colon+len(":"):] return after
} }
func validPort(p string) bool { func validPort(p string) bool {

View File

@@ -35,9 +35,9 @@ func (source *Source) Authenticate(ctx context.Context, user *user_model.User, u
// Allow PAM sources with `@` in their name, like from Active Directory // Allow PAM sources with `@` in their name, like from Active Directory
username := pamLogin username := pamLogin
email := pamLogin email := pamLogin
idx := strings.Index(pamLogin, "@") before, _, ok := strings.Cut(pamLogin, "@")
if idx > -1 { if ok {
username = pamLogin[:idx] username = before
} }
if user_model.ValidateEmail(email) != nil { if user_model.ValidateEmail(email) != nil {
if source.EmailDomain != "" { if source.EmailDomain != "" {

View File

@@ -21,10 +21,10 @@ import (
func (source *Source) Authenticate(ctx context.Context, user *user_model.User, userName, password string) (*user_model.User, error) { func (source *Source) Authenticate(ctx context.Context, user *user_model.User, userName, password string) (*user_model.User, error) {
// Verify allowed domains. // Verify allowed domains.
if len(source.AllowedDomains) > 0 { if len(source.AllowedDomains) > 0 {
idx := strings.Index(userName, "@") _, after, ok := strings.Cut(userName, "@")
if idx == -1 { if !ok {
return nil, user_model.ErrUserNotExist{Name: userName} return nil, user_model.ErrUserNotExist{Name: userName}
} else if !util.SliceContainsString(strings.Split(source.AllowedDomains, ","), userName[idx+1:], true) { } else if !util.SliceContainsString(strings.Split(source.AllowedDomains, ","), after, true) {
return nil, user_model.ErrUserNotExist{Name: userName} return nil, user_model.ErrUserNotExist{Name: userName}
} }
} }
@@ -61,9 +61,9 @@ func (source *Source) Authenticate(ctx context.Context, user *user_model.User, u
} }
username := userName username := userName
idx := strings.Index(userName, "@") before, _, ok := strings.Cut(userName, "@")
if idx > -1 { if ok {
username = userName[:idx] username = before
} }
user = &user_model.User{ user = &user_model.User{

View File

@@ -23,9 +23,6 @@ func TestOneDevDownloadRepo(t *testing.T) {
u, _ := url.Parse("https://code.onedev.io") u, _ := url.Parse("https://code.onedev.io")
ctx := t.Context() ctx := t.Context()
downloader := NewOneDevDownloader(ctx, u, "", "", "go-gitea-test_repo") downloader := NewOneDevDownloader(ctx, u, "", "", "go-gitea-test_repo")
if err != nil {
t.Fatalf("NewOneDevDownloader is nil: %v", err)
}
repo, err := downloader.GetRepoInfo(ctx) repo, err := downloader.GetRepoInfo(ctx)
assert.NoError(t, err) assert.NoError(t, err)
assertRepositoryEqual(t, &base.Repository{ assertRepositoryEqual(t, &base.Repository{

View File

@@ -238,8 +238,8 @@ func TestCommitStringParsing(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.testName, func(t *testing.T) { t.Run(test.testName, func(t *testing.T) {
testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage) testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
idx := strings.Index(testString, "DATA:") _, after, _ := strings.Cut(testString, "DATA:")
commit, err := NewCommit(0, 0, []byte(testString[idx+5:])) commit, err := NewCommit(0, 0, []byte(after))
if err != nil && test.shouldPass { if err != nil && test.shouldPass {
t.Errorf("Could not parse %s", testString) t.Errorf("Could not parse %s", testString)
return return

View File

@@ -44,11 +44,11 @@ func (parser *Parser) Reset() {
// AddLineToGraph adds the line as a row to the graph // AddLineToGraph adds the line as a row to the graph
func (parser *Parser) AddLineToGraph(graph *Graph, row int, line []byte) error { func (parser *Parser) AddLineToGraph(graph *Graph, row int, line []byte) error {
idx := bytes.Index(line, []byte("DATA:")) before, after, ok := bytes.Cut(line, []byte("DATA:"))
if idx < 0 { if !ok {
parser.ParseGlyphs(line) parser.ParseGlyphs(line)
} else { } else {
parser.ParseGlyphs(line[:idx]) parser.ParseGlyphs(before)
} }
var err error var err error
@@ -72,7 +72,7 @@ func (parser *Parser) AddLineToGraph(graph *Graph, row int, line []byte) error {
} }
} }
commitDone = true commitDone = true
if idx < 0 { if !ok {
if err != nil { if err != nil {
err = fmt.Errorf("missing data section on line %d with commit: %s. %w", row, string(line), err) err = fmt.Errorf("missing data section on line %d with commit: %s. %w", row, string(line), err)
} else { } else {
@@ -80,7 +80,7 @@ func (parser *Parser) AddLineToGraph(graph *Graph, row int, line []byte) error {
} }
continue continue
} }
err2 := graph.AddCommit(row, column, flowID, line[idx+5:]) err2 := graph.AddCommit(row, column, flowID, after)
if err != nil && err2 != nil { if err != nil && err2 != nil {
err = fmt.Errorf("%v %w", err2, err) err = fmt.Errorf("%v %w", err2, err)
continue continue