This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
jest-action.go
170 lines (145 loc) · 3.98 KB
/
jest-action.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
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/google/go-github/v24/github"
"github.com/ldez/ghactions"
)
type Report struct {
NumFailedTests int
NumPassedTests int
NumTotalTests int
NumFailedTestSuites int
NumPassedTestSuites int
NumTotalTestSuites int
Success bool
TestResults []*TestResult
}
type TestResult struct {
AssertionResults []*AssertionResult
Message string
FilePath string `json:"name"`
Status string
Summary string
}
type AssertionResult struct {
AncestorTitles []string
FailureMessages []string
FullName string
Location Location
Status string
Title string
}
type Location struct {
Column int
Line int
}
func main() {
var report Report
err := json.NewDecoder(os.Stdin).Decode(&report)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
action := ghactions.NewAction(ctx)
action.OnPush(func(client *github.Client, event *github.PushEvent) error {
return handlePush(ctx, client, event, report)
})
if err := action.Run(); err != nil {
log.Fatal(err)
}
}
func handlePush(ctx context.Context, client *github.Client, event *github.PushEvent, report Report) error {
if report.Success {
return nil
}
head := os.Getenv(ghactions.GithubSha)
owner, repoName := ghactions.GetRepoInfo()
// find the action's checkrun
checkName := os.Getenv(ghactions.GithubAction)
result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repoName, head, &github.ListCheckRunsOptions{
CheckName: github.String(checkName),
Status: github.String("in_progress"),
})
if err != nil {
return err
}
if len(result.CheckRuns) == 0 {
return fmt.Errorf("Unable to find check run for action: %s", checkName)
}
checkRun := result.CheckRuns[0]
// add annotations for test failures
workspacePath := os.Getenv(ghactions.GithubWorkspace) + "/"
var annotations []*github.CheckRunAnnotation
for _, t := range report.TestResults {
if t.Status == "passed" {
continue
}
path := strings.TrimPrefix(t.FilePath, workspacePath)
if len(t.AssertionResults) > 0 {
for _, a := range t.AssertionResults {
if a.Status == "passed" {
continue
}
if len(a.FailureMessages) == 0 {
a.FailureMessages = append(a.FailureMessages, a.FullName)
}
annotations = append(annotations, &github.CheckRunAnnotation{
Path: github.String(path),
StartLine: github.Int(a.Location.Line),
EndLine: github.Int(a.Location.Line),
AnnotationLevel: github.String("failure"),
Title: github.String(a.FullName),
Message: github.String(strings.Join(a.FailureMessages, "\n\n")),
})
}
} else {
// usually the case for failed test suites
annotations = append(annotations, &github.CheckRunAnnotation{
Path: github.String(path),
StartLine: github.Int(1),
EndLine: github.Int(1),
AnnotationLevel: github.String("failure"),
Title: github.String("Test Suite Error"),
Message: github.String(t.Message),
})
}
}
summary := fmt.Sprintf(
"Test Suites: %d failed, %d passed, %d total\n",
report.NumFailedTests,
report.NumPassedTests,
report.NumTotalTests,
)
summary += fmt.Sprintf(
"Tests: %d failed, %d passed, %d total",
report.NumFailedTestSuites,
report.NumPassedTestSuites,
report.NumTotalTestSuites,
)
// add annotations in #50 chunks
for i := 0; i < len(annotations); i += 50 {
end := i + 50
if end > len(annotations) {
end = len(annotations)
}
output := &github.CheckRunOutput{
Title: github.String("Result"),
Summary: github.String(summary),
Annotations: annotations[i:end],
}
_, _, err = client.Checks.UpdateCheckRun(ctx, owner, repoName, checkRun.GetID(), github.UpdateCheckRunOptions{
Name: checkName,
HeadSHA: github.String(head),
Output: output,
})
if err != nil {
return err
}
}
return fmt.Errorf(summary)
}