|
8 | 8 | import hudson.Extension;
|
9 | 9 | import hudson.model.Item;
|
10 | 10 | import hudson.security.ACL;
|
| 11 | +import java.io.IOException; |
| 12 | +import java.io.StringReader; |
| 13 | +import java.net.URL; |
11 | 14 | import jenkins.model.Jenkins;
|
12 |
| -import net.sf.json.JSONObject; |
13 | 15 | import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
|
14 | 16 | import org.kohsuke.github.GHEvent;
|
| 17 | +import org.kohsuke.github.GHEventPayload; |
| 18 | +import org.kohsuke.github.GitHub; |
15 | 19 | import org.slf4j.Logger;
|
16 | 20 | import org.slf4j.LoggerFactory;
|
17 | 21 |
|
@@ -61,43 +65,49 @@ protected Set<GHEvent> events() {
|
61 | 65 | */
|
62 | 66 | @Override
|
63 | 67 | protected void onEvent(GHEvent event, String payload) {
|
64 |
| - JSONObject json = JSONObject.fromObject(payload); |
65 |
| - String repoUrl = json.getJSONObject("repository").getString("url"); |
66 |
| - final String pusherName = json.getJSONObject("pusher").getString("name"); |
| 68 | + try { |
| 69 | + GHEventPayload.Push push = |
| 70 | + GitHub.offline().parseEventPayload(new StringReader(payload), GHEventPayload.Push.class); |
| 71 | + URL repoUrl = push.getRepository().getUrl(); |
| 72 | + final String pusherName = push.getPusher().getName(); |
| 73 | + LOGGER.info("Received PushEvent for {}", repoUrl); |
| 74 | + final GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl.toExternalForm()); |
67 | 75 |
|
68 |
| - LOGGER.info("Received POST for {}", repoUrl); |
69 |
| - final GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl); |
70 |
| - |
71 |
| - if (changedRepository != null) { |
72 |
| - // run in high privilege to see all the projects anonymous users don't see. |
73 |
| - // this is safe because when we actually schedule a build, it's a build that can |
74 |
| - // happen at some random time anyway. |
75 |
| - ACL.impersonate(ACL.SYSTEM, new Runnable() { |
76 |
| - @Override |
77 |
| - public void run() { |
78 |
| - for (Item job : Jenkins.getInstance().getAllItems(Item.class)) { |
79 |
| - GitHubTrigger trigger = triggerFrom(job, GitHubPushTrigger.class); |
80 |
| - if (trigger != null) { |
81 |
| - LOGGER.debug("Considering to poke {}", job.getFullDisplayName()); |
82 |
| - if (GitHubRepositoryNameContributor.parseAssociatedNames(job).contains(changedRepository)) { |
83 |
| - LOGGER.info("Poked {}", job.getFullDisplayName()); |
84 |
| - trigger.onPost(pusherName); |
85 |
| - } else { |
86 |
| - LOGGER.debug("Skipped {} because it doesn't have a matching repository.", |
87 |
| - job.getFullDisplayName()); |
| 76 | + if (changedRepository != null) { |
| 77 | + // run in high privilege to see all the projects anonymous users don't see. |
| 78 | + // this is safe because when we actually schedule a build, it's a build that can |
| 79 | + // happen at some random time anyway. |
| 80 | + ACL.impersonate(ACL.SYSTEM, new Runnable() { |
| 81 | + @Override |
| 82 | + public void run() { |
| 83 | + for (Item job : Jenkins.getInstance().getAllItems(Item.class)) { |
| 84 | + GitHubTrigger trigger = triggerFrom(job, GitHubPushTrigger.class); |
| 85 | + if (trigger != null) { |
| 86 | + String fullDisplayName = job.getFullDisplayName(); |
| 87 | + LOGGER.debug("Considering to poke {}", fullDisplayName); |
| 88 | + if (GitHubRepositoryNameContributor.parseAssociatedNames(job) |
| 89 | + .contains(changedRepository)) { |
| 90 | + LOGGER.info("Poked {}", fullDisplayName); |
| 91 | + trigger.onPost(pusherName); |
| 92 | + } else { |
| 93 | + LOGGER.debug("Skipped {} because it doesn't have a matching repository.", |
| 94 | + fullDisplayName); |
| 95 | + } |
88 | 96 | }
|
89 | 97 | }
|
90 | 98 | }
|
| 99 | + }); |
| 100 | + |
| 101 | + for (GitHubWebHook.Listener listener : Jenkins.getInstance() |
| 102 | + .getExtensionList(GitHubWebHook.Listener.class)) { |
| 103 | + listener.onPushRepositoryChanged(pusherName, changedRepository); |
91 | 104 | }
|
92 |
| - }); |
93 | 105 |
|
94 |
| - for (GitHubWebHook.Listener listener : Jenkins.getInstance() |
95 |
| - .getExtensionList(GitHubWebHook.Listener.class)) { |
96 |
| - listener.onPushRepositoryChanged(pusherName, changedRepository); |
| 106 | + } else { |
| 107 | + LOGGER.warn("Malformed repo url {}", repoUrl); |
97 | 108 | }
|
98 |
| - |
99 |
| - } else { |
100 |
| - LOGGER.warn("Malformed repo url {}", repoUrl); |
| 109 | + } catch (IOException e) { |
| 110 | + LOGGER.warn("Received malformed PushEvent: " + payload, e); |
101 | 111 | }
|
102 | 112 | }
|
103 | 113 | }
|
0 commit comments