Skip to content

Dependencies explicit versions #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2021 - 2022 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.sbm.boot.upgrade_27_30.checks;

import org.springframework.sbm.boot.asciidoctor.ChangeSection;
import org.springframework.sbm.boot.asciidoctor.Section;
import org.springframework.sbm.boot.asciidoctor.TodoList;
import org.springframework.sbm.boot.upgrade_27_30.Sbu30_PreconditionCheck;
import org.springframework.sbm.boot.upgrade_27_30.Sbu30_PreconditionCheckResult;
import org.springframework.sbm.boot.upgrade_27_30.Sbu30_UpgradeSectionBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class RedeclaredDependenciesBuilder implements Sbu30_UpgradeSectionBuilder {
private final RedeclaredDependenciesFinder finder;

public RedeclaredDependenciesBuilder(RedeclaredDependenciesFinder finder) {
this.finder = finder;
}

@Override
public boolean isApplicable(ProjectContext projectContext) {
return !finder.findMatches(projectContext).isEmpty();
}

@Override
public Section build(ProjectContext projectContext) {
Set<RedeclaredDependenciesFinder.RedeclaredDependency> matches = finder.findMatches(projectContext);
List<TodoList.Todo> todos = matches.stream()
.map(m -> TodoList.Todo.builder()
.text(String.format("Remove explicit declaration of version for artifact: %s, its already declared with version %s", m.getRedeclaredDependency().getCoordinates(), m.originalVersion()))
.build()).toList();

return ChangeSection.RelevantChangeSection.builder()
.title("Remove redundant explicit version declaration")
.relevanceSection()
.paragraph("""
The scan found one or more redeclared dependencies in build files.
Please check them and remove redundant declarations.
"""
)
.todoSection()
.todoList(TodoList.builder()
.todos(todos)
.build())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2021 - 2022 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.sbm.boot.upgrade_27_30.checks;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedManagedDependency;
import org.springframework.sbm.build.api.ApplicationModule;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class RedeclaredDependenciesFinder implements Sbm30_Finder<Set<Dependency>> {
private final Set<String> analysingDependencies = new HashSet<>();

public RedeclaredDependenciesFinder() {
}

public RedeclaredDependenciesFinder(Set<String> analysingDependencies) {
this.analysingDependencies.addAll(analysingDependencies);
}

@NotNull
@Override
public Set<RedeclaredDependency> findMatches(ProjectContext context) {
Set<RedeclaredDependency> result = new HashSet<>();
context.getApplicationModules().stream()
.forEach(am -> {
List<org.openrewrite.maven.tree.Dependency> requestedDependencies = ((MavenResolutionResult) (((OpenRewriteMavenBuildFile) am.getBuildFile()).getPom())).getPom().getRequestedDependencies();
Map<String, String> managedMap = prepareManagedDependenciesMap(am);
List<Dependency> declaredDependencies = am.getBuildFile().getDeclaredDependencies();
Set<RedeclaredDependency> redeclaredDependencies = requestedDependencies.stream()
.filter(d -> analysingDependencies.isEmpty() || analysingDependencies.contains(getGroupAndArtifactKey(d)))
.filter(d -> d.getVersion() != null && !d.getVersion().isEmpty())
.filter(d -> managedMap.containsKey(getGroupAndArtifactKey(d)))
.map(d -> {
Dependency dependency = Dependency.builder().groupId(d.getGroupId()).artifactId(d.getArtifactId()).version(d.getVersion()).build();
return new RedeclaredDependency(dependency, managedMap.get(getGroupAndArtifactKey(d)));
})
.collect(Collectors.toSet());
result.addAll(redeclaredDependencies);
});
return result;
}

@NotNull
private Map<String, String> prepareManagedDependenciesMap(ApplicationModule am) {
List<ResolvedManagedDependency> managedDependencies = ((MavenResolutionResult) (((OpenRewriteMavenBuildFile) am.getBuildFile()).getPom())).getPom().getDependencyManagement();
return managedDependencies.stream()
.filter(d -> d.getVersion() != null && !d.getVersion().isEmpty())
.collect(Collectors.toMap(this::getGroupAndArtifactKeyResolved, ResolvedManagedDependency::getVersion, (d1, d2) -> d2));
}

@NotNull
private String getGroupAndArtifactKeyResolved(ResolvedManagedDependency d) {
return d.getGroupId() + ":" + d.getArtifactId();
}

@NotNull
private String getGroupAndArtifactKey(org.openrewrite.maven.tree.Dependency d) {
return d.getGroupId() + ":" + d.getArtifactId();
}

public record RedeclaredDependency(Dependency redeclaredDependency,
String originalVersion) {

public Dependency getRedeclaredDependency() {
return redeclaredDependency;
}

public String getOriginalVersion() {
return originalVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2021 - 2022 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.sbm.boot.upgrade_27_30.checks;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.asciidoctor.ChangeSection;
import org.springframework.sbm.boot.asciidoctor.Section;
import org.springframework.sbm.boot.asciidoctor.TodoList;
import org.springframework.sbm.boot.upgrade_27_30.checks.RedeclaredDependenciesFinder.RedeclaredDependency;
import org.springframework.sbm.build.api.ApplicationModule;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.engine.context.ProjectContext;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/*
* Copyright 2021 - 2022 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.
*/

public class RedeclaredDependenciesBuilderTest {

@Test
void shouldBuildSectionWhenFinderHasMatches() {
Set<RedeclaredDependency> matches = Set.of(new RedeclaredDependency(
Dependency.builder()
.groupId("test.group")
.artifactId("test-artifact")
.version("2.0.0")
.build(),
"1.0.0"
));

ProjectContext context = mock(ProjectContext.class);
RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class);
when(finder.findMatches(context)).thenReturn(matches);
RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder);
assertThat(builder.isApplicable(context)).isTrue();
}

@Test
void shouldBuildSectionWhenFinderHasNoMatches() {
Set<RedeclaredDependency> matches = Set.of();
ProjectContext context = mock(ProjectContext.class);
RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class);
when(finder.findMatches(context)).thenReturn(matches);
RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder);
assertThat(builder.isApplicable(context)).isFalse();
}

@Test
void assertContent() {

Set<RedeclaredDependency> matches = Set.of(new RedeclaredDependency(
Dependency.builder()
.groupId("test.group")
.artifactId("test-artifact")
.version("2.0.0")
.build(),
"1.0.0"
),
new RedeclaredDependency(
Dependency.builder()
.groupId("test.group")
.artifactId("test-artifact2")
.version("3.0.0")
.build(),
"2.0.0"
));

ProjectContext context = mock(ProjectContext.class);
RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class);
when(finder.findMatches(context)).thenReturn(matches);
RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder);

Section section = builder.build(context);

assertThat(section).isInstanceOf(ChangeSection.class);
ChangeSection relevantChangeSection = ChangeSection.class.cast(section);
assertThat(relevantChangeSection.getTitle()).isEqualTo("Remove redundant explicit version declaration");
assertThat(relevantChangeSection.getRelevanceSection()).isNotNull();
assertThat(relevantChangeSection.getRelevanceSection().getParagraphs()).hasSize(1);
assertThat(relevantChangeSection.getRelevanceSection().getParagraphs().get(0).getText()).isEqualTo("""
The scan found one or more redeclared dependencies in build files.
Please check them and remove redundant declarations.
""");
List<TodoList.Todo> todos = relevantChangeSection.getTodoSection().getTodoLists().get(0).getTodos();
List<String> stringToDos = todos.stream()
.map(TodoList.Todo::getText)
.collect(Collectors.toList());
assertThat(stringToDos).contains("Remove explicit declaration of version for artifact: test.group:test-artifact:2.0.0, its already declared with version 1.0.0");
assertThat(stringToDos).contains("Remove explicit declaration of version for artifact: test.group:test-artifact2:3.0.0, its already declared with version 2.0.0");
}
}
Loading