Search in sources :

Example 1 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class BlueOceanWebURLBuilder method getTryBlueOceanURLs.

/**
     * Get the {@link TryBlueOceanURLs} instance for the {@link ModelObject}
     * associated with the current Stapler request.
     *
     * @return The {@link TryBlueOceanURLs} instance for the current classic
     * Jenkins page. The URL to the Blue Ocean homepage is returned if a more
     * appropriate URL is not found.
     */
@Nonnull
public static TryBlueOceanURLs getTryBlueOceanURLs() {
    StaplerRequest staplerRequest = Stapler.getCurrentRequest();
    List<Ancestor> list = staplerRequest.getAncestors();
    // Blue Ocean page we can link onto.
    for (int i = list.size() - 1; i >= 0; i--) {
        Ancestor ancestor = list.get(i);
        Object object = ancestor.getObject();
        if (object instanceof ModelObject) {
            String blueUrl = toBlueOceanURL((ModelObject) object);
            if (blueUrl != null) {
                if (object instanceof Item) {
                    return new TryBlueOceanURLs(blueUrl, ((Item) object).getUrl());
                } else if (object instanceof Run) {
                    return new TryBlueOceanURLs(blueUrl, ((Run) object).getUrl());
                } else {
                    return new TryBlueOceanURLs(blueUrl);
                }
            } else if (object instanceof Item) {
                return new TryBlueOceanURLs(getBlueHome(), ((Item) object).getUrl());
            }
        }
    }
    // Otherwise just return Blue Ocean home.
    return new TryBlueOceanURLs(getBlueHome());
}
Also used : Item(hudson.model.Item) ModelObject(hudson.model.ModelObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ModelObject(hudson.model.ModelObject) Run(hudson.model.Run) Ancestor(org.kohsuke.stapler.Ancestor) Nonnull(javax.annotation.Nonnull)

Example 2 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project generic-webhook-trigger-plugin by jenkinsci.

the class GenericWebHookRequestReceiverTest method testThatHeadersCanBeTransformedToList.

@Test
public void testThatHeadersCanBeTransformedToList() {
    final GenericWebHookRequestReceiver sut = new GenericWebHookRequestReceiver();
    final StaplerRequest request = mock(StaplerRequest.class);
    // 
    when(request.getHeaderNames()).thenReturn(new ArrayEnumeration(new String[] { "headerName1" }));
    // 
    when(request.getHeaders("headerName1")).thenReturn(new ArrayEnumeration(new String[] { "headerValue1" }));
    final Map<String, List<String>> actual = sut.getHeaders(request);
    final Map<String, List<String>> expected = new HashMap<>();
    expected.put("headername1", newArrayList("headerValue1"));
    // 
    assertThat(actual).isEqualTo(expected);
}
Also used : Maps.newHashMap(com.google.common.collect.Maps.newHashMap) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Test(org.junit.Test)

Example 3 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenNoJobInQueryStringMustReturnError.

@Test
public void whenNoJobInQueryStringMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn("foo=bar&blaah=blaah");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(404);
    String expectedOutput = "Parameter 'job' is missing.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 4 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenEmptyHeaderTypeMustReturnError.

@Test
public void whenEmptyHeaderTypeMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    // perform the test
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(403);
    String expectedOutput = "Only push event can be accepted.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 5 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenWrongHeaderTypeMustReturnError.

@Test
public void whenWrongHeaderTypeMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("junk");
    // perform the testÎ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(403);
    String expectedOutput = "Only push event can be accepted.";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4