-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix some points to integrate #6
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
@@ -52,22 +50,25 @@ 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; | ||
} | ||
var hc = new HttpPost(endpoint); | ||
HttpPost hc = new HttpPost(String.format("%s/intake/organizations/%s/workspaces/%s/events/jenkins", endpoint, orgWs.getOrganization(), orgWs.getWorkspace())); | ||
|
||
var builder = MultipartEntityBuilder.create(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to use Java 10 idiom |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We talked it this part directly, it seems server side can't handle compressed request correctly. |
||
|
||
try (CloseableHttpResponse response = httpClient.execute(hc)) { | ||
if (response.getStatusLine().getStatusCode() >= 300) { | ||
|
@@ -77,7 +78,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); | ||
|
@@ -86,4 +87,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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with assembling URL part. Can I use
String.format
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this looks great