Fixed setting of docs url for old trains

This commit is contained in:
Marcin Grzejszczak
2019-11-18 14:19:37 +01:00
parent 988e4f3b8a
commit eca8bb013f
8 changed files with 373 additions and 71 deletions

View File

@@ -345,12 +345,13 @@ public class Releaser implements ReleaserPropertiesAware {
}
}
public void updateSagan(File project, ProjectVersion releaseVersion) {
public void updateSagan(File project, ProjectVersion releaseVersion,
Projects projects) {
String currentBranch = this.projectGitHandler.currentBranch(project);
ProjectVersion originalVersion = new ProjectVersion(project);
try {
this.saganUpdater.updateSagan(project, currentBranch, originalVersion,
releaseVersion);
releaseVersion, projects);
log.info("\nSuccessfully updated Sagan for branch [{}]", currentBranch);
}
catch (Exception ex) {

View File

@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.cloud.release.internal.project.ProjectVersion;
import org.springframework.cloud.release.internal.project.Projects;
import org.springframework.util.StringUtils;
/**
@@ -48,14 +49,16 @@ public class SaganUpdater {
}
public void updateSagan(File projectFile, String branch,
ProjectVersion originalVersion, ProjectVersion currentVersion) {
ProjectVersion originalVersion, ProjectVersion currentVersion,
Projects projects) {
if (!this.releaserProperties.getSagan().isUpdateSagan()) {
log.info("Will not update sagan, since the switch to do so "
+ "is off. Set [releaser.sagan.update-sagan] to [true] to change that");
return;
}
ReleaseUpdate update = releaseUpdate(branch, originalVersion, currentVersion);
updateSaganForNonSnapshot(branch, originalVersion, currentVersion);
ReleaseUpdate update = releaseUpdate(branch, originalVersion, currentVersion,
projects);
updateSaganForNonSnapshot(branch, originalVersion, currentVersion, projects);
log.info("Updating Sagan releases with \n\n{}", update);
Project project = this.saganClient.updateRelease(currentVersion.projectName,
Collections.singletonList(update));
@@ -142,7 +145,7 @@ public class SaganUpdater {
}
private void updateSaganForNonSnapshot(String branch, ProjectVersion originalVersion,
ProjectVersion version) {
ProjectVersion version, Projects projects) {
if (!version.isSnapshot()) {
log.info(
"Version is non snapshot [{}]. Will remove all older versions and mark this as current",
@@ -156,7 +159,8 @@ public class SaganUpdater {
if (version.isRelease() || version.isServiceRelease()) {
String bumpedSnapshot = toSnapshot(version.bumpedVersion());
ReleaseUpdate snapshotUpdate = releaseUpdate(branch, originalVersion,
new ProjectVersion(version.projectName, bumpedSnapshot));
new ProjectVersion(version.projectName, bumpedSnapshot),
projects);
log.info("Updating Sagan with bumped snapshot \n\n[{}]", snapshotUpdate);
this.saganClient.updateRelease(version.projectName,
Collections.singletonList(snapshotUpdate));
@@ -183,13 +187,13 @@ public class SaganUpdater {
}
private ReleaseUpdate releaseUpdate(String branch, ProjectVersion originalVersion,
ProjectVersion version) {
ProjectVersion version, Projects projects) {
ReleaseUpdate update = new ReleaseUpdate();
update.groupId = originalVersion.groupId();
update.artifactId = version.projectName;
update.version = version.version;
update.releaseStatus = version(version);
update.apiDocUrl = referenceUrl(branch, version);
update.apiDocUrl = referenceUrl(branch, version, projects);
update.refDocUrl = update.apiDocUrl;
update.current = true;
return update;
@@ -218,7 +222,30 @@ public class SaganUpdater {
return "";
}
private String referenceUrl(String branch, ProjectVersion version) {
private String releaseTrainVersion(Projects projects) {
String releaseTrainProjectName = this.releaserProperties.getMetaRelease()
.getReleaseTrainProjectName();
return projects.containsProject(releaseTrainProjectName)
? projects.forName(releaseTrainProjectName).version : "";
}
private String referenceUrl(String branch, ProjectVersion version,
Projects projects) {
String releaseTrainVersion = releaseTrainVersion(projects);
// up till Greenwich we have a different URL for docs
// if there's no release train, will assume that 2.2.x is the version that has the
// new docs
boolean hasReleaseTrainVersion = StringUtils.hasText(releaseTrainVersion);
boolean newDocs = hasReleaseTrainVersion
? releaseTrainVersion.toLowerCase().charAt(0) > 'g'
: version.version.compareTo("2.2") > 0;
if (newDocs) {
return newReferenceUrl(branch, version);
}
return oldReferenceUrl(branch, version);
}
private String newReferenceUrl(String branch, ProjectVersion version) {
if (!version.isSnapshot()) {
// static/sleuth/{version}/
return "https://cloud.spring.io/spring-cloud-static/" + version.projectName
@@ -233,4 +260,19 @@ public class SaganUpdater {
+ "/reference/html/";
}
private String oldReferenceUrl(String branch, ProjectVersion version) {
if (!version.isSnapshot()) {
// static/sleuth/{version}/
return "https://cloud.spring.io/spring-cloud-static/" + version.projectName
+ "/{version}/";
}
if (branch.toLowerCase().contains("master")) {
// sleuth/
return "https://cloud.spring.io/" + version.projectName + "/"
+ version.projectName + ".html";
}
// sleuth/1.1.x/
return "https://cloud.spring.io/" + version.projectName + "/" + branch + "/";
}
}

View File

@@ -0,0 +1,259 @@
/*
* Copyright 2013-2019 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 org.springframework.cloud.release.internal.sagan;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.cloud.release.internal.project.ProjectVersion;
import org.springframework.cloud.release.internal.project.Projects;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.never;
/**
* @author Marcin Grzejszczak
*/
public class SaganUpdaterOldDocsTest {
SaganClient saganClient = Mockito.mock(SaganClient.class);
ReleaserProperties properties = new ReleaserProperties();
SaganUpdater saganUpdater = new SaganUpdater(this.saganClient, this.properties);
Projects projects = new Projects();
@Before
public void setup() {
Project project = new Project();
project.projectReleases.addAll(Arrays.asList(release("1.0.0.RC1"),
release("1.1.0.BUILD-SNAPSHOT"), release("2.0.0.M4")));
BDDMockito.given(this.saganClient.getProject(anyString())).willReturn(project);
}
private Release release(String version) {
Release release = new Release();
release.version = version;
release.current = true;
return release;
}
@Test
public void should_not_update_sagan_when_switch_is_off() {
this.properties.getSagan().setUpdateSagan(false);
this.saganUpdater.updateSagan(new File("."), "master", version("2.2.0.M1"),
version("2.2.0.M1"), projects);
then(this.saganClient).shouldHaveZeroInteractions();
}
@Test
public void should_update_sagan_releases_for_milestone() {
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.M1"),
version("1.0.0.M1"), projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.M1",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/",
"PRERELEASE")));
}
@Test
public void should_update_sagan_releases_for_rc() {
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.RC1"),
version("1.0.0.RC1"), projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.RC1",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/",
"PRERELEASE")));
}
@Test
public void should_not_update_docs_for_sagan_when_current_version_older() {
given(this.saganClient.updateRelease(BDDMockito.anyString(),
BDDMockito.anyList())).willReturn(a2_0_0_ReleaseProject());
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.RC1"),
version("1.0.0.RC1"), projects);
then(this.saganClient).should(BDDMockito.never())
.patchProject(BDDMockito.any(Project.class));
}
private Project a2_0_0_ReleaseProject() {
Project project = new Project();
Release release = new Release();
release.version = "2.0.0.RELEASE";
release.current = true;
project.projectReleases = Collections.singletonList(release);
return project;
}
@Test
public void should_not_update_docs_for_sagan_when_files_exist_but_content_does_not_differ()
throws IOException {
Project project = a2_0_0_ReleaseProject();
project.rawOverview = "new overview";
project.rawBootConfig = "new boot";
given(this.saganClient.updateRelease(BDDMockito.anyString(),
BDDMockito.anyList())).willReturn(project);
Path tmp = Files.createTempDirectory("releaser-test");
createFile(tmp, "sagan-index.adoc", "new overview");
createFile(tmp, "sagan-boot.adoc", "new boot");
SaganUpdater saganUpdater = new SaganUpdater(this.saganClient, this.properties) {
@Override
File docsModule(File projectFile) {
return tmp.toFile();
}
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"), projects);
then(this.saganClient).should(BDDMockito.never())
.patchProject(BDDMockito.any(Project.class));
}
@Test
public void should_update_docs_for_sagan_when_current_version_newer_and_only_overview_adoc_exists()
throws IOException {
given(this.saganClient.updateRelease(BDDMockito.anyString(),
BDDMockito.anyList())).willReturn(a2_0_0_ReleaseProject());
Path tmp = Files.createTempDirectory("releaser-test");
createFile(tmp, "sagan-index.adoc", "new text");
SaganUpdater saganUpdater = new SaganUpdater(this.saganClient, this.properties) {
@Override
File docsModule(File projectFile) {
return tmp.toFile();
}
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"), projects);
then(this.saganClient).should().patchProject(
BDDMockito.argThat(argument -> "new text".equals(argument.rawOverview)));
}
@Test
public void should_update_docs_for_sagan_when_current_version_newer_and_only_boot_adoc_exists()
throws IOException {
given(this.saganClient.updateRelease(BDDMockito.anyString(),
BDDMockito.anyList())).willReturn(a2_0_0_ReleaseProject());
Path tmp = Files.createTempDirectory("releaser-test");
createFile(tmp, "sagan-boot.adoc", "new text");
SaganUpdater saganUpdater = new SaganUpdater(this.saganClient, this.properties) {
@Override
File docsModule(File projectFile) {
return tmp.toFile();
}
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"), projects);
then(this.saganClient).should().patchProject(BDDMockito
.argThat(argument -> "new text".equals(argument.rawBootConfig)));
}
private void createFile(Path tmp, String filename, String text) throws IOException {
File overviewAdoc = new File(tmp.toString(), filename);
overviewAdoc.createNewFile();
Files.write(overviewAdoc.toPath(), text.getBytes());
}
private ProjectVersion version(String version) {
return new ProjectVersion("foo", version);
}
@Test
public void should_update_sagan_from_master() {
ProjectVersion projectVersion = version("1.0.0.BUILD-SNAPSHOT");
this.saganUpdater.updateSagan(new File("."), "master", projectVersion,
projectVersion, projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/foo.html", "SNAPSHOT")));
}
@Test
public void should_update_sagan_from_release_version() {
ProjectVersion projectVersion = version("1.0.0.RELEASE");
this.saganUpdater.updateSagan(new File("."), "master", projectVersion,
projectVersion, projects);
then(this.saganClient).should().deleteRelease("foo", "1.0.0.RC1");
then(this.saganClient).should().deleteRelease("foo", "1.0.0.BUILD-SNAPSHOT");
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.RELEASE",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/",
"GENERAL_AVAILABILITY")));
then(this.saganClient).should().deleteRelease("foo", "1.0.0.BUILD-SNAPSHOT");
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.1.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/foo.html", "SNAPSHOT")));
}
@Test
public void should_update_sagan_from_non_master() {
ProjectVersion projectVersion = version("1.1.0.BUILD-SNAPSHOT");
this.saganUpdater.updateSagan(new File("."), "1.1.x", projectVersion,
projectVersion, projects);
then(this.saganClient).should(never()).deleteRelease(anyString(), anyString());
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.1.0.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/1.1.x/", "SNAPSHOT")));
}
private ArgumentMatcher<List<ReleaseUpdate>> withReleaseUpdate(final String version,
final String refDocUrl, final String releaseStatus) {
return argument -> {
ReleaseUpdate item = argument.get(0);
return "foo".equals(item.artifactId)
&& releaseStatus.equals(item.releaseStatus)
&& version.equals(item.version) && refDocUrl.equals(item.apiDocUrl)
&& refDocUrl.equals(item.refDocUrl) && item.current;
};
}
}

View File

@@ -32,6 +32,7 @@ import org.mockito.Mockito;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.cloud.release.internal.project.ProjectVersion;
import org.springframework.cloud.release.internal.project.Projects;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@@ -49,11 +50,13 @@ public class SaganUpdaterTest {
SaganUpdater saganUpdater = new SaganUpdater(this.saganClient, this.properties);
Projects projects = new Projects();
@Before
public void setup() {
Project project = new Project();
project.projectReleases.addAll(Arrays.asList(release("1.0.0.RC1"),
release("1.1.0.BUILD-SNAPSHOT"), release("2.0.0.M4")));
project.projectReleases.addAll(Arrays.asList(release("2.2.0.RC1"),
release("2.3.0.BUILD-SNAPSHOT"), release("2.2.0.M4")));
BDDMockito.given(this.saganClient.getProject(anyString())).willReturn(project);
}
@@ -68,30 +71,30 @@ public class SaganUpdaterTest {
public void should_not_update_sagan_when_switch_is_off() {
this.properties.getSagan().setUpdateSagan(false);
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.M1"),
version("1.0.0.M1"));
this.saganUpdater.updateSagan(new File("."), "master", version("2.2.0.M1"),
version("2.2.0.M1"), projects);
then(this.saganClient).shouldHaveZeroInteractions();
}
@Test
public void should_update_sagan_releases_for_milestone() {
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.M1"),
version("1.0.0.M1"));
this.saganUpdater.updateSagan(new File("."), "master", version("2.2.0.M1"),
version("2.2.0.M1"), projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.M1",
BDDMockito.argThat(withReleaseUpdate("2.2.0.M1",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/reference/html/",
"PRERELEASE")));
}
@Test
public void should_update_sagan_releases_for_rc() {
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.RC1"),
version("1.0.0.RC1"));
this.saganUpdater.updateSagan(new File("."), "master", version("2.2.0.RC1"),
version("2.2.0.RC1"), projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.RC1",
BDDMockito.argThat(withReleaseUpdate("2.2.0.RC1",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/reference/html/",
"PRERELEASE")));
}
@@ -101,8 +104,8 @@ public class SaganUpdaterTest {
given(this.saganClient.updateRelease(BDDMockito.anyString(),
BDDMockito.anyList())).willReturn(a2_0_0_ReleaseProject());
this.saganUpdater.updateSagan(new File("."), "master", version("1.0.0.RC1"),
version("1.0.0.RC1"));
this.saganUpdater.updateSagan(new File("."), "master", version("2.2.0.RC1"),
version("2.2.0.RC1"), projects);
then(this.saganClient).should(BDDMockito.never())
.patchProject(BDDMockito.any(Project.class));
@@ -138,7 +141,7 @@ public class SaganUpdaterTest {
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"));
version("3.0.0.RC1"), projects);
then(this.saganClient).should(BDDMockito.never())
.patchProject(BDDMockito.any(Project.class));
@@ -160,7 +163,7 @@ public class SaganUpdaterTest {
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"));
version("3.0.0.RC1"), projects);
then(this.saganClient).should().patchProject(
BDDMockito.argThat(argument -> "new text".equals(argument.rawOverview)));
@@ -182,7 +185,7 @@ public class SaganUpdaterTest {
};
saganUpdater.updateSagan(new File("."), "master", version("3.0.0.RC1"),
version("3.0.0.RC1"));
version("3.0.0.RC1"), projects);
then(this.saganClient).should().patchProject(BDDMockito
.argThat(argument -> "new text".equals(argument.rawBootConfig)));
@@ -200,46 +203,46 @@ public class SaganUpdaterTest {
@Test
public void should_update_sagan_from_master() {
ProjectVersion projectVersion = version("1.0.0.BUILD-SNAPSHOT");
ProjectVersion projectVersion = version("2.2.0.BUILD-SNAPSHOT");
this.saganUpdater.updateSagan(new File("."), "master", projectVersion,
projectVersion);
projectVersion, projects);
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.BUILD-SNAPSHOT",
BDDMockito.argThat(withReleaseUpdate("2.2.0.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/reference/html/", "SNAPSHOT")));
}
@Test
public void should_update_sagan_from_release_version() {
ProjectVersion projectVersion = version("1.0.0.RELEASE");
ProjectVersion projectVersion = version("2.2.0.RELEASE");
this.saganUpdater.updateSagan(new File("."), "master", projectVersion,
projectVersion);
projectVersion, projects);
then(this.saganClient).should().deleteRelease("foo", "1.0.0.RC1");
then(this.saganClient).should().deleteRelease("foo", "1.0.0.BUILD-SNAPSHOT");
then(this.saganClient).should().deleteRelease("foo", "2.2.0.RC1");
then(this.saganClient).should().deleteRelease("foo", "2.2.0.BUILD-SNAPSHOT");
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.0.RELEASE",
BDDMockito.argThat(withReleaseUpdate("2.2.0.RELEASE",
"https://cloud.spring.io/spring-cloud-static/foo/{version}/reference/html/",
"GENERAL_AVAILABILITY")));
then(this.saganClient).should().deleteRelease("foo", "1.0.0.BUILD-SNAPSHOT");
then(this.saganClient).should().deleteRelease("foo", "2.2.0.BUILD-SNAPSHOT");
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.0.1.BUILD-SNAPSHOT",
BDDMockito.argThat(withReleaseUpdate("2.2.1.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/reference/html/", "SNAPSHOT")));
}
@Test
public void should_update_sagan_from_non_master() {
ProjectVersion projectVersion = version("1.1.0.BUILD-SNAPSHOT");
ProjectVersion projectVersion = version("2.3.0.BUILD-SNAPSHOT");
this.saganUpdater.updateSagan(new File("."), "1.1.x", projectVersion,
projectVersion);
this.saganUpdater.updateSagan(new File("."), "2.3.x", projectVersion,
projectVersion, projects);
then(this.saganClient).should(never()).deleteRelease(anyString(), anyString());
then(this.saganClient).should().updateRelease(BDDMockito.eq("foo"),
BDDMockito.argThat(withReleaseUpdate("1.1.0.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/1.1.x/reference/html/",
BDDMockito.argThat(withReleaseUpdate("2.3.0.BUILD-SNAPSHOT",
"https://cloud.spring.io/foo/2.3.x/reference/html/",
"SNAPSHOT")));
}