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
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)

View file

@ -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
}