Skip to content

Commit 83ae0d9

Browse files
authored
Merge pull request #16 from jglick/gitHubRepo-JENKINS-58716
[JENKINS-58716] -DgitHubRepo when possible to infer
2 parents f6fb2fd + 31c10e2 commit 83ae0d9

File tree

2 files changed

+63
-0
lines changed
  • git-changelist-maven-extension/src/main/java/io/jenkins/tools/incrementals/git_changelist_maven_extension
  • maven-plugin/src/main/java/io/jenkins/tools/incrementals/maven

2 files changed

+63
-0
lines changed

git-changelist-maven-extension/src/main/java/io/jenkins/tools/incrementals/git_changelist_maven_extension/Main.java

+30
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,36 @@ public void afterSessionStart(MavenSession session) throws MavenExecutionExcepti
137137
} else {
138138
log.info("Declining to override the `changelist` or `scmTag` properties");
139139
}
140+
if (!props.contains("gitHubRepo")) {
141+
String gitHubRepo;
142+
String changeFork = System.getenv("CHANGE_FORK");
143+
if (changeFork == null) {
144+
log.info("No information available to set -DgitHubRepo");
145+
gitHubRepo = null;
146+
} else if (changeFork.contains("/")) {
147+
gitHubRepo = changeFork;
148+
} else {
149+
String jobName = System.getenv("JOB_NAME");
150+
if (jobName == null) {
151+
log.info("CHANGE_FORK set but incomplete with JOB_NAME");
152+
gitHubRepo = null;
153+
} else {
154+
String[] pieces = jobName.split("/");
155+
if (pieces.length >= 2) { // e.g. Plugins/build-token-root-plugin/PR-21
156+
gitHubRepo = changeFork + "/" + pieces[pieces.length - 2]; // e.g. jglick/build-token-root-plugin
157+
} else {
158+
log.info("CHANGE_FORK set but incomplete and JOB_NAME also incomplete");
159+
gitHubRepo = null;
160+
}
161+
}
162+
}
163+
if (gitHubRepo != null) {
164+
log.info("Setting: -DgitHubRepo=" + gitHubRepo);
165+
props.setProperty("gitHubRepo", gitHubRepo);
166+
}
167+
} else {
168+
log.info("Declining to override the `gitHubRepo` property");
169+
}
140170
} else {
141171
log.debug("Skipping Git version setting unless run with -Dset.changelist");
142172
}

maven-plugin/src/main/java/io/jenkins/tools/incrementals/maven/IncrementalifyMojo.java

+33
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ public class IncrementalifyMojo extends AbstractVersionsUpdaterMojo {
122122
if (!PARENT_DEPENDENCIES.contains(parent.getDependencyConflictId())) {
123123
throw new MojoFailureException("Unexpected <parent> " + parent);
124124
}
125+
String connection = project.getScm().getConnection();
126+
String developerConnection = project.getScm().getDeveloperConnection();
127+
String url = project.getScm().getUrl();
128+
if (connection == null || developerConnection == null || url == null) {
129+
throw new MojoFailureException("<scm> must contain all of connection, developerConnection, and url");
130+
}
131+
ReplaceGitHubRepo connectionRGHR = replaceGitHubRepo(connection);
132+
ReplaceGitHubRepo developerConnectionRGHR = replaceGitHubRepo(developerConnection);
133+
ReplaceGitHubRepo urlRGHR = replaceGitHubRepo(url);
134+
if (!developerConnectionRGHR.gitHubRepo.equals(connectionRGHR.gitHubRepo) || !urlRGHR.gitHubRepo.equals(connectionRGHR.gitHubRepo)) {
135+
throw new MojoFailureException("Mismatch among gitHubRepo parts of <scm>: " + connectionRGHR.gitHubRepo + " vs. " + developerConnectionRGHR.gitHubRepo + " vs. " + urlRGHR.gitHubRepo);
136+
}
125137
String minimum_parent;
126138
if (parent.getDependencyConflictId().equals(JENKINS_POM)) {
127139
minimum_parent = MINIMUM_JENKINS_PARENT;
@@ -131,9 +143,30 @@ public class IncrementalifyMojo extends AbstractVersionsUpdaterMojo {
131143
if (new ComparableVersion(parent.getVersion()).compareTo(new ComparableVersion(minimum_parent)) < 0) {
132144
PomHelper.setProjectParentVersion(pom, minimum_parent);
133145
}
146+
prependProperty(pom, "gitHubRepo", connectionRGHR.gitHubRepo);
134147
prependProperty(pom, "changelist", "-SNAPSHOT");
135148
prependProperty(pom, "revision", m.group(1));
136149
PomHelper.setProjectValue(pom, "/project/scm/tag", "${scmTag}");
150+
PomHelper.setProjectValue(pom, "/project/scm/connection", connectionRGHR.interpolableText);
151+
PomHelper.setProjectValue(pom, "/project/scm/developerConnection", developerConnectionRGHR.interpolableText);
152+
PomHelper.setProjectValue(pom, "/project/scm/url", urlRGHR.interpolableText);
153+
}
154+
155+
private static final class ReplaceGitHubRepo {
156+
final String interpolableText;
157+
final String gitHubRepo;
158+
ReplaceGitHubRepo(String interpolableText, String gitHubRepo) {
159+
this.interpolableText = interpolableText;
160+
this.gitHubRepo = gitHubRepo;
161+
}
162+
}
163+
private static final Pattern TEXT = Pattern.compile("(.+[:/])((?:[^/]+)/(?:[^/]+?))((?:[.]git)?)");
164+
static ReplaceGitHubRepo replaceGitHubRepo(String text) throws MojoFailureException {
165+
Matcher m = TEXT.matcher(text);
166+
if (!m.matches()) {
167+
throw new MojoFailureException(text + " did not match " + TEXT);
168+
}
169+
return new ReplaceGitHubRepo(m.group(1) + "${gitHubRepo}" + m.group(3), m.group(2));
137170
}
138171

139172
private void prependProperty(ModifiedPomXMLEventReader pom, String name, String value) throws XMLStreamException, MojoFailureException {

0 commit comments

Comments
 (0)