Skip to content

Commit d62ba9e

Browse files
committed
Fix null chaining
1 parent a3a2097 commit d62ba9e

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.google.firebase.gradle.plugins.report
1717

1818
/**
1919
* @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.
20+
* @param pr GitHub PR number that was squashed to create this commit. Used only for display
21+
* purposes, does not affect logic.
2122
*/
2223
data class ReportCommit(val sha: String, val pr: Int)

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

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

18-
1918
/**
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.
19+
* Represents a single run of a test in CI. One unit/implementation test workflow run creates many
20+
* `TestReport`s, one for each tested SDK.
21+
*
2122
* @param name SDK name of the associated test run.
2223
* @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 status Conclusion status of the test run, `SUCCESS`/`FAILURE` for typical results, `OTHER`
25+
* for ongoing runs and unexpected data.
2426
* @param commit Commit SHA this test was run on.
2527
* @param url Link to the GHA test run info, including logs.
2628
*/

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

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

18-
import java.io.FileWriter
18+
import java.io.File
1919
import java.io.IOException
2020
import java.net.URI
2121
import java.net.http.HttpClient
@@ -35,8 +35,6 @@ 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
4038

4139
@SuppressWarnings("NewApi")
4240
class TestReportGenerator(private val apiToken: String) {
@@ -45,30 +43,39 @@ class TestReportGenerator(private val apiToken: String) {
4543
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build()
4644

4745
fun createReport(commitCount: Int) {
48-
val response =
46+
val response: JsonObject =
4947
request(
5048
URI.create("https://api.github.com/graphql"),
5149
JsonObject::class.java,
5250
generateGraphQLQuery(commitCount),
5351
)
5452
val commits =
5553
(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"))
54+
?.jsonObject
55+
?.get("repository")
56+
?.jsonObject
57+
?.get("ref")
58+
?.jsonObject
59+
?.get("target")
60+
?.jsonObject
61+
?.get("history")
62+
?.jsonObject
63+
?.get("nodes")
64+
?.jsonArray ?: throw RuntimeException("Missing fields in response: $response"))
6265
.stream()
6366
.limit(commitCount.toLong())
6467
.map { el: JsonElement ->
6568
val obj = el as JsonObject
6669
ReportCommit(
67-
obj["oid"]?.jsonPrimitive?.content ?: throw RuntimeException("Couldn't find commit SHA"),
70+
obj["oid"]?.jsonPrimitive?.content
71+
?: throw RuntimeException("Couldn't find commit SHA"),
6872
obj["associatedPullRequests"]
69-
?.jsonObject["nodes"]
70-
?.jsonArray[0]
71-
?.jsonObject["number"]
73+
?.jsonObject
74+
?.get("nodes")
75+
?.jsonArray
76+
?.get(0)
77+
?.jsonObject
78+
?.get("number")
7279
?.jsonPrimitive
7380
?.int ?: throw RuntimeException("Couldn't find PR number for commit $obj"),
7481
)
@@ -137,7 +144,7 @@ class TestReportGenerator(private val apiToken: String) {
137144
sdks =
138145
sdks
139146
.filter { s: String? -> successPercentage[s] != 100 }
140-
.sortedBy { o: String -> successPercentage[o]?: 0 }
147+
.sortedBy { o: String -> successPercentage[o] ?: 0 }
141148
if (sdks.isEmpty()) {
142149
return "*All tests passing*\n"
143150
}
@@ -173,7 +180,8 @@ class TestReportGenerator(private val apiToken: String) {
173180
output.append(" |")
174181
}
175182
output.append(" ")
176-
val successChance: Int = successPercentage[sdk] ?: throw RuntimeException("Success percentage missing for $sdk")
183+
val successChance: Int =
184+
successPercentage[sdk] ?: throw RuntimeException("Success percentage missing for $sdk")
177185
if (successChance == 100) {
178186
output.append("✅ 100%")
179187
} else {
@@ -192,9 +200,14 @@ class TestReportGenerator(private val apiToken: String) {
192200
val runs = request("actions/runs?head_sha=$commit")
193201
for (el in runs["workflow_runs"] as JsonArray) {
194202
val run = el as JsonObject
195-
val name = run["name"]?.jsonPrimitive?.content ?: throw RuntimeException("Couldn't find CI name")
203+
val name =
204+
run["name"]?.jsonPrimitive?.content ?: throw RuntimeException("Couldn't find CI name")
196205
if (name == "CI Tests") {
197-
return parseCITests(run["id"]?.jsonPrimitive?.content ?: throw RuntimeException("Couldn't find run id for $commit run $name"), commit)
206+
return parseCITests(
207+
run["id"]?.jsonPrimitive?.content
208+
?: throw RuntimeException("Couldn't find run id for $commit run $name"),
209+
commit,
210+
)
198211
}
199212
}
200213
return emptyList()
@@ -205,7 +218,9 @@ class TestReportGenerator(private val apiToken: String) {
205218
val jobs = request("actions/runs/$id/jobs")
206219
for (el in jobs["jobs"] as JsonArray) {
207220
val job = el as JsonObject
208-
val jobName = job["name"]?.jsonPrimitive?.content ?: throw RuntimeException("Couldn't find name for job $id")
221+
val jobName =
222+
job["name"]?.jsonPrimitive?.content
223+
?: throw RuntimeException("Couldn't find name for job $id")
209224
if (jobName.startsWith("Unit Tests (:")) {
210225
reports.add(parseJob(TestReport.Type.UNIT_TEST, job, commit))
211226
} else if (jobName.startsWith("Instrumentation Tests (:")) {
@@ -217,31 +232,30 @@ class TestReportGenerator(private val apiToken: String) {
217232

218233
private fun parseJob(type: TestReport.Type, job: JsonObject, commit: String): TestReport {
219234
var name =
220-
(job["name"]
221-
?.jsonPrimitive ?: throw RuntimeException("Job missing name"))
235+
(job["name"]?.jsonPrimitive ?: throw RuntimeException("Job missing name"))
222236
.content
223237
.split("(:")
224238
.dropLastWhile { it.isEmpty() }
225239
.toTypedArray()[1]
226240
name = name.substring(0, name.length - 1) // Remove trailing ")"
227241
val status =
228-
if (job["status"]?.jsonPrimitive?.content == "completed") {
229-
if (job["conclusion"]?.jsonPrimitive?.content == "success") {
230-
TestReport.Status.SUCCESS
242+
if (job["status"]?.jsonPrimitive?.content == "completed") {
243+
if (job["conclusion"]?.jsonPrimitive?.content == "success") {
244+
TestReport.Status.SUCCESS
245+
} else {
246+
TestReport.Status.FAILURE
247+
}
231248
} else {
232-
TestReport.Status.FAILURE
249+
TestReport.Status.OTHER
233250
}
234-
} else {
235-
TestReport.Status.OTHER
236-
}
237251
val url = job["html_url"]?.jsonPrimitive?.content ?: throw RuntimeException("PR missing URL")
238252
return TestReport(name, type, status, commit, url)
239253
}
240254

241255
private fun generateGraphQLQuery(commitCount: Int): JsonObject {
242256
return JsonObject(
243257
mapOf(
244-
"query" to
258+
"query" to
245259
JsonPrimitive(
246260
"""
247261
query {
@@ -267,8 +281,8 @@ class TestReportGenerator(private val apiToken: String) {
267281
}
268282
}
269283
"""
270-
),
271-
)
284+
)
285+
)
272286
)
273287
}
274288

@@ -285,13 +299,14 @@ class TestReportGenerator(private val apiToken: String) {
285299
*/
286300
private fun <T> request(uri: URI, clazz: Class<T>, payload: JsonObject? = null): T {
287301
val request =
288-
HttpRequest.newBuilder().apply {
289-
if (payload == null) {
290-
GET()
291-
} else {
292-
POST(HttpRequest.BodyPublishers.ofString(payload.toString()))
302+
HttpRequest.newBuilder()
303+
.apply {
304+
if (payload == null) {
305+
GET()
306+
} else {
307+
POST(HttpRequest.BodyPublishers.ofString(payload.toString()))
308+
}
293309
}
294-
}
295310
.uri(uri)
296311
.header("Authorization", "Bearer $apiToken")
297312
.header("X-GitHub-Api-Version", "2022-11-28")

0 commit comments

Comments
 (0)