Skip to content

Commit

Permalink
Merge branch 'master' into trello_bugfix_2201_missing_custom_batch_id
Browse files Browse the repository at this point in the history
  • Loading branch information
itaibh authored Mar 14, 2023
2 parents 3644895 + 4673f27 commit cc42d28
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
- Bump JobDSL to `1.72` to rely on non-vulnerable version.
- Rely on Jenkins core BOM for workflow plugins versions.

## [1.14] - 2023-02-27
### Fixed
- Fixed crash on newer Jenkins versions.

## [1.12] - 2019-11-13
### Added
- Allow setting Batch ID explicitly.

## [1.11] - 2019-11-13
### Added
- This CHANGELOG file.
- Batch close & notification support.
- Batch close & notification support.
15 changes: 11 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@
<version>${plugins.job-dsl.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness-htmlunit</artifactId>
<version>2.18-1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>m.g.o-public</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/com/applitools/jenkins/ApplitoolsCommon.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.applitools.jenkins;

import hudson.model.Build;
import hidden.jth.org.apache.http.client.methods.HttpDelete;
import hidden.jth.org.apache.http.client.methods.HttpUriRequest;
import hidden.jth.org.apache.http.client.utils.URIBuilder;
import hidden.jth.org.apache.http.impl.client.HttpClientBuilder;
import hidden.jth.org.apache.http.client.HttpClient;
import hudson.model.JobProperty;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.util.VirtualFile;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -87,27 +90,41 @@ public static void buildEnvVariablesForExternalUsage(Map<String, String> env, fi

public static void closeBatch(Run run, TaskListener listener, String serverURL, boolean notifyByCompletion, String applitoolsApiKey) throws IOException {
if (notifyByCompletion && applitoolsApiKey != null && !applitoolsApiKey.isEmpty()) {
String batchId = ApplitoolsStatusDisplayAction.generateBatchId(run.getParent().getDisplayName(), run.getNumber(), run.getTimestamp(), ApplitoolsCommon.checkApplitoolsArtifacts(run.getArtifacts(), run.getArtifactManager().root()));
HttpClient httpClient = new HttpClient();
URI targetUrl = new URI(serverURL, false);
targetUrl.setPath(String.format(BATCH_NOTIFICATION_PATH, batchId));
targetUrl.setQuery("apiKey=" + applitoolsApiKey);
String batchId = ApplitoolsStatusDisplayAction.generateBatchId(
run.getParent().getDisplayName(),
run.getNumber(),
run.getTimestamp(),
ApplitoolsCommon.checkApplitoolsArtifacts(
run.getArtifacts(),
run.getArtifactManager().root()
)
);
HttpClient httpClient = HttpClientBuilder.create().build();
URI targetUrl = null;
try {
targetUrl = new URIBuilder(serverURL)
.setPath(String.format(BATCH_NOTIFICATION_PATH, batchId))
.addParameter("apiKey", applitoolsApiKey)
.build();
} catch (URISyntaxException e) {
logger.warning("Couldn't build URI: " + e.getMessage());
}

DeleteMethod deleteRequest = new DeleteMethod(targetUrl.toString());
HttpUriRequest deleteRequest = new HttpDelete(targetUrl);
try {
listener.getLogger().println(String.format("Batch notification called with %s", batchId));
int statusCode = httpClient.executeMethod(deleteRequest);
listener.getLogger().printf("Batch notification called with %s%n", batchId);
int statusCode = httpClient.execute(deleteRequest).getStatusLine().getStatusCode();
listener.getLogger().println("Delete batch is done with " + Integer.toString(statusCode) + " status");
} finally {
deleteRequest.releaseConnection();
deleteRequest.abort();
}

}

}

public static Map<String, String> checkApplitoolsArtifacts(List<Run.Artifact> artifactList, VirtualFile file) {
Map<String, String> result = new HashMap();
Map<String, String> result = new HashMap<>();
if (artifactList.size() > 0 && file != null) {
for (Run.Artifact artifact : artifactList) {
String artifactFileName = artifact.getFileName();
Expand All @@ -119,7 +136,7 @@ public static Map<String, String> checkApplitoolsArtifacts(List<Run.Artifact> ar
String value = IOUtils.toString(stream, StandardCharsets.UTF_8.name()).replaceAll(System.getProperty("line.separator"), "");
result.put(artifactName, value);
} catch (java.io.IOException e) {
logger.warning("Could't get artifact " + artifactFileName + "." + e.getMessage());
logger.warning("Couldn't get artifact " + artifactFileName + "." + e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public ApplitoolsStatusDisplayAction(Run build) {

@Override
public String getIframeText() {
this.applitoolsValuesFromArtifacts = ApplitoolsCommon.checkApplitoolsArtifacts(this.build.getArtifacts(), this.build.getArtifactManager().root());
this.applitoolsValuesFromArtifacts =
ApplitoolsCommon.checkApplitoolsArtifacts(
this.build.getArtifacts(),
this.build.getArtifactManager().root());
try{
String iframeURL = generateIframeURL();
if (iframeURL == null)
Expand Down Expand Up @@ -92,35 +95,15 @@ private String generateIframeURL()
return serverURL + "/app/batchesnoauth/?startInfoBatchId=" + generateBatchId() + "&hideBatchList=true&intercom=false&agentId=eyes-jenkins-1.13";
}

// private void checkApplitoolsArtifacts() {
// this.applitoolsValuesFromArtifacts = new HashMap();
// ArtifactManager artifactManager = build.getArtifactManager();
// List<Run.Artifact> artifactList = build.getArtifacts();
// if (artifactList.size() > 0 && artifactManager != null) {
// for (Run.Artifact artifact : artifactList) {
// String artifactFileName = artifact.getFileName();
// Matcher m = artifactRegexp.matcher(artifactFileName);
// if (m.find()) {
// String artifactName = m.group(1);
// try {
// InputStream stream = artifactManager.root().child(artifactFileName).open();
// String value = IOUtils.toString(stream, StandardCharsets.UTF_8.name()).replaceAll(System.getProperty("line.separator"), "");
// this.applitoolsValuesFromArtifacts.put(artifactName, value);
// } catch (java.io.IOException e) {
// logger.warning("Could't get artifact " + artifactFileName + "." + e.getMessage());
// }
// }
// }
// }
// }

public static String generateBatchId(String projectName, int buildNumber, Calendar buildTimestamp) {
return generateBatchId(projectName, buildNumber, buildTimestamp, null);
}

public static String generateBatchId(String projectName, int buildNumber, Calendar buildTimestamp, Map<String, String> applitoolsValuesFromArtifacts)
public static String generateBatchId(String projectName, int buildNumber, Calendar buildTimestamp,
Map<String, String> applitoolsValuesFromArtifacts)
{
if (applitoolsValuesFromArtifacts != null && applitoolsValuesFromArtifacts.containsKey(ApplitoolsEnvironmentUtil.APPLITOOLS_BATCH_ID)) {
if (applitoolsValuesFromArtifacts != null &&
applitoolsValuesFromArtifacts.containsKey(ApplitoolsEnvironmentUtil.APPLITOOLS_BATCH_ID)) {
return applitoolsValuesFromArtifacts.get(ApplitoolsEnvironmentUtil.APPLITOOLS_BATCH_ID);
} else {
final String BATCH_ID_PREFIX = "jenkins";
Expand Down

0 comments on commit cc42d28

Please sign in to comment.