Skip to content

Build Scan URL parsing is more resilient #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ private static URL extractBaseUrl(URL buildScanUrl) {

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

private static URL toURL(String url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.gradle.enterprise.model;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.net.URL;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class NumberedBuildScanTests {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇 I love that you added tests for this!


@ParameterizedTest
@ValueSource(strings = {
"https://ge.example.com/s/7slcdesxr2xnw",
"https://ge.example.com/s/7slcdesxr2xnw/",
"https://ge.example.com/s/7slcdesxr2xnw/timeline",
"https://ge.example.com/s/7slcdesxr2xnw/console-log?page=1",
"https://ge.example.com/s/7slcdesxr2xnw/projects#:foo-service"
})
void validBuildScanUrlsAreCorrectlyParsed(String buildScanUrl) {
final NumberedBuildScan numberedBuildScan = NumberedBuildScan.parse("0," + buildScanUrl);
assertAll(
() -> assertEquals(new URL("https://ge.example.com"), numberedBuildScan.baseUrl()),
() -> assertEquals("7slcdesxr2xnw", numberedBuildScan.buildScanId())
);
}

@ParameterizedTest
@ValueSource(strings = {
"https://ge.example.com",
"https://ge.example.com/",
"https://ge.example.com/s",
"https://ge.example.com/s/",
"https://ge.example.com/S/",
"https://ge.example.com/S/7slcdesxr2xnw",
})
void invalidBuildScanUrlsThrowException(String buildScanUrl) {
assertThrows(IllegalArgumentException.class, () -> NumberedBuildScan.parse("0," + buildScanUrl));
}
}
1 change: 1 addition & 0 deletions release/changes.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- [FIX] Logging level when fetching Build Scan data from Gradle Enterprise is incorrect
- [FIX] Experiments accepting Build Scan URLs incorrectly parse Build Scan ID when path contains extra parts