Skip to content

Commit 8ce5581

Browse files
authored
Only add SCM to root POM in UpdateScmFromGitOrigin (#5812) (#7287)
1 parent 23c1afd commit 8ce5581

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openrewrite.Recipe;
2424
import org.openrewrite.TreeVisitor;
2525
import org.openrewrite.marker.GitProvenance;
26+
import org.openrewrite.maven.tree.MavenResolutionResult;
2627
import org.openrewrite.xml.AddToTagVisitor;
2728
import org.openrewrite.xml.ChangeTagValueVisitor;
2829
import org.openrewrite.xml.tree.Xml;
@@ -48,7 +49,7 @@ public class UpdateScmFromGitOrigin extends Recipe {
4849
String displayName = "Update SCM with Git origin";
4950

5051
String description = "Updates or adds the Maven `<scm>` tag based on the Git remote origin. " +
51-
"By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections.";
52+
"By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections to root POMs (POMs without a parent element).";
5253

5354
@Override
5455
public TreeVisitor<?, ExecutionContext> getVisitor() {
@@ -69,6 +70,15 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
6970

7071
if(!document.getRoot().getChild("scm").isPresent()) {
7172
if (Boolean.TRUE.equals(addIfMissing)) {
73+
// Only add SCM to root POMs (those without a parent)
74+
boolean hasParent = document.getMarkers()
75+
.findFirst(MavenResolutionResult.class)
76+
.map(mrr -> mrr.getParent() != null)
77+
.orElse(false);
78+
if (hasParent) {
79+
return document;
80+
}
81+
7282
// Build the SCM tag with all required elements
7383
String httpUrl = "https://" + gitOrigin.getHost() + "/" + gitOrigin.getPath();
7484
String gitPath = gitOrigin.getPath().endsWith(".git") ?

rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.junit.jupiter.api.Nested;
1919
import org.junit.jupiter.api.Test;
2020
import org.openrewrite.DocumentExample;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.marker.GitProvenance;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
2425

2526
import java.util.UUID;
2627

28+
import static org.openrewrite.java.Assertions.mavenProject;
2729
import static org.openrewrite.maven.Assertions.pomXml;
2830

2931
class UpdateScmFromGitOriginTest implements RewriteTest {
@@ -460,6 +462,62 @@ void stillUpdatesExistingScmWhenAddIfMissingIsTrue() {
460462
)
461463
);
462464
}
465+
466+
@Issue("https://github.com/openrewrite/rewrite/issues/5812")
467+
@Test
468+
void addsScmToRootPomOnly() {
469+
rewriteRun(
470+
spec -> spec.recipe(new UpdateScmFromGitOrigin(true)),
471+
mavenProject("parent",
472+
pomXml(
473+
"""
474+
<project>
475+
<modelVersion>4.0.0</modelVersion>
476+
<groupId>com.mycompany.app</groupId>
477+
<artifactId>my-app-parent</artifactId>
478+
<version>1</version>
479+
<packaging>pom</packaging>
480+
<modules>
481+
<module>module1</module>
482+
</modules>
483+
</project>
484+
""",
485+
"""
486+
<project>
487+
<modelVersion>4.0.0</modelVersion>
488+
<groupId>com.mycompany.app</groupId>
489+
<artifactId>my-app-parent</artifactId>
490+
<version>1</version>
491+
<packaging>pom</packaging>
492+
<modules>
493+
<module>module1</module>
494+
</modules>
495+
<scm>
496+
<url>https://github.com/example/repo</url>
497+
<connection>scm:git:https://github.com/example/repo.git</connection>
498+
<developerConnection>scm:git:git@github.com:example/repo.git</developerConnection>
499+
</scm>
500+
</project>
501+
""",
502+
spec -> spec.markers(gitProvenance("https://github.com/example/repo.git"))
503+
),
504+
mavenProject("module1",
505+
pomXml(
506+
"""
507+
<project>
508+
<modelVersion>4.0.0</modelVersion>
509+
<parent>
510+
<groupId>com.mycompany.app</groupId>
511+
<artifactId>my-app-parent</artifactId>
512+
<version>1</version>
513+
</parent>
514+
<artifactId>module1</artifactId>
515+
</project>
516+
"""
517+
))
518+
)
519+
);
520+
}
463521
}
464522

465523
@Nested

0 commit comments

Comments
 (0)