Skip to content

Commit a3a2097

Browse files
committed
Nullability changes
1 parent 0172b2f commit a3a2097

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

plugins/src/main/java/com/google/firebase/gradle/plugins/report/ReportCommit.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
*/
1616
package com.google.firebase.gradle.plugins.report
1717

18+
/**
19+
* @param sha Commit SHA.
20+
* @param pr GitHub PR number that was squashed to create this commit. Used only for display purposes, does not affect logic.
21+
*/
1822
data class ReportCommit(val sha: String, val pr: Int)

plugins/src/main/java/com/google/firebase/gradle/plugins/report/TestReport.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
*/
1616
package com.google.firebase.gradle.plugins.report
1717

18+
19+
/**
20+
* Represents a single run of a test in CI. One unit/implementation test workflow run creates many `TestReport`s, one for each tested SDK.
21+
* @param name SDK name of the associated test run.
22+
* @param type What type of test result this is, either unit or instrumentation test.
23+
* @param status Conclusion status of the test run, `SUCCESS`/`FAILURE` for typical results, `OTHER` for ongoing runs and unexpected data.
24+
* @param commit Commit SHA this test was run on.
25+
* @param url Link to the GHA test run info, including logs.
26+
*/
1827
data class TestReport(
1928
val name: String,
2029
val type: Type,

plugins/src/main/java/com/google/firebase/gradle/plugins/report/UnitTestReport.kt renamed to plugins/src/main/java/com/google/firebase/gradle/plugins/report/TestReportGenerator.kt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ import kotlinx.serialization.json.jsonPrimitive
3535
import org.gradle.internal.Pair
3636
import org.slf4j.Logger
3737
import org.slf4j.LoggerFactory
38+
import java.io.File
39+
import kotlin.io.use
3840

3941
@SuppressWarnings("NewApi")
40-
class UnitTestReport(private val apiToken: String) {
42+
class TestReportGenerator(private val apiToken: String) {
4143
private val LOG: Logger = LoggerFactory.getLogger("firebase-test-report")
4244
private val client: HttpClient =
4345
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build()
@@ -50,25 +52,25 @@ class UnitTestReport(private val apiToken: String) {
5052
generateGraphQLQuery(commitCount),
5153
)
5254
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"))
6062
.stream()
6163
.limit(commitCount.toLong())
6264
.map { el: JsonElement ->
6365
val obj = el as JsonObject
6466
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"),
7274
)
7375
}
7476
.toList()
@@ -99,9 +101,7 @@ class UnitTestReport(private val apiToken: String) {
99101
output.append("\n")
100102

101103
try {
102-
val writer = FileWriter("test-report.md")
103-
writer.append(output.toString())
104-
writer.close()
104+
File("test-report.md").writeText(output.toString())
105105
} catch (e: Exception) {
106106
throw RuntimeException("Error writing report file", e)
107107
}
@@ -120,7 +120,7 @@ class UnitTestReport(private val apiToken: String) {
120120
var sdkTestSuccess = 0
121121
for (commit in commits) {
122122
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)]!!
124124
if (report.status != TestReport.Status.OTHER) {
125125
sdkTestCount++
126126
if (report.status == TestReport.Status.SUCCESS) {
@@ -137,7 +137,7 @@ class UnitTestReport(private val apiToken: String) {
137137
sdks =
138138
sdks
139139
.filter { s: String? -> successPercentage[s] != 100 }
140-
.sortedBy { o: String -> successPercentage[o]!! }
140+
.sortedBy { o: String -> successPercentage[o]?: 0 }
141141
if (sdks.isEmpty()) {
142142
return "*All tests passing*\n"
143143
}
@@ -173,7 +173,7 @@ class UnitTestReport(private val apiToken: String) {
173173
output.append(" |")
174174
}
175175
output.append(" ")
176-
val successChance: Int = successPercentage.get(sdk)!!
176+
val successChance: Int = successPercentage[sdk] ?: throw RuntimeException("Success percentage missing for $sdk")
177177
if (successChance == 100) {
178178
output.append("✅ 100%")
179179
} else {
@@ -192,9 +192,9 @@ class UnitTestReport(private val apiToken: String) {
192192
val runs = request("actions/runs?head_sha=$commit")
193193
for (el in runs["workflow_runs"] as JsonArray) {
194194
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")
196196
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)
198198
}
199199
}
200200
return emptyList()
@@ -205,7 +205,7 @@ class UnitTestReport(private val apiToken: String) {
205205
val jobs = request("actions/runs/$id/jobs")
206206
for (el in jobs["jobs"] as JsonArray) {
207207
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")
209209
if (jobName.startsWith("Unit Tests (:")) {
210210
reports.add(parseJob(TestReport.Type.UNIT_TEST, job, commit))
211211
} else if (jobName.startsWith("Instrumentation Tests (:")) {
@@ -217,24 +217,24 @@ class UnitTestReport(private val apiToken: String) {
217217

218218
private fun parseJob(type: TestReport.Type, job: JsonObject, commit: String): TestReport {
219219
var name =
220-
job["name"]!!
221-
.jsonPrimitive
220+
(job["name"]
221+
?.jsonPrimitive ?: throw RuntimeException("Job missing name"))
222222
.content
223223
.split("(:")
224224
.dropLastWhile { it.isEmpty() }
225225
.toTypedArray()[1]
226226
name = name.substring(0, name.length - 1) // Remove trailing ")"
227227
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") {
230230
TestReport.Status.SUCCESS
231231
} else {
232232
TestReport.Status.FAILURE
233233
}
234234
} else {
235235
TestReport.Status.OTHER
236236
}
237-
val url = job["html_url"]!!.jsonPrimitive.content
237+
val url = job["html_url"]?.jsonPrimitive?.content ?: throw RuntimeException("PR missing URL")
238238
return TestReport(name, type, status, commit, url)
239239
}
240240

0 commit comments

Comments
 (0)