Skip to content

Commit

Permalink
Fix User Access Key With File Steps
Browse files Browse the repository at this point in the history
  • Loading branch information
aboe026 committed Jul 17, 2019
1 parent c8cecaf commit ac57de6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.http.entity.ContentType;

import com.ibm.devops.connect.CloudPublisher;
import com.ibm.devops.connect.DevOpsGlobalConfiguration;

public class UploadJUnitTestResult extends Builder implements SimpleBuildStep {

Expand All @@ -57,8 +58,10 @@ public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher
throws AbortException, InterruptedException, IOException {

Object fatalFailure = this.properties.get("fatal");
String buildUrl = Jenkins.getInstance().getRootUrl() + build.getUrl();
String userAccessKey = Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).getApiToken();

boolean success = workspace.act(new FileUploader(this.properties, listener, Jenkins.getInstance().getRootUrl() + build.getUrl(), CloudPublisher.getQualityDataUrl()));
boolean success = workspace.act(new FileUploader(this.properties, listener, buildUrl, CloudPublisher.getQualityDataUrl(), userAccessKey));
if (!success) {
if (fatalFailure != null && fatalFailure.toString().equals("true")) {
build.setResult(Result.FAILURE);
Expand Down Expand Up @@ -103,12 +106,14 @@ private static final class FileUploader implements FileCallable<Boolean> {
private TaskListener listener;
private String buildUrl;
private String postUrl;
private String userAccessKey;

public FileUploader(Map<String, String> properties, TaskListener listener, String buildUrl, String postUrl) {
public FileUploader(Map<String, String> properties, TaskListener listener, String buildUrl, String postUrl, String userAccessKey) {
this.properties = properties;
this.listener = listener;
this.buildUrl = buildUrl;
this.postUrl = postUrl;
this.userAccessKey = userAccessKey;
}

@Override public Boolean invoke(File f, VirtualChannel channel) {
Expand Down Expand Up @@ -169,7 +174,7 @@ public FileUploader(Map<String, String> properties, TaskListener listener, Strin

boolean success = false;
try {
success = CloudPublisher.uploadQualityData(entity, postUrl);
success = CloudPublisher.uploadQualityData(entity, postUrl, userAccessKey);
} catch (Exception ex) {
listener.error("Error uploading quality data: " + ex.getClass() + " - " + ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.http.entity.ContentType;

import com.ibm.devops.connect.CloudPublisher;
import com.ibm.devops.connect.DevOpsGlobalConfiguration;

public class UploadMetricsFile extends Builder implements SimpleBuildStep {

Expand Down Expand Up @@ -208,8 +209,9 @@ public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher

listener.getLogger().println("Uploading metric \"" + name + "\" to UrbanCode Velocity...");

boolean success = workspace.act(new FileUploader(filePath, payload.toString(), listener, CloudPublisher.getQualityDataUrl()));
String userAccessKey = Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).getApiToken();

boolean success = workspace.act(new FileUploader(filePath, payload.toString(), listener, CloudPublisher.getQualityDataUrl(), userAccessKey));
if (!success) {
if (this.fatal != null && this.fatal.toString().equals("true")) {
build.setResult(Result.FAILURE);
Expand Down Expand Up @@ -256,13 +258,15 @@ private static final class FileUploader implements FileCallable<Boolean> {
private String filePath;
private String payload;
private String postUrl;
private String userAccessKey;
private TaskListener listener;

public FileUploader(String filePath, String payload, TaskListener listener, String postUrl) {
public FileUploader(String filePath, String payload, TaskListener listener, String postUrl, String userAccessKey) {
this.filePath = filePath;
this.payload = payload;
this.listener = listener;
this.postUrl = postUrl;
this.userAccessKey = userAccessKey;
}

@Override public Boolean invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
Expand All @@ -283,7 +287,7 @@ public FileUploader(String filePath, String payload, TaskListener listener, Stri

boolean success = false;
try {
success = CloudPublisher.uploadQualityData(entity, postUrl);
success = CloudPublisher.uploadQualityData(entity, postUrl, userAccessKey);
} catch (Exception ex) {
listener.error("Error uploading metric file: " + ex.getClass() + " - " + ex.getMessage());
listener.error("Stack trace:");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ibm/devops/connect/CloudPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ public static String checkGate(String pipelineId, String stageName, String versi
return resStr;
}

public static boolean uploadQualityData(HttpEntity entity, String url) throws Exception {
public static boolean uploadQualityData(HttpEntity entity, String url, String userAccessKey) throws Exception {
CloudPublisher.ensureHttpClientInitialized();
String localLogPrefix= logPrefix + "uploadQualityData ";
String resStr = "";
CloseableHttpResponse response = null;

try {
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader("Authorization", "UserAccessKey " + Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).getApiToken());
postMethod.setHeader("Authorization", "UserAccessKey " + userAccessKey);
postMethod.setEntity(entity);

response = httpClient.execute(postMethod);
Expand Down

0 comments on commit ac57de6

Please sign in to comment.