From ff2469ec5b368c1f844c837fa1055e61573b3ad0 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 15 Dec 2024 07:20:08 +0100 Subject: [PATCH] Fix pipeline purge cli command (#4569) --- cli/pipeline/purge.go | 10 +++++----- cli/pipeline/purge_test.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cli/pipeline/purge.go b/cli/pipeline/purge.go index a9c5e39d1..c71b6ee79 100644 --- a/cli/pipeline/purge.go +++ b/cli/pipeline/purge.go @@ -96,13 +96,13 @@ func pipelinePurge(c *cli.Command, client woodpecker.Client) (err error) { // Create a map of pipeline IDs to keep keepMap := make(map[int64]struct{}) for _, p := range pipelinesKeep { - keepMap[p.ID] = struct{}{} + keepMap[p.Number] = struct{}{} } // Filter pipelines to only include those not in keepMap var pipelinesToPurge []*woodpecker.Pipeline for _, p := range pipelines { - if _, exists := keepMap[p.ID]; !exists { + if _, exists := keepMap[p.Number]; !exists { pipelinesToPurge = append(pipelinesToPurge, p) } } @@ -114,12 +114,12 @@ func pipelinePurge(c *cli.Command, client woodpecker.Client) (err error) { for i, p := range pipelinesToPurge { // cspell:words spurge - log.Debug().Msgf("%spurge %v/%v pipelines from repo '%v'", msgPrefix, i+1, len(pipelinesToPurge), repoIDOrFullName) + log.Debug().Msgf("%spurge %v/%v pipelines from repo '%v' (pipeline %v)", msgPrefix, i+1, len(pipelinesToPurge), repoIDOrFullName, p.Number) if dryRun { continue } - err := client.PipelineDelete(repoID, p.ID) + err := client.PipelineDelete(repoID, p.Number) if err != nil { return err } @@ -150,7 +150,7 @@ func fetchPipelines(client woodpecker.Client, repoID int64, duration time.Durati ListOptions: woodpecker.ListOptions{ Page: page, }, - After: time.Now().Add(-duration), + Before: time.Now().Add(-duration), }, ) }, -1) diff --git a/cli/pipeline/purge_test.go b/cli/pipeline/purge_test.go index be185277b..12356dd0b 100644 --- a/cli/pipeline/purge_test.go +++ b/cli/pipeline/purge_test.go @@ -29,7 +29,7 @@ func TestPipelinePurge(t *testing.T) { repoID: 1, args: []string{"purge", "--older-than", "1h", "repo/name"}, pipelinesKeep: []*woodpecker.Pipeline{ - {ID: 1}, + {Number: 1}, }, pipelines: []*woodpecker.Pipeline{}, }, @@ -38,12 +38,12 @@ func TestPipelinePurge(t *testing.T) { repoID: 1, args: []string{"purge", "--older-than", "1h", "repo/name"}, pipelinesKeep: []*woodpecker.Pipeline{ - {ID: 1}, + {Number: 1}, }, pipelines: []*woodpecker.Pipeline{ - {ID: 1}, - {ID: 2}, - {ID: 3}, + {Number: 1}, + {Number: 2}, + {Number: 3}, }, wantDelete: 2, }, @@ -62,15 +62,15 @@ func TestPipelinePurge(t *testing.T) { mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(func(_ int64, opt woodpecker.PipelineListOptions) ([]*woodpecker.Pipeline, error) { // Return keep pipelines for first call - if opt.After.IsZero() { + if opt.Before.IsZero() { if opt.Page == 1 { return tt.pipelinesKeep, nil } return []*woodpecker.Pipeline{}, nil } - // Return pipelines to purge for calls with After filter - if !opt.After.IsZero() { + // Return pipelines to purge for calls with Before filter + if !opt.Before.IsZero() { if opt.Page == 1 { return tt.pipelines, nil }