-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgithub.go
243 lines (216 loc) · 7.46 KB
/
github.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/cockroachdb/cockroach/pkg/cmd/internal/issues"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test"
"github.com/cockroachdb/cockroach/pkg/internal/team"
rperrors "github.com/cockroachdb/cockroach/pkg/roachprod/errors"
"github.com/cockroachdb/cockroach/pkg/roachprod/logger"
"github.com/cockroachdb/cockroach/pkg/roachprod/vm"
)
type githubIssues struct {
disable bool
cluster *clusterImpl
vmCreateOpts *vm.CreateOpts
issuePoster func(context.Context, issues.Logger, issues.IssueFormatter, issues.PostRequest) error
teamLoader func() (team.Map, error)
}
func newGithubIssues(disable bool, c *clusterImpl, vmCreateOpts *vm.CreateOpts) *githubIssues {
return &githubIssues{
disable: disable,
vmCreateOpts: vmCreateOpts,
cluster: c,
issuePoster: issues.Post,
teamLoader: team.DefaultLoadTeams,
}
}
func roachtestPrefix(p string) string {
return "ROACHTEST_" + p
}
// generateHelpCommand creates a HelpCommand for createPostRequest
func generateHelpCommand(
clusterName string, start time.Time, end time.Time,
) func(renderer *issues.Renderer) {
return func(renderer *issues.Renderer) {
issues.HelpCommandAsLink(
"roachtest README",
"https://github.com/cockroachdb/cockroach/blob/master/pkg/cmd/roachtest/README.md",
)(renderer)
issues.HelpCommandAsLink(
"How To Investigate (internal)",
"https://cockroachlabs.atlassian.net/l/c/SSSBr8c7",
)(renderer)
issues.HelpCommandAsLink(
"Grafana",
fmt.Sprintf("https://go.crdb.dev/p/roachfana/%s/%d/%d", clusterName, start.UnixMilli(), end.UnixMilli()),
)(renderer)
}
}
// postIssueCondition encapsulates a condition that causes issue
// posting to be skipped. The `reason` field contains a textual
// description as to why issue posting was skipped.
type postIssueCondition struct {
cond func(g *githubIssues, t test.Test) bool
reason string
}
var defaultOpts = issues.DefaultOptionsFromEnv()
var skipConditions = []postIssueCondition{
{
cond: func(g *githubIssues, _ test.Test) bool { return g.disable },
reason: "issue posting was disabled via command line flag",
},
{
cond: func(g *githubIssues, _ test.Test) bool { return !defaultOpts.CanPost() },
reason: "GitHub API token not set",
},
{
cond: func(g *githubIssues, _ test.Test) bool { return !defaultOpts.IsReleaseBranch() },
reason: fmt.Sprintf("not a release branch: %q", defaultOpts.Branch),
},
{
cond: func(_ *githubIssues, t test.Test) bool { return t.Spec().(*registry.TestSpec).Run == nil },
reason: "TestSpec.Run is nil",
},
{
cond: func(_ *githubIssues, t test.Test) bool { return t.Spec().(*registry.TestSpec).Cluster.NodeCount == 0 },
reason: "Cluster.NodeCount is zero",
},
}
// shouldPost two values: whether GitHub posting should happen, and a
// reason for skipping (non-empty only when posting should *not*
// happen).
func (g *githubIssues) shouldPost(t test.Test) (bool, string) {
post := true
var reason string
for _, sc := range skipConditions {
if sc.cond(g, t) {
post = false
reason = sc.reason
break
}
}
return post, reason
}
func (g *githubIssues) createPostRequest(
testName string,
start time.Time,
end time.Time,
spec *registry.TestSpec,
firstFailure failure,
message string,
) (issues.PostRequest, error) {
var mention []string
var projColID int
issueOwner := spec.Owner
issueName := testName
messagePrefix := ""
var infraFlake bool
// Overrides to shield eng teams from potential flakes
switch {
case failureContainsError(firstFailure, errClusterProvisioningFailed):
issueOwner = registry.OwnerDevInf
issueName = "cluster_creation"
messagePrefix = fmt.Sprintf("test %s was skipped due to ", testName)
infraFlake = true
case failureContainsError(firstFailure, rperrors.ErrSSH255):
issueOwner = registry.OwnerTestEng
issueName = "ssh_problem"
messagePrefix = fmt.Sprintf("test %s failed due to ", testName)
infraFlake = true
case failureContainsError(firstFailure, errDuringPostAssertions):
messagePrefix = fmt.Sprintf("test %s failed during post test assertions (see test-post-assertions.log) due to ", testName)
}
// Issues posted from roachtest are identifiable as such, and they are also release blockers
// (this label may be removed by a human upon closer investigation).
labels := []string{"O-roachtest"}
if !spec.NonReleaseBlocker && !infraFlake {
labels = append(labels, "release-blocker")
}
teams, err := g.teamLoader()
if err != nil {
return issues.PostRequest{}, err
}
if sl, ok := teams.GetAliasesForPurpose(ownerToAlias(issueOwner), team.PurposeRoachtest); ok {
for _, alias := range sl {
mention = append(mention, "@"+string(alias))
if label := teams[alias].Label; label != "" {
labels = append(labels, label)
}
}
projColID = teams[sl[0]].TriageColumnID
}
branch := os.Getenv("TC_BUILD_BRANCH")
if branch == "" {
branch = "<unknown branch>"
}
artifacts := fmt.Sprintf("/%s", testName)
clusterParams := map[string]string{
roachtestPrefix("cloud"): spec.Cluster.Cloud,
roachtestPrefix("cpu"): fmt.Sprintf("%d", spec.Cluster.CPUs),
roachtestPrefix("ssd"): fmt.Sprintf("%d", spec.Cluster.SSDs),
}
// Emit CPU architecture only if it was specified; otherwise, it's captured below, assuming cluster was created.
if spec.Cluster.Arch != "" {
clusterParams[roachtestPrefix("arch")] = string(spec.Cluster.Arch)
}
// These params can be probabilistically set, so we pass them here to
// show what their actual values are in the posted issue.
if g.vmCreateOpts != nil {
clusterParams[roachtestPrefix("fs")] = g.vmCreateOpts.SSDOpts.FileSystem
clusterParams[roachtestPrefix("localSSD")] = fmt.Sprintf("%v", g.vmCreateOpts.SSDOpts.UseLocalSSD)
}
if g.cluster != nil {
clusterParams[roachtestPrefix("encrypted")] = fmt.Sprintf("%v", g.cluster.encAtRest)
if spec.Cluster.Arch == "" {
// N.B. when Arch is specified, it cannot differ from cluster's arch.
// Hence, we only emit when arch was unspecified.
clusterParams[roachtestPrefix("arch")] = string(g.cluster.arch)
}
}
issueMessage := messagePrefix + message
if spec.RedactResults {
issueMessage = "The details about this test failure may contain sensitive information; " +
"consult the logs for details. WARNING: DO NOT COPY UNREDACTED ARTIFACTS TO THIS ISSUE."
}
return issues.PostRequest{
MentionOnCreate: mention,
ProjectColumnID: projColID,
PackageName: "roachtest",
TestName: issueName,
Message: issueMessage,
Artifacts: artifacts,
ExtraLabels: labels,
ExtraParams: clusterParams,
HelpCommand: generateHelpCommand(clusterName, start, end),
}, nil
}
func (g *githubIssues) MaybePost(t *testImpl, l *logger.Logger, message string) error {
doPost, skipReason := g.shouldPost(t)
if !doPost {
l.Printf("skipping GitHub issue posting (%s)", skipReason)
return nil
}
postRequest, err := g.createPostRequest(t.Name(), t.start, t.end, t.spec, t.firstFailure(), message)
if err != nil {
return err
}
return g.issuePoster(
context.Background(),
l,
issues.UnitTestFormatter,
postRequest,
)
}