-
Notifications
You must be signed in to change notification settings - Fork 1.1k
JENKINS-56284 Make compute changelog extensible #813
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
Open
gmshake
wants to merge
2
commits into
jenkinsci:master
Choose a base branch
from
gmshake:feature/JENKINS-56284
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, this is something I don't understand. Why extending
FakeGitSCMExtension
instead ofGitSCMExtension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure which one is better. If I remember correctly, extending GitSCMExtension is also OK.