-
Notifications
You must be signed in to change notification settings - Fork 110
/
main.go
154 lines (138 loc) · 4.42 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
// Package main is a go test analyzer that publishes results to a MongoDB database.
package main
import (
"context"
"os"
"strconv"
"time"
"github.com/edaniels/golog"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.uber.org/multierr"
"go.viam.com/utils"
"gotest.tools/gotestsum/testjson"
)
var logger = golog.NewDebugLogger("analyzetests")
func main() {
utils.ContextualMain(mainWithArgs, logger)
}
func mainWithArgs(ctx context.Context, args []string, logger golog.Logger) error {
exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
Stdout: os.Stdin,
})
if err != nil {
return err
}
mongoURI, ok := os.LookupEnv("MONGODB_TEST_OUTPUT_URI")
if !ok || mongoURI == "" {
logger.Warn("no MongoDB URI found; skipping")
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
if err != nil {
return err
}
if err := client.Connect(ctx); err != nil {
return err
}
if err := client.Ping(ctx, readpref.Primary()); err != nil {
return multierr.Combine(err, client.Disconnect(ctx))
}
defer func() {
utils.UncheckedError(client.Disconnect(ctx))
}()
gitSHA, _ := os.LookupEnv("GITHUB_X_HEAD_SHA")
repository, _ := os.LookupEnv("GITHUB_REPOSITORY")
var gitHubRunID, gitHubRunNumber, gitHubRunAttempt int64
gitHubRunIDStr, ok := os.LookupEnv("GITHUB_RUN_ID")
if ok {
var err error
gitHubRunID, err = strconv.ParseInt(gitHubRunIDStr, 10, 64)
if err != nil {
return err
}
}
gitHubRunNumberStr, ok := os.LookupEnv("GITHUB_RUN_NUMBER")
if ok {
var err error
gitHubRunNumber, err = strconv.ParseInt(gitHubRunNumberStr, 10, 64)
if err != nil {
return err
}
}
gitHubRunAttemptStr, ok := os.LookupEnv("GITHUB_RUN_ATTEMPT")
if ok {
var err error
gitHubRunAttempt, err = strconv.ParseInt(gitHubRunAttemptStr, 10, 64)
if err != nil {
return err
}
}
baseRef, ok := os.LookupEnv("GITHUB_X_PR_BASE_REF")
isPullRequest := ok && baseRef != ""
branchName, _ := os.LookupEnv("GITHUB_X_HEAD_REF")
createdAt := time.Now()
parseTest := func(status string, tc testjson.TestCase) testResult {
root, sub := tc.Test.Split()
return testResult{
CreatedAt: createdAt,
GitSHA: gitSHA,
GitBranch: branchName,
GitHubRepository: repository,
GitHubRunID: gitHubRunID,
GitHubRunNumber: gitHubRunNumber,
GitHubRunAttempt: gitHubRunAttempt,
GitHubIsPullRequest: isPullRequest,
Status: status,
Package: tc.Package,
Test: tc.Test.Name(),
RootTest: root,
SubTest: sub,
Elapsed: tc.Elapsed.Milliseconds(),
}
}
parseTests := func(status string, tcs []testjson.TestCase) []testResult {
var results []testResult
results = make([]testResult, 0, len(tcs))
for _, tc := range tcs {
results = append(results, parseTest(status, tc))
}
return results
}
var results []testResult
for _, pkgName := range exec.Packages() {
pkg := exec.Package(pkgName)
results = append(results, parseTests("passed", pkg.Passed)...)
results = append(results, parseTests("skipped", pkg.Skipped)...)
results = append(results, parseTests("failed", testjson.FilterFailedUnique(pkg.Failed))...)
}
if len(results) == 0 {
return nil
}
resultsIfc := make([]interface{}, 0, len(results))
for _, res := range results {
resultsIfc = append(resultsIfc, res)
}
coll := client.Database("tests").Collection("results")
_, err = coll.InsertMany(context.Background(), resultsIfc)
return err
}
type testResult struct {
CreatedAt time.Time `bson:"created_at"`
GitSHA string `bson:"git_sha,omitempty"`
GitBranch string `bson:"git_branch"`
GitHubRepository string `bson:"github_repository"`
GitHubRunID int64 `bson:"github_run_id,omitempty"`
GitHubRunNumber int64 `bson:"github_run_number,omitempty"`
GitHubRunAttempt int64 `bson:"github_run_attempt,omitempty"`
GitHubIsPullRequest bool `bson:"github_is_pull_request"`
Status string `bson:"status"`
Package string `bson:"package"`
Test string `bson:"test"`
RootTest string `bson:"root_test"`
SubTest string `bson:"sub_test,omitempty"`
Elapsed int64 `bson:"elapsed,omitempty"`
}