-
Notifications
You must be signed in to change notification settings - Fork 398
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
[FIXED JENKINS-41811] Expose event origin to listeners #164
Changes from 4 commits
ac334ce
7adda3b
366b786
3515775
cddb1aa
45ade81
e2db2f8
a0567d6
622467e
148bf7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,13 @@ | |
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>git</artifactId> | ||
<version>2.4.0</version> | ||
<version>2.4.0</version> <!-- should really be 2.6.4 to match scm-api but ok as git wass forward compat --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>scm-api</artifactId> | ||
<version>2.0.3</version> | ||
</dependency> | ||
|
||
<dependency> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import hudson.util.StreamTaskListener; | ||
import jenkins.model.Jenkins; | ||
import jenkins.model.ParameterizedJobMixIn; | ||
import jenkins.scm.api.SCMEvent; | ||
import jenkins.triggers.SCMTriggerItem.SCMTriggerItems; | ||
import org.apache.commons.jelly.XMLOutput; | ||
import org.jenkinsci.plugins.github.GitHubPlugin; | ||
|
@@ -31,6 +32,7 @@ | |
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.stapler.AncestorInPath; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.Stapler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -57,7 +59,7 @@ | |
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class GitHubPushTrigger extends Trigger<Job<?, ?>> implements GitHubTrigger { | ||
public class GitHubPushTrigger extends Trigger<Job<?, ?>> implements GitHubTrigger2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we just drop this Interface implementation? Do you know somebody who use it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't know who is using it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can just continue to implement old interface (marked as deprecated) and do nothing with its methods and just use brand new method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or switch to java8? |
||
|
||
@DataBoundConstructor | ||
public GitHubPushTrigger() { | ||
|
@@ -75,6 +77,13 @@ public void onPost() { | |
* Called when a POST is made. | ||
*/ | ||
public void onPost(String triggeredByUser) { | ||
onPost(SCMEvent.originOf(Stapler.getCurrentRequest()), triggeredByUser); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So why not call |
||
} | ||
|
||
/** | ||
* Called when a POST is made. | ||
*/ | ||
public void onPost(final String origin, String triggeredByUser) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S maybe replace it with more common object with fields that will be expandable in future? |
||
final String pushBy = triggeredByUser; | ||
DescriptorImpl d = getDescriptor(); | ||
d.checkThreadPoolSizeAndUpdateIfNecessary(); | ||
|
@@ -87,6 +96,9 @@ private boolean runPolling() { | |
PrintStream logger = listener.getLogger(); | ||
long start = System.currentTimeMillis(); | ||
logger.println("Started on " + DateFormat.getDateTimeInstance().format(new Date())); | ||
if (origin != null) { | ||
logger.println("Started by event from " + origin); | ||
} | ||
boolean result = SCMTriggerItems.asSCMTriggerItem(job).poll(listener).hasChanges(); | ||
logger.println("Done. Took " + Util.getTimeSpanString(System.currentTimeMillis() - start)); | ||
if (result) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
* and triggers a build. | ||
* | ||
* @author aaronwalker | ||
* @deprecated extend {@link GitHubTrigger2} instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Careful—does this produce deprecation warnings for implementers of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who has implemented it? it is doubtful the interface is even required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think @jglick mean compiler warnings, but i guess they work only on annotations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this interface is required for https://github.com/jenkinsci/github-sqs-plugin/blob/master/src/main/java/com/base2services/jenkins/SqsBuildTrigger.java or if it is a gratuitous linkage between the two plugins. It is the only OSS consumer of this interface... the only reason for it to exist is so that the two trigger options will both work for triggering builds... which seems a really crappy UX i.e. is it obvious to the user that checking one or both of the above check boxes will do exactly the same thing? And those check boxes are kind of against the idiomatic Jenkins way for configuring hooks, i.e. through Poll SCM I really think all this needs a good tidy up... but I am not going to take that on in this PR ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (FTR the "Build when a message is published to an SQS Queue" sounds more that a bit scary... what kind of message and what queue? Does it mean that every time any message is pushed to any SQS Queue anywhere in the world that my build will be triggered?) |
||
*/ | ||
public interface GitHubTrigger { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cloudbees.jenkins; | ||
|
||
import hudson.triggers.Trigger; | ||
|
||
/** | ||
* Optional interface that can be implemented by {@link Trigger} that watches out for a change in GitHub | ||
* and triggers a build. | ||
* | ||
* @author aaronwalker | ||
*/ | ||
public interface GitHubTrigger2 extends GitHubTrigger { | ||
|
||
// TODO: document me | ||
void onPost(String origin, String triggeredByUser); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,18 @@ | |
import hudson.model.UnprotectedRootAction; | ||
import hudson.util.SequentialExecutionQueue; | ||
import jenkins.model.Jenkins; | ||
import jenkins.scm.api.SCMEvent; | ||
import org.apache.commons.lang3.Validate; | ||
import org.jenkinsci.plugins.github.GitHubPlugin; | ||
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber; | ||
import org.jenkinsci.plugins.github.internal.GHPluginConfigException; | ||
import org.jenkinsci.plugins.github.webhook.GHEventHeader; | ||
import org.jenkinsci.plugins.github.webhook.GHEventPayload; | ||
import org.jenkinsci.plugins.github.webhook.RequirePostWithGHHookPayload; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.github.GHEvent; | ||
import org.kohsuke.stapler.Stapler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -114,7 +118,7 @@ public List<Item> reRegisterAllHooks() { | |
public void doIndex(@Nonnull @GHEventHeader GHEvent event, @Nonnull @GHEventPayload String payload) { | ||
from(GHEventsSubscriber.all()) | ||
.filter(isInterestedIn(event)) | ||
.transform(processEvent(event, payload)).toList(); | ||
.transform(processEvent(SCMEvent.originOf(Stapler.getCurrentRequest()), event, payload)).toList(); | ||
} | ||
|
||
private <T extends Item> Function<T, T> reRegisterHookForJob() { | ||
|
@@ -153,7 +157,11 @@ public static Jenkins getJenkinsInstance() throws IllegalStateException { | |
* Other plugins may be interested in listening for these updates. | ||
* | ||
* @since 1.8 | ||
* @deprecated working theory is that this API is not required any more with the {@link SCMEvent} based API, | ||
* if wrong, please raise a JIRA | ||
*/ | ||
@Deprecated | ||
@Restricted(NoExternalUse.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can see the only consumer of this API is the 1.x GitHub Branch Source. If there are other use cases for consumers remaining post SCM API 2.0.x updates then this would need to be augmented to pass through the origin. I'd much rather kill off the code path, so by adding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jglick that single impl is not in GHBS 2.0.x |
||
public abstract static class Listener implements ExtensionPoint { | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
import com.cloudbees.jenkins.GitHubPushTrigger; | ||
import com.cloudbees.jenkins.GitHubRepositoryName; | ||
import com.cloudbees.jenkins.GitHubRepositoryNameContributor; | ||
import com.cloudbees.jenkins.GitHubTrigger; | ||
import com.cloudbees.jenkins.GitHubWebHook; | ||
import hudson.Extension; | ||
import hudson.ExtensionList; | ||
import hudson.model.Item; | ||
import hudson.security.ACL; | ||
import java.io.IOException; | ||
|
@@ -64,7 +64,7 @@ protected Set<GHEvent> events() { | |
* @param payload payload of gh-event. Never blank | ||
*/ | ||
@Override | ||
protected void onEvent(GHEvent event, String payload) { | ||
protected void onEvent(final String origin, GHEvent event, String payload) { | ||
GHEventPayload.Push push; | ||
try { | ||
push = GitHub.offline().parseEventPayload(new StringReader(payload), GHEventPayload.Push.class); | ||
|
@@ -74,7 +74,7 @@ protected void onEvent(GHEvent event, String payload) { | |
} | ||
URL repoUrl = push.getRepository().getUrl(); | ||
final String pusherName = push.getPusher().getName(); | ||
LOGGER.info("Received PushEvent for {}", repoUrl); | ||
LOGGER.info("Received PushEvent for {} from {}", repoUrl, origin); | ||
final GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl.toExternalForm()); | ||
|
||
if (changedRepository != null) { | ||
|
@@ -85,14 +85,14 @@ protected void onEvent(GHEvent event, String payload) { | |
@Override | ||
public void run() { | ||
for (Item job : Jenkins.getInstance().getAllItems(Item.class)) { | ||
GitHubTrigger trigger = triggerFrom(job, GitHubPushTrigger.class); | ||
GitHubPushTrigger trigger = triggerFrom(job, GitHubPushTrigger.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Highlights the pointlessness of the GitHubTrigger interface when the only other user could avoid it completely: https://github.com/jenkinsci/github-sqs-plugin/blob/d5886f8c3669e4ca61c1c359cd216aca713c7aeb/src/main/java/com/base2services/jenkins/github/GitHubTriggerProcessor.java#L87 which doesn't need to down-cast to the interface type as it knows that the |
||
if (trigger != null) { | ||
String fullDisplayName = job.getFullDisplayName(); | ||
LOGGER.debug("Considering to poke {}", fullDisplayName); | ||
if (GitHubRepositoryNameContributor.parseAssociatedNames(job) | ||
.contains(changedRepository)) { | ||
LOGGER.info("Poked {}", fullDisplayName); | ||
trigger.onPost(pusherName); | ||
trigger.onPost(origin, pusherName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must at least catch |
||
} else { | ||
LOGGER.debug("Skipped {} because it doesn't have a matching repository.", | ||
fullDisplayName); | ||
|
@@ -102,8 +102,7 @@ public void run() { | |
} | ||
}); | ||
|
||
for (GitHubWebHook.Listener listener : Jenkins.getInstance() | ||
.getExtensionList(GitHubWebHook.Listener.class)) { | ||
for (GitHubWebHook.Listener listener : ExtensionList.lookup(GitHubWebHook.Listener.class)) { | ||
listener.onPushRepositoryChanged(pusherName, changedRepository); | ||
} | ||
|
||
|
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.
Workflow tests need rework for the updated git dependency, but as git plugin didn't break the rules of SCM API we don't need to update to 2.6.4 / 3.0.4... just would be missing the SCMEvent integration in git plugin, but the tests in this plugin do not rely on that functionality, so no harm