-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-56284 Make compute changelog extensible
- Loading branch information
Showing
4 changed files
with
194 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/hudson/plugins/git/extensions/GitSCMChangelogExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hudson.plugins.git.extensions; | ||
|
||
import java.io.IOException; | ||
|
||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.git.GitException; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.plugins.git.Revision; | ||
import org.jenkinsci.plugins.gitclient.ChangelogCommand; | ||
import org.jenkinsci.plugins.gitclient.GitClient; | ||
|
||
/** | ||
* FIXME JavaDoc | ||
* @author Zhenlei Huang | ||
*/ | ||
public abstract class GitSCMChangelogExtension extends FakeGitSCMExtension { | ||
|
||
/** | ||
* Called before a {@link ChangelogCommand} is executed to allow extensions to alter its behaviour. | ||
* @param scm GitSCM object | ||
* @param build run context | ||
* @param git GitClient | ||
* @param listener build log | ||
* @param cmd changelog command to be decorated | ||
* @param revToBuild The revision selected for this build | ||
* @return true in case decorated, false otherwise | ||
* @throws IOException on input or output error | ||
* @throws InterruptedException when interrupted | ||
* @throws GitException on git error | ||
*/ | ||
public abstract boolean decorateChangelogCommand(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, ChangelogCommand cmd, Revision revToBuild) throws IOException, InterruptedException, GitException; | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/jenkins/plugins/git/ChangelogToPreviousBuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package jenkins.plugins.git; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.git.Branch; | ||
import hudson.plugins.git.GitException; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.plugins.git.Revision; | ||
import hudson.plugins.git.extensions.GitSCMChangelogExtension; | ||
import hudson.plugins.git.util.Build; | ||
import hudson.plugins.git.util.BuildChooserContext; | ||
import hudson.remoting.Channel; | ||
import org.jenkinsci.plugins.gitclient.ChangelogCommand; | ||
import org.jenkinsci.plugins.gitclient.GitClient; | ||
|
||
/** | ||
* FIXME JavaDoc | ||
* @author Zhenlei Huang | ||
*/ | ||
public class ChangelogToPreviousBuild extends GitSCMChangelogExtension { | ||
|
||
@Override | ||
public boolean decorateChangelogCommand(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, ChangelogCommand cmd, Revision revToBuild) throws IOException, InterruptedException, GitException { | ||
boolean decorated = false; | ||
|
||
for (Branch b : revToBuild.getBranches()) { | ||
Build lastRevWas = scm.getBuildChooser().prevBuildForChangelog(b.getName(), scm.getBuildData(build.getPreviousBuild()), git, new BuildChooserContextImpl(build.getParent(), build, build.getEnvironment(listener))); | ||
if (lastRevWas != null && lastRevWas.revision != null && git.isCommitInRepo(lastRevWas.getSHA1())) { | ||
cmd.includes(revToBuild.getSha1()); | ||
cmd.excludes(lastRevWas.getSHA1()); | ||
decorated = true; | ||
} | ||
} | ||
|
||
return decorated; | ||
} | ||
|
||
// Copy of GitSCM.BuildChooserContextImpl | ||
/*package*/ static class BuildChooserContextImpl implements BuildChooserContext, Serializable { | ||
@SuppressFBWarnings(value="SE_BAD_FIELD", justification="known non-serializable field") | ||
final Job project; | ||
@SuppressFBWarnings(value="SE_BAD_FIELD", justification="known non-serializable field") | ||
final Run build; | ||
final EnvVars environment; | ||
|
||
BuildChooserContextImpl(Job project, Run build, EnvVars environment) { | ||
this.project = project; | ||
this.build = build; | ||
this.environment = environment; | ||
} | ||
|
||
public <T> T actOnBuild(ContextCallable<Run<?,?>, T> callable) throws IOException, InterruptedException { | ||
return callable.invoke(build, FilePath.localChannel); | ||
} | ||
|
||
public <T> T actOnProject(ContextCallable<Job<?,?>, T> callable) throws IOException, InterruptedException { | ||
return callable.invoke(project, FilePath.localChannel); | ||
} | ||
|
||
public Run<?, ?> getBuild() { | ||
return build; | ||
} | ||
|
||
public EnvVars getEnvironment() { | ||
return environment; | ||
} | ||
|
||
private Object writeReplace() { | ||
return Channel.current().export(BuildChooserContext.class,new BuildChooserContext() { | ||
public <T> T actOnBuild(ContextCallable<Run<?,?>, T> callable) throws IOException, InterruptedException { | ||
return callable.invoke(build,Channel.current()); | ||
} | ||
|
||
public <T> T actOnProject(ContextCallable<Job<?,?>, T> callable) throws IOException, InterruptedException { | ||
return callable.invoke(project,Channel.current()); | ||
} | ||
|
||
public Run<?, ?> getBuild() { | ||
return build; | ||
} | ||
|
||
public EnvVars getEnvironment() { | ||
return environment; | ||
} | ||
}); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/jenkins/plugins/git/LegacyGitSCMChangelogExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jenkins.plugins.git; | ||
|
||
import java.io.IOException; | ||
|
||
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.extensions.GitSCMChangelogExtension; | ||
import hudson.plugins.git.extensions.impl.ChangelogToBranch; | ||
import org.jenkinsci.plugins.gitclient.ChangelogCommand; | ||
import org.jenkinsci.plugins.gitclient.GitClient; | ||
|
||
|
||
/** | ||
* FIXME JavaDoc | ||
* Legacy changelog impl. | ||
*/ | ||
public class LegacyGitSCMChangelogExtension extends GitSCMChangelogExtension { | ||
|
||
|
||
@Override | ||
public boolean decorateChangelogCommand(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, ChangelogCommand cmd, Revision revToBuild) throws IOException, InterruptedException, GitException { | ||
boolean exclusion = false; | ||
|
||
// TODO Refactor ChangelogToBranch | ||
ChangelogToBranch changelogToBranch = scm.getExtensions().get(ChangelogToBranch.class); | ||
if (changelogToBranch != null) { | ||
listener.getLogger().println("Using 'Changelog to branch' strategy."); | ||
cmd.includes(revToBuild.getSha1()); | ||
cmd.excludes(changelogToBranch.getOptions().getRef()); | ||
exclusion = true; | ||
} else { | ||
exclusion = new ChangelogToPreviousBuild().decorateChangelogCommand(scm, build, git, listener, cmd, revToBuild); | ||
} | ||
|
||
if (!exclusion) { | ||
// this is the first time we are building this branch, so there's no base line to compare against. | ||
// if we force the changelog, it'll contain all the changes in the repo, which is not what we want. | ||
listener.getLogger().println("First time build. Skipping changelog."); | ||
} | ||
|
||
return exclusion; | ||
} | ||
} |