Skip to content

Commit f75df83

Browse files
committed
fix-task(artifactory-properties): Artifactory properties will now be constructed manually. Urls with internal in them will not be set. (Fixes IDETECT-2158)
(cherry picked from commit 0ebf100)
1 parent 9f40f9a commit f75df83

File tree

2 files changed

+61
-101
lines changed

2 files changed

+61
-101
lines changed

buildSrc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ dependencies {
2727
implementation("commons-io:commons-io:2.6")
2828
implementation("org.apache.commons:commons-lang3:3.0")
2929
implementation("com.synopsys.integration:integration-common:20.0.0")
30+
implementation("com.synopsys.integration:integration-rest:3.0.0")
3031
implementation("com.synopsys.integration:common-gradle-plugin:1.2.3")
3132
}

buildSrc/src/main/java/com/synopsys/integration/detect/artifactory/UpdateArtifactoryPropertiesTask.java

Lines changed: 60 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -22,141 +22,100 @@
2222
*/
2323
package com.synopsys.integration.detect.artifactory;
2424

25-
import java.io.File;
26-
import java.util.ArrayList;
27-
import java.util.Arrays;
28-
import java.util.Collections;
29-
import java.util.HashMap;
30-
import java.util.List;
25+
import java.io.IOException;
3126
import java.util.Optional;
3227

3328
import org.apache.commons.lang3.StringUtils;
3429
import org.gradle.api.DefaultTask;
3530
import org.gradle.api.Project;
3631
import org.gradle.api.tasks.TaskAction;
3732

38-
import com.google.gson.Gson;
3933
import com.synopsys.integration.Common;
40-
import com.synopsys.integration.executable.Executable;
41-
import com.synopsys.integration.executable.ExecutableOutput;
42-
import com.synopsys.integration.executable.ExecutableRunnerException;
43-
import com.synopsys.integration.executable.ProcessBuilderRunner;
34+
import com.synopsys.integration.exception.IntegrationException;
4435
import com.synopsys.integration.log.IntLogger;
4536
import com.synopsys.integration.log.Slf4jIntLogger;
37+
import com.synopsys.integration.rest.HttpMethod;
38+
import com.synopsys.integration.rest.HttpUrl;
39+
import com.synopsys.integration.rest.body.BodyContent;
40+
import com.synopsys.integration.rest.body.StringBodyContent;
41+
import com.synopsys.integration.rest.client.AuthenticatingIntHttpClient;
42+
import com.synopsys.integration.rest.client.BasicAuthHttpClient;
43+
import com.synopsys.integration.rest.client.IntHttpClient;
44+
import com.synopsys.integration.rest.proxy.ProxyInfo;
45+
import com.synopsys.integration.rest.request.Request;
46+
import com.synopsys.integration.rest.response.Response;
47+
import com.synopsys.integration.rest.support.AuthenticationSupport;
48+
import com.synopsys.integration.rest.support.UrlSupport;
4649

4750
public class UpdateArtifactoryPropertiesTask extends DefaultTask {
48-
private static final String LATEST_PROPERTY_KEY = "DETECT_LATEST";
49-
5051
private final IntLogger logger = new Slf4jIntLogger(getLogger());
51-
private final Gson gson = new Gson();
5252
private final Project project = this.getProject();
5353

5454
@TaskAction
5555
public void updateArtifactoryProperties() {
56-
57-
final String projectVersion = project.getVersion().toString();
58-
final boolean isSnapshot = StringUtils.endsWith(projectVersion, "-SNAPSHOT");
56+
String projectName = project.getName();
57+
String projectVersion = project.getVersion().toString();
58+
boolean isSnapshot = StringUtils.endsWith(projectVersion, "-SNAPSHOT");
5959

6060
if (isSnapshot || "true".equals(project.findProperty("qa.build"))) {
6161
logger.alwaysLog("For a snapshot or qa build, artifactory properties will not be updated.");
62-
} else {
63-
try {
64-
logger.alwaysLog("For a release build, an update of artifactory properties will be attempted.");
62+
return;
63+
}
6564

66-
final String artifactoryDeployerUsername = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_DEPLOYER_USERNAME);
67-
final String artifactoryDeployerPassword = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_DEPLOYER_PASSWORD);
68-
final String artifactoryDeploymentUrl = getExtensionProperty(Common.PROPERTY_DEPLOY_ARTIFACTORY_URL);
69-
final String artifactoryRepository = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_REPO);
70-
final String artifactoryDownloadUrl = getExtensionProperty(Common.PROPERTY_DOWNLOAD_ARTIFACTORY_URL);
65+
logger.alwaysLog("For a release build, an update of artifactory properties will be attempted.");
7166

72-
final String artifactoryCredentials = String.format("%s:%s", artifactoryDeployerUsername, artifactoryDeployerPassword);
73-
final List<String> defaultCurlArgs = Arrays.asList("--silent", "--insecure", "--user", artifactoryCredentials, "--header", "Content-Type: application/json");
67+
String artifactoryDeployerUsername = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_DEPLOYER_USERNAME);
68+
String artifactoryDeployerPassword = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_DEPLOYER_PASSWORD);
69+
String artifactoryDeploymentUrl = getExtensionProperty(Common.PROPERTY_DEPLOY_ARTIFACTORY_URL);
70+
String artifactoryRepository = getExtensionProperty(Common.PROPERTY_ARTIFACTORY_REPO);
71+
String artifactoryDownloadUrl = getExtensionProperty(Common.PROPERTY_DOWNLOAD_ARTIFACTORY_URL);
7472

75-
final Optional<ArtifactSearchResultElement> currentArtifact = findCurrentArtifact(defaultCurlArgs, artifactoryDeploymentUrl, artifactoryRepository);
73+
AuthenticatingIntHttpClient httpClient = new BasicAuthHttpClient(logger, 200, true, ProxyInfo.NO_PROXY_INFO, new AuthenticationSupport(new UrlSupport()), artifactoryDeployerUsername, artifactoryDeployerPassword);
7674

77-
if (currentArtifact.isPresent()) {
78-
final String majorVersion = projectVersion.split("\\.")[0];
79-
final String majorVersionPropertyKey = String.format("%s_%s", LATEST_PROPERTY_KEY, majorVersion);
80-
final String downloadUri = currentArtifact.get().getDownloadUri();
81-
final String updatedDownloadUri = downloadUri.replace(artifactoryDeploymentUrl, artifactoryDownloadUrl);
75+
String majorVersion = projectVersion.split("\\.")[0];
76+
String latestPropertyKey = "DETECT_LATEST";
77+
String majorVersionPropertyKey = String.format("%s_%s", latestPropertyKey, majorVersion);
78+
String constructedDownloadUri = String.format("%s/%s/com/synopsys/integration/%s/%s/%s-%s.jar", artifactoryDownloadUrl, artifactoryRepository, projectName, projectVersion, projectName, projectVersion);
8279

83-
setArtifactoryProperty(defaultCurlArgs, artifactoryDeploymentUrl, artifactoryRepository, LATEST_PROPERTY_KEY, updatedDownloadUri);
84-
setArtifactoryProperty(defaultCurlArgs, artifactoryDeploymentUrl, artifactoryRepository, majorVersionPropertyKey, updatedDownloadUri);
85-
} else {
86-
logger.alwaysLog(String.format("Artifactory properties won't be updated since %s-%s was not found.", project.getName(), projectVersion));
87-
}
88-
} catch (final ExecutableRunnerException e) {
89-
logger.alwaysLog(String.format("Manual corrections to the properties for %s-%s may be necessary.", project.getName(), projectVersion));
90-
logger.error(String.format("Error correcting the artifactory properties: %s", e.getMessage()), e);
91-
}
80+
if (constructedDownloadUri.contains("internal")) {
81+
logger.error(String.format("Failing due to url containing \"internal\". URL: %s", constructedDownloadUri));
82+
logger.alwaysLog(String.format("Manual corrections to the properties for %s-%s may be necessary.", projectName, projectVersion));
83+
return;
84+
}
85+
86+
try {
87+
setArtifactoryProperty(httpClient, artifactoryDeploymentUrl, artifactoryRepository, latestPropertyKey, constructedDownloadUri);
88+
setArtifactoryProperty(httpClient, artifactoryDeploymentUrl, artifactoryRepository, majorVersionPropertyKey, constructedDownloadUri);
89+
} catch (IntegrationException | IOException e) {
90+
logger.alwaysLog(String.format("Manual corrections to the properties for %s-%s may be necessary.", projectName, projectVersion));
91+
logger.error(String.format("Error setting the artifactory properties: %s", e.getMessage()), e);
9292
}
9393
}
9494

95-
private String getExtensionProperty(final String propertyName) {
95+
private String getExtensionProperty(String propertyName) {
9696
return Optional.ofNullable(project.findProperty(propertyName))
9797
.map(Object::toString)
9898
.orElseThrow(() -> new IllegalArgumentException(String.format("Missing Gradle extension property '%s' which is required to set Artifactory properties.", propertyName)));
9999
}
100100

101-
private Optional<ArtifactSearchResultElement> findCurrentArtifact(final List<String> defaultCurlArgs, final String artifactoryDeploymentUrl, final String artifactoryRepo) {
102-
final String projectName = project.getName();
103-
final String projectVersion = project.getVersion().toString();
104-
105-
try {
106-
final String url = String.format("%s/api/search/artifact?name=%s-%s.jar&repos=%s", artifactoryDeploymentUrl, projectName, projectVersion, artifactoryRepo);
107-
final ArtifactSearchResult artifactSearchResult = getArtifactoryItems(url, defaultCurlArgs);
108-
109-
if (artifactSearchResult.getResults() == null) {
110-
logger.error(String.format("Failed to get any results from %s.", url));
111-
return Optional.empty();
112-
}
113-
114-
if (artifactSearchResult.getResults().size() != 1) {
115-
logger.error(String.format("Unexpected number of search results. Expected 1 but found %d.", artifactSearchResult.getResults().size()));
116-
return Optional.empty();
101+
private void setArtifactoryProperty(IntHttpClient httpClient, String artifactoryDeploymentUrl, String deploymentRepositoryKey, String propertyKey, String propertyValue)
102+
throws IntegrationException, IOException {
103+
HttpUrl baseUrl = new HttpUrl(String.format("%s/api/metadata/%s/com/synopsys/integration/%s", artifactoryDeploymentUrl, deploymentRepositoryKey, project.getName()));
104+
BodyContent bodyContent = new StringBodyContent(String.format("{\"props\":{\"%s\":\"%s\"}}", propertyKey, propertyValue));
105+
Request request = new Request.Builder()
106+
.url(baseUrl)
107+
.method(HttpMethod.PATCH)
108+
.mimeType("application/json")
109+
.bodyContent(bodyContent)
110+
.build();
111+
112+
try (Response response = httpClient.execute(request)) {
113+
if (response.isStatusCodeSuccess()) {
114+
logger.alwaysLog(String.format("Successfully set Artifactory property '%s' to '%s'.", propertyKey, propertyValue));
115+
} else {
116+
logger.error(String.format("Failed to update Artifactory property '%s'.", propertyKey));
117+
logger.error(response.getContentString());
117118
}
118-
119-
return artifactSearchResult.getResults().stream().findFirst();
120-
} catch (final ExecutableRunnerException e) {
121-
logger.error(String.format("Could not find the current artifact: %s", e.getMessage()), e);
122119
}
123-
124-
return Optional.empty();
125-
}
126-
127-
private ArtifactSearchResult getArtifactoryItems(final String url, final List<String> defaultCurlArgs) throws ExecutableRunnerException {
128-
final List<String> curlArgs = new ArrayList<>(Arrays.asList("--header", "X-Result-Detail: info", url));
129-
curlArgs.addAll(defaultCurlArgs);
130-
final ExecutableOutput executableOutput = curlResponse(curlArgs);
131-
132-
return gson.fromJson(executableOutput.getStandardOutput(), ArtifactSearchResult.class);
133-
}
134-
135-
// TODO: Don't use curl. Lets make an HttpClient and do it proper.
136-
private ExecutableOutput setArtifactoryProperty(final List<String> defaultCurlArgs, final String artifactoryDeploymentUrl, final String deploymentRepositoryKey, final String propertyKey, final String propertyValue)
137-
throws ExecutableRunnerException {
138-
139-
final List<String> curlArgs = new ArrayList<>(defaultCurlArgs);
140-
final String artifactLocation = String.format("%s/api/metadata/%s/com/synopsys/integration/%s", artifactoryDeploymentUrl, deploymentRepositoryKey, project.getName());
141-
curlArgs.addAll(Arrays.asList(
142-
"--request",
143-
"PATCH",
144-
"--data",
145-
String.format("{\"props\":{\"%s\":\"%s\"}}", propertyKey, propertyValue),
146-
artifactLocation
147-
));
148-
149-
logger.alwaysLog(String.format("Setting %s to %s on %s.", propertyKey, propertyValue, artifactLocation));
150-
return curlResponse(curlArgs);
151-
}
152-
153-
public ExecutableOutput curlResponse(final List<String> curlArgs) throws ExecutableRunnerException {
154-
final File workingDirectory = new File(System.getProperty("user.dir"));
155-
final List<String> command = new ArrayList<>(Collections.singletonList("curl"));
156-
command.addAll(curlArgs);
157-
final Executable executable = new Executable(workingDirectory, new HashMap<>(), command);
158-
159-
final ProcessBuilderRunner processBuilderRunner = new ProcessBuilderRunner(logger);
160-
return processBuilderRunner.execute(executable);
161120
}
162121
}

0 commit comments

Comments
 (0)