Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pipeline purge cli command #4569

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cli/pipeline/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions cli/pipeline/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
Expand All @@ -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,
},
Expand All @@ -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
}
Expand Down