forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishGradleCheckTestResults.groovy
194 lines (180 loc) · 9.08 KB
/
publishGradleCheckTestResults.groovy
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/** Library to fetch failing tests at the end of gradle-check run and index the results in an OpenSearch cluster.
*
* @param Map args = [:] args A map of the following parameters
* @param args.prNumber <required> - The pull_request number that triggered the gradle-check run. If Null then use post_merge_action string.
* @param args.prDescription <required> - The subject of the pull_request. If prNumber is null then it signifies push action on branch.
*/
import hudson.tasks.test.AbstractTestResultAction
import groovy.json.JsonOutput
import java.text.SimpleDateFormat
import java.util.Date
void call(Map args = [:]) {
def lib = library(identifier: 'jenkins@main', retriever: legacySCM(scm))
def finalJsonDoc = ""
def buildNumber = currentBuild.number
def buildDescription = currentBuild.description
def buildDuration = currentBuild.duration
def buildResult = currentBuild.result
def buildStartTime = currentBuild.startTimeInMillis
def prNumber = args.prNumber.toString()
def invokeType = args.invokeType.toString()
def prOwner = args.prOwner.toString()
def prTitle = args.prTitle.toString()
def gitReference = args.gitReference.toString()
def currentDate = new Date()
def formattedDate = new SimpleDateFormat("MM-yyyy").format(currentDate)
def indexName = "gradle-check-${formattedDate}"
def test_docs = getFailedTestRecords(buildNumber, prNumber, invokeType, prOwner, prTitle, gitReference, buildResult, buildDuration, buildStartTime)
if (test_docs) {
for (doc in test_docs) {
def jsonDoc = JsonOutput.toJson(doc)
finalJsonDoc += "{\"index\": {\"_index\": \"${indexName}\"}}\n" + "${jsonDoc}\n"
}
writeFile file: "failed-test-records.json", text: finalJsonDoc
def fileContents = readFile(file: "failed-test-records.json").trim()
println("File Content is:\n${fileContents}")
indexFailedTestData()
}
}
List<Map<String, String>> getFailedTestRecords(buildNumber, prNumber, invokeType, prOwner, prTitle, gitReference, buildResult, buildDuration, buildStartTime) {
def testResults = []
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def testsTotal = testResultAction.totalCount
def testsFailed = testResultAction.failCount
def testsSkipped = testResultAction.skipCount
def testsPassed = testsTotal - testsFailed - testsSkipped
def failedTests = testResultAction.getFailedTests()
if (failedTests){
for (test in failedTests) {
def failDocument = ['build_number': buildNumber, 'pull_request': prNumber, 'pull_request_owner': prOwner , 'invoke_type': invokeType, 'pull_request_title': prTitle, 'git_reference': gitReference, 'test_class': test.getParent().getName(), 'test_name': test.fullName, 'test_status': 'FAILED', 'build_result': buildResult, 'test_fail_count': testsFailed, 'test_skipped_count': testsSkipped, 'test_passed_count': testsPassed, 'build_duration': buildDuration, 'build_start_time': buildStartTime]
testResults.add(failDocument)
}
} else {
println("No test failed.")
}
}
return testResults
}
void indexFailedTestData() {
withCredentials([
string(credentialsId: 'jenkins-health-metrics-account-number', variable: 'METRICS_HOST_ACCOUNT'),
string(credentialsId: 'jenkins-health-metrics-cluster-endpoint', variable: 'METRICS_HOST_URL')
]) {
withAWS(role: 'OpenSearchJenkinsAccessRole', roleAccount: "${METRICS_HOST_ACCOUNT}", duration: 900, roleSessionName: 'jenkins-session') {
def awsAccessKey = env.AWS_ACCESS_KEY_ID
def awsSecretKey = env.AWS_SECRET_ACCESS_KEY
def awsSessionToken = env.AWS_SESSION_TOKEN
sh """
set +e
set +x
MONTH_YEAR=\$(date +"%m-%Y")
INDEX_NAME="gradle-check-\$MONTH_YEAR"
INDEX_MAPPING='{
"mappings": {
"properties": {
"build_duration": {
"type": "float"
},
"build_number": {
"type": "integer"
},
"build_result": {
"type": "keyword"
},
"build_start_time": {
"type": "date"
},
"git_reference": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"invoke_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"pull_request": {
"type": "keyword"
},
"pull_request_owner": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"pull_request_title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"test_class": {
"type": "keyword"
},
"test_fail_count": {
"type": "integer"
},
"test_name": {
"type": "keyword"
},
"test_passed_count": {
"type": "integer"
},
"test_skipped_count": {
"type": "integer"
},
"test_status": {
"type": "keyword"
}
}
}
}'
echo "INDEX NAME IS \$INDEX_NAME"
curl -I "${METRICS_HOST_URL}/\$INDEX_NAME" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" | grep -E "HTTP\\/[0-9]+(\\.[0-9]+)? 200"
if [ \$? -eq 0 ]; then
echo "Index already exists. Indexing Results"
else
echo "Index does not exist. Creating..."
create_index_response=\$(curl -s -XPUT "${METRICS_HOST_URL}/\${INDEX_NAME}" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H 'Content-Type: application/json' -d "\${INDEX_MAPPING}")
if [[ \$create_index_response == *'"acknowledged":true'* ]]; then
echo "Index created successfully."
else
echo "Failed to create index. Error message: \$create_index_response"
exit 1
fi
fi
if [ -s failed-test-records.json ]; then
echo "File Exists, indexing results."
curl -XPOST "${METRICS_HOST_URL}/\$INDEX_NAME/_bulk" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H "Content-Type: application/x-ndjson" --data-binary "@failed-test-records.json"
else
echo "File Does not exist. No failing test records to process."
fi
"""
}
}
}
boolean isNullOrEmpty(String str) { return (str == 'Null' || str == null || str.allWhitespace || str.isEmpty()) }