Fix pipeline purge cli command (#4569)

This commit is contained in:
Robert Kaussow 2024-12-15 07:20:08 +01:00 committed by GitHub
parent 0870873f84
commit ff2469ec5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 13 deletions

View file

@ -96,13 +96,13 @@ func pipelinePurge(c *cli.Command, client woodpecker.Client) (err error) {
// Create a map of pipeline IDs to keep // Create a map of pipeline IDs to keep
keepMap := make(map[int64]struct{}) keepMap := make(map[int64]struct{})
for _, p := range pipelinesKeep { for _, p := range pipelinesKeep {
keepMap[p.ID] = struct{}{} keepMap[p.Number] = struct{}{}
} }
// Filter pipelines to only include those not in keepMap // Filter pipelines to only include those not in keepMap
var pipelinesToPurge []*woodpecker.Pipeline var pipelinesToPurge []*woodpecker.Pipeline
for _, p := range pipelines { for _, p := range pipelines {
if _, exists := keepMap[p.ID]; !exists { if _, exists := keepMap[p.Number]; !exists {
pipelinesToPurge = append(pipelinesToPurge, p) pipelinesToPurge = append(pipelinesToPurge, p)
} }
} }
@ -114,12 +114,12 @@ func pipelinePurge(c *cli.Command, client woodpecker.Client) (err error) {
for i, p := range pipelinesToPurge { for i, p := range pipelinesToPurge {
// cspell:words spurge // 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 { if dryRun {
continue continue
} }
err := client.PipelineDelete(repoID, p.ID) err := client.PipelineDelete(repoID, p.Number)
if err != nil { if err != nil {
return err return err
} }
@ -150,7 +150,7 @@ func fetchPipelines(client woodpecker.Client, repoID int64, duration time.Durati
ListOptions: woodpecker.ListOptions{ ListOptions: woodpecker.ListOptions{
Page: page, Page: page,
}, },
After: time.Now().Add(-duration), Before: time.Now().Add(-duration),
}, },
) )
}, -1) }, -1)

View file

@ -29,7 +29,7 @@ func TestPipelinePurge(t *testing.T) {
repoID: 1, repoID: 1,
args: []string{"purge", "--older-than", "1h", "repo/name"}, args: []string{"purge", "--older-than", "1h", "repo/name"},
pipelinesKeep: []*woodpecker.Pipeline{ pipelinesKeep: []*woodpecker.Pipeline{
{ID: 1}, {Number: 1},
}, },
pipelines: []*woodpecker.Pipeline{}, pipelines: []*woodpecker.Pipeline{},
}, },
@ -38,12 +38,12 @@ func TestPipelinePurge(t *testing.T) {
repoID: 1, repoID: 1,
args: []string{"purge", "--older-than", "1h", "repo/name"}, args: []string{"purge", "--older-than", "1h", "repo/name"},
pipelinesKeep: []*woodpecker.Pipeline{ pipelinesKeep: []*woodpecker.Pipeline{
{ID: 1}, {Number: 1},
}, },
pipelines: []*woodpecker.Pipeline{ pipelines: []*woodpecker.Pipeline{
{ID: 1}, {Number: 1},
{ID: 2}, {Number: 2},
{ID: 3}, {Number: 3},
}, },
wantDelete: 2, 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) { mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(func(_ int64, opt woodpecker.PipelineListOptions) ([]*woodpecker.Pipeline, error) {
// Return keep pipelines for first call // Return keep pipelines for first call
if opt.After.IsZero() { if opt.Before.IsZero() {
if opt.Page == 1 { if opt.Page == 1 {
return tt.pipelinesKeep, nil return tt.pipelinesKeep, nil
} }
return []*woodpecker.Pipeline{}, nil return []*woodpecker.Pipeline{}, nil
} }
// Return pipelines to purge for calls with After filter // Return pipelines to purge for calls with Before filter
if !opt.After.IsZero() { if !opt.Before.IsZero() {
if opt.Page == 1 { if opt.Page == 1 {
return tt.pipelines, nil return tt.pipelines, nil
} }