2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pipeline
|
2019-04-06 19:32:14 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2024-11-26 10:50:48 +00:00
|
|
|
"time"
|
2024-07-17 23:26:35 +00:00
|
|
|
|
|
|
|
"github.com/urfave/cli/v3"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/common"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
|
2024-12-01 19:39:47 +00:00
|
|
|
shared_utils "go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2024-05-02 06:43:46 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
2024-11-26 10:50:48 +00:00
|
|
|
//nolint:mnd
|
2024-11-18 18:40:45 +00:00
|
|
|
func buildPipelineListCmd() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "ls",
|
|
|
|
Usage: "show pipeline history",
|
|
|
|
ArgsUsage: "<repo-id|repo-full-name>",
|
|
|
|
Action: List,
|
|
|
|
Flags: append(common.OutputFlags("table"), []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "branch",
|
|
|
|
Usage: "branch filter",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "event",
|
|
|
|
Usage: "event filter",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "status filter",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "limit",
|
|
|
|
Usage: "limit the list size",
|
|
|
|
Value: 25,
|
|
|
|
},
|
2024-11-26 10:50:48 +00:00
|
|
|
&cli.TimestampFlag{
|
|
|
|
Name: "before",
|
2024-11-30 14:23:44 +00:00
|
|
|
Usage: "only return pipelines before this date (RFC3339)",
|
2024-11-26 10:50:48 +00:00
|
|
|
Config: cli.TimestampConfig{
|
|
|
|
Layouts: []string{
|
|
|
|
time.RFC3339,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&cli.TimestampFlag{
|
|
|
|
Name: "after",
|
2024-11-30 14:23:44 +00:00
|
|
|
Usage: "only return pipelines after this date (RFC3339)",
|
2024-11-26 10:50:48 +00:00
|
|
|
Config: cli.TimestampConfig{
|
|
|
|
Layouts: []string{
|
|
|
|
time.RFC3339,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:40:45 +00:00
|
|
|
}...),
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
func List(ctx context.Context, c *cli.Command) error {
|
|
|
|
client, err := internal.NewClient(ctx, c)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-01 19:39:47 +00:00
|
|
|
pipelines, err := pipelineList(c, client)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-01 19:39:47 +00:00
|
|
|
return pipelineOutput(c, pipelines)
|
2024-05-02 06:43:46 +00:00
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
|
2024-12-01 19:39:47 +00:00
|
|
|
func pipelineList(c *cli.Command, client woodpecker.Client) ([]*woodpecker.Pipeline, error) {
|
2024-05-02 06:43:46 +00:00
|
|
|
repoIDOrFullName := c.Args().First()
|
|
|
|
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
2024-12-01 19:39:47 +00:00
|
|
|
return nil, err
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 10:50:48 +00:00
|
|
|
opt := woodpecker.PipelineListOptions{}
|
|
|
|
|
2024-12-01 19:39:47 +00:00
|
|
|
if before := c.Timestamp("before"); !before.IsZero() {
|
2024-11-26 10:50:48 +00:00
|
|
|
opt.Before = before
|
|
|
|
}
|
2024-12-01 19:39:47 +00:00
|
|
|
if after := c.Timestamp("after"); !after.IsZero() {
|
2024-11-26 10:50:48 +00:00
|
|
|
opt.After = after
|
|
|
|
}
|
|
|
|
|
2019-04-06 19:32:14 +00:00
|
|
|
branch := c.String("branch")
|
|
|
|
event := c.String("event")
|
|
|
|
status := c.String("status")
|
2024-07-17 23:26:35 +00:00
|
|
|
limit := int(c.Int("limit"))
|
2019-04-06 19:32:14 +00:00
|
|
|
|
2024-12-01 19:39:47 +00:00
|
|
|
pipelines, err := shared_utils.Paginate(func(page int) ([]*woodpecker.Pipeline, error) {
|
|
|
|
return client.PipelineList(repoID,
|
|
|
|
woodpecker.PipelineListOptions{
|
|
|
|
ListOptions: woodpecker.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
},
|
|
|
|
Before: opt.Before,
|
|
|
|
After: opt.After,
|
|
|
|
Branch: branch,
|
|
|
|
Events: []string{event},
|
|
|
|
Status: status,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2024-12-01 19:39:47 +00:00
|
|
|
return pipelines, nil
|
2024-05-02 06:43:46 +00:00
|
|
|
}
|