Shouldn't fail the update for Gradle projects

This commit is contained in:
Marcin Grzejszczak
2019-01-23 14:47:16 +01:00
parent dd80be4646
commit f4a2ff11a9
3 changed files with 73 additions and 18 deletions

View File

@@ -25,14 +25,28 @@ public class ProjectVersion {
}
public ProjectVersion(File project) {
PomReader pomReader = new PomReader();
Model model = pomReader.readPom(project);
this.projectName = nameWithoutParent(model.getArtifactId());
this.version = model.getVersion();
this.model = model;
if (new File(project, "build.gradle").exists()) {
ProjectVersion projectVersion = notMavenProject(project);
this.projectName = projectVersion.projectName;
this.version = projectVersion.version;
this.model = null;
} else {
PomReader pomReader = new PomReader();
Model model = pomReader.readPom(project);
this.projectName = nameWithoutParent(model.getArtifactId());
this.version = model.getVersion();
this.model = model;
}
}
private String nameWithoutParent(String projectName) {
public static ProjectVersion notMavenProject(File file) {
File parentFolder = file.getParentFile() != null ? file.getParentFile() : file;
String name = parentFolder.getName();
String version = "1.0.0.BUILD-SNAPSHOT";
return new ProjectVersion(nameWithoutParent(name), version);
}
private static String nameWithoutParent(String projectName) {
boolean containsParent = projectName.endsWith("-parent");
if (!containsParent) {
return projectName;

View File

@@ -23,6 +23,7 @@ import org.springframework.cloud.release.internal.pom.ProjectVersion;
import org.springframework.cloud.release.internal.pom.Projects;
import org.springframework.cloud.release.internal.project.ProjectBuilder;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.util.StringUtils;
/**
* @author Marcin Grzejszczak
@@ -62,7 +63,7 @@ public class PostReleaseActions implements Closeable {
return;
}
File file = this.projectGitHandler.cloneTestSamplesProject();
ProjectVersion projectVersion = new ProjectVersion(file);
ProjectVersion projectVersion = newProjectVersion(file);
String releaseTrainVersion = projects.releaseTrain(this.properties).version;
Projects newProjects = addVersionForTestsProject(projects, projectVersion, releaseTrainVersion);
updateWithVersions(file, newProjects);
@@ -92,15 +93,17 @@ public class PostReleaseActions implements Closeable {
.flatMap(Collection::stream)
.collect(Collectors.toList());
log.info("Updated all samples!");
List<String> exceptionMessages = projectUrlAndExceptions.stream()
String exceptionMessages = projectUrlAndExceptions.stream()
.filter(ProjectUrlAndException::hasException)
.map(e -> "Project [" + e.key + "] for url [" + e.url + "] "
+ "has exception [" + Arrays
.toString(NestedExceptionUtils.getMostSpecificCause(e.ex)
.getStackTrace()) + "]")
.collect(Collectors.toList());
if (!exceptionMessages.isEmpty()) {
throw new IllegalStateException("Exceptions were found while updating samples\n" + String.join("\n", exceptionMessages));
+ "has exception [\n\n" + Arrays
.stream(NestedExceptionUtils.getMostSpecificCause(e.ex)
.getStackTrace())
.map(StackTraceElement::toString)
.collect(Collectors.joining("\n")) + "]")
.collect(Collectors.joining("\n"));
if (StringUtils.hasText(exceptionMessages)) {
throw new IllegalStateException("Exceptions were found while updating samples\n" + exceptionMessages);
} else {
log.info("No exceptions were found while updating the samples");
}
@@ -139,7 +142,8 @@ public class PostReleaseActions implements Closeable {
File file = this.projectGitHandler
.cloneAndGuessBranch(url, releaseTrainVersion, projectVersion);
Projects newPostRelease = new Projects(postRelease);
newPostRelease.add(new ProjectVersion(file));
ProjectVersion newProjectVersion = newProjectVersion(file);
newPostRelease.add(newProjectVersion);
updateWithVersions(file, newPostRelease);
this.projectGitHandler
.commit(file, "Updated versions after [" + releaseTrainVersion + "] "
@@ -148,12 +152,24 @@ public class PostReleaseActions implements Closeable {
this.projectGitHandler.pushCurrentBranch(file);
}
private ProjectVersion newProjectVersion(File file) {
try {
return new ProjectVersion(file);
} catch (Exception ex) {
ProjectVersion projectVersion = ProjectVersion.notMavenProject(file);
String name = projectVersion.projectName;
String version = projectVersion.version;
log.warn("Exception occurred while trying to read the pom file. Will assume that the project name is [" + name + "] and version [" + version + "]", ex);
return projectVersion;
}
}
private void updateWithVersions(File file, Projects newPostRelease) {
this.projectPomUpdater
.updateProjectFromReleaseTrain(file, newPostRelease,
new ProjectVersion(file), false);
newProjectVersion(file), false);
this.gradleUpdater.updateProjectFromBom(file, newPostRelease,
new ProjectVersion(file), false);
newProjectVersion(file), false);
}
private ProjectAndFuture run(String key, String url, Runnable runnable) {
@@ -194,7 +210,7 @@ public class PostReleaseActions implements Closeable {
return;
}
File file = this.projectGitHandler.cloneReleaseTrainDocumentationProject();
ProjectVersion projectVersion = new ProjectVersion(file);
ProjectVersion projectVersion = newProjectVersion(file);
String releaseTrainVersion = projects.releaseTrain(this.properties).version;
Projects newProjects = addVersionForTestsProject(projects, projectVersion, releaseTrainVersion);
updateWithVersions(file, newProjects);

View File

@@ -223,6 +223,31 @@ public class PostReleaseActionsTests {
.forName("spring-boot-dependencies").version).isEqualTo("2.0.4.RELEASE");
}
@Test
public void should_assume_that_project_version_is_snapshot_when_no_pom_is_present() throws Exception {
this.properties.getMetaRelease().setEnabled(true);
this.properties.getGit().getAllTestSampleUrls().clear();
this.properties.getGit().getAllTestSampleUrls().put("spring-cloud-sleuth",
Collections.singletonList(tmpFile("spring-cloud-static/")
.getAbsolutePath() + "/"));
AtomicReference<Projects> postReleaseProjects = new AtomicReference<>();
PostReleaseActions actions = new PostReleaseActions(this.projectGitHandler,
this.updater, this.gradleUpdater, this.builder, this.properties) {
@Override
Projects getPostReleaseProjects(Projects projects) {
postReleaseProjects.set(super.getPostReleaseProjects(projects));
return postReleaseProjects.get();
}
};
actions.updateAllTestSamples(currentGa());
Map.Entry<String, List<File>> entry = this.clonedTestProjects.entrySet()
.stream()
.filter(s -> s.getKey().contains("spring-cloud-static"))
.findFirst().orElseThrow(() -> new IllegalStateException("Not found"));
}
private String sleuthParentPomVersion() {
return this.testPomReader.readPom(new File(this.cloned, "sleuth/pom.xml"))
.getParent().getVersion();