Skip to content

Commit

Permalink
Merge branch '2.3.x' into 2.4.x
Browse files Browse the repository at this point in the history
Closes gh-27081
  • Loading branch information
wilkinsona committed Jun 24, 2021
2 parents 99a8409 + 7617f0d commit 2ea8367
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ public void run(ApplicationArguments args) throws Exception {
byte[] content = Files.readAllBytes(new File(buildInfoLocation).toPath());
BuildInfoResponse buildInfoResponse = this.objectMapper.readValue(content, BuildInfoResponse.class);
BuildInfo buildInfo = buildInfoResponse.getBuildInfo();
ReleaseInfo releaseInfo = ReleaseInfo.from(buildInfo);
String artifactsLocation = nonOptionArgs.get(3);
this.sonatype.publish(ReleaseInfo.from(buildInfo), new File(artifactsLocation).toPath());
this.sonatype.publish(releaseInfo, new File(artifactsLocation).toPath());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,6 @@ public SonatypeService(RestTemplateBuilder builder, SonatypeProperties sonatypeP
this.artifactCollector = new ArtifactCollector(sonatypeProperties.getExclude());
}

/**
* Checks if artifacts are already published to Maven Central.
* @return true if artifacts are published
* @param releaseInfo the release information
*/
public boolean artifactsPublished(ReleaseInfo releaseInfo) {
try {
ResponseEntity<?> entity = this.restTemplate
.getForEntity(String.format(NEXUS_REPOSITORY_PATH + "%s/spring-boot-%s.jar.sha1",
releaseInfo.getVersion(), releaseInfo.getVersion()), byte[].class);
if (HttpStatus.OK.equals(entity.getStatusCode())) {
logger.info("Already published to Sonatype.");
return true;
}
}
catch (HttpClientErrorException ex) {

}
return false;
}

/**
* Publishes the release by creating a staging repository and deploying to it the
* artifacts at the given {@code artifactsRoot}. The repository is then closed and,
Expand All @@ -117,6 +96,9 @@ public boolean artifactsPublished(ReleaseInfo releaseInfo) {
* @param artifactsRoot the root directory of the artifacts to stage
*/
public void publish(ReleaseInfo releaseInfo, Path artifactsRoot) {
if (artifactsPublished(releaseInfo)) {
return;
}
logger.info("Creating staging repository");
String buildId = releaseInfo.getBuildNumber();
String repositoryId = createStagingRepository(buildId);
Expand All @@ -130,6 +112,22 @@ public void publish(ReleaseInfo releaseInfo, Path artifactsRoot) {
logger.info("Staging repository released");
}

private boolean artifactsPublished(ReleaseInfo releaseInfo) {
try {
ResponseEntity<?> entity = this.restTemplate
.getForEntity(String.format(NEXUS_REPOSITORY_PATH + "%s/spring-boot-%s.jar.sha1",
releaseInfo.getVersion(), releaseInfo.getVersion()), byte[].class);
if (HttpStatus.OK.equals(entity.getStatusCode())) {
logger.info("Already published to Sonatype.");
return true;
}
}
catch (HttpClientErrorException ex) {

}
return false;
}

private String createStagingRepository(String buildId) {
Map<String, Object> body = new HashMap<>();
body.put("data", Collections.singletonMap("description", buildId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,23 @@ void tearDown() {
}

@Test
void artifactsPublishedWhenPublishedShouldReturnTrue() {
void publishWhenAlreadyPublishedShouldNotPublish() {
this.server.expect(requestTo(String.format(
"/service/local/repositories/releases/content/org/springframework/boot/spring-boot/%s/spring-boot-%s.jar.sha1",
"1.1.0.RELEASE", "1.1.0.RELEASE"))).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess().body("ce8d8b6838ecceb68962b9150b18682f4237ccf71".getBytes()));
boolean published = this.service.artifactsPublished(getReleaseInfo());
assertThat(published).isTrue();
Path artifactsRoot = new File("src/test/resources/io/spring/concourse/releasescripts/sonatype/artifactory-repo")
.toPath();
this.service.publish(getReleaseInfo(), artifactsRoot);
this.server.verify();
}

@Test
void artifactsPublishedWhenNotPublishedShouldReturnFalse() {
void publishWithSuccessfulClose() throws IOException {
this.server.expect(requestTo(String.format(
"/service/local/repositories/releases/content/org/springframework/boot/spring-boot/%s/spring-boot-%s.jar.sha1",
"1.1.0.RELEASE", "1.1.0.RELEASE"))).andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.NOT_FOUND));
boolean published = this.service.artifactsPublished(getReleaseInfo());
assertThat(published).isFalse();
this.server.verify();
}

@Test
void publishWithSuccessfulClose() throws IOException {
this.server.expect(requestTo("/service/local/staging/profiles/1a2b3c4d/start"))
.andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json"))
.andExpect(header("Accept", "application/json, application/*+json"))
Expand Down Expand Up @@ -145,6 +139,10 @@ void publishWithSuccessfulClose() throws IOException {

@Test
void publishWithCloseFailureDueToRuleViolations() throws IOException {
this.server.expect(requestTo(String.format(
"/service/local/repositories/releases/content/org/springframework/boot/spring-boot/%s/spring-boot-%s.jar.sha1",
"1.1.0.RELEASE", "1.1.0.RELEASE"))).andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.NOT_FOUND));
this.server.expect(requestTo("/service/local/staging/profiles/1a2b3c4d/start"))
.andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json"))
.andExpect(header("Accept", "application/json, application/*+json"))
Expand Down

0 comments on commit 2ea8367

Please sign in to comment.