From 63069d1323b7497c2db805bf569b964887eeb9da Mon Sep 17 00:00:00 2001 From: Asaf Gabai Date: Thu, 2 Nov 2023 12:40:14 +0200 Subject: [PATCH] External resources repository can also be configured using an environment variable. --- .../idea/configuration/GlobalSettings.java | 13 ++++--- .../idea/configuration/ServerConfigImpl.java | 38 +++++++++++++------ .../ide/idea/scan/ScanBinaryExecutor.java | 23 ++++++----- .../com/jfrog/ide/idea/scan/ScanManager.java | 2 +- .../com/jfrog/ide/idea/scan/ScannerBase.java | 3 -- .../idea/scan/SourceCodeScannerManager.java | 3 -- .../ide/idea/ui/JFrogLocalToolWindow.java | 2 +- .../JFrogGlobalConfiguration.form | 6 +-- .../JFrogGlobalConfiguration.java | 32 ++++++++-------- .../idea/configuration/ConfigurationTest.java | 32 ++++++++++++---- .../ide/idea/scan/ScanBinaryExecutorTest.java | 14 +++---- 11 files changed, 97 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/jfrog/ide/idea/configuration/GlobalSettings.java b/src/main/java/com/jfrog/ide/idea/configuration/GlobalSettings.java index 4e264c05..85ae3b5d 100644 --- a/src/main/java/com/jfrog/ide/idea/configuration/GlobalSettings.java +++ b/src/main/java/com/jfrog/ide/idea/configuration/GlobalSettings.java @@ -28,6 +28,7 @@ import com.jfrog.ide.idea.events.ApplicationEvents; import com.jfrog.ide.idea.log.Logger; import lombok.Getter; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; @@ -66,7 +67,7 @@ public GlobalSettings getState() { serverConfig.setWatches(this.serverConfig.getWatches()); serverConfig.setConnectionRetries(this.serverConfig.getConnectionRetries()); serverConfig.setConnectionTimeout(this.serverConfig.getConnectionTimeout()); - serverConfig.setCustomResourcesRepo(this.serverConfig.getCustomResourcesRepo()); + serverConfig.setExternalResourcesRepo(this.serverConfig.getExternalResourcesRepo()); GlobalSettings settings = new GlobalSettings(); settings.serverConfig = serverConfig; @@ -81,11 +82,12 @@ public GlobalSettings getState() { @Override public void loadState(@NotNull GlobalSettings state) { XmlSerializerUtil.copyBean(state, this); + serverConfig.readMissingConfFromEnv(); } @Override public void noStateLoaded() { - reloadXrayCredentials(); + reloadMissingConfiguration(); } /** @@ -131,18 +133,19 @@ private void setAdvancedSettings(ServerConfigImpl serverConfig) { this.serverConfig.setExcludedPaths(serverConfig.getExcludedPaths()); this.serverConfig.setConnectionRetries(serverConfig.getConnectionRetries()); this.serverConfig.setConnectionTimeout(serverConfig.getConnectionTimeout()); - this.serverConfig.setCustomResourcesRepo(serverConfig.getCustomResourcesRepo()); + this.serverConfig.setExternalResourcesRepo(serverConfig.getExternalResourcesRepo()); this.serverConfig.setPolicyType(serverConfig.getPolicyType()); this.serverConfig.setProject(serverConfig.getProject()); this.serverConfig.setWatches(serverConfig.getWatches()); } /** - * Reloads Xray credentials. + * Reloads missing configuration from the plugin settings, environment variables or JFrog CLI configuration. * * @return true if credentials exist and Xray is configured, false otherwise. */ - public boolean reloadXrayCredentials() { + public boolean reloadMissingConfiguration() { + serverConfig.readMissingConfFromEnv(); if (serverConfig.isXrayConfigured()) { return true; } diff --git a/src/main/java/com/jfrog/ide/idea/configuration/ServerConfigImpl.java b/src/main/java/com/jfrog/ide/idea/configuration/ServerConfigImpl.java index a631dfa8..0a081cb2 100644 --- a/src/main/java/com/jfrog/ide/idea/configuration/ServerConfigImpl.java +++ b/src/main/java/com/jfrog/ide/idea/configuration/ServerConfigImpl.java @@ -33,6 +33,7 @@ import com.jfrog.ide.idea.ui.configuration.ConnectionRetriesSpinner; import com.jfrog.ide.idea.ui.configuration.ConnectionTimeoutSpinner; import org.apache.commons.collections4.CollectionUtils; +import org.codehaus.plexus.util.StringUtils; import org.jfrog.build.client.ProxyConfiguration; import javax.annotation.CheckForNull; @@ -62,14 +63,15 @@ public enum ConnectionType { } private static final String JFROG_SETTINGS_CREDENTIALS_KEY = "credentials"; - static final String ARTIFACTORY_URL_ENV = "JFROG_IDE_ARTIFACTORY_URL"; public static final String JFROG_SETTINGS_KEY = "com.jfrog.idea"; static final String PLATFORM_URL_ENV = "JFROG_IDE_PLATFORM_URL"; + static final String ARTIFACTORY_URL_ENV = "JFROG_IDE_ARTIFACTORY_URL"; static final String XRAY_URL_ENV = "JFROG_IDE_XRAY_URL"; static final String USERNAME_ENV = "JFROG_IDE_USERNAME"; static final String PASSWORD_ENV = "JFROG_IDE_PASSWORD"; static final String ACCESS_TOKEN_ENV = "JFROG_IDE_ACCESS_TOKEN"; static final String PROJECT_ENV = "JFROG_IDE_PROJECT"; + static final String EXTERNAL_RESOURCES_REPO_ENV = "JFROG_IDE_RELEASES_REPO"; @OptionTag private ConnectionType connectionType; @@ -101,7 +103,7 @@ public enum ConnectionType { @Tag private Integer connectionTimeout; @Tag - private String customResourcesRepo; + private String externalResourcesRepo; // The subsystem key of the plugin configuration in the PasswordSafe @Transient private String jfrogSettingsCredentialsKey = JFROG_SETTINGS_KEY; @@ -123,7 +125,7 @@ public enum ConnectionType { this.excludedPaths = builder.excludedPaths; this.connectionRetries = builder.connectionRetries; this.connectionTimeout = builder.connectionTimeout; - this.customResourcesRepo = builder.customResourcesRepo; + this.externalResourcesRepo = builder.externalResourcesRepo; this.jfrogSettingsCredentialsKey = builder.jfrogSettingsCredentialsKey; } @@ -159,13 +161,13 @@ public boolean equals(Object o) { Objects.equals(getExcludedPaths(), other.getExcludedPaths()) && getConnectionRetries() == other.getConnectionRetries() && getConnectionTimeout() == other.getConnectionTimeout() && - getCustomResourcesRepo() == other.getCustomResourcesRepo(); + getExternalResourcesRepo() == other.getExternalResourcesRepo(); } @Override public int hashCode() { return Objects.hash(getConnectionType(), getUrl(), getXrayUrl(), getArtifactoryUrl(), getPassword(), getAccessToken(), - getUsername(), getProject(), getExcludedPaths(), getConnectionRetries(), getConnectionTimeout(), getCustomResourcesRepo()); + getUsername(), getProject(), getExcludedPaths(), getConnectionRetries(), getConnectionTimeout(), getExternalResourcesRepo()); } @Override @@ -262,8 +264,8 @@ public int getConnectionTimeout() { } @Override - public String getCustomResourcesRepo() { - return this.customResourcesRepo; + public String getExternalResourcesRepo() { + return this.externalResourcesRepo; } public String getJFrogSettingsCredentialsKey() { @@ -411,8 +413,8 @@ void setConnectionTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; } - void setCustomResourcesRepo(String customResourcesRepo) { - this.customResourcesRepo = customResourcesRepo; + void setExternalResourcesRepo(String externalResourcesRepo) { + this.externalResourcesRepo = externalResourcesRepo; } public void setJFrogSettingsCredentialsKey(String jfrogSettingsCredentialsKey) { @@ -464,6 +466,18 @@ public void readConnectionDetailsFromEnv() { } } + /** + * Read missing configuration from environment variables. + */ + public void readMissingConfFromEnv() { + if (isBlank(getExternalResourcesRepo())) { + String externalResourcesRepoEnv = EnvironmentUtil.getValue(EXTERNAL_RESOURCES_REPO_ENV); + if (isNotBlank(externalResourcesRepoEnv)) { + setExternalResourcesRepo(externalResourcesRepoEnv); + } + } + } + /** * Read the connection details from JFrog CLI's config. The configuration is read by executing JFrog CLI. * If no JFrog CLI server configuration was found or the config @@ -518,7 +532,7 @@ public static class Builder { private String watches; private int connectionRetries; private int connectionTimeout; - private String customResourcesRepo; + private String externalResourcesRepo; public ServerConfigImpl build() { return new ServerConfigImpl(this); @@ -591,8 +605,8 @@ public Builder setConnectionTimeout(int connectionTimeout) { return this; } - public Builder setCustomResourcesRepo(String customResourcesRepo) { - this.customResourcesRepo = customResourcesRepo; + public Builder setExternalResourcesRepo(String externalResourcesRepo) { + this.externalResourcesRepo = externalResourcesRepo; return this; } diff --git a/src/main/java/com/jfrog/ide/idea/scan/ScanBinaryExecutor.java b/src/main/java/com/jfrog/ide/idea/scan/ScanBinaryExecutor.java index 3b75f14d..fa49e814 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/ScanBinaryExecutor.java +++ b/src/main/java/com/jfrog/ide/idea/scan/ScanBinaryExecutor.java @@ -47,7 +47,6 @@ import static com.jfrog.ide.idea.scan.ScanUtils.getOSAndArc; import static com.jfrog.ide.idea.utils.Utils.HOME_PATH; import static java.lang.String.join; -import static org.apache.commons.lang3.StringUtils.defaultIfEmpty; /** * @author Tal Arian @@ -112,10 +111,10 @@ protected void setOsDistribution() { } } - String getBinaryDownloadURL(String customResourcesRepo) { + String getBinaryDownloadURL(String externalResourcesRepo) { String downloadUrlPrefix = ""; - if (!StringUtils.isEmpty(customResourcesRepo)) { - downloadUrlPrefix = String.format("%s/artifactory/", customResourcesRepo); + if (!StringUtils.isEmpty(externalResourcesRepo)) { + downloadUrlPrefix = String.format("%s/artifactory/", externalResourcesRepo); } return String.format("%s%s/%s/%s", downloadUrlPrefix, BINARY_DOWNLOAD_URL, getOsDistribution(), DOWNLOAD_SCANNER_NAME); } @@ -197,13 +196,13 @@ private void updateBinaryIfNeeded() throws IOException { return; } ServerConfig server = GlobalSettings.getInstance().getServerConfig(); - String customResourcesRepo = server.getCustomResourcesRepo(); - ArtifactoryManagerBuilder artifactoryManagerBuilder = createManagerBuilder(StringUtils.isEmpty(customResourcesRepo), server); + String externalResourcesRepo = server.getExternalResourcesRepo(); + ArtifactoryManagerBuilder artifactoryManagerBuilder = createManagerBuilder(StringUtils.isEmpty(externalResourcesRepo), server); try (ArtifactoryManager artifactoryManager = artifactoryManagerBuilder.build()) { if (Files.exists(binaryTargetPath)) { // Check for new version of the binary try (FileInputStream archiveBinaryFile = new FileInputStream(archiveTargetPath.toFile())) { - String latestBinaryChecksum = getFileChecksumFromServer(artifactoryManager, customResourcesRepo); + String latestBinaryChecksum = getFileChecksumFromServer(artifactoryManager, externalResourcesRepo); String currentBinaryCheckSum = DigestUtils.sha256Hex(archiveBinaryFile); if (latestBinaryChecksum.equals(currentBinaryCheckSum)) { nextUpdateCheck = currentTime.plusDays(UPDATE_INTERVAL); @@ -215,13 +214,13 @@ private void updateBinaryIfNeeded() throws IOException { } else { log.debug(String.format("Resource %s is not found. Downloading it.", binaryTargetPath)); } - downloadBinary(artifactoryManager, customResourcesRepo); + downloadBinary(artifactoryManager, externalResourcesRepo); } } } - public String getFileChecksumFromServer(ArtifactoryManager artifactoryManager, String customResourcesRepo) throws IOException { - String url = getBinaryDownloadURL(customResourcesRepo); + public String getFileChecksumFromServer(ArtifactoryManager artifactoryManager, String externalResourcesRepo) throws IOException { + String url = getBinaryDownloadURL(externalResourcesRepo); Header[] headers = artifactoryManager.downloadHeaders(url); for (Header header : headers) { if (StringUtils.equalsIgnoreCase(header.getName(), "x-checksum-sha256")) { @@ -276,8 +275,8 @@ protected Output getOutputObj(Path outputFile) throws IOException { return om.readValue(outputFile.toFile(), Output.class); } - protected void downloadBinary(ArtifactoryManager artifactoryManager, String customResourcesRepo) throws IOException { - String downloadUrl = getBinaryDownloadURL(customResourcesRepo); + protected void downloadBinary(ArtifactoryManager artifactoryManager, String externalResourcesRepo) throws IOException { + String downloadUrl = getBinaryDownloadURL(externalResourcesRepo); File downloadArchive = artifactoryManager.downloadToFile(downloadUrl, archiveTargetPath.toString()); log.debug(String.format("Downloading: %s", downloadUrl)); if (downloadArchive == null) { diff --git a/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java b/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java index c95b670f..d849148d 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java +++ b/src/main/java/com/jfrog/ide/idea/scan/ScanManager.java @@ -76,7 +76,7 @@ public void startScan() { return; } - if (!GlobalSettings.getInstance().reloadXrayCredentials()) { + if (!GlobalSettings.getInstance().reloadMissingConfiguration()) { Logger.getInstance().error("Xray server is not configured."); return; } diff --git a/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java b/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java index 900c470a..e18248b1 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java +++ b/src/main/java/com/jfrog/ide/idea/scan/ScannerBase.java @@ -292,9 +292,6 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat if (project.isDisposed()) { return; } - if (!GlobalSettings.getInstance().reloadXrayCredentials()) { - throw new RuntimeException("Xray server is not configured."); - } // Prevent multiple simultaneous scans if (!scanInProgress.compareAndSet(false, true)) { log.info("Scan already in progress"); diff --git a/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java b/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java index aaefc128..72e8ef30 100644 --- a/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java +++ b/src/main/java/com/jfrog/ide/idea/scan/SourceCodeScannerManager.java @@ -122,9 +122,6 @@ public void run(@NotNull com.intellij.openapi.progress.ProgressIndicator indicat if (project.isDisposed()) { return; } - if (!GlobalSettings.getInstance().reloadXrayCredentials()) { - throw new RuntimeException("Xray server is not configured."); - } // Prevent multiple simultaneous scans if (!scanInProgress.compareAndSet(false, true)) { log.info("Advanced source code scan is already in progress"); diff --git a/src/main/java/com/jfrog/ide/idea/ui/JFrogLocalToolWindow.java b/src/main/java/com/jfrog/ide/idea/ui/JFrogLocalToolWindow.java index e3ca4f51..3382fd95 100644 --- a/src/main/java/com/jfrog/ide/idea/ui/JFrogLocalToolWindow.java +++ b/src/main/java/com/jfrog/ide/idea/ui/JFrogLocalToolWindow.java @@ -143,7 +143,7 @@ private void alertIfCacheExpired() { private void refreshView(boolean reloadCredentials) { GlobalSettings globalSettings = GlobalSettings.getInstance(); if ((!reloadCredentials && !globalSettings.areXrayCredentialsSet()) || - !globalSettings.reloadXrayCredentials()) { + !globalSettings.reloadMissingConfiguration()) { setLeftPanelContent(ComponentUtils.createNoCredentialsView()); return; } diff --git a/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.form b/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.form index cef93a36..890d1216 100644 --- a/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.form +++ b/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.form @@ -585,12 +585,12 @@ - + - + @@ -622,7 +622,7 @@ - + diff --git a/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.java b/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.java index 522a5446..2f949bd5 100644 --- a/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.java +++ b/src/main/java/com/jfrog/ide/idea/ui/configuration/JFrogGlobalConfiguration.java @@ -122,7 +122,7 @@ public class JFrogGlobalConfiguration implements Configurable, Configurable.NoSc private JBTextField excludedPaths; private ActionLink scanOptionsRestoreDefaultsActionLink; private ActionLink connectionOptionsRestoreDefaultsActionLink; - private JCheckBox useCustomRepositoryCheckBox; + private JCheckBox useExternalRepositoryCheckBox; private JLabel repositoryNameJLabel; private JBTextField repositoryNameJBTextField; private JLabel repositoryNameDescJLabel; @@ -148,7 +148,7 @@ public JFrogGlobalConfiguration() { // Advanced initConnectionOptionsRestoreDefaultsActionLink(); initScanOptionsRestoreDefaultsActionLink(); - initUseCustomRepositoryCheckBox(); + initUseExternalRepositoryCheckBox(); loadConfig(); } @@ -233,8 +233,8 @@ private ServerConfigImpl createServerConfig() { .setWatches(watches.getText()) .setConnectionRetries(connectionRetries.getNumber()) .setConnectionTimeout(connectionTimeout.getNumber()); - if (useCustomRepositoryCheckBox.isSelected()) { - builder.setCustomResourcesRepo(repositoryNameJBTextField.getText()); + if (useExternalRepositoryCheckBox.isSelected()) { + builder.setExternalResourcesRepo(repositoryNameJBTextField.getText()); } return builder.build(); } @@ -284,11 +284,11 @@ private void loadConfig() { watches.setText(serverConfig.getWatches()); connectionRetries.setValue(serverConfig.getConnectionRetries()); connectionTimeout.setValue(serverConfig.getConnectionTimeout()); - if (!StringUtils.isEmpty(serverConfig.getCustomResourcesRepo())) { - useCustomRepositoryCheckBox.setSelected(true); - repositoryNameJBTextField.setText(serverConfig.getCustomResourcesRepo()); + if (!StringUtils.isEmpty(serverConfig.getExternalResourcesRepo())) { + useExternalRepositoryCheckBox.setSelected(true); + repositoryNameJBTextField.setText(serverConfig.getExternalResourcesRepo()); } else { - useCustomRepositoryCheckBox.setSelected(false); + useExternalRepositoryCheckBox.setSelected(false); } } else { clearText(platformUrl, xrayUrl, artifactoryUrl, username, password); @@ -299,9 +299,9 @@ private void loadConfig() { connectionRetries.setValue(ConnectionRetriesSpinner.RANGE.initial); connectionTimeout.setValue(ConnectionTimeoutSpinner.RANGE.initial); ssoLoginSelection.setSelected(true); - useCustomRepositoryCheckBox.setSelected(false); + useExternalRepositoryCheckBox.setSelected(false); } - updateCustomRepositoryFields(); + updateExternalRepositoryFields(); initAuthMethodSelection(); } @@ -684,16 +684,16 @@ private void initScanOptionsRestoreDefaultsActionLink() { } /** - * Initialize the "Use custom repository" checkbox in the "Advanced" tab. + * Initialize the "Use external repository" checkbox in the "Advanced" tab. */ - private void initUseCustomRepositoryCheckBox() { - useCustomRepositoryCheckBox.addActionListener(e -> ApplicationManager.getApplication().executeOnPooledThread(() -> { - updateCustomRepositoryFields(); + private void initUseExternalRepositoryCheckBox() { + useExternalRepositoryCheckBox.addActionListener(e -> ApplicationManager.getApplication().executeOnPooledThread(() -> { + updateExternalRepositoryFields(); })); } - private void updateCustomRepositoryFields() { - boolean enabled = useCustomRepositoryCheckBox.isSelected(); + private void updateExternalRepositoryFields() { + boolean enabled = useExternalRepositoryCheckBox.isSelected(); repositoryNameJLabel.setEnabled(enabled); repositoryNameJBTextField.setEnabled(enabled); repositoryNameDescJLabel.setEnabled(enabled); diff --git a/src/test/java/com/jfrog/ide/idea/configuration/ConfigurationTest.java b/src/test/java/com/jfrog/ide/idea/configuration/ConfigurationTest.java index fa161792..96de4cf6 100644 --- a/src/test/java/com/jfrog/ide/idea/configuration/ConfigurationTest.java +++ b/src/test/java/com/jfrog/ide/idea/configuration/ConfigurationTest.java @@ -21,7 +21,7 @@ public class ConfigurationTest extends LightJavaCodeInsightFixtureTestCase { private static final String JFROG_PROJECT = "ideaTest"; private static final int CONNECTION_TIMEOUT = 70; private static final int CONNECTION_RETRIES = 5; - private static final String CUSTOM_RESOURCES_REPO = "releases"; + private static final String EXTERNAL_RESOURCES_REPO = "releases"; private static final String PASSWORD = "prince"; private static final String USERNAME = "diana"; private static final String WATCH = "heimdall"; @@ -42,7 +42,7 @@ protected void tearDown() throws Exception { * Test credentials storage. */ public void testStoreCredentials() { - ServerConfigImpl serverConfig = createServerConfig(true, true); + ServerConfigImpl serverConfig = createServerConfig(true, true, true); // Add credentials serverConfig.addCredentialsToPasswordSafe(); @@ -64,7 +64,7 @@ public void testStoreCredentials() { public void testSetServerConfig() { // Create overriding server config GlobalSettings globalSettings = new GlobalSettings(); - ServerConfigImpl overrideServerConfig = createServerConfig(true, true); + ServerConfigImpl overrideServerConfig = createServerConfig(true, true, true); // Save credentials in the PasswordSafe and delete credentials from the overriding server. // We do this to simulate GlobalSettings load from file. @@ -82,7 +82,7 @@ public void testSetServerConfig() { assertEquals(PASSWORD, actualServerConfig.getPassword()); assertEquals(CONNECTION_RETRIES, actualServerConfig.getConnectionRetries()); assertEquals(CONNECTION_TIMEOUT, actualServerConfig.getConnectionTimeout()); - assertEquals(CUSTOM_RESOURCES_REPO, actualServerConfig.getCustomResourcesRepo()); + assertEquals(EXTERNAL_RESOURCES_REPO, actualServerConfig.getExternalResourcesRepo()); assertEquals(EXCLUDED_PATHS, actualServerConfig.getExcludedPaths()); assertEquals(JFROG_PROJECT, actualServerConfig.getProject()); assertEquals(WATCH, actualServerConfig.getWatches()); @@ -127,7 +127,7 @@ public void testSetServerConfigFromEnv() { // Create overriding server config GlobalSettings globalSettings = new GlobalSettings(); - ServerConfigImpl overrideServerConfig = createServerConfig(false, false); + ServerConfigImpl overrideServerConfig = createServerConfig(false, false, false); // Check that the server in the global settings was overridden by the environment variables globalSettings.setServerConfig(overrideServerConfig); @@ -150,6 +150,22 @@ public void testSetServerConfigFromEnv() { } } + public void testReadMissingConfFromEnv() { + try (MockedStatic mockController = Mockito.mockStatic(EnvironmentUtil.class)) { + mockController.when(() -> EnvironmentUtil.getValue(EXTERNAL_RESOURCES_REPO_ENV)).thenReturn("releases-test"); + + // Create overriding server config + GlobalSettings globalSettings = new GlobalSettings(); + ServerConfigImpl overrideServerConfig = createServerConfig(true, true, false); + globalSettings.setServerConfig(overrideServerConfig); + + // Check that the external resources repository field was overridden + ServerConfigImpl actualServerConfig = globalSettings.getServerConfig(); + actualServerConfig.readMissingConfFromEnv(); + assertEquals("releases-test", actualServerConfig.getExternalResourcesRepo()); + } + } + /** * Create server config for the tests. * @@ -157,7 +173,7 @@ public void testSetServerConfigFromEnv() { * @param artifactoryUrl - True if should set Artifactory URL * @return server config */ - ServerConfigImpl createServerConfig(boolean xrayUrl, boolean artifactoryUrl) { + ServerConfigImpl createServerConfig(boolean xrayUrl, boolean artifactoryUrl, boolean externalResourcesRepo) { return new ServerConfigImpl.Builder() .setJFrogSettingsCredentialsKey(JFROG_SETTINGS_CREDENTIALS_KEY) .setUrl(PLATFORM_URL) @@ -167,7 +183,7 @@ ServerConfigImpl createServerConfig(boolean xrayUrl, boolean artifactoryUrl) { .setPassword(PASSWORD) .setConnectionRetries(CONNECTION_RETRIES) .setConnectionTimeout(CONNECTION_TIMEOUT) - .setCustomResourcesRepo(CUSTOM_RESOURCES_REPO) + .setExternalResourcesRepo(externalResourcesRepo ? EXTERNAL_RESOURCES_REPO : null) .setExcludedPaths(EXCLUDED_PATHS) .setProject(JFROG_PROJECT) .setWatches(WATCH) @@ -178,6 +194,6 @@ ServerConfigImpl createServerConfig(boolean xrayUrl, boolean artifactoryUrl) { * Clean up PasswordSafe. */ private void cleanUp() { - createServerConfig(true, true).removeCredentialsFromPasswordSafe(); + createServerConfig(true, true, true).removeCredentialsFromPasswordSafe(); } } diff --git a/src/test/java/com/jfrog/ide/idea/scan/ScanBinaryExecutorTest.java b/src/test/java/com/jfrog/ide/idea/scan/ScanBinaryExecutorTest.java index 9265c1a2..16481044 100644 --- a/src/test/java/com/jfrog/ide/idea/scan/ScanBinaryExecutorTest.java +++ b/src/test/java/com/jfrog/ide/idea/scan/ScanBinaryExecutorTest.java @@ -86,14 +86,14 @@ public void testSarifParserNotApplicResults() throws IOException { } public void testGetBinaryDownloadURL() { - final String customRepoName = "test-releases-repo"; - final String expectedCustomRepoUrl = "test-releases-repo/artifactory/xsc-gen-exe-analyzer-manager-local/"; - final String expectedNoCustomRepoUrl = "xsc-gen-exe-analyzer-manager-local/"; + final String externalRepoName = "test-releases-repo"; + final String expectedExternalRepoUrl = "test-releases-repo/artifactory/xsc-gen-exe-analyzer-manager-local/"; + final String expectedNoExternalRepoUrl = "xsc-gen-exe-analyzer-manager-local/"; - String actualNoCustomRepoUrl = scanner.getBinaryDownloadURL(null); - assertTrue(actualNoCustomRepoUrl.startsWith(expectedNoCustomRepoUrl)); - String actualCustomRepoUrl = scanner.getBinaryDownloadURL(customRepoName); - assertTrue(actualCustomRepoUrl.startsWith(expectedCustomRepoUrl)); + String actualNoExternalRepoUrl = scanner.getBinaryDownloadURL(null); + assertTrue(actualNoExternalRepoUrl.startsWith(expectedNoExternalRepoUrl)); + String actualExternalRepoUrl = scanner.getBinaryDownloadURL(externalRepoName); + assertTrue(actualExternalRepoUrl.startsWith(expectedExternalRepoUrl)); } private ScansConfig readScansConfigYAML(Path inputPath) throws IOException {