2019-04-06 19:32:14 +00:00
|
|
|
package build
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
var buildListCmd = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "ls",
|
|
|
|
Usage: "show build history",
|
|
|
|
ArgsUsage: "<repo/name>",
|
|
|
|
Action: buildList,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
|
|
|
common.FormatFlag(tmplBuildList),
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
|
|
|
func buildList(c *cli.Context) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
builds, err := client.BuildList(owner, name)
|
|
|
|
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
|
|
|
|
for _, build := range builds {
|
|
|
|
if count >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if branch != "" && build.Branch != branch {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if event != "" && build.Event != event {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != "" && build.Status != status {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := tmpl.Execute(os.Stdout, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// template for build list information
|
|
|
|
var tmplBuildList = "\x1b[33mBuild #{{ .Number }} \x1b[0m" + `
|
|
|
|
Status: {{ .Status }}
|
|
|
|
Event: {{ .Event }}
|
|
|
|
Commit: {{ .Commit }}
|
|
|
|
Branch: {{ .Branch }}
|
|
|
|
Ref: {{ .Ref }}
|
|
|
|
Author: {{ .Author }} {{ if .Email }}<{{.Email}}>{{ end }}
|
|
|
|
Message: {{ .Message }}
|
|
|
|
`
|