-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
91 lines (81 loc) · 2.4 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package list
import (
"fmt"
"strings"
"github.com/craftamap/bb/util/logging"
"github.com/craftamap/bb/cmd/options"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)
const (
SUCCESSFUL = "SUCCESSFUL"
FAILED = "FAILED"
STOPPED = "STOPPED"
RUNNING = "RUNNING"
)
func Add(pipelinesCmd *cobra.Command, globalOpts *options.GlobalOptions) {
listCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List pipeline executions this repository",
Long: "List pipeline executions this repository",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo
pipelines, err := c.PipelineList(bbrepo.RepoOrga, bbrepo.RepoSlug)
if err != nil {
logging.Error(err)
return
}
if len(*pipelines) == 0 {
fmt.Println("No builds/statuses found for this pull request")
return
}
hashToDescription := map[string]string{}
for _, pipeline := range *pipelines {
revHash := pipeline.Target.(map[string]interface{})["commit"].(map[string]interface{})["hash"].(string)
commitDescription := ""
commit, err := c.GetCommit(bbrepo.RepoOrga, bbrepo.RepoSlug, revHash)
if err == nil {
commitDescription = strings.Split(commit.Message, "\n")[0]
}
hashToDescription[revHash] = commitDescription
}
for _, pipeline := range *pipelines {
var statusIcon string
switch pipeline.PipelineState.Result.Name {
case SUCCESSFUL:
statusIcon = aurora.Green("✓").String()
case FAILED, STOPPED:
statusIcon = aurora.Red("X").String()
}
switch pipeline.PipelineState.Stage.Name {
case RUNNING:
statusIcon = aurora.Yellow("⏱️").String()
}
revHash := pipeline.Target.(map[string]interface{})["commit"].(map[string]interface{})["hash"].(string)
description := hashToDescription[revHash]
revHashShort := revHash[:7]
branchName := pipeline.Target.(map[string]interface{})["ref_name"].(string)
fmt.Printf(
"%s #%d %s (%ds) %s\n",
statusIcon,
aurora.Index(242, pipeline.BuildNumber),
description,
pipeline.BuildSecondsUsed,
aurora.Index(242,
fmt.Sprintf("(%s, %s, %s)",
pipeline.Creator.DisplayName,
revHashShort,
branchName,
)),
)
}
},
}
pipelinesCmd.AddCommand(listCmd)
}