Skip to content

Commit 2f68f96

Browse files
authored
Merge pull request #398 from gradle/erichaagdev/flexible-build-scan-url-parsing
Build Scan URL parsing is more resilient
2 parents f0b8b5d + 74ca0d2 commit 2f68f96

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

components/fetch-build-scan-data-cmdline-tool/src/main/java/com/gradle/enterprise/model/NumberedBuildScan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ private static URL extractBaseUrl(URL buildScanUrl) {
6565

6666
private static String extractBuildScanId(URL buildScanUrl) {
6767
String[] pathSegments = buildScanUrl.getPath().split("/");
68-
if (pathSegments.length == 0) {
68+
if (pathSegments.length <= 2 || !pathSegments[1].equals("s")) {
6969
throw new IllegalArgumentException("Invalid Build Scan URL: " + buildScanUrl);
7070
}
71-
return pathSegments[pathSegments.length - 1];
71+
return pathSegments[2];
7272
}
7373

7474
private static URL toURL(String url) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.gradle.enterprise.model;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.ValueSource;
5+
6+
import java.net.URL;
7+
8+
import static org.junit.jupiter.api.Assertions.assertAll;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
class NumberedBuildScanTests {
13+
14+
@ParameterizedTest
15+
@ValueSource(strings = {
16+
"https://ge.example.com/s/7slcdesxr2xnw",
17+
"https://ge.example.com/s/7slcdesxr2xnw/",
18+
"https://ge.example.com/s/7slcdesxr2xnw/timeline",
19+
"https://ge.example.com/s/7slcdesxr2xnw/console-log?page=1",
20+
"https://ge.example.com/s/7slcdesxr2xnw/projects#:foo-service"
21+
})
22+
void validBuildScanUrlsAreCorrectlyParsed(String buildScanUrl) {
23+
final NumberedBuildScan numberedBuildScan = NumberedBuildScan.parse("0," + buildScanUrl);
24+
assertAll(
25+
() -> assertEquals(new URL("https://ge.example.com"), numberedBuildScan.baseUrl()),
26+
() -> assertEquals("7slcdesxr2xnw", numberedBuildScan.buildScanId())
27+
);
28+
}
29+
30+
@ParameterizedTest
31+
@ValueSource(strings = {
32+
"https://ge.example.com",
33+
"https://ge.example.com/",
34+
"https://ge.example.com/s",
35+
"https://ge.example.com/s/",
36+
"https://ge.example.com/S/",
37+
"https://ge.example.com/S/7slcdesxr2xnw",
38+
})
39+
void invalidBuildScanUrlsThrowException(String buildScanUrl) {
40+
assertThrows(IllegalArgumentException.class, () -> NumberedBuildScan.parse("0," + buildScanUrl));
41+
}
42+
}

release/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- [FIX] Logging level when fetching Build Scan data from Gradle Enterprise is incorrect
2+
- [FIX] Experiments accepting Build Scan URLs incorrectly parse Build Scan ID when path contains extra parts

0 commit comments

Comments
 (0)