Skip to content

Commit

Permalink
fix(downloader): correct snyk-to-html url
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Aug 5, 2024
1 parent b2ce8e5 commit 580a078
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
import static java.lang.String.format;

public class DownloadService {
public static final String SNYK_INTEGRATION_NAME = "JENKINS_PLUGIN";
private static final String SNYK_DOWNLOAD_PRIMARY = "https://downloads.snyk.io/%s/%s/%s";
private static final String SNYK_DOWNLOAD_SECONDARY = "https://static.snyk.io/%s/%s/%s";
public static final String SNYK_INTEGRATION_NAME = "JENKINS_PLUGIN";
public static final List<String> SNYK_CLI_DOWNLOAD_URLS = Collections.unmodifiableList(Arrays.asList(SNYK_DOWNLOAD_PRIMARY, SNYK_DOWNLOAD_SECONDARY));

private DownloadService() {
// squid:S1118
}

public static URL constructDownloadUrlForSnyk(@Nonnull String urlTemplate, @Nonnull String product, @Nonnull String version, @Nonnull Platform platform) throws IOException {
URL urlNoUtm = new URL(format(urlTemplate, product, version, platform.snykWrapperFileName));
URL urlNoUtm;

if (product.equals("cli")) {
urlNoUtm = new URL(format(urlTemplate, product, version, platform.snykWrapperFileName));
} else { // snyk-to-html
urlNoUtm = new URL(format(urlTemplate, product, version, platform.snykToHtmlWrapperFileName));
}
return new URL(urlNoUtm.toString() + "?utm_source=" + SNYK_INTEGRATION_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.snyk.jenkins.tools.internal;

import org.junit.Test;

import io.snyk.jenkins.tools.Platform;

import java.io.IOException;
import java.net.URL;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;


public class DownloadServiceTest {

@Test
public void constructDownloadUrlForSnyk_shouldReturnExpectedUrlForCli() throws IOException {
String urlTemplate = "https://downloads.snyk.io/%s/%s/%s";
String product = "cli";
String version = "stable";
String queryParam = "?utm_source=" + DownloadService.SNYK_INTEGRATION_NAME;
Platform platform = Platform.MAC_OS;

URL expectedUrl = new URL("https://downloads.snyk.io/" + product + "/" + version + "/" + "snyk-macos" + queryParam);
URL actualUrl = DownloadService.constructDownloadUrlForSnyk(urlTemplate, product, version, platform);

assertThat(actualUrl, notNullValue());
assertThat(actualUrl, equalTo(expectedUrl));
}

@Test
public void constructDownloadUrlForSnyk_shouldReturnExpectedUrlForSnykToHtml() throws IOException {
String urlTemplate = "https://downloads.snyk.io/%s/%s/%s";
String product = "snyk-to-html";
String version = "stable";
String queryParam = "?utm_source=" + DownloadService.SNYK_INTEGRATION_NAME;
Platform platform = Platform.MAC_OS;

URL expectedUrl = new URL("https://downloads.snyk.io/" + product + "/" + version + "/" + "snyk-to-html-macos" + queryParam);
URL actualUrl = DownloadService.constructDownloadUrlForSnyk(urlTemplate, product, version, platform);

assertThat(actualUrl, notNullValue());
assertThat(actualUrl, equalTo(expectedUrl));
}
}

0 comments on commit 580a078

Please sign in to comment.