Skip to content
Permalink
Browse files
Merge pull request #532 from dwnusbaum/JENKINS-46929
[JENKINS-46929] Limit number of projects added to response headers
  • Loading branch information
MarkEWaite committed Sep 22, 2017
2 parents 815a015 + 1600418 commit 35745f2e3f81252840cb47b276068e033fc4bef5
Showing 2 changed files with 38 additions and 2 deletions.
@@ -166,8 +166,13 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object nod
throws IOException, ServletException {
rsp.setStatus(SC_OK);
rsp.setContentType("text/plain");
for (ResponseContributor c : contributors) {
c.addHeaders(req, rsp);
for (int i = 0; i < contributors.size(); i++) {
if (i == MAX_REPORTED_CONTRIBUTORS) {
rsp.addHeader("Triggered", "<" + (contributors.size() - i) + " more>");
break;
} else {
contributors.get(i).addHeaders(req, rsp);
}
}
PrintWriter w = rsp.getWriter();
for (ResponseContributor c : contributors) {
@@ -609,6 +614,7 @@ public String getShortDescription() {
}

private static final Logger LOGGER = Logger.getLogger(GitStatus.class.getName());
private static final int MAX_REPORTED_CONTRIBUTORS = 10;

/** Allow arbitrary notify commit parameters.
*
@@ -11,6 +11,7 @@
import hudson.tasks.Shell;
import hudson.triggers.SCMTrigger;
import java.io.File;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.*;

@@ -23,9 +24,13 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.WithoutJenkins;

import javax.servlet.http.HttpServletRequest;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

public class GitStatusTest extends AbstractGitProject {

@@ -516,4 +521,29 @@ private void doNotifyCommitWithDefaultParameter(final boolean allowed, String sa
private boolean isWindows() {
return File.pathSeparatorChar == ';';
}

@Test
@Issue("JENKINS-46929")
public void testDoNotifyCommitTriggeredHeadersLimited() throws Exception {
SCMTrigger[] projectTriggers = new SCMTrigger[50];
for (int i = 0; i < projectTriggers.length; i++) {
projectTriggers[i] = setupProjectWithTrigger("a", "master", false);
}

HttpResponse rsp = this.gitStatus.doNotifyCommit(requestWithNoParameter, "a", "master", null);

// Up to 10 "Triggered" headers + 1 extra warning are returned.
StaplerRequest sReq = mock(StaplerRequest.class);
StaplerResponse sRsp = mock(StaplerResponse.class);
Mockito.when(sRsp.getWriter()).thenReturn(mock(PrintWriter.class));
rsp.generateResponse(sReq, sRsp, null);
Mockito.verify(sRsp, Mockito.times(11)).addHeader(Mockito.eq("Triggered"), Mockito.anyString());

// All triggers run.
for (int i = 0; i < projectTriggers.length; i++) {
Mockito.verify(projectTriggers[i]).run();
}

assertEquals("URL: a Branches: master", this.gitStatus.toString());
}
}

0 comments on commit 35745f2

Please sign in to comment.