Skip to content

Commit d40800d

Browse files
authored
Merge pull request #31747 from holly-cummins/read-scm-from-pom
Read scm-url information from pom, where environment information is missing
2 parents e514edc + 0875cb1 commit d40800d

File tree

11 files changed

+175
-23
lines changed

11 files changed

+175
-23
lines changed

devtools/gradle/gradle-extension-plugin/src/main/java/io/quarkus/extension/gradle/tasks/ExtensionDescriptorTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private void computeArtifactCoords(ObjectNode extObject) {
311311
}
312312

313313
private void computeSourceLocation(ObjectNode extObject) {
314-
Map<String, String> repo = ScmInfoProvider.getSourceRepo();
314+
Map<String, String> repo = ScmInfoProvider.getSourceRepo(null);
315315
if (repo != null) {
316316
ObjectNode metadata = getMetadataNode(extObject);
317317

independent-projects/extension-maven-plugin/pom.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@
128128
<!-- set tmpdir as early as possible because failsafe sets it too late for JDK16 -->
129129
<argLine>-Djava.io.tmpdir="${project.build.directory}"</argLine>
130130
<excludedEnvironmentVariables>MAVEN_OPTS</excludedEnvironmentVariables>
131-
<environmentVariables>
132-
<GITHUB_REPOSITORY>some/repo</GITHUB_REPOSITORY>
133-
</environmentVariables>
134131
</configuration>
135132
</plugin>
136133
<plugin>
@@ -300,6 +297,19 @@
300297
<version>3.8.7</version>
301298
<scope>test</scope>
302299
</dependency>
300+
<!-- Needed for system stubs -->
301+
<dependency>
302+
<groupId>org.mockito</groupId>
303+
<artifactId>mockito-core</artifactId>
304+
<version>5.1.1</version>
305+
<scope>test</scope>
306+
</dependency>
307+
<dependency>
308+
<groupId>uk.org.webcompere</groupId>
309+
<artifactId>system-stubs-jupiter</artifactId>
310+
<version>2.0.1</version>
311+
<scope>test</scope>
312+
</dependency>
303313
<dependency>
304314
<groupId>org.apache.maven.shared</groupId>
305315
<artifactId>maven-invoker</artifactId>

independent-projects/extension-maven-plugin/src/main/java/io/quarkus/maven/BootstrapWorkspaceProvider.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,30 @@ public class BootstrapWorkspaceProvider {
1717
private final Path base;
1818
private boolean initialized;
1919
private LocalProject origin;
20+
private final boolean honourAlternatePomFile;
2021

2122
public BootstrapWorkspaceProvider() {
23+
this("", true);
24+
}
25+
26+
// Mostly for use by tests
27+
BootstrapWorkspaceProvider(String dirName) {
28+
// If we get passed in a directory explicitly, don't try and tack the "-f" value onto it
29+
this(dirName, false);
30+
}
31+
32+
private BootstrapWorkspaceProvider(String dirName, boolean honourAlternatePomFile) {
33+
this.honourAlternatePomFile = honourAlternatePomFile;
2234
// load the workspace lazily on request, in case the component is injected but the logic using it is skipped
23-
base = Paths.get("").normalize().toAbsolutePath();
35+
base = Paths.get(dirName).normalize().toAbsolutePath();
2436
}
2537

2638
public LocalProject origin() {
2739
if (!initialized) {
2840
Path modulePath = base;
2941
final String alternatePomParam = BootstrapMavenOptions.newInstance()
3042
.getOptionValue(BootstrapMavenOptions.ALTERNATE_POM_FILE);
31-
if (alternatePomParam != null) {
43+
if (alternatePomParam != null && honourAlternatePomFile) {
3244
final Path path = Paths.get(alternatePomParam);
3345
if (path.isAbsolute()) {
3446
modulePath = path;

independent-projects/extension-maven-plugin/src/main/java/io/quarkus/maven/ExtensionDescriptorMojo.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.maven.artifact.Artifact;
2323
import org.apache.maven.execution.MavenSession;
24+
import org.apache.maven.model.Scm;
2425
import org.apache.maven.plugin.AbstractMojo;
2526
import org.apache.maven.plugin.MojoExecutionException;
2627
import org.apache.maven.plugin.logging.Log;
@@ -678,17 +679,47 @@ public boolean visitLeave(DependencyNode node) {
678679
}
679680

680681
private void addSource(ObjectNode extObject) throws MojoExecutionException {
681-
Map<String, String> repo = ScmInfoProvider.getSourceRepo();
682+
Scm scm = getScm();
683+
String scmUrl = scm != null ? scm.getUrl() : null;
684+
685+
Map<String, String> repo = new ScmInfoProvider().getSourceRepo(scmUrl);
682686
if (repo != null) {
683687
ObjectNode metadata = getMetadataNode(extObject);
684688
for (Map.Entry<String, String> e : repo.entrySet()) {
685-
// Tools may not be able to handle nesting in metadata, so do fake-nesting
686-
metadata.put("scm-" + e.getKey(), e.getValue());
689+
// Ignore if already set
690+
String value = e.getValue();
691+
String fieldName = "scm-" + e.getKey();
692+
if (!metadata.has(fieldName) && value != null) {
693+
// Tools may not be able to handle nesting in metadata, so do fake-nesting
694+
metadata.put(fieldName, value);
695+
}
687696

688697
}
689698
}
690699
}
691700

701+
private Scm getScm() {
702+
// We have three ways to do this; project.getScm() will query the derived model. Sadly, inherited <scm> entries are usually wrong, unless the parent is in the same project
703+
// We can use getOriginalModel and getParent to walk the tree, but this will miss parents in poms outside the current execution, which might include a local reactor that we'd actually want to query
704+
// Or we can use the bootstrap provider
705+
Scm scm = null;
706+
final Artifact artifact = project.getArtifact();
707+
LocalProject localProject = workspaceProvider.getProject(artifact.getGroupId(), artifact.getArtifactId());
708+
709+
if (localProject == null) {
710+
final Log log = getLog();
711+
log.warn("Workspace provider could not resolve local project for " + artifact.getGroupId() + ":"
712+
+ artifact.getArtifactId());
713+
}
714+
715+
while (scm == null && localProject != null) {
716+
scm = localProject.getRawModel().getScm();
717+
localProject = localProject.getLocalParent();
718+
}
719+
return scm;
720+
721+
}
722+
692723
public void addJavaVersion(ObjectNode extObject) {
693724
ObjectNode metadataNode = getMetadataNode(extObject);
694725
// Ignore if already set

independent-projects/extension-maven-plugin/src/test/java/io/quarkus/maven/ExtensionDescriptorMojoTest.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,29 @@
3333
import org.junit.jupiter.api.Assertions;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.extension.ExtendWith;
3637

3738
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
3839
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
40+
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
41+
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
42+
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
3943

44+
@ExtendWith(SystemStubsExtension.class)
4045
class ExtensionDescriptorMojoTest extends AbstractMojoTestCase {
4146

47+
@SystemStub
48+
private EnvironmentVariables environment;
49+
4250
private static final boolean RESOLVE_OFFLINE = true;
4351
// Test resources end up in target/test-classes after filtering
4452
public static final String TEST_RESOURCES = "target/test-classes/";
4553

4654
@BeforeEach
4755
public void setup() throws Exception {
4856
super.setUp();
57+
// Make sure that we don't have the GITHUB_REPOSITORY environment variable masking what this mojo does
58+
environment.set("GITHUB_REPOSITORY", null);
4959
}
5060

5161
@AfterEach
@@ -82,6 +92,7 @@ public void shouldCreateExtensionProperties()
8292
@Test
8393
public void shouldCreateMetadata()
8494
throws Exception {
95+
8596
ExtensionDescriptorMojo mojo = makeMojo("simple-pom-with-checks-disabled");
8697
File yamlFile = getGeneratedExtensionMetadataFile(mojo.project.getBasedir(),
8798
"target/classes/META-INF/quarkus-extension.yaml");
@@ -97,11 +108,29 @@ public void shouldCreateMetadata()
97108
assertYamlContains(fileContents, "name", "an arbitrary name");
98109
assertYamlContains(fileContents, "artifact", "io.quackiverse:test-artifact::jar:1.4.2-SNAPSHOT");
99110

100-
// From maven this property should be set, running in an IDE it won't be unless specially configured
101-
if (System.getenv("GITHUB_REPOSITORY") != null) {
102-
// Lazily test that the scm is there but is an object
103-
assertYamlContains(fileContents, "scm-url", "https://github.com/some/repo");
111+
assertYamlContains(fileContents, "scm-url", "https://github.com/from/pom");
112+
113+
}
114+
115+
@Test
116+
public void shouldReadLocalParentsForScmInfo()
117+
throws Exception {
118+
119+
ExtensionDescriptorMojo mojo = makeMojo("simple-pom-with-checks-disabled-and-local-parent/child");
120+
File yamlFile = getGeneratedExtensionMetadataFile(mojo.project.getBasedir(),
121+
"target/classes/META-INF/quarkus-extension.yaml");
122+
123+
// Tidy up any artifacts from previous runs
124+
if (yamlFile.exists()) {
125+
Files.delete(yamlFile.toPath());
104126
}
127+
mojo.execute();
128+
assertTrue(yamlFile.exists());
129+
130+
String fileContents = readFileAsString(yamlFile);
131+
assertYamlContains(fileContents, "artifact", "io.quackiverse:test-artifact-child::jar:1.4.2-SNAPSHOT");
132+
133+
assertYamlContains(fileContents, "scm-url", "https://github.com/from/parent");
105134

106135
}
107136

@@ -222,6 +251,7 @@ private ExtensionDescriptorMojo makeMojo(String dirName) throws Exception {
222251
newMojoExecution("extension-descriptor"));
223252
mojo.repoSystem = mvn.getSystem();
224253
mojo.repoSession = mvn.getSession();
254+
mojo.workspaceProvider = new BootstrapWorkspaceProvider(basedir.getAbsolutePath());
225255
return mojo;
226256
}
227257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>io.quackiverse</groupId>
5+
<artifactId>test-artifact-child</artifactId>
6+
<version>1.4.2-SNAPSHOT</version>
7+
<name>an arbitrary name</name>
8+
9+
<parent>
10+
<groupId>io.quackiverse</groupId>
11+
<artifactId>test-artifact-parent</artifactId>
12+
<version>1.4.2-SNAPSHOT</version>
13+
<relativePath>../</relativePath>
14+
</parent>
15+
16+
<properties>
17+
<skipExtensionValidation>true</skipExtensionValidation>
18+
<ignoreNotDetectedQuarkusCoreVersion>true</ignoreNotDetectedQuarkusCoreVersion>
19+
</properties>
20+
21+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>io.quackiverse</groupId>
5+
<artifactId>test-artifact-parent</artifactId>
6+
<version>1.4.2-SNAPSHOT</version>
7+
<name>an arbitrary parent</name>
8+
<packaging>pom</packaging>
9+
10+
11+
<scm>
12+
<url>https://github.com/from/parent</url>
13+
</scm>
14+
15+
<properties>
16+
<skipExtensionValidation>true</skipExtensionValidation>
17+
<ignoreNotDetectedQuarkusCoreVersion>true</ignoreNotDetectedQuarkusCoreVersion>
18+
</properties>
19+
</project>

independent-projects/extension-maven-plugin/src/test/resources/simple-pom-with-checks-disabled/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<ignoreNotDetectedQuarkusCoreVersion>true</ignoreNotDetectedQuarkusCoreVersion>
1212
</properties>
1313

14+
<scm>
15+
<url>https://github.com/from/pom</url>
16+
</scm>
17+
1418
<build>
1519
<plugins>
1620
<plugin>

independent-projects/tools/devtools-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@
116116
<dependency>
117117
<groupId>uk.org.webcompere</groupId>
118118
<artifactId>system-stubs-jupiter</artifactId>
119-
<scope>test</scope>
120119
<version>2.0.1</version>
120+
<scope>test</scope>
121121
</dependency>
122122
</dependencies>
123123
</project>

independent-projects/tools/devtools-common/src/main/java/io/quarkus/devtools/project/extensions/ScmInfoProvider.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
public class ScmInfoProvider {
77

8-
public static Map<String, String> getSourceRepo() {
8+
public static Map<String, String> getSourceRepo(String valueFromBuildFile) {
99
// We could try and parse the .git/config file, but that will be fragile
10-
// Let's assume we only care about the repo for official-ish builds produced via github actions
10+
// We could use JGit, something like https://wiki.eclipse.org/JGit/User_Guide#Repository but it seems a lot for our needs
1111
String repo = System.getenv("GITHUB_REPOSITORY");
12+
Map info = null;
1213
if (repo != null) {
13-
Map info = new HashMap();
14+
info = new HashMap();
1415
String qualifiedRepo = "https://github.com/" + repo;
1516
// Don't try and guess where slashes will be, just deal with any double slashes by brute force
1617
qualifiedRepo = qualifiedRepo.replace("github.com//", "github.com/");
17-
1818
info.put("url", qualifiedRepo);
19-
return info;
19+
20+
} else if (valueFromBuildFile != null) {
21+
info = new HashMap();
22+
info.put("url", valueFromBuildFile);
2023
}
21-
return null;
24+
return info;
2225
}
2326

2427
}

0 commit comments

Comments
 (0)