Skip to content
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

Ootb webhooks support #58

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jenkins.triggers.SCMTriggerItem;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import com.google.common.base.Objects;
Expand Down Expand Up @@ -92,6 +93,12 @@ private boolean match(SCM scm, URIish url) {
if(urIish.getPath().startsWith("/scm")){
urIish = urIish.setPath(urIish.getPath().substring(4));
}

// needed because bitbucket self hosted does not transfer any host information
if (StringUtils.isEmpty(url.getHost())) {
urIish = urIish.setHost(url.getHost());
}

LOGGER.log(Level.FINE, "Trying to match {0} ", urIish.toString() + "<-->" + url.toString());
if (GitStatus.looselyMatches(urIish, url)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,53 @@ public void processPayload(JSONObject payload, HttpServletRequest request) {
LOGGER.log(Level.INFO, "Processing new Webhooks payload");
processWebhookPayload(payload);
}
} else if (payload.has("actor") && payload.has("repository")) {
} else if (payload.has("actor") && payload.has("repository") && payload.getJSONObject("repository").has("links")) {
if ("repo:push".equals(request.getHeader("x-event-key"))) {
LOGGER.log(Level.INFO, "Processing new Webhooks payload");
processWebhookPayloadBitBucketServer(payload);
}
} else if (payload.has("actor")) {
// we assume that the passed hook was from bitbucket server https://confluence.atlassian.com/bitbucketserver/managing-webhooks-in-bitbucket-server-938025878.html
LOGGER.log(Level.INFO, "Processing webhook for self-hosted bitbucket instance");
processWebhookPayloadBitBucketSelfHosted(payload);
} else {
LOGGER.log(Level.INFO, "Processing old POST service payload");
processPostServicePayload(payload);
}
}

private void processWebhookPayload(JSONObject payload) {
/**
* parses the payload from self hosted bitbucket instance which uses the default webhooks implementation.
*
* https://confluence.atlassian.com/bitbucketserver0510/managing-webhooks-in-bitbucket-server-951390737.html
* https://confluence.atlassian.com/bitbucketserver0510/event-payload-951390742.html
*
* @param payload The payload matching the definition in https://confluence.atlassian.com/bitbucketserver0510/event-payload-951390742.html
*/
private void processWebhookPayloadBitBucketSelfHosted(JSONObject payload) {
JSONObject repo;

// find the repository hidden in different objects
if (payload.has("repository")) { // for push to repository
repo = payload.getJSONObject("repository");
} else if (payload.has("pullRequest")) { // for all PR events
repo = payload.getJSONObject("pullRequest").getJSONObject("toRef").getJSONObject("repository");
} else {
LOGGER.log(Level.WARNING, "Not possible to trigger job for event '{0}'. Only PR events and pushes are supported for now.", payload.get("eventKey"));
LOGGER.log(Level.FINE, payload.toString());
return;
}

String user = payload.getJSONObject("actor").getString("name");
String url = repo.getJSONObject("project").getString("key").toLowerCase() + "/" + repo.getString("slug");

// always use git no other repo type supported on self hosted solution
String scm = "git";
probe.triggerMatchingJobs(user, url, scm, payload.toString());

}

private void processWebhookPayload(JSONObject payload) {
if (payload.has("repository")) {
JSONObject repo = payload.getJSONObject("repository");
LOGGER.log(Level.INFO, "Received commit hook notification for {0}", repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sshd.common.util.IoUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -103,5 +111,33 @@ public void testProcessPostServicePayload() {

verify(probe).triggerMatchingJobs("old_user", "https://staging.bitbucket.org/old_user/old_repo", "git", payload.toString());
}


@Test
public void processWebhookPayloadBitBucketSelfHostedPush() throws IOException {
String user = "user";
String url = "proj/repository";

try (InputStream input = getClass().getClassLoader().getResourceAsStream("bitbucket_pr_merge_payload.json")) {
JSONObject payload = JSONObject.fromObject(IOUtils.toString(input));
payloadProcessor.processPayload(payload, request);

verify(probe).triggerMatchingJobs(user, url, "git", payload.toString());
}

}

@Test
public void processWebhookPayloadBitBucketSelfHostedPR() throws IOException {
String user = "user";
String url = "proj/repository";

try (InputStream input = getClass().getClassLoader().getResourceAsStream("bitbucket_pr_merge_payload.json")) {
JSONObject payload = JSONObject.fromObject(IOUtils.toString(input));
payloadProcessor.processPayload(payload, request);

verify(probe).triggerMatchingJobs(user, url, "git", payload.toString());
}
}

}
108 changes: 108 additions & 0 deletions src/test/resources/bitbucket_pr_merge_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"eventKey":"pr:merged",
"date":"2017-09-19T10:39:36+1000",
"actor":{
"name":"user",
"emailAddress":"user@example.com",
"id":2,
"displayName":"User",
"active":true,
"slug":"user",
"type":"NORMAL"
},
"pullRequest":{
"id":9,
"version":2,
"title":"file edited online with Bitbucket",
"state":"MERGED",
"open":false,
"closed":true,
"createdDate":1505781560908,
"updatedDate":1505781576361,
"closedDate":1505781576361,
"fromRef":{
"id":"refs/heads/admin/file-1505781548644",
"displayId":"admin/file-1505781548644",
"latestCommit":"45f9690c928915a5e1c4366d5ee1985eea03f05d",
"repository":{
"slug":"repository",
"id":84,
"name":"repository",
"scmId":"git",
"state":"AVAILABLE",
"statusMessage":"Available",
"forkable":true,
"project":{
"key":"PROJ",
"id":84,
"name":"project",
"public":false,
"type":"NORMAL"
},
"public":false
}
},
"toRef":{
"id":"refs/heads/master",
"displayId":"master",
"latestCommit":"8d2ad38c918fa6943859fca2176c89ea98b92a21",
"repository":{
"slug":"repository",
"id":84,
"name":"repository",
"scmId":"git",
"state":"AVAILABLE",
"statusMessage":"Available",
"forkable":true,
"project":{
"key":"PROJ",
"id":84,
"name":"project",
"public":false,
"type":"NORMAL"
},
"public":false
}
},
"locked":false,
"author":{
"user":{
"name":"admin",
"emailAddress":"admin@example.com",
"id":1,
"displayName":"Administrator",
"active":true,
"slug":"admin",
"type":"NORMAL"
},
"role":"AUTHOR",
"approved":false,
"status":"UNAPPROVED"
},
"reviewers":[

],
"participants":[
{
"user":{
"name":"user",
"emailAddress":"user@example.com",
"id":2,
"displayName":"User",
"active":true,
"slug":"user",
"type":"NORMAL"
},
"role":"PARTICIPANT",
"approved":false,
"status":"UNAPPROVED"
}
],
"properties":{
"mergeCommit":{
"displayId":"7e48f426f0a",
"id":"7e48f426f0a6e47c5b5e862c31be6ca965f82c9c"
}
}
}
}
43 changes: 43 additions & 0 deletions src/test/resources/bitbucket_push_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"eventKey":"repo:refs_changed",
"date":"2017-09-19T09:45:32+1000",
"actor":{
"name":"admin",
"emailAddress":"admin@example.com",
"id":1,
"displayName":"Administrator",
"active":true,
"slug":"admin",
"type":"NORMAL"
},
"repository":{
"slug":"repository",
"id":84,
"name":"repository",
"scmId":"git",
"state":"AVAILABLE",
"statusMessage":"Available",
"forkable":true,
"project":{
"key":"PROJ",
"id":84,
"name":"project",
"public":false,
"type":"NORMAL"
},
"public":false
},
"changes":[
{
"ref":{
"id":"refs/heads/master",
"displayId":"master",
"type":"BRANCH"
},
"refId":"refs/heads/master",
"fromHash":"ecddabb624f6f5ba43816f5926e580a5f680a932",
"toHash":"178864a7d521b6f5e720b386b2c2b0ef8563e0dc",
"type":"UPDATE"
}
]
}