Skip to content

Commit ff6f807

Browse files
authored
BI-510 - Add support for usage-report to Artifactory
1 parent d41f317 commit ff6f807

File tree

9 files changed

+118
-27
lines changed

9 files changed

+118
-27
lines changed

build-info-api/src/main/java/org/jfrog/build/api/Build.java

+3
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,9 @@ public void append(Build other) {
560560
appendProperties(other);
561561
appendModules(other);
562562
appendBuildDependencies(other);
563+
if (this.issues == null) {
564+
this.issues = new Issues();
565+
}
563566
this.issues.append(other.getIssues());
564567
}
565568

build-info-client/src/main/java/org/jfrog/build/client/ArtifactoryHttpClient.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.http.StatusLine;
3232
import org.apache.http.client.methods.HttpGet;
3333
import org.apache.http.client.methods.HttpPut;
34-
import org.apache.http.message.AbstractHttpMessage;
3534
import org.apache.http.util.EntityUtils;
3635
import org.jfrog.build.api.util.Log;
3736
import org.jfrog.build.util.URI;
@@ -214,12 +213,12 @@ private void consumeEntity(HttpResponse response) throws IOException {
214213

215214
public JsonParser createJsonParser(InputStream in) throws IOException {
216215
JsonFactory jsonFactory = createJsonFactory();
217-
return jsonFactory.createJsonParser(in);
216+
return jsonFactory.createParser(in);
218217
}
219218

220219
public JsonParser createJsonParser(String content) throws IOException {
221220
JsonFactory jsonFactory = createJsonFactory();
222-
return jsonFactory.createJsonParser(content);
221+
return jsonFactory.createParser(content);
223222
}
224223

225224
public JsonFactory createJsonFactory() {

build-info-extractor/src/main/java/org/jfrog/build/extractor/BuildInfoExtractorUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static String buildInfoToJsonString(Build buildInfo) throws IOException {
199199

200200
public static Build jsonStringToBuildInfo(String json) throws IOException {
201201
JsonFactory jsonFactory = createJsonFactory();
202-
JsonParser parser = jsonFactory.createJsonParser(new StringReader(json));
202+
JsonParser parser = jsonFactory.createParser(new StringReader(json));
203203
return jsonFactory.getCodec().readValue(parser, Build.class);
204204
}
205205

@@ -217,7 +217,7 @@ public static <T extends Serializable> String buildInfoToJsonString(T buildCompo
217217

218218
public static <T extends Serializable> T jsonStringToGeneric(String json, Class<T> clazz) throws IOException {
219219
JsonFactory jsonFactory = createJsonFactory();
220-
JsonParser parser = jsonFactory.createJsonParser(new StringReader(json));
220+
JsonParser parser = jsonFactory.createParser(new StringReader(json));
221221
return jsonFactory.getCodec().readValue(parser, clazz);
222222
}
223223

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/client/ArtifactoryBaseClient.java

-3
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,13 @@ public boolean isRepoExist(String repo) throws IOException {
133133
String encodedUrl = ArtifactoryHttpClient.encodeUrl(fullItemUrl);
134134
HttpRequestBase httpRequest = new HttpGet(encodedUrl);
135135
HttpResponse httpResponse = null;
136-
int connectionRetries = httpClient.getConnectionRetries();
137136
try {
138137
httpResponse = httpClient.getHttpClient().execute(httpRequest);
139138
StatusLine statusLine = httpResponse.getStatusLine();
140139
if (statusLine.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
141140
return false;
142141
}
143142
} finally {
144-
// We are using the same client for multiple operations therefore we need to restore the connectionRetries configuration.
145-
httpClient.setConnectionRetries(connectionRetries);
146143
if (httpResponse != null) {
147144
EntityUtils.consumeQuietly(httpResponse.getEntity());
148145
}

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/client/ArtifactoryBuildInfoClient.java

+48-17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.jfrog.build.client.bintrayResponse.BintrayResponseFactory;
4949
import org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails;
5050
import org.jfrog.build.extractor.clientConfiguration.util.DeploymentUrlUtils;
51+
import org.jfrog.build.extractor.clientConfiguration.util.JsonSerializer;
52+
import org.jfrog.build.extractor.usageReport.UsageReporter;
5153
import org.jfrog.build.util.VersionCompatibilityType;
5254
import org.jfrog.build.util.VersionException;
5355

@@ -79,6 +81,8 @@ public class ArtifactoryBuildInfoClient extends ArtifactoryBaseClient implements
7981
public static final String APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON = "application/vnd.org.jfrog.artifactory+json";
8082
public static final String APPLICATION_JSON = "application/json";
8183
public static final String ITEM_LAST_MODIFIED = "/api/storage/";
84+
private static final String USAGE_API = "/api/system/usage";
85+
private static final ArtifactoryVersion USAGE_ARTIFACTORY_MIN_VERSION = new ArtifactoryVersion("6.9.0");
8286

8387
/**
8488
* Creates a new client for the given Artifactory url.
@@ -101,7 +105,7 @@ public ArtifactoryBuildInfoClient(String artifactoryUrl, String username, String
101105
}
102106

103107
public ArtifactoryBuildInfoClient(String artifactoryUrl, String username, String password, Log log) {
104-
super(artifactoryUrl, username, password, StringUtils.EMPTY, log);
108+
this(artifactoryUrl, username, password, StringUtils.EMPTY, log);
105109
}
106110
/**
107111
* @return A list of local repositories available for deployment.
@@ -299,22 +303,16 @@ private void sendHttpEntityRequest(HttpEntityEnclosingRequestBase request, Strin
299303

300304
private HttpResponse sendHttpRequest(HttpUriRequest request, int ... httpStatuses) throws IOException {
301305
HttpResponse httpResponse;
302-
int connectionRetries = httpClient.getConnectionRetries();
303-
try {
304-
httpResponse = httpClient.getHttpClient().execute(request);
305-
StatusLine statusLine = httpResponse.getStatusLine();
306-
for (int status : httpStatuses) {
307-
if (statusLine.getStatusCode() == status) {
308-
return httpResponse;
309-
}
306+
httpResponse = httpClient.getHttpClient().execute(request);
307+
StatusLine statusLine = httpResponse.getStatusLine();
308+
for (int status : httpStatuses) {
309+
if (statusLine.getStatusCode() == status) {
310+
return httpResponse;
310311
}
311-
312-
HttpEntity responseEntity = httpResponse.getEntity();
313-
throw new IOException(statusLine.getStatusCode() + httpClient.getMessageFromEntity(responseEntity));
314-
} finally {
315-
// We are using the same client for multiple operations therefore we need to restore the connectionRetries configuration.
316-
httpClient.setConnectionRetries(connectionRetries);
317312
}
313+
314+
HttpEntity responseEntity = httpResponse.getEntity();
315+
throw new IOException(statusLine.getStatusCode() + httpClient.getMessageFromEntity(responseEntity));
318316
}
319317

320318
public ItemLastModified getItemLastModified(String path) throws IOException {
@@ -468,7 +466,7 @@ private String createJsonRequestBody(BintrayUploadInfoOverride info) throws IOEx
468466
private BintrayResponse parseResponse(HttpResponse response) throws IOException {
469467
InputStream content = response.getEntity().getContent();
470468
int status = response.getStatusLine().getStatusCode();
471-
JsonParser parser = httpClient.createJsonFactory().createJsonParser(content);
469+
JsonParser parser = httpClient.createJsonFactory().createParser(content);
472470
BintrayResponse responseObject = BintrayResponseFactory.createResponse(status, parser);
473471
return responseObject;
474472
}
@@ -855,5 +853,38 @@ private String getResponseMessage(List<ArtifactoryUploadResponse.Error> errorLis
855853
}
856854
return builder.toString();
857855
}
858-
}
859856

857+
public void reportUsage(UsageReporter usageReporter) throws IOException {
858+
// Check Artifactory supported version.
859+
ArtifactoryVersion version = getArtifactoryVersion();
860+
if (version.isNotFound()) {
861+
throw new IOException("Could not get Artifactory version.");
862+
}
863+
if (!version.isAtLeast(USAGE_ARTIFACTORY_MIN_VERSION)) {
864+
throw new IOException("Usage report is not supported on targeted Artifactory server.");
865+
}
866+
867+
// Create request.
868+
String url = artifactoryUrl + USAGE_API;
869+
String encodedUrl = ArtifactoryHttpClient.encodeUrl(url);
870+
String requestBody = new JsonSerializer<UsageReporter>().toJSON(usageReporter);
871+
StringEntity entity = new StringEntity(requestBody, "UTF-8");
872+
entity.setContentType("application/json");
873+
HttpPost request = new HttpPost(encodedUrl);
874+
request.setEntity(entity);
875+
876+
// Send request
877+
HttpResponse httpResponse = null;
878+
try {
879+
httpResponse = httpClient.getHttpClient().execute(request);
880+
StatusLine statusLine = httpResponse.getStatusLine();
881+
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
882+
throw new IOException(String.format("Artifactory response: %s %s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
883+
}
884+
} finally {
885+
if (httpResponse != null) {
886+
EntityUtils.consumeQuietly(httpResponse.getEntity());
887+
}
888+
}
889+
}
890+
}

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/client/ArtifactoryDependenciesClient.java

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public ArtifactoryDependenciesClient(String artifactoryUrl, String username, Str
6161
super(artifactoryUrl, username, password, accessToken, logger);
6262
}
6363

64+
public ArtifactoryDependenciesClient(String artifactoryUrl, String username, String password, Log logger) {
65+
this(artifactoryUrl, username, password, StringUtils.EMPTY, logger);
66+
}
67+
6468
public ArtifactoryDependenciesClient(String artifactoryUrl, ArtifactoryHttpClient httpClient, Log logger) {
6569
super(artifactoryUrl, httpClient, logger);
6670
}

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/JsonSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public String toJSON(T object) throws IOException {
3939
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
4040
jsonFactory.setCodec(mapper);
4141
StringWriter writer = new StringWriter();
42-
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
42+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
4343
jsonGenerator.useDefaultPrettyPrinter();
4444
jsonGenerator.writeObject(object);
4545
return writer.getBuffer().toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.jfrog.build.extractor.usageReport;
2+
3+
import org.jfrog.build.api.util.Log;
4+
import org.jfrog.build.client.ProxyConfiguration;
5+
import org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* Created by Bar Belity on 20/11/2019.
11+
*/
12+
public class UsageReporter {
13+
private String productId;
14+
private FeatureId[] features;
15+
16+
public UsageReporter(String productId, String[] featureIds) {
17+
this.productId = productId;
18+
setFeatures(featureIds);
19+
}
20+
21+
public void reportUsage(String artifactoryUrl, String username, String password, String accessToken, ProxyConfiguration proxyConfiguration, Log log) throws IOException {
22+
ArtifactoryBuildInfoClient client = new ArtifactoryBuildInfoClient(artifactoryUrl, username, password, accessToken, log);
23+
if (proxyConfiguration != null) {
24+
client.setProxyConfiguration(proxyConfiguration);
25+
}
26+
client.reportUsage(this);
27+
}
28+
29+
public String getProductId() {
30+
return productId;
31+
}
32+
33+
public FeatureId[] getFeatures() {
34+
return features;
35+
}
36+
37+
private void setFeatures(String[] featureIds) {
38+
features = new FeatureId[featureIds.length];
39+
int featureIndex = 0;
40+
for (String featureId : featureIds) {
41+
features[featureIndex] = new FeatureId(featureId);
42+
featureIndex++;
43+
}
44+
}
45+
46+
public class FeatureId {
47+
private String featureId;
48+
49+
public FeatureId (String featureId) {
50+
this.featureId = featureId;
51+
}
52+
53+
public String getFeatureId() {
54+
return featureId;
55+
}
56+
}
57+
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
build-info-extractor-gradle-version=4.9.x-SNAPSHOT
1+
build-info-extractor-gradle-version=4.11.x-SNAPSHOT
22
build-info-extractor-npm-version=2.13.x-SNAPSHOT
33
build-info-latest-release-version=2.13.x-SNAPSHOT
44
build-info-extractor-ivy-version=2.13.x-SNAPSHOT

0 commit comments

Comments
 (0)