Skip to content

Commit

Permalink
SnapshotDependencies checker moved to java - fixes #827 (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgalek authored Oct 1, 2024
1 parent fb29f27 commit 9041e39
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,62 @@ class VerifyReleaseIntegrationTest extends BaseIntegrationTest {
result.task(":verifyRelease").outcome == TaskOutcome.SUCCESS
}

def "should work in multimodule project setup"() {
given:
initialMultiModuleProjectConfiguration()

when:
def result = runGradle(':verifyRelease')

then:
result.task(":verifyRelease").outcome == TaskOutcome.SUCCESS
}

void initialMultiModuleProjectConfiguration() {
buildFile('''
scmVersion {
versionCreator "versionWithBranch"
}
allprojects {
version = scmVersion.version
}
''')
generateSettingsFile(temporaryFolder)
generateGitIgnoreFile(temporaryFolder)
generateSubmoduleBuildFile("module1")
repository.commit(['.'], "initial commit of top level project")
runGradle(":createRelease", "-Prelease.version=1.0.0", '-Prelease.disableChecks')
}

void generateSubmoduleBuildFile(String projectName) {
File submoduleDir = new File(temporaryFolder, projectName)
submoduleDir.mkdirs()
File buildFile = new File(submoduleDir, "build.gradle")
buildFile << '''
plugins {
id 'java-platform'
}
dependencies {
constraints {
api "multimodule-project:multimodule-project:${version}"
}
}
'''
}

void generateSettingsFile(File dir) {
File settings = new File(dir, "settings.gradle")
settings << """
rootProject.name = "multimodule-project"
include(':module1')
"""
}

void generateGitIgnoreFile(File dir) {
File gitIgnore = new File(dir, ".gitignore")
gitIgnore << """\
.gradle
""".stripIndent()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pl.allegro.tech.build.axion.release.domain;

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyConstraint;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class SnapshotDependenciesChecker {

public Collection<String> snapshotVersions(Project project) {
Set<String> projectVersions = project.getRootProject().getAllprojects().stream()
.map(this::toFullVersion)
.collect(Collectors.toSet());

Set<Configuration> configurations = project.getRootProject().getAllprojects().stream()
.flatMap(p -> p.getConfigurations().stream())
.collect(Collectors.toSet());

Set<String> allDependenciesVersions = new HashSet<>();
for (Configuration config : configurations) {
Set<String> versions = config.getAllDependencies().stream()
.filter(this::isSnapshot)
.map(this::toFullVersion)
.collect(Collectors.toSet());

Set<String> constraintVersions = config.getAllDependencyConstraints().stream()
.filter(this::isSnapshot)
.map(this::toFullVersion)
.collect(Collectors.toSet());

allDependenciesVersions.addAll(versions);
allDependenciesVersions.addAll(constraintVersions);
}
allDependenciesVersions.removeAll(projectVersions);
return allDependenciesVersions;
}

private boolean isSnapshot(Dependency dependency) {
return dependency.getVersion() != null && dependency.getVersion().endsWith("-SNAPSHOT");
}

private boolean isSnapshot(DependencyConstraint dependency) {
return dependency.getVersion() != null && dependency.getVersion().endsWith("-SNAPSHOT");
}

private String toFullVersion(Object it) {
if (it instanceof Dependency) {
Dependency dependency = (Dependency) it;
return String.format("%s:%s:%s", dependency.getGroup(), dependency.getName(), dependency.getVersion());
} else if (it instanceof DependencyConstraint) {
DependencyConstraint constraint = (DependencyConstraint) it;
return String.format("%s:%s:%s", constraint.getGroup(), constraint.getName(), constraint.getVersion());
}
return "";
}
}

0 comments on commit 9041e39

Please sign in to comment.