Skip to content

Commit

Permalink
[FIXED JENKINS-41812] Expose event origin to listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Feb 7, 2017
1 parent df66dbe commit addd033
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -173,7 +173,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>
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/hudson/plugins/git/GitStatus.java
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -108,7 +110,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;
Expand Down Expand Up @@ -149,8 +151,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() {
Expand Down Expand Up @@ -187,7 +190,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
*/
Expand Down Expand Up @@ -261,11 +264,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);
}


}

Expand All @@ -282,9 +310,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));
}

lastStaticBuildParameters = null;
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/jenkins/plugins/git/GitSCMSource.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit addd033

Please sign in to comment.