forked from konflux-ci/ci-helper-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue_comment.go
296 lines (246 loc) · 11.2 KB
/
issue_comment.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2018 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"regexp"
"strings"
"time"
"github.com/google/go-github/v58/github"
reporters "github.com/onsi/ginkgo/v2/reporters"
"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
"github.com/redhat-appstudio/qe-tools/pkg/prow"
"github.com/rs/zerolog"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
targetAuthor = "openshift-ci[bot]"
junitFilename = "junit.xml"
junitFilenameRegex = `(junit.xml)`
openshiftCITestSuiteName = "openshift-ci job"
e2eTestSuiteName = "Red Hat App Studio E2E tests"
LogKeyProwJobURL = "prow_job_url"
dropdownSummaryString = "Click to view logs"
regexToFetchProwURL = `(https:\/\/prow.ci.openshift.org\/view\/gs\/test-platform-results\/pr-logs\/pull.*)\)`
)
type PRCommentHandler struct {
githubapp.ClientCreator
}
type FailedTestCasesReport struct {
headerString string
failedTestCaseNames []string
hasBootstrapFailure bool
}
func (h *PRCommentHandler) Handles() []string {
return []string{"issue_comment"}
}
func (h *PRCommentHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
var event github.IssueCommentEvent
if err := json.Unmarshal(payload, &event); err != nil {
return errors.Wrap(err, "failed to parse issue comment event payload")
}
if !event.GetIssue().IsPullRequest() || event.GetAction() != "created" {
return nil
}
installationID := githubapp.GetInstallationIDFromEvent(&event)
ctx, logger := githubapp.PreparePRContext(ctx, installationID, event.GetRepo(), event.GetIssue().GetNumber())
client, err := h.NewInstallationClient(installationID)
if err != nil {
return err
}
author := event.GetComment().GetUser().GetLogin()
body := event.GetComment().GetBody()
if !strings.HasPrefix(author, targetAuthor) {
logger.Debug().Msgf("Issue comment was not created by the user: %s. Ignoring this comment", targetAuthor)
return nil
}
// extract the Prow job's URL
prowJobURL, err := extractProwJobURLFromCommentBody(body)
if err != nil {
return fmt.Errorf("unable to extract Prow job's URL from the PR comment's body: %+v", err)
}
logger = attachProwURLLogKeysToLogger(ctx, logger, prowJobURL)
cfg := prow.ScannerConfig{
ProwJobURL: prowJobURL,
FileNameFilter: []string{junitFilenameRegex},
}
scanner, err := prow.NewArtifactScanner(cfg)
if err != nil {
return fmt.Errorf("failed to initialize ArtifactScanner: %+v", err)
}
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 10*time.Minute, true, func(context.Context) (done bool, err error) {
if err := scanner.Run(); err != nil {
logger.Error().Err(err).Msgf("Failed to scan artifacts from the Prow job...Retrying")
return false, nil
}
return true, nil
})
if err != nil {
logger.Error().Err(err).Msgf("Timed out while scanning artifacts for Prow job %s. Will Stop processing this comment", prowJobURL)
return err
}
overallJUnitSuites, err := getTestSuitesFromXMLFile(scanner, logger, junitFilename)
// make sure that the Prow job didn't fail while creating the cluster
if err != nil && !strings.Contains(err.Error(), fmt.Sprintf("couldn't find the %s file", junitFilename)) {
return fmt.Errorf("failed to get JUnitTestSuites from the file %s: %+v", junitFilename, err)
}
failedTCReport := setHeaderString(logger, overallJUnitSuites)
failedTCReport.extractFailedTestCases(scanner, logger, overallJUnitSuites)
if err = failedTCReport.updateCommentWithFailedTestCasesReport(ctx, logger, client, event, body); err != nil {
return err
}
return nil
}
// extractProwJobURLFromCommentBody extracts the
// Prow job's URL from the given PR comment's body
func extractProwJobURLFromCommentBody(commentBody string) (string, error) {
r, _ := regexp.Compile(regexToFetchProwURL)
sliceOfMatchingString := r.FindAllStringSubmatch(commentBody, -1)
for _, matchesAndGroups := range sliceOfMatchingString {
for _, subsStr := range matchesAndGroups {
if !strings.Contains(subsStr, "images") && !strings.HasSuffix(subsStr, ")") {
return subsStr, nil
}
}
}
return "", fmt.Errorf("regex string %s found no matches for the comment body: %s", regexToFetchProwURL, commentBody)
}
// getTestSuitesFromXMLFile returns all the JUnitTestSuites
// present within a file with the given name
func getTestSuitesFromXMLFile(scanner *prow.ArtifactScanner, logger zerolog.Logger, filename string) (*reporters.JUnitTestSuites, error) {
overallJUnitSuites := &reporters.JUnitTestSuites{}
for _, artifactsFilenameMap := range scanner.ArtifactStepMap {
for artifactFilename, artifact := range artifactsFilenameMap {
if string(artifactFilename) == filename {
if err := xml.Unmarshal([]byte(artifact.Content), overallJUnitSuites); err != nil {
logger.Error().Err(err).Msg("cannot decode JUnit suite into xml")
return &reporters.JUnitTestSuites{}, err
}
return overallJUnitSuites, nil
}
}
}
return &reporters.JUnitTestSuites{}, fmt.Errorf("couldn't find the %s file", filename)
}
// setHeaderString initialises struct FailedTestCasesReport's
// 'headerString' field based on phase at which Prow job failed
func setHeaderString(logger zerolog.Logger, overallJUnitSuites *reporters.JUnitTestSuites) *FailedTestCasesReport {
failedTCReport := FailedTestCasesReport{}
if len(overallJUnitSuites.TestSuites) == 0 {
logger.Debug().Msg("The given Prow job failed while creating the cluster")
failedTCReport.headerString = ":rotating_light: **This is a CI system failure, please consult with the QE team.**\n"
} else if len(overallJUnitSuites.TestSuites) == 1 && overallJUnitSuites.TestSuites[0].Name == openshiftCITestSuiteName {
logger.Debug().Msg("The given Prow job failed during bootstrapping the cluster")
failedTCReport.hasBootstrapFailure = true
failedTCReport.headerString = ":rotating_light: **Error occurred during the cluster's Bootstrapping phase, list of failed Spec(s)**: \n"
} else {
logger.Debug().Msg("The given Prow job failed while running the E2E tests")
failedTCReport.headerString = ":rotating_light: **Error occurred while running the E2E tests, list of failed Spec(s)**: \n"
}
return &failedTCReport
}
// extractFailedTestCases initialises the FailedTestCasesReport struct's
// 'failedTestCaseNames' field with the names of failed test cases
// within given JUnitTestSuites -- if the given JUnitTestSuites is !nil.
// And if it's nil, 'failedTestCaseNames' field is init with content of
// "build-log.txt" file, if it exists.
func (failedTCReport *FailedTestCasesReport) extractFailedTestCases(scanner *prow.ArtifactScanner, logger zerolog.Logger, overallJUnitSuites *reporters.JUnitTestSuites) {
if len(overallJUnitSuites.TestSuites) == 0 {
parentStepName := "/"
buildLogFileName := "build-log.txt"
if asMap := scanner.ArtifactStepMap[prow.ArtifactStepName(parentStepName)]; asMap != nil {
if asMap[prow.ArtifactFilename(buildLogFileName)].Content == "" {
logger.Error().Msgf("Failed to fetch content of the file: %s within the `%s` parent directory", buildLogFileName, parentStepName)
return
}
testCaseEntry := returnContentWrappedInDropdown(dropdownSummaryString, asMap[prow.ArtifactFilename(buildLogFileName)].Content)
failedTCReport.failedTestCaseNames = append(failedTCReport.failedTestCaseNames, testCaseEntry)
} else {
logger.Error().Msgf("Failed to find any files within the directory: %s", parentStepName)
}
return
}
for _, testSuite := range overallJUnitSuites.TestSuites {
if failedTCReport.hasBootstrapFailure || (testSuite.Name == e2eTestSuiteName && (testSuite.Failures > 0 || testSuite.Errors > 0)) {
for _, tc := range testSuite.TestCases {
if tc.Failure != nil || tc.Error != nil {
logger.Debug().Msgf("Found a Test Case (suiteName/testCaseName): %s/%s, that didn't pass", testSuite.Name, tc.Name)
tcMessage := ""
if failedTCReport.hasBootstrapFailure {
tcMessage = "```\n" + returnLastNLines(tc.SystemErr, 16) + "\n```"
} else if tc.Status == "timedout" {
tcMessage = returnContentWrappedInDropdown(dropdownSummaryString, tc.SystemErr)
} else if tc.Failure != nil {
tcMessage = "```\n" + tc.Failure.Message + "\n```"
} else {
tcMessage = "```\n" + tc.Error.Message + "\n```"
}
testCaseEntry := "* :arrow_right: " + "[**`" + tc.Status + "`**] " + tc.Name + "\n" + tcMessage
failedTCReport.failedTestCaseNames = append(failedTCReport.failedTestCaseNames, testCaseEntry)
}
}
}
}
}
// updateCommentWithFailedTestCasesReport updates the
// PR comment's body with the names of failed test cases
func (failedTCReport *FailedTestCasesReport) updateCommentWithFailedTestCasesReport(ctx context.Context, logger zerolog.Logger, client *github.Client, event github.IssueCommentEvent, commentBody string) error {
repoOwner := event.GetRepo().GetOwner().GetLogin()
repoName := event.GetRepo().GetName()
commentID := event.GetComment().GetID()
if failedTCReport.failedTestCaseNames != nil && len(failedTCReport.failedTestCaseNames) > 0 {
msg := failedTCReport.headerString
for _, failedTCName := range failedTCReport.failedTestCaseNames {
msg = msg + fmt.Sprintf("\n %s\n", failedTCName)
}
msg = msg + "\n-------------------------------\n\n" + commentBody
prComment := github.IssueComment{
Body: &msg,
}
err := wait.PollUntilContextTimeout(context.Background(), 15*time.Second, 1*time.Minute, true, func(context.Context) (done bool, err error) {
if _, _, err := client.Issues.EditComment(ctx, repoOwner, repoName, commentID, &prComment); err != nil {
logger.Error().Err(err).Msgf("Failed to edit the comment...Retrying")
return false, nil
}
return true, nil
})
if err != nil {
logger.Error().Err(err).Msgf("Failed to edit comment (ID: %v) due to the error: %+v. Will Stop processing this comment", commentID, err)
return err
}
logger.Debug().Msgf("Successfully updated comment (with ID:%d) with the names of failed test cases", commentID)
} else {
logger.Debug().Msgf("Unable to find any details to update. Declining to update comment (with ID:%d)", commentID)
}
return nil
}
func attachProwURLLogKeysToLogger(ctx context.Context, logger zerolog.Logger, prowJobURL string) zerolog.Logger {
logctx := zerolog.Ctx(ctx).With()
if prowJobURL != "" {
logctx = logctx.Str(LogKeyProwJobURL, prowJobURL)
return logctx.Logger()
}
return logger
}
func returnLastNLines(content string, n int) string {
systemErrString := strings.Split(content, "\n")
return strings.Join(systemErrString[len(systemErrString)-n:], "\n")
}
func returnContentWrappedInDropdown(summary, content string) string {
return "<details><summary>" + summary + "</summary><br><pre>" + content + "</pre></details>"
}