-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (141 loc) · 3.3 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"log"
"net/http"
"os"
"strconv"
"time"
)
import "encoding/json"
type TravisPayload struct {
Status int `json:"status"`
StatusMessage string `json:"status_message"`
Branch string `json:"branch"`
/*Repository struct {
Name string `json:"name"`
OwnerName string `json:"ower_name"`
} `json:"repository"`*/
}
type CoverallsPayload struct {
Branch string
Coverage float64
Delta float64
//Repository string
}
var URL = os.Getenv("LAMETRIC_URL")
var TOKEN = os.Getenv("LAMETRIC_TOKEN")
var APP_ADDR = os.Getenv("APP_ADDR")
var buildChan chan *TravisPayload
var coverChan chan *CoverallsPayload
func coverageIcon(coverage float64) string {
if coverage > 90 {
return "i667" // green
} else if coverage > 80 {
return "i665" // orange
}
return "i660" // red
}
func progressLabel(delta float64) string {
if delta > 0 {
return "+" + strconv.FormatFloat(delta, 'f', 2, 64)
}
return strconv.FormatFloat(delta, 'f', 2, 64)
}
func progressIcon(delta float64) string {
if delta > 0.1 {
return "i120" // up
} else if delta < -0.1 {
return "i124" // down
}
return "i401" // equal
}
func statusIcon(status int) string {
if status == 0 {
return "i606"
}
return "i605"
}
func updateLametricApp(t *TravisPayload, c *CoverallsPayload) {
app := App{
Frames: []Frame{
Frame{
Index: 0,
Text: t.Branch + ": " + t.StatusMessage,
Icon: statusIcon(t.Status),
},
Frame{
Index: 1,
Icon: coverageIcon(c.Coverage),
GoalData: &Goal{
End: 100,
Current: int(c.Coverage + 0.5), // round
Unit: "%",
},
},
Frame{
Index: 2,
Text: progressLabel(c.Delta) + "%",
Icon: progressIcon(c.Delta),
},
},
}
if err := Push(URL, TOKEN, &app); err != nil {
log.Println("Failed to update LaMetric app:", err)
}
}
func handleTravis(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
r.ParseForm()
payload := r.Form["payload"][0]
log.Println("DEBUG: ", payload)
var p TravisPayload
err := json.Unmarshal([]byte(payload), &p)
if err != nil {
log.Println("Failed to parse Travis payload: ", err)
return
}
buildChan <- &p
}
func handleCoveralls(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
r.ParseForm()
log.Println("DEBUG: ", r.Form)
coverage, err := strconv.ParseFloat(r.Form["covered_percent"][0], 64)
if err != nil {
log.Println("Failed to get coverage", err)
return
}
delta, err := strconv.ParseFloat(r.Form["coverage_change"][0], 64)
if err != nil {
log.Println("Failed to get coverage delta", err)
return
}
//repo_name := r.Form["repo_name"][0]
branch := r.Form["branch"][0]
coverChan <- &CoverallsPayload{branch, coverage, delta}
}
func loop() {
var lastCover *CoverallsPayload
var lastBuild *TravisPayload
var timeout <-chan time.Time
for {
select {
case lastCover = <-coverChan:
timeout = time.After(4 * time.Second)
case lastBuild = <-buildChan:
timeout = time.After(4 * time.Second)
case <-timeout:
if lastBuild != nil && lastCover != nil {
updateLametricApp(lastBuild, lastCover)
}
}
}
}
func main() {
buildChan = make(chan *TravisPayload)
coverChan = make(chan *CoverallsPayload)
go loop()
http.HandleFunc("/travis", handleTravis)
http.HandleFunc("/coveralls", handleCoveralls)
log.Fatal(http.ListenAndServe(APP_ADDR, nil))
}