@@ -35,9 +35,11 @@ import kotlinx.serialization.json.jsonPrimitive
35
35
import org.gradle.internal.Pair
36
36
import org.slf4j.Logger
37
37
import org.slf4j.LoggerFactory
38
+ import java.io.File
39
+ import kotlin.io.use
38
40
39
41
@SuppressWarnings(" NewApi" )
40
- class UnitTestReport (private val apiToken : String ) {
42
+ class TestReportGenerator (private val apiToken : String ) {
41
43
private val LOG : Logger = LoggerFactory .getLogger(" firebase-test-report" )
42
44
private val client: HttpClient =
43
45
HttpClient .newBuilder().connectTimeout(Duration .ofSeconds(10 )).build()
@@ -50,25 +52,25 @@ class UnitTestReport(private val apiToken: String) {
50
52
generateGraphQLQuery(commitCount),
51
53
)
52
54
val commits =
53
- response[" data" ]!!
54
- .jsonObject[" repository" ]!!
55
- .jsonObject[" ref" ]!!
56
- .jsonObject[" target" ]!!
57
- .jsonObject[" history" ]!!
58
- .jsonObject[" nodes" ]!!
59
- .jsonArray
55
+ ( response[" data" ]
56
+ ? .jsonObject[" repository" ]
57
+ ? .jsonObject[" ref" ]
58
+ ? .jsonObject[" target" ]
59
+ ? .jsonObject[" history" ]
60
+ ? .jsonObject[" nodes" ]
61
+ ? .jsonArray ? : throw RuntimeException ( " Missing fields in response: $response " ))
60
62
.stream()
61
63
.limit(commitCount.toLong())
62
64
.map { el: JsonElement ->
63
65
val obj = el as JsonObject
64
66
ReportCommit (
65
- obj[" oid" ]!! .jsonPrimitive.content,
66
- obj[" associatedPullRequests" ]!!
67
- .jsonObject[" nodes" ]!!
68
- .jsonArray[0 ]
69
- .jsonObject[" number" ]!!
70
- .jsonPrimitive
71
- .int,
67
+ obj[" oid" ]? .jsonPrimitive? .content ? : throw RuntimeException ( " Couldn't find commit SHA " ) ,
68
+ obj[" associatedPullRequests" ]
69
+ ? .jsonObject[" nodes" ]
70
+ ? .jsonArray[0 ]
71
+ ? .jsonObject[" number" ]
72
+ ? .jsonPrimitive
73
+ ? .int ? : throw RuntimeException ( " Couldn't find PR number for commit $obj " ) ,
72
74
)
73
75
}
74
76
.toList()
@@ -99,9 +101,7 @@ class UnitTestReport(private val apiToken: String) {
99
101
output.append(" \n " )
100
102
101
103
try {
102
- val writer = FileWriter (" test-report.md" )
103
- writer.append(output.toString())
104
- writer.close()
104
+ File (" test-report.md" ).writeText(output.toString())
105
105
} catch (e: Exception ) {
106
106
throw RuntimeException (" Error writing report file" , e)
107
107
}
@@ -120,7 +120,7 @@ class UnitTestReport(private val apiToken: String) {
120
120
var sdkTestSuccess = 0
121
121
for (commit in commits) {
122
122
if (lookup.containsKey(Pair .of(sdk, commit))) {
123
- val report: TestReport = lookup.get( Pair .of(sdk, commit)) !!
123
+ val report: TestReport = lookup[ Pair .of(sdk, commit)] !!
124
124
if (report.status != TestReport .Status .OTHER ) {
125
125
sdkTestCount++
126
126
if (report.status == TestReport .Status .SUCCESS ) {
@@ -137,7 +137,7 @@ class UnitTestReport(private val apiToken: String) {
137
137
sdks =
138
138
sdks
139
139
.filter { s: String? -> successPercentage[s] != 100 }
140
- .sortedBy { o: String -> successPercentage[o]!! }
140
+ .sortedBy { o: String -> successPercentage[o]? : 0 }
141
141
if (sdks.isEmpty()) {
142
142
return " *All tests passing*\n "
143
143
}
@@ -173,7 +173,7 @@ class UnitTestReport(private val apiToken: String) {
173
173
output.append(" |" )
174
174
}
175
175
output.append(" " )
176
- val successChance: Int = successPercentage.get( sdk) !!
176
+ val successChance: Int = successPercentage[sdk] ? : throw RuntimeException ( " Success percentage missing for $ sdk" )
177
177
if (successChance == 100 ) {
178
178
output.append(" ✅ 100%" )
179
179
} else {
@@ -192,9 +192,9 @@ class UnitTestReport(private val apiToken: String) {
192
192
val runs = request(" actions/runs?head_sha=$commit " )
193
193
for (el in runs[" workflow_runs" ] as JsonArray ) {
194
194
val run = el as JsonObject
195
- val name = run[" name" ]!! .jsonPrimitive.content
195
+ val name = run[" name" ]? .jsonPrimitive? .content ? : throw RuntimeException ( " Couldn't find CI name " )
196
196
if (name == " CI Tests" ) {
197
- return parseCITests(run[" id" ]!! .jsonPrimitive.content, commit)
197
+ return parseCITests(run[" id" ]? .jsonPrimitive? .content ? : throw RuntimeException ( " Couldn't find run id for $commit run $name " ) , commit)
198
198
}
199
199
}
200
200
return emptyList()
@@ -205,7 +205,7 @@ class UnitTestReport(private val apiToken: String) {
205
205
val jobs = request(" actions/runs/$id /jobs" )
206
206
for (el in jobs[" jobs" ] as JsonArray ) {
207
207
val job = el as JsonObject
208
- val jobName = job[" name" ]!! .jsonPrimitive.content
208
+ val jobName = job[" name" ]? .jsonPrimitive? .content ? : throw RuntimeException ( " Couldn't find name for job $id " )
209
209
if (jobName.startsWith(" Unit Tests (:" )) {
210
210
reports.add(parseJob(TestReport .Type .UNIT_TEST , job, commit))
211
211
} else if (jobName.startsWith(" Instrumentation Tests (:" )) {
@@ -217,24 +217,24 @@ class UnitTestReport(private val apiToken: String) {
217
217
218
218
private fun parseJob (type : TestReport .Type , job : JsonObject , commit : String ): TestReport {
219
219
var name =
220
- job[" name" ]!!
221
- .jsonPrimitive
220
+ ( job[" name" ]
221
+ ? .jsonPrimitive ? : throw RuntimeException ( " Job missing name " ))
222
222
.content
223
223
.split(" (:" )
224
224
.dropLastWhile { it.isEmpty() }
225
225
.toTypedArray()[1 ]
226
226
name = name.substring(0 , name.length - 1 ) // Remove trailing ")"
227
227
val status =
228
- if (job[" status" ]!! .jsonPrimitive.content == " completed" ) {
229
- if (job[" conclusion" ]!! .jsonPrimitive.content == " success" ) {
228
+ if (job[" status" ]? .jsonPrimitive? .content == " completed" ) {
229
+ if (job[" conclusion" ]? .jsonPrimitive? .content == " success" ) {
230
230
TestReport .Status .SUCCESS
231
231
} else {
232
232
TestReport .Status .FAILURE
233
233
}
234
234
} else {
235
235
TestReport .Status .OTHER
236
236
}
237
- val url = job[" html_url" ]!! .jsonPrimitive.content
237
+ val url = job[" html_url" ]? .jsonPrimitive? .content ? : throw RuntimeException ( " PR missing URL " )
238
238
return TestReport (name, type, status, commit, url)
239
239
}
240
240
0 commit comments