diff --git a/src/main/java/org/jenkinsci/plugins/github/webhook/WebhookManager.java b/src/main/java/org/jenkinsci/plugins/github/webhook/WebhookManager.java index b13194df1..99be98617 100644 --- a/src/main/java/org/jenkinsci/plugins/github/webhook/WebhookManager.java +++ b/src/main/java/org/jenkinsci/plugins/github/webhook/WebhookManager.java @@ -141,6 +141,11 @@ public void run() { */ public void unregisterFor(GitHubRepositoryName name, List aliveRepos) { try { + if (!(name.resolve(allowedToManageHooks()).iterator().hasNext())) { + LOGGER.debug("Skipped adding GitHub webhook for {} because not configured to Manage Hooks", name); + return; + } + GHRepository repo = checkNotNull( from(name.resolve(allowedToManageHooks())).firstMatch(withAdminAccess()).orNull(), "There are no credentials with admin access to manage hooks on %s", name @@ -176,6 +181,12 @@ protected Function createHookSubscribedTo(final Li @Override protected GHHook applyNullSafe(@Nonnull GitHubRepositoryName name) { try { + if (!(name.resolve(allowedToManageHooks()).iterator().hasNext())) { + LOGGER.debug("Skipped adding GitHub webhook for {} because not configured to Manage Hooks", + name); + return null; + } + GHRepository repo = checkNotNull( from(name.resolve(allowedToManageHooks())).firstMatch(withAdminAccess()).orNull(), "There are no credentials with admin access to manage hooks on %s", name diff --git a/src/test/java/org/jenkinsci/plugins/github/admin/GitHubHookRegisterProblemMonitorTest.java b/src/test/java/org/jenkinsci/plugins/github/admin/GitHubHookRegisterProblemMonitorTest.java index 4bbabbf86..e59b1871b 100644 --- a/src/test/java/org/jenkinsci/plugins/github/admin/GitHubHookRegisterProblemMonitorTest.java +++ b/src/test/java/org/jenkinsci/plugins/github/admin/GitHubHookRegisterProblemMonitorTest.java @@ -5,6 +5,7 @@ import hudson.model.FreeStyleProject; import hudson.model.Item; import hudson.plugins.git.GitSCM; +import org.jenkinsci.plugins.github.config.GitHubServerConfig; import org.jenkinsci.plugins.github.extension.GHSubscriberEvent; import org.jenkinsci.plugins.github.extension.GHEventsSubscriber; import org.jenkinsci.plugins.github.webhook.WebhookManager; @@ -149,6 +150,9 @@ public void shouldReportAboutHookProblemOnRegister() throws IOException { job.addTrigger(new GitHubPushTrigger()); job.setScm(REPO_GIT_SCM); + GitHubServerConfig conf = new GitHubServerConfig(WebhookManagerTest.HOOK_ENDPOINT.toString()); + conf.setManageHooks(true); + WebhookManager.forHookUrl(WebhookManagerTest.HOOK_ENDPOINT) .registerFor((Item) job).run(); @@ -157,6 +161,9 @@ public void shouldReportAboutHookProblemOnRegister() throws IOException { @Test public void shouldReportAboutHookProblemOnUnregister() { + GitHubServerConfig conf = new GitHubServerConfig(WebhookManagerTest.HOOK_ENDPOINT.toString()); + conf.setManageHooks(true); + WebhookManager.forHookUrl(WebhookManagerTest.HOOK_ENDPOINT) .unregisterFor(REPO, Collections.emptyList());