-
Notifications
You must be signed in to change notification settings - Fork 16
/
summary.go
135 lines (116 loc) · 3.25 KB
/
summary.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
package github
import (
"fmt"
"os"
"time"
"github.com/saucelabs/saucectl/internal/job"
"github.com/saucelabs/saucectl/internal/report"
)
// Reporter generates Job Summaries for GitHub.
// https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
type Reporter struct {
startTime time.Time
stepSummaryFile string
results []report.TestResult
}
func NewJobSummaryReporter() Reporter {
return Reporter{
startTime: time.Now(),
stepSummaryFile: os.Getenv("GITHUB_STEP_SUMMARY"),
}
}
func (r *Reporter) isActive() bool {
return r.stepSummaryFile != ""
}
func (r *Reporter) Add(t report.TestResult) {
if !r.isActive() {
return
}
r.results = append(r.results, t)
}
func hasDevice(results []report.TestResult) bool {
for _, t := range results {
if t.DeviceName != "" {
return true
}
}
return false
}
func (r *Reporter) Render() {
if !r.isActive() {
return
}
endTime := time.Now()
hasDevices := hasDevice(r.results)
errors := 0
inProgress := 0
content := renderHeader(hasDevices)
for _, result := range r.results {
if result.Status == job.StateInProgress || result.Status == job.StateNew {
inProgress++
}
if result.Status == job.StateFailed || result.Status == job.StateError {
errors++
}
content += renderTestResult(result, hasDevices)
}
content += renderFooter(errors, inProgress, len(r.results), endTime.Sub(r.startTime))
err := os.WriteFile(r.stepSummaryFile, []byte(content), 0x644)
if err != nil {
fmt.Printf("Unable to save summary: %v\n", err)
}
}
func (r *Reporter) Reset() {
if !r.isActive() {
return
}
}
func (r *Reporter) ArtifactRequirements() []report.ArtifactType {
return []report.ArtifactType{}
}
func renderHeader(hasDevices bool) string {
deviceTitle := ""
deviceSeparator := ""
if hasDevices {
deviceTitle = " Device |"
deviceSeparator = " --- |"
}
content := fmt.Sprintf("| | Name | Duration | Status | Browser | Platform |%s\n", deviceTitle)
content += fmt.Sprintf("| --- | --- | --- | --- | --- | --- |%s\n", deviceSeparator)
return content
}
func statusToEmoji(status string) string {
switch status {
case job.StateInProgress, job.StateNew:
return ":clock10:"
case job.StatePassed, job.StateComplete:
return ":white_check_mark:"
case job.StateUnknown:
return ":interrobang:"
case job.StateError, job.StateFailed:
return ":x:"
default:
return ":warning:"
}
}
func renderTestResult(t report.TestResult, hasDevices bool) string {
content := ""
mark := statusToEmoji(t.Status)
deviceValue := ""
if hasDevices {
deviceValue = fmt.Sprintf(" %s |", t.DeviceName)
}
content += fmt.Sprintf("| %s | [%s](%s) | %.0fs | %s | %s | %s |%s\n",
mark, t.Name, t.URL, t.Duration.Seconds(), t.Status, t.Browser, t.Platform, deviceValue)
return content
}
func renderFooter(errors, inProgress, tests int, dur time.Duration) string {
if errors != 0 {
relative := float64(errors) / float64(tests) * 100
return fmt.Sprintf("\n:x: %d of %d suites have failed (%.0f%%) in %s\n\n", errors, tests, relative, dur.Truncate(1*time.Second))
}
if inProgress != 0 {
return fmt.Sprintf("\n:clock10: All suites have launched in %s\n\n", dur.Truncate(1*time.Second))
}
return fmt.Sprintf("\n:white_check_mark: All suites have passed in %s\n\n", dur.Truncate(1*time.Second))
}