From 2349ee1eef5ef43a87de6f44541b82f1818f827e Mon Sep 17 00:00:00 2001 From: zhedazijingang Date: Thu, 28 Aug 2025 16:25:39 +0800 Subject: [PATCH] refactor: use slices.Contains to simplify (#5468) Signed-off-by: zhedazijingang --- server/model/task.go | 15 +++------------ server/pipeline/cancel.go | 9 ++------- server/queue/fifo.go | 7 +++---- shared/token/token.go | 9 ++------- 4 files changed, 10 insertions(+), 30 deletions(-) diff --git a/server/model/task.go b/server/model/task.go index 215305da3..a30ad6b4c 100644 --- a/server/model/task.go +++ b/server/model/task.go @@ -16,6 +16,7 @@ package model import ( "fmt" + "slices" "strings" "go.woodpecker-ci.org/woodpecker/v3/pipeline" @@ -83,12 +84,7 @@ func (t *Task) ShouldRun() bool { } func (t *Task) runsOnFailure() bool { - for _, status := range t.RunOn { - if status == string(StatusFailure) { - return true - } - } - return false + return slices.Contains(t.RunOn, string(StatusFailure)) } func (t *Task) runsOnSuccess() bool { @@ -96,10 +92,5 @@ func (t *Task) runsOnSuccess() bool { return true } - for _, status := range t.RunOn { - if status == string(StatusSuccess) { - return true - } - } - return false + return slices.Contains(t.RunOn, string(StatusSuccess)) } diff --git a/server/pipeline/cancel.go b/server/pipeline/cancel.go index e39c20010..78612562d 100644 --- a/server/pipeline/cancel.go +++ b/server/pipeline/cancel.go @@ -17,6 +17,7 @@ package pipeline import ( "context" "fmt" + "slices" "github.com/rs/zerolog/log" @@ -108,13 +109,7 @@ func cancelPreviousPipelines( user *model.User, ) error { // check this event should cancel previous pipelines - eventIncluded := false - for _, ev := range repo.CancelPreviousPipelineEvents { - if ev == pipeline.Event { - eventIncluded = true - break - } - } + eventIncluded := slices.Contains(repo.CancelPreviousPipelineEvents, pipeline.Event) if !eventIncluded { return nil } diff --git a/server/queue/fifo.go b/server/queue/fifo.go index 0977e3dae..5b951b288 100644 --- a/server/queue/fifo.go +++ b/server/queue/fifo.go @@ -18,6 +18,7 @@ import ( "container/list" "context" "fmt" + "slices" "sync" "time" @@ -362,10 +363,8 @@ func (q *fifo) depsInQueue(task *model.Task) bool { } for possibleDepID := range q.running { log.Debug().Msgf("queue: running right now: %v", possibleDepID) - for _, dep := range task.Dependencies { - if possibleDepID == dep { - return true - } + if slices.Contains(task.Dependencies, possibleDepID) { + return true } } return false diff --git a/shared/token/token.go b/shared/token/token.go index 1f3deaa12..4ecc27cb4 100644 --- a/shared/token/token.go +++ b/shared/token/token.go @@ -17,6 +17,7 @@ package token import ( "fmt" "net/http" + "slices" "github.com/golang-jwt/jwt/v5" "github.com/rs/zerolog/log" @@ -55,13 +56,7 @@ func Parse(allowedTypes []Type, raw string, fn SecretFunc) (*Token, error) { return nil, jwt.ErrTokenUnverifiable } - hasAllowedType := false - for _, k := range allowedTypes { - if k == token.Type { - hasAllowedType = true - break - } - } + hasAllowedType := slices.Contains(allowedTypes, token.Type) if !hasAllowedType { return nil, jwt.ErrInvalidType