|
22 | 22 | */ |
23 | 23 | package com.synopsys.integration.detect.artifactory; |
24 | 24 |
|
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; |
31 | 26 | import java.util.Optional; |
32 | 27 |
|
33 | 28 | import org.apache.commons.lang3.StringUtils; |
34 | 29 | import org.gradle.api.DefaultTask; |
35 | 30 | import org.gradle.api.Project; |
36 | 31 | import org.gradle.api.tasks.TaskAction; |
37 | 32 |
|
38 | | -import com.google.gson.Gson; |
39 | 33 | 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; |
44 | 35 | import com.synopsys.integration.log.IntLogger; |
45 | 36 | 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; |
46 | 49 |
|
47 | 50 | public class UpdateArtifactoryPropertiesTask extends DefaultTask { |
48 | | - private static final String LATEST_PROPERTY_KEY = "DETECT_LATEST"; |
49 | | - |
50 | 51 | private final IntLogger logger = new Slf4jIntLogger(getLogger()); |
51 | | - private final Gson gson = new Gson(); |
52 | 52 | private final Project project = this.getProject(); |
53 | 53 |
|
54 | 54 | @TaskAction |
55 | 55 | 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"); |
59 | 59 |
|
60 | 60 | if (isSnapshot || "true".equals(project.findProperty("qa.build"))) { |
61 | 61 | 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 | + } |
65 | 64 |
|
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."); |
71 | 66 |
|
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); |
74 | 72 |
|
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); |
76 | 74 |
|
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); |
82 | 79 |
|
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); |
92 | 92 | } |
93 | 93 | } |
94 | 94 |
|
95 | | - private String getExtensionProperty(final String propertyName) { |
| 95 | + private String getExtensionProperty(String propertyName) { |
96 | 96 | return Optional.ofNullable(project.findProperty(propertyName)) |
97 | 97 | .map(Object::toString) |
98 | 98 | .orElseThrow(() -> new IllegalArgumentException(String.format("Missing Gradle extension property '%s' which is required to set Artifactory properties.", propertyName))); |
99 | 99 | } |
100 | 100 |
|
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()); |
117 | 118 | } |
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); |
122 | 119 | } |
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); |
161 | 120 | } |
162 | 121 | } |
0 commit comments