Skip to content

Commit

Permalink
Merge pull request #16 from jglick/gitHubRepo-JENKINS-58716
Browse files Browse the repository at this point in the history
[JENKINS-58716] -DgitHubRepo when possible to infer
  • Loading branch information
jglick authored Aug 24, 2020
2 parents f6fb2fd + 31c10e2 commit 83ae0d9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@ public void afterSessionStart(MavenSession session) throws MavenExecutionExcepti
} else {
log.info("Declining to override the `changelist` or `scmTag` properties");
}
if (!props.contains("gitHubRepo")) {
String gitHubRepo;
String changeFork = System.getenv("CHANGE_FORK");
if (changeFork == null) {
log.info("No information available to set -DgitHubRepo");
gitHubRepo = null;
} else if (changeFork.contains("/")) {
gitHubRepo = changeFork;
} else {
String jobName = System.getenv("JOB_NAME");
if (jobName == null) {
log.info("CHANGE_FORK set but incomplete with JOB_NAME");
gitHubRepo = null;
} else {
String[] pieces = jobName.split("/");
if (pieces.length >= 2) { // e.g. Plugins/build-token-root-plugin/PR-21
gitHubRepo = changeFork + "/" + pieces[pieces.length - 2]; // e.g. jglick/build-token-root-plugin
} else {
log.info("CHANGE_FORK set but incomplete and JOB_NAME also incomplete");
gitHubRepo = null;
}
}
}
if (gitHubRepo != null) {
log.info("Setting: -DgitHubRepo=" + gitHubRepo);
props.setProperty("gitHubRepo", gitHubRepo);
}
} else {
log.info("Declining to override the `gitHubRepo` property");
}
} else {
log.debug("Skipping Git version setting unless run with -Dset.changelist");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public class IncrementalifyMojo extends AbstractVersionsUpdaterMojo {
if (!PARENT_DEPENDENCIES.contains(parent.getDependencyConflictId())) {
throw new MojoFailureException("Unexpected <parent> " + parent);
}
String connection = project.getScm().getConnection();
String developerConnection = project.getScm().getDeveloperConnection();
String url = project.getScm().getUrl();
if (connection == null || developerConnection == null || url == null) {
throw new MojoFailureException("<scm> must contain all of connection, developerConnection, and url");
}
ReplaceGitHubRepo connectionRGHR = replaceGitHubRepo(connection);
ReplaceGitHubRepo developerConnectionRGHR = replaceGitHubRepo(developerConnection);
ReplaceGitHubRepo urlRGHR = replaceGitHubRepo(url);
if (!developerConnectionRGHR.gitHubRepo.equals(connectionRGHR.gitHubRepo) || !urlRGHR.gitHubRepo.equals(connectionRGHR.gitHubRepo)) {
throw new MojoFailureException("Mismatch among gitHubRepo parts of <scm>: " + connectionRGHR.gitHubRepo + " vs. " + developerConnectionRGHR.gitHubRepo + " vs. " + urlRGHR.gitHubRepo);
}
String minimum_parent;
if (parent.getDependencyConflictId().equals(JENKINS_POM)) {
minimum_parent = MINIMUM_JENKINS_PARENT;
Expand All @@ -131,9 +143,30 @@ public class IncrementalifyMojo extends AbstractVersionsUpdaterMojo {
if (new ComparableVersion(parent.getVersion()).compareTo(new ComparableVersion(minimum_parent)) < 0) {
PomHelper.setProjectParentVersion(pom, minimum_parent);
}
prependProperty(pom, "gitHubRepo", connectionRGHR.gitHubRepo);
prependProperty(pom, "changelist", "-SNAPSHOT");
prependProperty(pom, "revision", m.group(1));
PomHelper.setProjectValue(pom, "/project/scm/tag", "${scmTag}");
PomHelper.setProjectValue(pom, "/project/scm/connection", connectionRGHR.interpolableText);
PomHelper.setProjectValue(pom, "/project/scm/developerConnection", developerConnectionRGHR.interpolableText);
PomHelper.setProjectValue(pom, "/project/scm/url", urlRGHR.interpolableText);
}

private static final class ReplaceGitHubRepo {
final String interpolableText;
final String gitHubRepo;
ReplaceGitHubRepo(String interpolableText, String gitHubRepo) {
this.interpolableText = interpolableText;
this.gitHubRepo = gitHubRepo;
}
}
private static final Pattern TEXT = Pattern.compile("(.+[:/])((?:[^/]+)/(?:[^/]+?))((?:[.]git)?)");
static ReplaceGitHubRepo replaceGitHubRepo(String text) throws MojoFailureException {
Matcher m = TEXT.matcher(text);
if (!m.matches()) {
throw new MojoFailureException(text + " did not match " + TEXT);
}
return new ReplaceGitHubRepo(m.group(1) + "${gitHubRepo}" + m.group(3), m.group(2));
}

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

0 comments on commit 83ae0d9

Please sign in to comment.