-
Notifications
You must be signed in to change notification settings - Fork 40.1k
/
BintrayService.java
192 lines (170 loc) · 6.93 KB
/
BintrayService.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
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.concourse.releasescripts.bintray;
import java.net.URI;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
import io.spring.concourse.releasescripts.ReleaseInfo;
import io.spring.concourse.releasescripts.sonatype.SonatypeProperties;
import io.spring.concourse.releasescripts.sonatype.SonatypeService;
import org.awaitility.core.ConditionTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import static org.awaitility.Awaitility.waitAtMost;
/**
* Central class for interacting with Bintray's REST API.
*
* @author Madhura Bhave
*/
@Component
public class BintrayService {
private static final Logger logger = LoggerFactory.getLogger(BintrayService.class);
private static final String BINTRAY_URL = "https://api.bintray.com/";
private static final String GRADLE_PLUGIN_REQUEST = "[ { \"name\": \"gradle-plugin\", \"values\": [\"org.springframework.boot:org.springframework.boot:spring-boot-gradle-plugin\"] } ]";
private final RestTemplate restTemplate;
private final BintrayProperties bintrayProperties;
private final SonatypeProperties sonatypeProperties;
private final SonatypeService sonatypeService;
public BintrayService(RestTemplateBuilder builder, BintrayProperties bintrayProperties,
SonatypeProperties sonatypeProperties, SonatypeService sonatypeService) {
this.bintrayProperties = bintrayProperties;
this.sonatypeProperties = sonatypeProperties;
this.sonatypeService = sonatypeService;
String username = bintrayProperties.getUsername();
String apiKey = bintrayProperties.getApiKey();
if (StringUtils.hasLength(username)) {
builder = builder.basicAuthentication(username, apiKey);
}
this.restTemplate = builder.build();
}
public boolean isDistributionStarted(ReleaseInfo releaseInfo) {
logger.debug("Checking if distribution is started");
RequestEntity<Void> request = getPackageFilesRequest(releaseInfo, 1);
try {
logger.debug("Checking Bintray");
this.restTemplate.exchange(request, PackageFile[].class).getBody();
return true;
}
catch (HttpClientErrorException ex) {
if (ex.getStatusCode() != HttpStatus.NOT_FOUND) {
throw ex;
}
return false;
}
}
public boolean isDistributionComplete(ReleaseInfo releaseInfo, Set<String> requiredDigests, Duration timeout) {
return isDistributionComplete(releaseInfo, requiredDigests, timeout, Duration.ofSeconds(20));
}
public boolean isDistributionComplete(ReleaseInfo releaseInfo, Set<String> requiredDigests, Duration timeout,
Duration pollInterval) {
logger.debug("Checking if distribution is complete");
RequestEntity<Void> request = getPackageFilesRequest(releaseInfo, 1);
try {
waitAtMost(timeout).with().pollDelay(Duration.ZERO).pollInterval(pollInterval).until(() -> {
logger.debug("Checking Bintray");
try {
PackageFile[] published = this.restTemplate.exchange(request, PackageFile[].class).getBody();
return hasPublishedAll(published, requiredDigests);
}
catch (HttpClientErrorException.NotFound ex) {
return false;
}
});
}
catch (ConditionTimeoutException ex) {
logger.debug("Timeout checking Bintray");
return false;
}
return true;
}
private boolean hasPublishedAll(PackageFile[] published, Set<String> requiredDigests) {
if (published == null || published.length == 0) {
logger.debug("Bintray returned no published files");
return false;
}
Set<String> remaining = new HashSet<>(requiredDigests);
for (PackageFile publishedFile : published) {
logger.debug(
"Found published file " + publishedFile.getName() + " with digest " + publishedFile.getSha256());
remaining.remove(publishedFile.getSha256());
}
if (remaining.isEmpty()) {
logger.debug("Found all required digests");
return true;
}
logger.debug(remaining.size() + " digests have not been published:");
remaining.forEach(logger::debug);
return false;
}
private RequestEntity<Void> getPackageFilesRequest(ReleaseInfo releaseInfo, int includeUnpublished) {
return RequestEntity.get(URI.create(BINTRAY_URL + "packages/" + this.bintrayProperties.getSubject() + "/"
+ this.bintrayProperties.getRepo() + "/" + releaseInfo.getGroupId() + "/versions/"
+ releaseInfo.getVersion() + "/files?include_unpublished=" + includeUnpublished)).build();
}
/**
* Add attributes to Spring Boot's Gradle plugin.
* @param releaseInfo the release information
*/
public void publishGradlePlugin(ReleaseInfo releaseInfo) {
logger.debug("Publishing Gradle plugin");
RequestEntity<String> requestEntity = RequestEntity
.post(URI.create(BINTRAY_URL + "packages/" + this.bintrayProperties.getSubject() + "/"
+ this.bintrayProperties.getRepo() + "/" + releaseInfo.getGroupId() + "/versions/"
+ releaseInfo.getVersion() + "/attributes"))
.contentType(MediaType.APPLICATION_JSON).body(GRADLE_PLUGIN_REQUEST);
try {
this.restTemplate.exchange(requestEntity, Object.class);
logger.debug("Publishing Gradle plugin complete");
}
catch (HttpClientErrorException ex) {
logger.info("Failed to add attribute to Gradle plugin");
throw ex;
}
}
/**
* Sync artifacts from Bintray to Maven Central.
* @param releaseInfo the release information
*/
public void syncToMavenCentral(ReleaseInfo releaseInfo) {
logger.info("Calling Bintray to sync to Sonatype");
if (this.sonatypeService.artifactsPublished(releaseInfo)) {
logger.info("Artifacts already published");
return;
}
RequestEntity<SonatypeProperties> requestEntity = RequestEntity
.post(URI.create(String.format(BINTRAY_URL + "maven_central_sync/%s/%s/%s/versions/%s",
this.bintrayProperties.getSubject(), this.bintrayProperties.getRepo(), releaseInfo.getGroupId(),
releaseInfo.getVersion())))
.contentType(MediaType.APPLICATION_JSON).body(this.sonatypeProperties);
try {
this.restTemplate.exchange(requestEntity, Object.class);
logger.debug("Sync complete");
}
catch (HttpClientErrorException ex) {
logger.info("Failed to sync");
throw ex;
}
}
}