-
Notifications
You must be signed in to change notification settings - Fork 401
[JENKINS-39590] Switch to GitHub.parseEventPayload for event parsing
#155
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
Changes from all commits
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,18 +3,21 @@ | |||||||||||||||
| import com.cloudbees.jenkins.GitHubRepositoryName; | ||||||||||||||||
| import hudson.Extension; | ||||||||||||||||
| import hudson.model.Item; | ||||||||||||||||
| import net.sf.json.JSONObject; | ||||||||||||||||
| import java.io.IOException; | ||||||||||||||||
| import java.io.StringReader; | ||||||||||||||||
| import java.util.Set; | ||||||||||||||||
| import javax.inject.Inject; | ||||||||||||||||
| import org.jenkinsci.plugins.github.admin.GitHubHookRegisterProblemMonitor; | ||||||||||||||||
| import org.jenkinsci.plugins.github.extension.GHEventsSubscriber; | ||||||||||||||||
| import org.kohsuke.github.GHEvent; | ||||||||||||||||
| import org.kohsuke.github.GHEventPayload; | ||||||||||||||||
| import org.kohsuke.github.GHOrganization; | ||||||||||||||||
| import org.kohsuke.github.GHRepository; | ||||||||||||||||
| import org.kohsuke.github.GitHub; | ||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||
|
|
||||||||||||||||
| import javax.inject.Inject; | ||||||||||||||||
| import java.util.Set; | ||||||||||||||||
|
|
||||||||||||||||
| import static com.google.common.collect.Sets.immutableEnumSet; | ||||||||||||||||
| import static net.sf.json.JSONObject.fromObject; | ||||||||||||||||
| import static org.kohsuke.github.GHEvent.PING; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
|
|
@@ -35,7 +38,6 @@ public class PingGHEventSubscriber extends GHEventsSubscriber { | |||||||||||||||
| * This subscriber is not applicable to any item | ||||||||||||||||
| * | ||||||||||||||||
| * @param project ignored | ||||||||||||||||
| * | ||||||||||||||||
| * @return always false | ||||||||||||||||
| */ | ||||||||||||||||
| @Override | ||||||||||||||||
|
|
@@ -59,15 +61,21 @@ protected Set<GHEvent> events() { | |||||||||||||||
| */ | ||||||||||||||||
| @Override | ||||||||||||||||
| protected void onEvent(GHEvent event, String payload) { | ||||||||||||||||
|
Member
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. Does onEvent report errors itself? Then catch not needed.
Member
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. github-plugin/src/main/java/org/jenkinsci/plugins/github/extension/GHEventsSubscriber.java Lines 208 to 214 in 93d4069
so try-catch useless wrapper, undo change and check that parser reports meaningful error.
Member
Author
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. That Throwable catch should be the last ditch catch. Propagation to it is not a good pattern. I'll see if I can decipher what you mean tomorrow on computer not phone. But in general you should catch and log as close and as specific as possible while you know what sense to make...
Member
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. In just you can trust to any underlying extension as it may throw NPE, so catching throwables and reporting is standard save ass pattern. If exception throws meaningful information then additional exception steps doesn't add anything useful for debugging. |
||||||||||||||||
| JSONObject parsedPayload = fromObject(payload); | ||||||||||||||||
| JSONObject repository = parsedPayload.optJSONObject("repository"); | ||||||||||||||||
| GHEventPayload.Ping ping; | ||||||||||||||||
| try { | ||||||||||||||||
| ping = GitHub.offline().parseEventPayload(new StringReader(payload), GHEventPayload.Ping.class); | ||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||
| LOGGER.warn("Received malformed PingEvent: " + payload, e); | ||||||||||||||||
| return; | ||||||||||||||||
| } | ||||||||||||||||
| GHRepository repository = ping.getRepository(); | ||||||||||||||||
| if (repository != null) { | ||||||||||||||||
| LOGGER.info("{} webhook received from repo <{}>!", event, repository.getString("html_url")); | ||||||||||||||||
| monitor.resolveProblem(GitHubRepositoryName.create(repository.getString("html_url"))); | ||||||||||||||||
| LOGGER.info("{} webhook received from repo <{}>!", event, repository.getHtmlUrl()); | ||||||||||||||||
| monitor.resolveProblem(GitHubRepositoryName.create(repository.getHtmlUrl().toExternalForm())); | ||||||||||||||||
| } else { | ||||||||||||||||
| JSONObject organization = parsedPayload.optJSONObject("organization"); | ||||||||||||||||
| GHOrganization organization = ping.getOrganization(); | ||||||||||||||||
| if (organization != null) { | ||||||||||||||||
| LOGGER.info("{} webhook received from org <{}>!", event, organization.getString("url")); | ||||||||||||||||
| LOGGER.info("{} webhook received from org <{}>!", event, organization.getUrl()); | ||||||||||||||||
| } else { | ||||||||||||||||
| LOGGER.warn("{} webhook received with unexpected payload", event); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
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.
Please avoid gratuitous reformatting of otherwise unmodified lines.