From baf7c88207cda4760530fc3a7ce623a3f7b4fd3e Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 22 Oct 2024 16:17:31 +0900 Subject: [PATCH 1/4] Use Java 1.8 idiom --- pom.xml | 4 ++-- src/main/java/io/jenkins/plugins/launchable/Ingester.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index b920eaf..31fcdc3 100644 --- a/pom.xml +++ b/pom.xml @@ -27,8 +27,8 @@ org.apache.maven.plugins maven-compiler-plugin - 10 - 10 + 1.8 + 1.8 diff --git a/src/main/java/io/jenkins/plugins/launchable/Ingester.java b/src/main/java/io/jenkins/plugins/launchable/Ingester.java index 08b6ab7..a587eb7 100644 --- a/src/main/java/io/jenkins/plugins/launchable/Ingester.java +++ b/src/main/java/io/jenkins/plugins/launchable/Ingester.java @@ -60,9 +60,9 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti if (endpoint==null) { endpoint = DEFAULT_UPLOAD_URL; } - var hc = new HttpPost(endpoint); + HttpPost hc = new HttpPost(endpoint); - var builder = MultipartEntityBuilder.create(); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("metadata", properties.build().toString(), ContentType.APPLICATION_JSON); builder.addBinaryBody("file", report, ContentType.APPLICATION_XML, "junitResult.xml"); From 07e3d55aab598b0559ae12c8527d8ff1c83c2548 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 22 Oct 2024 16:17:59 +0900 Subject: [PATCH 2/4] to fix spotbugs error --- src/main/java/io/jenkins/plugins/launchable/Ingester.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/launchable/Ingester.java b/src/main/java/io/jenkins/plugins/launchable/Ingester.java index a587eb7..bd05d32 100644 --- a/src/main/java/io/jenkins/plugins/launchable/Ingester.java +++ b/src/main/java/io/jenkins/plugins/launchable/Ingester.java @@ -77,7 +77,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti } } } - } catch (Exception e) { + } catch (IOException e) { // don't let our bug get in the way of orderly execution of jobs, as that'd be the fasest way to // get kicked out of installations. LOGGER.log(Level.WARNING, "Failed to submit test results", e); From 78d1b34574e6e892bbcc033705f7e4cda4cfaa2f Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 22 Oct 2024 23:47:59 +0900 Subject: [PATCH 3/4] build request path --- .../jenkins/plugins/launchable/Ingester.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/launchable/Ingester.java b/src/main/java/io/jenkins/plugins/launchable/Ingester.java index bd05d32..4d72798 100644 --- a/src/main/java/io/jenkins/plugins/launchable/Ingester.java +++ b/src/main/java/io/jenkins/plugins/launchable/Ingester.java @@ -52,15 +52,18 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti if (apiKey==null) return; // not yet configured + OrganizationWorkspace orgWs = OrganizationWorkspace.fromApiKey(apiKey.getPlainText()); + // attempted to use JDK HttpRequest, but gave up due to the lack of multipart support // TODO: how do I obtain a properly configured HttpClient for the proxy setting in Jenkins? try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + String endpoint = System.getenv("INSIGHT_UPLOAD_URL") ; - String endpoint = System.getenv("INSIGHT_UPLOAD_URL"); if (endpoint==null) { endpoint = DEFAULT_UPLOAD_URL; } - HttpPost hc = new HttpPost(endpoint); + HttpPost hc = new HttpPost(String.format("%s/intake/organizations/%s/workspaces/%s/events/jenkins", endpoint, orgWs.getOrganization(), orgWs.getWorkspace())); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("metadata", properties.build().toString(), ContentType.APPLICATION_JSON); @@ -86,4 +89,40 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti private static final Logger LOGGER = Logger.getLogger(Ingester.class.getName()); private static final String DEFAULT_UPLOAD_URL = "https://api.mercury.launchableinc.com/TODO"; + + private static class OrganizationWorkspace { + private String organization; + + private String workspace; + + private OrganizationWorkspace() { + } + + private OrganizationWorkspace(String organization, String workspace) { + this.organization = organization; + this.workspace = workspace; + } + + static OrganizationWorkspace fromApiKey(String key) { + String[] splits = key.split(":", 3); + if (!(splits.length == 3)) { + return new OrganizationWorkspace(); + } + + String[] user = splits[1].split("/",2); + if (!(user.length == 2)) { + return new OrganizationWorkspace(); + } + + return new OrganizationWorkspace(user[0], user[1]); + } + + public String getOrganization() { + return organization; + } + + public String getWorkspace() { + return workspace; + } + } } From 61a25537d45d229841508ad4d000fc9ea912de85 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Wed, 23 Oct 2024 14:00:33 +0900 Subject: [PATCH 4/4] set content-encoding/gzip header --- src/main/java/io/jenkins/plugins/launchable/Ingester.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/launchable/Ingester.java b/src/main/java/io/jenkins/plugins/launchable/Ingester.java index 4d72798..0b10019 100644 --- a/src/main/java/io/jenkins/plugins/launchable/Ingester.java +++ b/src/main/java/io/jenkins/plugins/launchable/Ingester.java @@ -1,21 +1,19 @@ package io.jenkins.plugins.launchable; import hudson.Extension; -import hudson.model.Descriptor; import hudson.tasks.junit.TestResult; import hudson.util.Secret; -import hudson.util.TextFile; import jenkins.model.GlobalConfiguration; import net.sf.json.JSONObject; -import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; import org.apache.http.client.entity.GzipCompressingEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; import org.kohsuke.stapler.StaplerRequest; import java.io.File; @@ -64,13 +62,13 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti } HttpPost hc = new HttpPost(String.format("%s/intake/organizations/%s/workspaces/%s/events/jenkins", endpoint, orgWs.getOrganization(), orgWs.getWorkspace())); - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("metadata", properties.build().toString(), ContentType.APPLICATION_JSON); builder.addBinaryBody("file", report, ContentType.APPLICATION_XML, "junitResult.xml"); hc.setEntity(new GzipCompressingEntity(builder.build())); hc.addHeader("Authorization", "Bearer " + apiKey.getPlainText()); + hc.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); try (CloseableHttpResponse response = httpClient.execute(hc)) { if (response.getStatusLine().getStatusCode() >= 300) {