Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-30626] Handle pings from organization webhooks
Browse files Browse the repository at this point in the history
Closes #89
  • Loading branch information
jglick authored and lanwen committed Sep 24, 2015
1 parent 6b819f4 commit 03b1c87
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Expand Up @@ -2,6 +2,7 @@

import hudson.Extension;
import hudson.model.Job;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
import org.kohsuke.github.GHEvent;
import org.slf4j.Logger;
Expand Down Expand Up @@ -52,8 +53,18 @@ protected Set<GHEvent> events() {
*/
@Override
protected void onEvent(GHEvent event, String payload) {
// something like <https://github.com/bar/foo>
String repo = fromObject(payload).getJSONObject("repository").getString("url");
LOGGER.info("{} webhook received from repo <{}>!", event, repo);
JSONObject parsedPayload = fromObject(payload);
JSONObject repository = parsedPayload.optJSONObject("repository");
if (repository != null) {
// something like <https://github.com/bar/foo>
LOGGER.info("{} webhook received from repo <{}>!", event, repository.getString("url"));
} else {
JSONObject organization = parsedPayload.optJSONObject("organization");
if (organization != null) {
LOGGER.info("{} webhook received from org <{}>!", event, organization.getString("url"));
} else {
LOGGER.warn("{} webhook received with unexpected payload", event);
}
}
}
}
Expand Up @@ -10,6 +10,7 @@
import static com.cloudbees.jenkins.GitHubWebHookFullTest.classpath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.jvnet.hudson.test.Issue;

/**
* @author lanwen (Merkushev Kirill)
Expand All @@ -30,4 +31,12 @@ public void shouldBeNotApplicableForProjects() throws Exception {
public void shouldParsePingPayload() throws Exception {
new PingGHEventSubscriber().onEvent(GHEvent.PING, classpath("payloads/ping.json"));
}

@Issue("JENKINS-30626")
@Test
@WithoutJenkins
public void shouldParseOrgPingPayload() throws Exception {
new PingGHEventSubscriber().onEvent(GHEvent.PING, classpath("payloads/orgping.json"));
}

}
@@ -0,0 +1,52 @@
{
"zen": "Mind your words, they are important.",
"hook_id": 5926787,
"hook": {
"url": "https://api.github.com/orgs/cloudbeers/hooks/5926787",
"ping_url": "https://api.github.com/orgs/cloudbeers/hooks/5926787/pings",
"id": 5926787,
"name": "web",
"active": true,
"events": [
"*"
],
"config": {
"url": "https://jenkins.ci.cloudbees.com/github-webhook/",
"content_type": "json",
"insecure_ssl": "0",
"secret": ""
},
"updated_at": "2015-09-24T10:13:54Z",
"created_at": "2015-09-24T10:13:54Z"
},
"organization": {
"login": "cloudbeers",
"id": 4181899,
"url": "https://api.github.com/orgs/cloudbeers",
"repos_url": "https://api.github.com/orgs/cloudbeers/repos",
"events_url": "https://api.github.com/orgs/cloudbeers/events",
"members_url": "https://api.github.com/orgs/cloudbeers/members{/member}",
"public_members_url": "https://api.github.com/orgs/cloudbeers/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/4181899?v=3",
"description": null
},
"sender": {
"login": "jglick",
"id": 154109,
"avatar_url": "https://avatars.githubusercontent.com/u/154109?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/jglick",
"html_url": "https://github.com/jglick",
"followers_url": "https://api.github.com/users/jglick/followers",
"following_url": "https://api.github.com/users/jglick/following{/other_user}",
"gists_url": "https://api.github.com/users/jglick/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jglick/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jglick/subscriptions",
"organizations_url": "https://api.github.com/users/jglick/orgs",
"repos_url": "https://api.github.com/users/jglick/repos",
"events_url": "https://api.github.com/users/jglick/events{/privacy}",
"received_events_url": "https://api.github.com/users/jglick/received_events",
"type": "User",
"site_admin": false
}
}

0 comments on commit 03b1c87

Please sign in to comment.