From 1aef7af5dfdb607eaeaf4ed509648edb2f6d3567 Mon Sep 17 00:00:00 2001 From: Christoph Pirkl Date: Tue, 31 May 2022 10:33:11 +0200 Subject: [PATCH] #313 Fixed type of dependency change --- project-keeper/error_code_config.yml | 2 +- .../GolangDependencyChangeCalculator.java | 64 +++++++++++++++++++ .../analyze/golang/GolangServices.java | 2 +- .../analyze/golang/GolangSourceAnalyzer.java | 21 ++---- .../golang/GolangSourceAnalyzerIT.java | 25 +++++++- 5 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangDependencyChangeCalculator.java diff --git a/project-keeper/error_code_config.yml b/project-keeper/error_code_config.yml index 04d047c4..fae3512d 100644 --- a/project-keeper/error_code_config.yml +++ b/project-keeper/error_code_config.yml @@ -2,4 +2,4 @@ error-tags: PK-CORE: packages: - com.exasol.projectkeeper - highest-index: 147 + highest-index: 148 diff --git a/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangDependencyChangeCalculator.java b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangDependencyChangeCalculator.java new file mode 100644 index 00000000..8de401d4 --- /dev/null +++ b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangDependencyChangeCalculator.java @@ -0,0 +1,64 @@ +package com.exasol.projectkeeper.sources.analyze.golang; + +import static java.util.stream.Collectors.toList; + +import java.nio.file.Path; +import java.util.List; + +import com.exasol.errorreporting.ExaError; +import com.exasol.projectkeeper.shared.config.ProjectKeeperConfig.Source; +import com.exasol.projectkeeper.shared.dependencies.ProjectDependencies; +import com.exasol.projectkeeper.shared.dependencies.ProjectDependency; +import com.exasol.projectkeeper.shared.dependencies.ProjectDependency.Type; +import com.exasol.projectkeeper.shared.dependencychanges.DependencyChange; +import com.exasol.projectkeeper.shared.dependencychanges.DependencyChangeReport; + +class GolangDependencyChangeCalculator { + + private final GolangServices golangServices; + private final Path projectDir; + private final Source source; + private final ProjectDependencies dependencies; + private List changes; + + public GolangDependencyChangeCalculator(final GolangServices golangServices, final Path projectDir, + final Source source, final ProjectDependencies dependencies) { + this.golangServices = golangServices; + this.projectDir = projectDir; + this.source = source; + this.dependencies = dependencies; + } + + static DependencyChangeReport calculateDepencencyChanges(final GolangServices golangServices, final Path projectDir, + final Source source, final ProjectDependencies dependencies) { + return new GolangDependencyChangeCalculator(golangServices, projectDir, source, dependencies).calculate(); + } + + private DependencyChangeReport calculate() { + this.changes = this.golangServices.getDependencyChanges(this.projectDir, this.source.getPath()); + final DependencyChangeReport dependencyChanges = new DependencyChangeReport(); + dependencyChanges.setCompileDependencyChanges(getDependencyChanges(Type.COMPILE)); + dependencyChanges.setPluginDependencyChanges(getDependencyChanges(Type.PLUGIN)); + dependencyChanges.setRuntimeDependencyChanges(getDependencyChanges(Type.RUNTIME)); + dependencyChanges.setTestDependencyChanges(getDependencyChanges(Type.TEST)); + return dependencyChanges; + } + + private List getDependencyChanges(final Type type) { + return this.changes.stream().filter(change -> getType(change) == type).collect(toList()); + } + + private Type getType(final DependencyChange change) { + final String name = change.getArtifactId(); + if (name.equals(GolangServices.GOLANG_DEPENDENCY_NAME)) { + return Type.COMPILE; + } + return this.dependencies.getDependencies().stream() // + .filter(dep -> dep.getName().equals(name)) // + .map(ProjectDependency::getType) // + .findFirst() // + .orElseThrow(() -> new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-148").message( + "Error finding type of dependency {{artifactId}}, all available dependencies: {{all dependencies}}", + name, this.dependencies.getDependencies()).ticketMitigation().toString())); + } +} \ No newline at end of file diff --git a/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangServices.java b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangServices.java index 912d47fc..f69f8fa8 100644 --- a/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangServices.java +++ b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangServices.java @@ -31,6 +31,7 @@ * dependency changes. */ class GolangServices { + public static final String GOLANG_DEPENDENCY_NAME = "golang"; private static final Logger LOGGER = Logger.getLogger(GolangServices.class.getName()); private static final List COMMAND_LIST_DIRECT_DEPDENDENCIES = List.of("go", "list", "-f", "{{if not .Indirect}}{{.}}{{end}}", "-m", "all"); @@ -185,7 +186,6 @@ private String getContent(final GitRepository repo, final Path relativeModFilePa } private static class DependencyChangeCalculator { - private static final String GOLANG_DEPENDENCY_NAME = "golang"; private final Map oldDependencies; private final Map newDependencies; private final GoModFile oldModFile; diff --git a/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzer.java b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzer.java index 77672ea7..89e9c4b6 100644 --- a/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzer.java +++ b/project-keeper/src/main/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzer.java @@ -1,6 +1,5 @@ package com.exasol.projectkeeper.sources.analyze.golang; -import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toList; import java.nio.file.Path; @@ -10,7 +9,7 @@ import com.exasol.projectkeeper.shared.config.ProjectKeeperConfig; import com.exasol.projectkeeper.shared.config.ProjectKeeperConfig.Source; import com.exasol.projectkeeper.shared.config.ProjectKeeperConfig.SourceType; -import com.exasol.projectkeeper.shared.dependencychanges.DependencyChangeReport; +import com.exasol.projectkeeper.shared.dependencies.ProjectDependencies; import com.exasol.projectkeeper.sources.AnalyzedGolangSource; import com.exasol.projectkeeper.sources.AnalyzedSource; import com.exasol.projectkeeper.sources.analyze.LanguageSpecificSourceAnalyzer; @@ -42,10 +41,13 @@ public List analyze(final Path projectDir, final List so private AnalyzedSource analyzeSource(final Path projectDir, final Source source) { validateGolangSource(source); + final Path sourceDir = projectDir.resolve(source.getPath().getParent()); final boolean isRoot = projectDir.relativize(source.getPath()).equals(Path.of("go.mod")); final Path moduleDir = source.getPath().getParent(); final ModuleInfo moduleInfo = this.golangServices.getModuleInfo(moduleDir); final String projectName = source.getPath().normalize().getParent().getFileName().toString(); + final ProjectDependencies dependencies = GolangDependencyCalculator.calculateDependencies(this.golangServices, + sourceDir, moduleInfo); return AnalyzedGolangSource.builder() // .version(this.golangServices.getProjectVersion()) // .isRootProject(isRoot) // @@ -53,9 +55,9 @@ private AnalyzedSource analyzeSource(final Path projectDir, final Source source) .modules(source.getModules()) // .path(source.getPath()) // .projectName(projectName).moduleName(moduleInfo.getModuleName()) // - .dependencies( - GolangDependencyCalculator.calculateDependencies(this.golangServices, projectDir, moduleInfo)) // - .dependencyChanges(calculateDependencyChanges(projectDir, source)) // + .dependencies(dependencies) // + .dependencyChanges(GolangDependencyChangeCalculator.calculateDepencencyChanges(this.golangServices, + projectDir, source, dependencies)) // .build(); } @@ -72,13 +74,4 @@ private void validateGolangSource(final Source source) { } } - private DependencyChangeReport calculateDependencyChanges(final Path projectDir, final Source source) { - final DependencyChangeReport dependencyChanges = new DependencyChangeReport(); - dependencyChanges - .setCompileDependencyChanges(this.golangServices.getDependencyChanges(projectDir, source.getPath())); - dependencyChanges.setPluginDependencyChanges(emptyList()); - dependencyChanges.setRuntimeDependencyChanges(emptyList()); - dependencyChanges.setTestDependencyChanges(emptyList()); - return dependencyChanges; - } } diff --git a/project-keeper/src/test/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzerIT.java b/project-keeper/src/test/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzerIT.java index 4d9bae99..ecae4834 100644 --- a/project-keeper/src/test/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzerIT.java +++ b/project-keeper/src/test/java/com/exasol/projectkeeper/sources/analyze/golang/GolangSourceAnalyzerIT.java @@ -4,9 +4,11 @@ import static java.util.Collections.emptySet; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertThrows; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import org.junit.jupiter.api.BeforeEach; @@ -18,6 +20,8 @@ import com.exasol.projectkeeper.shared.dependencies.License; import com.exasol.projectkeeper.shared.dependencies.ProjectDependency; import com.exasol.projectkeeper.shared.dependencies.ProjectDependency.Type; +import com.exasol.projectkeeper.shared.dependencychanges.DependencyChangeReport; +import com.exasol.projectkeeper.shared.dependencychanges.NewDependency; import com.exasol.projectkeeper.sources.AnalyzedSource; import com.exasol.projectkeeper.test.GolangProjectFixture; @@ -69,7 +73,26 @@ void testWrongVersionConfigType() { void testGetDependencyLicenses() { this.fixture.prepareProjectFiles(); final ProjectKeeperConfig config = createDefaultConfigWithAbsolutePath().build(); - final List dependencies = analyzeSingleProject(config).getDependencies().getDependencies(); + final AnalyzedSource analyzedProject = analyzeSingleProject(config); + assertDependencyLicenses(analyzedProject.getDependencies().getDependencies()); + assertDependencyChanges(analyzedProject.getDependencyChanges()); + } + + private void assertDependencyChanges(final DependencyChangeReport dependencyChanges) { + assertAll(() -> assertThat("plugin dependencies", dependencyChanges.getPluginDependencyChanges(), hasSize(0)), + () -> assertThat("runtime dependencies", dependencyChanges.getRuntimeDependencyChanges(), hasSize(0)), + () -> assertThat("compile dependencies", dependencyChanges.getCompileDependencyChanges(), + contains(newDep("golang", "1.17"), newDep("github.com/exasol/exasol-driver-go", "v0.4.0"))), + () -> assertThat("test dependencies", dependencyChanges.getTestDependencyChanges(), + contains(newDep("github.com/exasol/exasol-test-setup-abstraction-server/go-client", + "v0.0.0-20220520062645-0dd00179907c")))); + } + + private NewDependency newDep(final String name, final String version) { + return new NewDependency(null, name, version); + } + + private void assertDependencyLicenses(final List dependencies) { assertThat(dependencies, hasSize(2)); final ProjectDependency dependency1 = ProjectDependency.builder().name("github.com/exasol/exasol-driver-go") .licenses(List.of(new License("MIT", "https://github.com/exasol/exasol-driver-go/blob/v0.4.0/LICENSE")))