-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins.go
76 lines (60 loc) · 1.81 KB
/
jenkins.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
package main
import (
"time"
"github.com/benmatselby/precis/jenkins"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func getJenkins() *widgets.Table {
client := jenkins.New(jenkinsURL, jenkinsUsername, jenkinsPassword)
jobs, err := client.GetJobs(jenkinsView)
if err != nil {
return renderError("Jenkins", err.Error())
}
rows := [][]string{
{"build", "state", "finished"},
}
sadRows := []int{}
happyRows := []int{}
buildRows := []int{}
waitRows := []int{}
for _, job := range jobs {
if job.LastBuild.Result == "" && job.LastBuild.Timestamp != 0 {
job.LastBuild.Result = "RUNNING"
}
if job.LastBuild.Result == "" && job.LastBuild.Timestamp == 0 {
job.LastBuild.Result = "WAITING"
}
finishedAt := time.Unix(0, int64(time.Millisecond)*job.LastBuild.Timestamp).Format("02-01-2006 15:04")
rows = append(rows, []string{job.DisplayName, job.LastBuild.Result, finishedAt})
if job.LastBuild.Result == "FAILURE" {
sadRows = append(sadRows, len(rows)-1)
} else if job.LastBuild.Result == "RUNNING" {
buildRows = append(buildRows, len(rows)-1)
} else if job.LastBuild.Result == "WAITING" {
waitRows = append(waitRows, len(rows)-1)
} else {
happyRows = append(happyRows, len(rows)-1)
}
}
w := widgets.NewTable()
w.Rows = rows
w.TextStyle = ui.NewStyle(ui.ColorWhite)
w.TextAlignment = ui.AlignLeft
w.Border = true
w.Title = "Jenkins Builds - " + jenkinsURL
w.TitleStyle = ui.Style{Fg: ui.ColorGreen}
for _, line := range sadRows {
w.RowStyles[line] = ui.Style{Fg: ui.ColorRed}
}
for _, line := range buildRows {
w.RowStyles[line] = ui.Style{Fg: ui.ColorYellow}
}
for _, line := range waitRows {
w.RowStyles[line] = ui.Style{Fg: ui.ColorCyan}
}
for _, line := range happyRows {
w.RowStyles[line] = ui.Style{Fg: ui.ColorGreen}
}
return w
}