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 (
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-21 14:36:41 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
var pipelineListCmd = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "ls",
|
2022-10-18 01:24:12 +00:00
|
|
|
Usage: "show pipeline history",
|
2019-04-06 19:32:14 +00:00
|
|
|
ArgsUsage: "<repo/name>",
|
2022-10-18 01:24:12 +00:00
|
|
|
Action: pipelineList,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
2022-10-18 01:24:12 +00:00
|
|
|
common.FormatFlag(tmplPipelineList),
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "branch",
|
|
|
|
Usage: "branch filter",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "event",
|
|
|
|
Usage: "event filter",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "status",
|
|
|
|
Usage: "status filter",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.IntFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "limit",
|
|
|
|
Usage: "limit the list size",
|
|
|
|
Value: 25,
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func pipelineList(c *cli.Context) error {
|
2019-04-06 19:32:14 +00:00
|
|
|
repo := c.Args().First()
|
|
|
|
owner, name, err := internal.ParseRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := internal.NewClient(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelines, err := client.PipelineList(owner, name)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := c.String("branch")
|
|
|
|
event := c.String("event")
|
|
|
|
status := c.String("status")
|
|
|
|
limit := c.Int("limit")
|
|
|
|
|
|
|
|
var count int
|
2022-10-18 01:24:12 +00:00
|
|
|
for _, pipeline := range pipelines {
|
2019-04-06 19:32:14 +00:00
|
|
|
if count >= limit {
|
|
|
|
break
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if branch != "" && pipeline.Branch != branch {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if event != "" && pipeline.Event != event {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if status != "" && pipeline.Status != status {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := tmpl.Execute(os.Stdout, pipeline); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// template for pipeline list information
|
|
|
|
var tmplPipelineList = "\x1b[33mBuild #{{ .Number }} \x1b[0m" + `
|
2019-04-06 19:32:14 +00:00
|
|
|
Status: {{ .Status }}
|
|
|
|
Event: {{ .Event }}
|
|
|
|
Commit: {{ .Commit }}
|
|
|
|
Branch: {{ .Branch }}
|
|
|
|
Ref: {{ .Ref }}
|
|
|
|
Author: {{ .Author }} {{ if .Email }}<{{.Email}}>{{ end }}
|
|
|
|
Message: {{ .Message }}
|
|
|
|
`
|