diff --git a/pom.xml b/pom.xml index 07157e7bd..15d4a3d9e 100644 --- a/pom.xml +++ b/pom.xml @@ -133,6 +133,11 @@ commons-lang3 3.9 + + org.jenkins-ci.plugins + git + 4.3.0-SNAPSHOT + diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java index a6028afc3..7f40b9393 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java @@ -34,9 +34,16 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.model.Item; import hudson.model.Queue; +import hudson.model.Run; +import hudson.model.TaskListener; +import hudson.plugins.git.GitException; import hudson.plugins.git.GitSCM; +import hudson.plugins.git.Revision; import hudson.plugins.git.browser.GithubWeb; +import hudson.plugins.git.extensions.GitSCMChangelogExtension; import hudson.security.ACL; + +import java.io.IOException; import java.net.URL; import java.util.HashSet; import java.util.Random; @@ -51,6 +58,8 @@ import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.transport.RefSpec; +import org.jenkinsci.plugins.gitclient.ChangelogCommand; +import org.jenkinsci.plugins.gitclient.GitClient; import org.jenkinsci.plugins.github.config.GitHubServerConfig; /** @@ -339,6 +348,7 @@ public GitSCM build() { if (r instanceof PullRequestSCMRevision) { PullRequestSCMRevision rev = (PullRequestSCMRevision) r; withRevision(new AbstractGitSCMSource.SCMRevisionImpl(head, rev.getPullHash())); + withExtension(new GitSCMPullRequestChangelogExtension(rev.getBaseHash())); } } return super.build(); @@ -347,4 +357,22 @@ public GitSCM build() { withRevision(r); } } + + /** + * Compute changelog of pull request. + */ + static final class GitSCMPullRequestChangelogExtension extends GitSCMChangelogExtension { + final String targetHash; + + public GitSCMPullRequestChangelogExtension(String targetHash) { + this.targetHash = targetHash; + } + + public boolean decorateChangelogCommand(GitSCM gitSCM, Run run, GitClient git, TaskListener taskListener, ChangelogCommand cmd, Revision revToBuild) throws IOException, InterruptedException, GitException { + taskListener.getLogger().println("Using 'Changelog for PullRequest' strategy."); + cmd.includes(revToBuild.getSha1()); + cmd.excludes(targetHash); + return true; + } + } }