refactor: use slices.Contains to simplify (#5468)

Signed-off-by: zhedazijingang <unwrap_or_else@outlook.com>
This commit is contained in:
zhedazijingang 2025-08-28 16:25:39 +08:00 committed by GitHub
parent 1bee41ddea
commit 2349ee1eef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 30 deletions

View File

@ -16,6 +16,7 @@ package model
import ( import (
"fmt" "fmt"
"slices"
"strings" "strings"
"go.woodpecker-ci.org/woodpecker/v3/pipeline" "go.woodpecker-ci.org/woodpecker/v3/pipeline"
@ -83,12 +84,7 @@ func (t *Task) ShouldRun() bool {
} }
func (t *Task) runsOnFailure() bool { func (t *Task) runsOnFailure() bool {
for _, status := range t.RunOn { return slices.Contains(t.RunOn, string(StatusFailure))
if status == string(StatusFailure) {
return true
}
}
return false
} }
func (t *Task) runsOnSuccess() bool { func (t *Task) runsOnSuccess() bool {
@ -96,10 +92,5 @@ func (t *Task) runsOnSuccess() bool {
return true return true
} }
for _, status := range t.RunOn { return slices.Contains(t.RunOn, string(StatusSuccess))
if status == string(StatusSuccess) {
return true
}
}
return false
} }

View File

@ -17,6 +17,7 @@ package pipeline
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -108,13 +109,7 @@ func cancelPreviousPipelines(
user *model.User, user *model.User,
) error { ) error {
// check this event should cancel previous pipelines // check this event should cancel previous pipelines
eventIncluded := false eventIncluded := slices.Contains(repo.CancelPreviousPipelineEvents, pipeline.Event)
for _, ev := range repo.CancelPreviousPipelineEvents {
if ev == pipeline.Event {
eventIncluded = true
break
}
}
if !eventIncluded { if !eventIncluded {
return nil return nil
} }

View File

@ -18,6 +18,7 @@ import (
"container/list" "container/list"
"context" "context"
"fmt" "fmt"
"slices"
"sync" "sync"
"time" "time"
@ -362,10 +363,8 @@ func (q *fifo) depsInQueue(task *model.Task) bool {
} }
for possibleDepID := range q.running { for possibleDepID := range q.running {
log.Debug().Msgf("queue: running right now: %v", possibleDepID) log.Debug().Msgf("queue: running right now: %v", possibleDepID)
for _, dep := range task.Dependencies { if slices.Contains(task.Dependencies, possibleDepID) {
if possibleDepID == dep { return true
return true
}
} }
} }
return false return false

View File

@ -17,6 +17,7 @@ package token
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"slices"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -55,13 +56,7 @@ func Parse(allowedTypes []Type, raw string, fn SecretFunc) (*Token, error) {
return nil, jwt.ErrTokenUnverifiable return nil, jwt.ErrTokenUnverifiable
} }
hasAllowedType := false hasAllowedType := slices.Contains(allowedTypes, token.Type)
for _, k := range allowedTypes {
if k == token.Type {
hasAllowedType = true
break
}
}
if !hasAllowedType { if !hasAllowedType {
return nil, jwt.ErrInvalidType return nil, jwt.ErrInvalidType