Skip to content

Commit 4b47d73

Browse files
committed
Add regex
1 parent 9abb3cc commit 4b47d73

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,10 @@ class TestReportGenerator(private val apiToken: String) {
330330
.map { link: String ->
331331
val parts = link.split(",").dropLastWhile { it.isEmpty() }
332332
for (part in parts) {
333-
if (part.endsWith("rel=\"next\"")) {
334-
// <foo>; rel="next" -> foo
335-
val url =
336-
part
337-
.split(">;")
338-
.dropLastWhile { it.isEmpty() }
339-
.toTypedArray()[0]
340-
.split("<")
341-
.dropLastWhile { it.isEmpty() }
342-
.toTypedArray()[1]
343-
val p = request<JsonObject>(URI.create(url), JsonObject::class.java)
333+
val m = NEXT_LINK_REGEX.matchEntire(part)
334+
if (m != null) {
335+
val url = m.groups[0]?.value ?: throw RuntimeException("Malformed groups")
336+
val p = request(URI.create(url), JsonObject::class.java)
344337
return@map JsonObject(
345338
json.keys.associateWith { key: String ->
346339
if (json[key] is JsonArray && p.containsKey(key) && p[key] is JsonArray) {
@@ -368,5 +361,29 @@ class TestReportGenerator(private val apiToken: String) {
368361
companion object {
369362
private const val URL_PREFIX = "https://api.github.com/repos/firebase/firebase-android-sdk/"
370363
private const val GITHUB_API_VERSION = "2022-11-28"
364+
// Pulls the URL corresponding to the rel="next" link header, if present.
365+
// Ignores other link header values ("prev", etc) and ignores parameters
366+
// eg `<http://www.foo.bar/>; baz="qux"; rel="next";` -> `http://www.foo.bar/`
367+
private val NEXT_LINK_REGEX =
368+
Regex(
369+
"<" + // eg `<http://www.foo.bar/>`
370+
"(" + // URL group
371+
/**/ "[^>]*" + // Only ignoring `>`, other illegal characters assumed not present
372+
")" +
373+
">" +
374+
"\\s*" +
375+
";" + // Link separator
376+
"(" + // Ignore other parameters, eg `foo="bar";`
377+
/**/ "\\s*" +
378+
/**/ "\\w+" + // Key
379+
/**/ "=" +
380+
/**/ "\"\\w*\"" + // Quoted value
381+
/**/ "\\s*" +
382+
/**/ ";" +
383+
")" +
384+
"\\s*" +
385+
"rel=\"next\""
386+
// "<([^>]*)>\\s*;(\\s*\\w+=\"\\w*\"\\s*;)\\s*rel=\"next\""
387+
)
371388
}
372389
}

0 commit comments

Comments
 (0)