Skip to content
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

[JENKINS-58716] -DgitHubRepo when possible to infer #16

Merged
merged 1 commit into from Aug 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
jglick marked this conversation as resolved.
Show resolved Hide resolved
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
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