-
Notifications
You must be signed in to change notification settings - Fork 369
Skip GitHubOrgWebHook.register when using a GH app receiving all relevant events #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
49d22ad
283d4ab
c43a8e4
a749a1b
b53e392
4fe93ed
e00792b
3cec758
f7513a6
19599ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
import hudson.util.ListBoxModel; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.net.MalformedURLException; | ||
import java.time.Duration; | ||
import java.time.format.DateTimeParseException; | ||
|
@@ -96,9 +97,11 @@ | |
import org.jenkins.ui.icon.IconSpec; | ||
import org.jenkinsci.Symbol; | ||
import org.jenkinsci.plugins.github.config.GitHubServerConfig; | ||
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.DoNotUse; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.github.GHEvent; | ||
import org.kohsuke.github.GHMyself; | ||
import org.kohsuke.github.GHOrganization; | ||
import org.kohsuke.github.GHRepository; | ||
|
@@ -1627,6 +1630,35 @@ public void afterSave(@NonNull SCMNavigatorOwner owner) { | |
// FIXME MINOR HACK ALERT | ||
StandardCredentials credentials = | ||
Connector.lookupScanCredentials((Item) owner, getApiUri(), credentialsId, repoOwner); | ||
if (credentials instanceof GitHubAppCredentials) { | ||
try { | ||
List<GHEvent> handledEvents = ((GitHubAppCredentials) credentials) | ||
.getAppInstallation() | ||
.getEvents(); | ||
Method eventsM = GHEventsSubscriber.class.getMethod("events"); // TODO protected | ||
eventsM.setAccessible(true); | ||
boolean good = true; | ||
for (GHEventsSubscriber subscriber : GHEventsSubscriber.all()) { | ||
@SuppressWarnings("unchecked") | ||
Set<GHEvent> subscribedEvents = (Set<GHEvent>) eventsM.invoke(subscriber); | ||
if (!handledEvents.containsAll(subscribedEvents)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would make sure no events go unhandled :+1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be too strict—to activate this patch you need to include the repository events which you might otherwise have skipped if you are only dealing with a small list of repositories—but I erred on the conservative side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will break on |
||
DescriptorImpl.LOGGER.log( | ||
Level.WARNING, | ||
"The GitHub App is not configured to receive some desired events: {0}", | ||
subscribedEvents); | ||
good = false; | ||
} | ||
} | ||
if (good) { | ||
DescriptorImpl.LOGGER.info( | ||
"The GitHub App is configured to receive some desired events; no additional webhook need be installed on the organization"); | ||
return; | ||
} | ||
} catch (Exception x) { | ||
DescriptorImpl.LOGGER.log( | ||
Level.WARNING, "Could not check whether the GitHub App receives all desired events", x); | ||
} | ||
} | ||
GitHub hub = Connector.connect(getApiUri(), credentials); | ||
try { | ||
GitHubOrgWebHook.register(hub, repoOwner); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The uninstallation of an app sends out an
"action":"deleted"
installation event, similar to installation of an app which also sends out an installation event but with"action":"created"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but AFAICT this Java method is not called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah looking at it, it might be designed to act on a deregister for a webhook, but I cannot find any such event for it on the Github API document :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the intention was for this to be called when the
OrganizationFolder
was deleted, but from e424aa2 I am guessing this was never implemented.