Skip to content

Commit

Permalink
#313 Fixed type of dependency change
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed May 31, 2022
1 parent 709e21c commit 1aef7af
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
2 changes: 1 addition & 1 deletion project-keeper/error_code_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error-tags:
PK-CORE:
packages:
- com.exasol.projectkeeper
highest-index: 147
highest-index: 148
Original file line number Diff line number Diff line change
@@ -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<DependencyChange> 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<DependencyChange> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> COMMAND_LIST_DIRECT_DEPDENDENCIES = List.of("go", "list", "-f",
"{{if not .Indirect}}{{.}}{{end}}", "-m", "all");
Expand Down Expand Up @@ -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<String, GoModDependency> oldDependencies;
private final Map<String, GoModDependency> newDependencies;
private final GoModFile oldModFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -42,20 +41,23 @@ public List<AnalyzedSource> analyze(final Path projectDir, final List<Source> 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) //
.advertise(source.isAdvertise()) //
.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();
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -69,7 +73,26 @@ void testWrongVersionConfigType() {
void testGetDependencyLicenses() {
this.fixture.prepareProjectFiles();
final ProjectKeeperConfig config = createDefaultConfigWithAbsolutePath().build();
final List<ProjectDependency> 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<ProjectDependency> 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")))
Expand Down

0 comments on commit 1aef7af

Please sign in to comment.