Skip to content
Permalink
Browse files
[FIXED JENKINS-41812] Expose event origin to listeners
  • Loading branch information
stephenc committed Feb 7, 2017
1 parent dc0dc71 commit 371243786bcf626bf67b487a416c98b0ea9365a1
Showing 3 changed files with 44 additions and 10 deletions.
@@ -161,7 +161,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.0.2</version>
<version>2.0.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -1,5 +1,6 @@
package hudson.plugins.git;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
@@ -22,6 +23,7 @@
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMEvent;
import jenkins.triggers.SCMTriggerItem;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
@@ -112,7 +114,7 @@ public String toString() {
return s.toString();
}

public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(required=true) String url,
public HttpResponse doNotifyCommit(StaplerRequest request, @QueryParameter(required=true) String url,
@QueryParameter(required=false) String branches,
@QueryParameter(required=false) String sha1) throws ServletException, IOException {
lastURL = url;
@@ -153,8 +155,9 @@ public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(r
if (jenkins == null) {
return HttpResponses.error(SC_BAD_REQUEST, new Exception("Jenkins.getInstance() null for : " + url));
}
String origin = SCMEvent.originOf(request);
for (Listener listener : jenkins.getExtensionList(Listener.class)) {
contributors.addAll(listener.onNotifyCommit(uri, sha1, buildParameters, branchesArray));
contributors.addAll(listener.onNotifyCommit(origin, uri, sha1, buildParameters, branchesArray));
}

return new HttpResponse() {
@@ -194,7 +197,7 @@ private static String normalizePath(String path) {
}

/**
* Contributes to a {@link #doNotifyCommit(HttpServletRequest, String, String, String)} response.
* Contributes to a {@link #doNotifyCommit(StaplerRequest, String, String, String)} response.
*
* @since 1.4.1
*/
@@ -271,11 +274,36 @@ public List<ResponseContributor> onNotifyCommit(URIish uri, @Nullable String sha
* @param branches the (optional) branch information.
* @return any response contributors for the response to the push request.
* @since 2.4.0
* @deprecated use {@link #onNotifyCommit(String, URIish, String, List, String...)}
*/
@Deprecated
public List<ResponseContributor> onNotifyCommit(URIish uri, @Nullable String sha1, List<ParameterValue> buildParameters, String... branches) {
return onNotifyCommit(uri, sha1, branches);
}

/**
* Called when there is a change notification on a specific repository url.
*
* @param origin the origin of the notification (use {@link SCMEvent#originOf(StaplerRequest)} if in
* doubt) or {@code null} if the origin is unknown.
* @param uri the repository uri.
* @param sha1 SHA1 hash of commit to build
* @param buildParameters parameters to be passed to the build.
* Ignored unless build parameter flag is set
* due to security risk of accepting parameters from
* unauthenticated sources
* @param branches the (optional) branch information.
* @return any response contributors for the response to the push request.
* @since 2.6.5
*/
public List<ResponseContributor> onNotifyCommit(@CheckForNull String origin,
URIish uri,
@Nullable String sha1,
List<ParameterValue> buildParameters,
String... branches) {
return onNotifyCommit(uri, sha1, buildParameters, branches);
}


}

@@ -292,9 +320,10 @@ public static class JenkinsAbstractProjectListener extends Listener {
* {@inheritDoc}
*/
@Override
public List<ResponseContributor> onNotifyCommit(URIish uri, String sha1, List<ParameterValue> buildParameters, String... branches) {
public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, String sha1, List<ParameterValue> buildParameters, String... branches) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Received notification for uri = " + uri + " ; sha1 = " + sha1 + " ; branches = " + Arrays.toString(branches));
LOGGER.fine("Received notification from " + StringUtils.defaultIfBlank(origin, "?")
+ " for uri = " + uri + " ; sha1 = " + sha1 + " ; branches = " + Arrays.toString(branches));
}

GitStatus.clearLastStaticBuildParameters();
@@ -28,6 +28,7 @@
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import edu.umd.cs.findbugs.annotations.NonNull;
@@ -280,9 +281,12 @@ public ListBoxModel doFillGitToolItems() {

@Extension
public static class ListenerImpl extends GitStatus.Listener {

@Override
public List<GitStatus.ResponseContributor> onNotifyCommit(URIish uri, final String sha1, List<ParameterValue> buildParameters, String... branches) {
public List<GitStatus.ResponseContributor> onNotifyCommit(String origin,
URIish uri,
@Nullable final String sha1,
List<ParameterValue> buildParameters,
String... branches) {
List<GitStatus.ResponseContributor> result = new ArrayList<GitStatus.ResponseContributor>();
final boolean notified[] = {false};
// run in high privilege to see all the projects anonymous users don't see.
@@ -298,7 +302,7 @@ public List<GitStatus.ResponseContributor> onNotifyCommit(URIish uri, final Stri
if (branches.length > 0) {
final URIish u = uri;
for (final String branch: branches) {
SCMHeadEvent.fireNow(new SCMHeadEvent<String>(SCMEvent.Type.UPDATED, branch){
SCMHeadEvent.fireNow(new SCMHeadEvent<String>(SCMEvent.Type.UPDATED, branch, origin){
@Override
public boolean isMatch(@NonNull SCMNavigator navigator) {
return false;
@@ -364,7 +368,8 @@ public boolean isMatch(@NonNull SCM scm) {
continue;
}
if (GitStatus.looselyMatches(uri, remote)) {
LOGGER.info("Triggering the indexing of " + owner.getFullDisplayName());
LOGGER.info("Triggering the indexing of " + owner.getFullDisplayName()
+ " as a result of event from " + origin);
owner.onSCMSourceUpdated(source);
result.add(new GitStatus.ResponseContributor() {
@Override

0 comments on commit 3712437

Please sign in to comment.