Skip to content

Commit

Permalink
TS-41530 Fix Gradle upload to Teamscale
Browse files Browse the repository at this point in the history
  • Loading branch information
DreierF committed Jan 17, 2025
1 parent 9667ca1 commit bbda641
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We use [semantic versioning](http://semver.org/):
- PATCH version when you make backwards compatible bug fixes.

# Next version
- [fix] _teamscale-gradle-plugin_: 401 Unauthorized error when trying to upload reports to Teamscale

# 34.2.1
- [fix] _agent_: Warning about multiple agents being present is misleading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -61,6 +62,8 @@ public class TeamscaleMockServer {
private List<String> impactedTests;
private final List<String> profilerEvents = new ArrayList<>();
private ProfilerConfiguration profilerConfiguration;
private String username = null;
private String accessToken = null;

public TeamscaleMockServer(int port) throws IOException {
service = Service.ignite();
Expand All @@ -78,6 +81,12 @@ public TeamscaleMockServer(int port) throws IOException {
}

/** Configures the server to accept report uploads and store them within the mock for later retrieval. */
public TeamscaleMockServer withAuthentication(String username, String accessToken) {
this.username = username;
this.accessToken = accessToken;
return this;
}

public TeamscaleMockServer acceptingReportUploads() {

Check warning on line 90 in common-system-test/src/main/java/com/teamscale/test/commons/TeamscaleMockServer.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

common-system-test/src/main/java/com/teamscale/test/commons/TeamscaleMockServer.java#L90

[New] Interface comment missing https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F41530_gradle_upload%3AHEAD&id=1A79A6C45132075D9AD7866B41D78853
service.post("api/v5.9.0/projects/:projectName/external-analysis/session/auto-create/report",
this::handleReport);
Expand Down Expand Up @@ -122,6 +131,8 @@ public TestwiseCoverageReport parseUploadedTestwiseCoverageReport(int index) thr
}

private String handleImpactedTests(Request request, Response response) throws IOException {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
impactedTestCommits.add(request.queryParams("end-revision") + ", " + request.queryParams("end"));
impactedTestRepositories.add(request.queryParams("repository"));
Expand All @@ -132,6 +143,8 @@ private String handleImpactedTests(Request request, Response response) throws IO
}

private String handleProfilerRegistration(Request request, Response response) throws JsonProcessingException {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
profilerEvents.add(
"Profiler registered and requested configuration " + request.queryParams("configuration-id"));
Expand All @@ -142,18 +155,24 @@ private String handleProfilerRegistration(Request request, Response response) th
}

private String handleProfilerHeartbeat(Request request, Response response) {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
profilerEvents.add("Profiler " + request.params(":profilerId") + " sent heartbeat");
return "";
}

private String handleProfilerLogs(Request request, Response response) {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
profilerEvents.add("Profiler " + request.params(":profilerId") + " sent logs");
return "";
}

private String handleProfilerUnregister(Request request, Response response) {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
profilerEvents.add("Profiler " + request.params(":profilerId") + " unregistered");
return "foo";
Expand All @@ -164,6 +183,8 @@ public List<String> getProfilerEvents() {
}

private String handleReport(Request request, Response response) throws IOException, ServletException {
requireAuthentication(request, response);

collectedUserAgents.add(request.headers("User-Agent"));
uploadCommits.add(request.queryParams("revision") + ", " + request.queryParams("t"));
uploadRepositories.add(request.queryParams("repository"));
Expand All @@ -189,4 +210,18 @@ public void shutdown() {
service.awaitStop();
}

private void requireAuthentication(Request request, Response response) {
if (username != null && accessToken != null) {
String authHeader = request.headers("Authorization");
if (authHeader == null || !authHeader.equals(buildBasicAuthHeader(username, accessToken))) {
response.status(401);
throw new IllegalArgumentException("Unauthorized");
}
}
}

private String buildBasicAuthHeader(String username, String accessToken) {
String credentials = username + ":" + accessToken;
return "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public void systemTest() throws Exception {
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "OFF");

TeamscaleMockServer teamscaleMockServer = new TeamscaleMockServer(SystemTestUtils.TEAMSCALE_PORT).acceptingReportUploads();
TeamscaleMockServer teamscaleMockServer = new TeamscaleMockServer(SystemTestUtils.TEAMSCALE_PORT)
.withAuthentication("fake", "fake")
.acceptingReportUploads();

new SystemUnderTest().foo();
SystemTestUtils.dumpCoverage(SystemTestUtils.AGENT_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class TiaMavenSystemTest {

@BeforeEach
public void startFakeTeamscaleServer() throws Exception {
teamscaleMockServer = new TeamscaleMockServer(SystemTestUtils.TEAMSCALE_PORT).acceptingReportUploads()
teamscaleMockServer = new TeamscaleMockServer(SystemTestUtils.TEAMSCALE_PORT)
.withAuthentication("build", "6lJKEvNHeTxGPhMAi4D84DWqzoSFL1p4")
.acceptingReportUploads()
.withImpactedTests("bar/UnitTest/utBla()", "bar/UnitTest/utFoo()",
"bar/IntegIT/itBla()", "bar/IntegIT/itFoo()");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ data class ServerConfiguration(
}
fun toClient() = TeamscaleClient(
url,
project ?: throw GradleException("Teamscale project name must not be null!"),
userName ?: throw GradleException("Teamscale user name must not be null!"),
userAccessToken ?: throw GradleException("Teamscale user access token must not be null!")
userAccessToken ?: throw GradleException("Teamscale user access token must not be null!"),
project ?: throw GradleException("Teamscale project name must not be null!")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TeamscalePluginTest {
@BeforeEach
fun startFakeTeamscaleServer() {
teamscaleMockServer = TeamscaleMockServer(FAKE_TEAMSCALE_PORT)
.withAuthentication("build", "82l1jtkIx6xG7DDG34FLsKhejcHz1cMu")
.acceptingReportUploads()
.withImpactedTests("com/example/project/JUnit4Test/systemTest")
}
Expand Down

0 comments on commit bbda641

Please sign in to comment.