Skip to content

Commit 652fcf1

Browse files
committed
reporter: add build failure handling and output collection
1 parent 172ae16 commit 652fcf1

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

cmd/go-ctrf-json-reporter/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
)
1010

11+
var buildFailed bool
12+
1113
func main() {
1214
var outputFile string
1315
var verbose bool
@@ -68,4 +70,27 @@ func main() {
6870
_, _ = fmt.Fprintf(os.Stderr, "Error writing the report to file: %v\n", err)
6971
os.Exit(1)
7072
}
73+
74+
if !verbose {
75+
buildOutput := reporter.GetBuildOutput()
76+
fmt.Println(buildOutput)
77+
}
78+
79+
if report.Results.Extra != nil {
80+
extraMap := report.Results.Extra.(map[string]interface{})
81+
if _, ok := extraMap["buildFail"]; ok {
82+
buildFailed = true
83+
}
84+
if _, ok := extraMap["FailedBuild"]; ok {
85+
buildFailed = true
86+
}
87+
}
88+
89+
if report.Results.Summary.Failed > 0 {
90+
buildFailed = true
91+
}
92+
93+
if buildFailed {
94+
os.Exit(1)
95+
}
7196
}

reporter/reporter.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type TestEvent struct {
2323
Output string
2424
}
2525

26+
var buildOutput []string
27+
2628
func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.Report, error) {
2729
var testEvents []TestEvent
2830
decoder := json.NewDecoder(r)
@@ -47,6 +49,43 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
4749
}
4850
fmt.Println(string(jsonEvent))
4951
}
52+
53+
if event.Action == "build-output" || event.Action == "build-fail" || event.Action == "fail" {
54+
if report.Results.Extra == nil {
55+
report.Results.Extra = make(map[string]interface{})
56+
}
57+
extraMap := report.Results.Extra.(map[string]interface{})
58+
59+
if event.Action == "fail" {
60+
if _, ok := extraMap["FailedBuild"]; !ok {
61+
extraMap["FailedBuild"] = true
62+
}
63+
}
64+
65+
if event.Action == "build-output" {
66+
if _, ok := extraMap["buildOutput"]; !ok {
67+
extraMap["buildOutput"] = []TestEvent{}
68+
}
69+
buildOutputEvents := extraMap["buildOutput"].([]TestEvent)
70+
extraMap["buildOutput"] = append(buildOutputEvents, event)
71+
buildOutput = append(buildOutput, event.Output)
72+
continue
73+
}
74+
75+
if event.Action == "build-fail" {
76+
if _, ok := extraMap["buildFail"]; !ok {
77+
extraMap["buildFail"] = []TestEvent{}
78+
}
79+
buildFailEvents := extraMap["buildFail"].([]TestEvent)
80+
extraMap["buildFail"] = append(buildFailEvents, event)
81+
break
82+
}
83+
}
84+
85+
if event.Action == "output" {
86+
buildOutput = append(buildOutput, event.Output)
87+
}
88+
5089
if event.Test == "" {
5190
continue
5291
}
@@ -162,6 +201,10 @@ func WriteReportToFile(filename string, report *ctrf.Report) error {
162201
return nil
163202
}
164203

204+
func GetBuildOutput() string {
205+
return strings.Join(buildOutput, "")
206+
}
207+
165208
func secondsToMillis(seconds float64) int64 {
166209
return int64(seconds * 1000)
167210
}

0 commit comments

Comments
 (0)