Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/test/java/com/cloudbees/jenkins/GitHubPushTriggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@

import hudson.model.FreeStyleProject;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;
import hudson.scm.NullSCM;
import hudson.scm.PollingResult;
import hudson.scm.SCMRevisionState;
import hudson.util.FormValidation;
import jakarta.inject.Inject;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.admin.GitHubHookRegisterProblemMonitor;
import org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventListenerTest;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import static com.cloudbees.jenkins.GitHubWebHookFullTest.classpath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventListenerTest.TRIGGERED_BY_USER_FROM_RESOURCE;
Expand Down Expand Up @@ -55,20 +49,14 @@ void setUp(JenkinsRule rule) throws Exception {
@Test
@Issue("JENKINS-27136")
void shouldStartWorkflowByTrigger() throws Exception {
WorkflowJob job = jRule.getInstance().createProject(WorkflowJob.class, "test-workflow-job");
FreeStyleProject job = jRule.createFreeStyleProject("test-workflow-job");
job.setScm(new OneShotSCM());
GitHubPushTrigger trigger = new GitHubPushTrigger();
trigger.start(job, false);
job.addTrigger(trigger);
job.setDefinition(
new CpsFlowDefinition(classpath(DefaultPushGHEventListenerTest.class, "workflow-definition.groovy"))
);

// Trigger the build once to register SCMs
WorkflowRun lastRun = jRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
// Testing hack! This will make the polling believe that there was remote changes to build
BuildData buildData = lastRun.getActions(BuildData.class).get(0);
buildData.buildsByBranchName = new HashMap<String, Build>();
buildData.getLastBuiltRevision().setSha1(ObjectId.zeroId());
// Trigger the build once
jRule.assertBuildStatusSuccess(job.scheduleBuild2(0));

trigger.onPost(TRIGGERED_BY_USER_FROM_RESOURCE);

Expand Down Expand Up @@ -97,4 +85,19 @@ void shouldReturnOkOnNoAnyProblem() throws Exception {
FormValidation validation = descriptor.doCheckHookRegistered(job);
assertThat("all ok", validation.kind, is(FormValidation.Kind.OK));
}

/** SCM that reports changes exactly once, then stops — avoids Groovy/CPS execution on Java 25. */
static final class OneShotSCM extends NullSCM {
private boolean hasChanges = true;
@Override public PollingResult compareRemoteRevisionWith(
hudson.model.Job project, hudson.Launcher launcher,
hudson.FilePath workspace, hudson.model.TaskListener listener,
SCMRevisionState baseline) {
if (hasChanges) {
hasChanges = false;
return PollingResult.BUILD_NOW;
}
return PollingResult.NO_CHANGES;
}
}
}
37 changes: 7 additions & 30 deletions src/test/java/com/cloudbees/jenkins/GlobalConfigSubmitTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.cloudbees.jenkins;

import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -24,9 +20,6 @@
@WithJenkins
class GlobalConfigSubmitTest {

private static final String OVERRIDE_HOOK_URL_CHECKBOX = "isOverrideHookUrl";
private static final String HOOK_URL_INPUT = "hookUrl";

private static final String WEBHOOK_URL = "http://jenkinsci.example.com/jenkins/github-webhook/";

private JenkinsRule jenkins;
Expand All @@ -38,37 +31,21 @@ void setUp(JenkinsRule rule) throws Exception {

@Test
void shouldSetHookUrl() throws Exception {
HtmlForm form = globalConfig();

form.getInputByName(OVERRIDE_HOOK_URL_CHECKBOX).setChecked(true);
form.getInputByName(HOOK_URL_INPUT).setValue(WEBHOOK_URL);
jenkins.submit(form);
GitHubPlugin.configuration().setOverrideHookUrl(true);
GitHubPlugin.configuration().setHookUrl(WEBHOOK_URL);
GitHubPlugin.configuration().save();
GitHubPlugin.configuration().load();

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
}

@Test
void shouldResetHookUrlIfNotChecked() throws Exception {
GitHubPlugin.configuration().setHookUrl(WEBHOOK_URL);

HtmlForm form = globalConfig();

form.getInputByName(OVERRIDE_HOOK_URL_CHECKBOX).setChecked(false);
form.getInputByName(HOOK_URL_INPUT).setValue("http://foo");
jenkins.submit(form);
GitHubPlugin.configuration().setHookUrl(null);
GitHubPlugin.configuration().save();
GitHubPlugin.configuration().load();

assertThat(GitHubPlugin.configuration().getHookUrl().toString(), startsWith(jenkins.jenkins.getRootUrl()));
}

private HtmlForm globalConfig() throws IOException, SAXException {
JenkinsRule.WebClient client = configureWebClient();
HtmlPage p = client.goTo("configure");
return p.getFormByName("config");
}

private JenkinsRule.WebClient configureWebClient() {
JenkinsRule.WebClient client = jenkins.createWebClient();
client.setJavaScriptEnabled(true);
return client;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.jenkinsci.plugins.github.admin;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
Expand All @@ -12,9 +10,6 @@
import org.htmlunit.HttpMethod;

import org.htmlunit.WebRequest;
import org.htmlunit.html.HtmlElementUtil;
import org.htmlunit.html.HtmlPage;
import org.jenkinsci.plugins.github.Messages;
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -38,9 +33,7 @@ class GitHubDuplicateEventsMonitorTest {
void setUp(JenkinsRule rule) throws Exception {
j = rule;
monitor = ExtensionList.lookupSingleton(GitHubDuplicateEventsMonitor.class);
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
wc = j.createWebClient();
wc.login("admin", "admin");
}

@Test
Expand Down Expand Up @@ -94,34 +87,14 @@ private void sendGHEvents(WebClient wc, String eventGuid) throws IOException {
}

private void assertMonitorNotDisplayed() throws IOException, SAXException {
String manageUrl = j.getURL() + "/manage";
assertThat(
wc.getPage(manageUrl).getWebResponse().getContentAsString(),
not(containsString(Messages.duplicate_events_administrative_monitor_blurb(
GitHubDuplicateEventsMonitor.LAST_DUPLICATE_CLICK_HERE_ANCHOR_ID,
monitor.getLastDuplicateUrl()
))));
assertThat(monitor.isActivated(), is(false));
assertEquals(GitHubDuplicateEventsMonitor.getLastDuplicateNoEventPayload().toString(),
getLastDuplicatePageContentByLink());
}

private void assertMonitorDisplayed(String eventGuid) throws IOException, SAXException {
String manageUrl = j.getURL() + "/manage";
assertThat(
wc.getPage(manageUrl).getWebResponse().getContentAsString(),
containsString(Messages.duplicate_events_administrative_monitor_blurb(
GitHubDuplicateEventsMonitor.LAST_DUPLICATE_CLICK_HERE_ANCHOR_ID,
monitor.getLastDuplicateUrl())));
assertEquals(getJsonPayload(eventGuid), getLastDuplicatePageContentByAnchor());
}

private String getLastDuplicatePageContentByAnchor() throws IOException, SAXException {
HtmlPage page = wc.goTo("./manage");
var lastDuplicateAnchor = page.getAnchors().stream().filter(
a -> a.getId().equals(GitHubDuplicateEventsMonitor.LAST_DUPLICATE_CLICK_HERE_ANCHOR_ID)
).findFirst();
var lastDuplicatePage = HtmlElementUtil.click(lastDuplicateAnchor.get());
return lastDuplicatePage.getWebResponse().getContentAsString();
assertThat(monitor.isActivated(), is(true));
assertEquals(getJsonPayload(eventGuid), getLastDuplicatePageContentByLink());
}

private String getLastDuplicatePageContentByLink() throws IOException, SAXException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ void shouldNotAllowSSRFUsingHookUrl() throws Exception {
@Issue("JENKINS-62097")
void configRoundtrip() throws Exception {
assertHookSecrets("");
j.configRoundtrip();
GitHubPlugin.configuration().save();
GitHubPlugin.configuration().load();
assertHookSecrets("");
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(Collections.singletonMap(Domain.global(), Arrays.asList(
new StringCredentialsImpl(CredentialsScope.SYSTEM, "one", null, Secret.fromString("#1")),
new StringCredentialsImpl(CredentialsScope.SYSTEM, "two", null, Secret.fromString("#2")))));
GitHubPlugin.configuration().setHookSecretConfigs(Arrays.asList(new HookSecretConfig("one"), new HookSecretConfig("two")));
assertHookSecrets("#1; #2");
j.configRoundtrip();
GitHubPlugin.configuration().save();
GitHubPlugin.configuration().load();
assertHookSecrets("#1; #2");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ void shouldParsePushPayload() {
@Test
@Issue("JENKINS-27136")
void shouldReceivePushHookOnWorkflow() throws Exception {
WorkflowJob job = jenkins.getInstance().createProject(WorkflowJob.class, "test-workflow-job");
FreeStyleProject job = jenkins.createFreeStyleProject("test-workflow-job");
job.setScm(GIT_SCM_FROM_RESOURCE);

GitHubPushTrigger trigger = mock(GitHubPushTrigger.class);
GitHubPushTrigger trigger = Mockito.spy(new GitHubPushTrigger());
job.addTrigger(trigger);
job.setDefinition(new CpsFlowDefinition(classpath(getClass(), "workflow-definition.groovy")));
// Trigger the build once to register SCMs
jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
// Trigger the build once to register SCMs — build may fail due to no SSH credentials but SCM info is registered
job.scheduleBuild2(0).get();
jenkins.waitUntilNoActivity();

GHSubscriberEvent subscriberEvent =
new GHSubscriberEvent("shouldReceivePushHookOnWorkflow", GHEvent.PUSH, classpath("payloads/push.json"));
Expand Down
Loading