-
Notifications
You must be signed in to change notification settings - Fork 153
/
AqlHelper.java
219 lines (194 loc) · 10.7 KB
/
AqlHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package org.jfrog.build.extractor.clientConfiguration.util;
import org.apache.commons.lang3.StringUtils;
import org.jfrog.build.api.search.AqlSearchResult;
import org.jfrog.build.api.util.Log;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;
import org.jfrog.filespecs.aql.AqlConverter;
import org.jfrog.filespecs.entities.Aql;
import org.jfrog.filespecs.entities.FilesGroup;
import java.io.IOException;
import java.util.*;
public class AqlHelper {
protected static final String LATEST = "LATEST";
protected static final String LAST_RELEASE = "LAST_RELEASE";
protected static final String DELIMITER = "/";
protected static final String ESCAPE_CHAR = "\\";
protected Log log;
protected ArtifactoryManager artifactoryManager;
protected String buildName;
protected String buildNumber;
protected final FilesGroup filesGroup;
AqlHelper(ArtifactoryManager artifactoryManager, Log log, FilesGroup file) throws IOException {
this.artifactoryManager = artifactoryManager;
this.log = log;
this.filesGroup = file;
buildQueryAdditionalParts();
}
private void buildQueryAdditionalParts() throws IOException {
this.buildName = getBuildName(this.filesGroup.getBuild());
// AQL doesn't support projects.
this.buildNumber = getBuildNumber(artifactoryManager, buildName, this.filesGroup.getBuild(), null);
}
public List<AqlSearchResult.SearchEntry> run() throws IOException {
String aql;
if (this.filesGroup.getSpecType() == FilesGroup.SpecType.BUILD) {
// The file-specs-java library doesn't support files groups of type BUILD.
// To handle that, we create the AQL query body separately here, put it in the files group, use the files group
// to create a full, valid AQL query and finally revert the files group by removing the AQL from it.
String queryBody = createAqlBodyForBuild(buildName, buildNumber);
Aql query = new Aql();
query.setFind(queryBody);
this.filesGroup.setAql(query);
aql = AqlConverter.convertFilesGroupToAql(this.filesGroup);
this.filesGroup.setAql(null);
} else {
aql = AqlConverter.convertFilesGroupToAql(this.filesGroup);
}
log.debug("Searching Artifactory using AQL query:\n" + aql);
AqlSearchResult aqlSearchResult = artifactoryManager.searchArtifactsByAql(aql);
List<AqlSearchResult.SearchEntry> queryResults = aqlSearchResult.getResults();
List<AqlSearchResult.SearchEntry> artifactsSha1SearchResults;
if (this.filesGroup.getSpecType() == FilesGroup.SpecType.BUILD) {
artifactsSha1SearchResults = queryResults;
} else {
if (!StringUtils.isBlank(this.buildName) && !StringUtils.isBlank(this.buildNumber)) {
artifactsSha1SearchResults = fetchBuildArtifactsSha1();
} else {
artifactsSha1SearchResults = new ArrayList<>();
}
}
List<AqlSearchResult.SearchEntry> results = filterResult(queryResults, artifactsSha1SearchResults);
return (results == null ? new ArrayList<>() : results);
}
/**
* If buildName specified, filter the results to keep only artifacts matching the requested build
*/
private List<AqlSearchResult.SearchEntry> filterResult(List<AqlSearchResult.SearchEntry> queryResults, List<AqlSearchResult.SearchEntry> artifactsSha1SerachResults) throws IOException {
if (StringUtils.isNotBlank(buildName) && queryResults.size() > 0) {
Map<String, Boolean> buildArtifactsSha1 = extractSha1FromAqlResponse(artifactsSha1SerachResults);
queryResults = filterAqlSearchResultsByBuild(queryResults, buildArtifactsSha1, buildName, buildNumber);
}
return queryResults;
}
/**
* Sends an aql query to get all Sha1 value of the requested build, then returns the search results.
*/
private List<AqlSearchResult.SearchEntry> fetchBuildArtifactsSha1() throws IOException {
// If a user without admin privileges tries to send AQL query that includes 'actual_sha1' only, a bad request will be return.
// In order to fix this, we include name, repo & path.
String includeSha1Field = ".include(\"name\",\"repo\",\"path\",\"actual_sha1\")";
String buildAql = String.format("items.find(%s)%s", createAqlBodyForBuild(buildName, buildNumber), includeSha1Field);
log.debug("Searching Artifactory for build's checksums using AQL query:\n" + buildAql);
AqlSearchResult aqlSearchResult = artifactoryManager.searchArtifactsByAql(buildAql);
return aqlSearchResult.getResults();
}
private static String createAqlBodyForBuild(String buildName, String buildNumber) {
return String.format("{\"artifact.module.build.name\": \"%s\",\"artifact.module.build.number\": \"%s\"}", buildName, buildNumber);
}
private static String getBuildName(String build) {
if (StringUtils.isBlank(build)) {
return build;
}
// The delimiter must not be prefixed with escapeChar (if it is, it should be part of the build number)
// the code below gets substring from before the last delimiter.
// If the new string ends with escape char it means the last delimiter was part of the build number and we need
// to go back to the previous delimiter.
// If no proper delimiter was found the full string will be the build name.
String buildName = StringUtils.substringBeforeLast(build, DELIMITER);
while (StringUtils.isNotBlank(buildName) && buildName.contains(DELIMITER) && buildName.endsWith(ESCAPE_CHAR)) {
buildName = StringUtils.substringBeforeLast(buildName, DELIMITER);
}
return buildName.endsWith(ESCAPE_CHAR) ? build : buildName;
}
private String getBuildNumber(ArtifactoryManager client, String buildName, String build, String project) throws IOException {
String buildNumber = "";
if (StringUtils.isNotBlank(buildName)) {
if (!build.startsWith(buildName)) {
throw new IllegalStateException(String.format("build '%s' does not start with build name '%s'.", build, buildName));
}
// Case build number was not provided, the build name and the build are the same. build number will be latest
if (build.equals(buildName)) {
buildNumber = LATEST;
} else {
// Get build name by removing build name and the delimiter
buildNumber = build.substring(buildName.length() + DELIMITER.length());
// Remove the escape chars before the delimiters
buildNumber = buildNumber.replace(ESCAPE_CHAR + DELIMITER, DELIMITER);
}
String retrievedBuildNumber = client.getLatestBuildNumber(buildName, buildNumber, project);
if (retrievedBuildNumber == null) {
logBuildNotFound(buildName, buildNumber);
}
buildNumber = retrievedBuildNumber;
}
return buildNumber;
}
private void logBuildNotFound(String buildName, String buildNumber) {
StringBuilder sb = new StringBuilder("The build name ").append(buildName);
if (LAST_RELEASE.equals(buildNumber.trim())) {
sb.append(" with the status RELEASED");
}
sb.append(" could not be found.");
log.warn(sb.toString());
}
/**
* When searching artifacts of a specific build, artifactory uses the checksum list in the published build-info,
* and returns all the items that match this list.
* That kind of search may return duplicated or unnecessary artifacts due to late move/copy/promote operations.
* Therefore, we need to filter the search results according to the following priorities:
* 1st priority: Match {Sha1, build name, build number}
* 2nd priority: Match {Sha1, build name}
* 3rd priority: Match {Sha1}
*/
private static List<AqlSearchResult.SearchEntry> filterAqlSearchResultsByBuild(List<AqlSearchResult.SearchEntry> itemsToFilter, Map<String, Boolean> buildArtifactsSha1, String buildName, String buildNumber) {
// Maps that contain the search results, mapped by the priority they match.
Map<String, List<AqlSearchResult.SearchEntry>> firstPriority = new HashMap<>();
Map<String, List<AqlSearchResult.SearchEntry>> secondPriority = new HashMap<>();
Map<String, List<AqlSearchResult.SearchEntry>> thirdPriority = new HashMap<>();
// List that contains the filtered results, after step 2.
List<AqlSearchResult.SearchEntry> filteredResults = new ArrayList<>();
// Step 1 - Populate 3 priorities mappings.
for (AqlSearchResult.SearchEntry item : itemsToFilter) {
if (!buildArtifactsSha1.containsKey(item.getActualSha1())) {
continue;
}
boolean isBuildNameMatch = buildName.equals(item.getBuildName());
boolean isBuildNumberMatch = buildNumber.equals(item.getBuildNumber());
if (isBuildNameMatch) {
if (isBuildNumberMatch) {
addToListInMap(firstPriority, item);
continue;
}
addToListInMap(secondPriority, item);
continue;
}
addToListInMap(thirdPriority, item);
}
// Step 2 - Append mappings to the final results, respectively.
for (Map.Entry<String, Boolean> entry : buildArtifactsSha1.entrySet()) {
String shaToMatch = entry.getKey();
if (firstPriority.containsKey(shaToMatch)) {
filteredResults.addAll(firstPriority.get(shaToMatch));
} else if (secondPriority.containsKey(shaToMatch)) {
filteredResults.addAll(secondPriority.get(shaToMatch));
} else if (thirdPriority.containsKey(shaToMatch)) {
filteredResults.addAll(thirdPriority.get(shaToMatch));
}
}
return filteredResults;
}
private static void addToListInMap(Map<String, List<AqlSearchResult.SearchEntry>> map, AqlSearchResult.SearchEntry item) {
List<AqlSearchResult.SearchEntry> curList = map.get(item.getActualSha1());
if (curList == null) curList = new ArrayList<>();
curList.add(item);
map.put(item.getActualSha1(), curList);
}
/**
* Maps all Sha1 values that exist in the results found
*/
private static Map<String, Boolean> extractSha1FromAqlResponse(List<AqlSearchResult.SearchEntry> searchResults) {
Map<String, Boolean> resultsMap = new HashMap<>();
searchResults.forEach((result) -> resultsMap.put(result.getActualSha1(), true));
return resultsMap;
}
}