From 2c99e85e95faadec7f57baaab3c2937a35c7f7ef Mon Sep 17 00:00:00 2001 From: Dusty Gutzmann Date: Thu, 5 Nov 2020 20:21:31 -0700 Subject: [PATCH 1/2] feat(updating reregisterallhooks method): only register parent, not all children Instead of trying to register a webhook for each child job in a multibranch-pipeline project, only register the base project --- .../java/com/cloudbees/jenkins/GitHubWebHook.java | 2 ++ .../plugins/github/util/JobInfoHelpers.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java index 3033771a2..e7a396967 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java @@ -36,6 +36,7 @@ import static org.jenkinsci.plugins.github.util.FluentIterableWrapper.from; import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isAlive; import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isBuildable; +import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isNotChild; import static org.jenkinsci.plugins.github.webhook.WebhookManager.forHookUrl; @@ -104,6 +105,7 @@ public List reRegisterAllHooks() { return from(getJenkinsInstance().getAllItems(Item.class)) .filter(isBuildable()) .filter(isAlive()) + .filter(isNotChild()) .transform(reRegisterHookForJob()) .toList(); } diff --git a/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java b/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java index c935f2f43..787b369e9 100644 --- a/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java +++ b/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java @@ -141,5 +141,19 @@ protected Job asJob() { } }; } + + /** + * Can be useful to ignore child jobs on reregistering hooks + * + * @return predicate with true on apply if item is not a child of WorkflowMultiBranchProject + */ + public static Predicate isNotChild() { + return new Predicate() { + public boolean apply(ITEM item) { + return !(item.getParent().getClass().getName().equals("org.jenkinsci.plugins.workflow.multibranch" + + ".WorkflowMultiBranchProject")); + } + }; + } } From ec4bc9bc22029b784d2ec70eb4ae0c12f9ae1593 Mon Sep 17 00:00:00 2001 From: Dusty Gutzmann Date: Mon, 9 Nov 2020 19:50:03 -0700 Subject: [PATCH 2/2] Switching to SCMsourceOwner --- src/main/java/com/cloudbees/jenkins/GitHubWebHook.java | 4 ++-- .../org/jenkinsci/plugins/github/util/JobInfoHelpers.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java index e7a396967..8a3a24bfb 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java @@ -36,7 +36,7 @@ import static org.jenkinsci.plugins.github.util.FluentIterableWrapper.from; import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isAlive; import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isBuildable; -import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isNotChild; +import static org.jenkinsci.plugins.github.util.JobInfoHelpers.isNotSCMSourceOwner; import static org.jenkinsci.plugins.github.webhook.WebhookManager.forHookUrl; @@ -105,7 +105,7 @@ public List reRegisterAllHooks() { return from(getJenkinsInstance().getAllItems(Item.class)) .filter(isBuildable()) .filter(isAlive()) - .filter(isNotChild()) + .filter(isNotSCMSourceOwner()) .transform(reRegisterHookForJob()) .toList(); } diff --git a/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java b/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java index 787b369e9..2867eb2dd 100644 --- a/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java +++ b/src/main/java/org/jenkinsci/plugins/github/util/JobInfoHelpers.java @@ -11,6 +11,7 @@ import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import jenkins.model.ParameterizedJobMixIn; +import jenkins.scm.api.SCMSourceOwner; import org.jenkinsci.plugins.github.extension.GHEventsSubscriber; import javax.annotation.CheckForNull; @@ -147,11 +148,10 @@ protected Job asJob() { * * @return predicate with true on apply if item is not a child of WorkflowMultiBranchProject */ - public static Predicate isNotChild() { + public static Predicate isNotSCMSourceOwner() { return new Predicate() { public boolean apply(ITEM item) { - return !(item.getParent().getClass().getName().equals("org.jenkinsci.plugins.workflow.multibranch" - + ".WorkflowMultiBranchProject")); + return !(item.getParent() instanceof SCMSourceOwner); } }; }