1515 */
1616package com.google.firebase.gradle.plugins.report
1717
18- import java.io.FileWriter
18+ import java.io.File
1919import java.io.IOException
2020import java.net.URI
2121import java.net.http.HttpClient
@@ -35,8 +35,6 @@ import kotlinx.serialization.json.jsonPrimitive
3535import org.gradle.internal.Pair
3636import org.slf4j.Logger
3737import org.slf4j.LoggerFactory
38- import java.io.File
39- import kotlin.io.use
4038
4139@SuppressWarnings(" NewApi" )
4240class 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