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

Ignore payload processing for diagnostics:ping #65

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bf845bb
Merge pull request #49 from fbelzunc/update-pom
fbelzunc Nov 2, 2017
6e80330
[JENKINS-47782] Add findbugs-exclude.xml
fbelzunc Nov 2, 2017
186ba37
Merge pull request #50 from fbelzunc/JENKINS-47782
fbelzunc Nov 2, 2017
0653a65
[JENKINS-47782] Skip findbugs
fbelzunc Nov 2, 2017
1eb4e44
Merge pull request #51 from jenkinsci/JENKINS-47782
fbelzunc Nov 2, 2017
9c1768c
[maven-release-plugin] prepare release bitbucket-1.1.6
fbelzunc Nov 2, 2017
2ed382b
[maven-release-plugin] prepare for next development iteration
fbelzunc Nov 2, 2017
4c5aa5f
Add Jenkinsfile
fbelzunc Dec 6, 2017
5133e43
Merge pull request #54 from fbelzunc/add-jenkins-file
fbelzunc Dec 6, 2017
5701ae3
[FIXED JENKINS-28877] Adding BitBucket server webhook (#53)
fbelzunc Dec 6, 2017
b5a4a06
[maven-release-plugin] prepare release bitbucket-1.1.7
fbelzunc Dec 6, 2017
1d65669
[maven-release-plugin] prepare for next development iteration
fbelzunc Dec 6, 2017
362bf39
[JENKINS-28877] remove /scm before GitStatus.looselyMatches to match …
Dec 6, 2017
0316730
Merge pull request #55 from entfrickler/sshgit
fbelzunc Dec 6, 2017
9824177
[maven-release-plugin] prepare release bitbucket-1.1.8
fbelzunc Dec 6, 2017
ce068b7
[maven-release-plugin] prepare for next development iteration
fbelzunc Dec 6, 2017
46718cb
Fix: Bitbucket push can now kick off Jenkins Pipeline Jobs
jeredp Feb 15, 2019
bdc06ef
Replace username with nickname due to Atlassian API deprecation https…
LeeU1911 May 31, 2019
5ad4366
Try with username then nickname then display_name to ensure backward …
LeeU1911 May 31, 2019
2fd9812
Merge pull request #64 from LeeU1911/master
fbelzunc Jun 1, 2019
7f5c856
[maven-release-plugin] prepare release bitbucket-1.1.9
fbelzunc Jun 1, 2019
020c1ea
[maven-release-plugin] prepare for next development iteration
fbelzunc Jun 1, 2019
0fbc86c
Merge pull request #63 from jeredp/master
fbelzunc Jul 4, 2019
0c5568c
[maven-release-plugin] prepare release bitbucket-1.1.10
fbelzunc Jul 4, 2019
f5e941f
[maven-release-plugin] prepare for next development iteration
fbelzunc Jul 4, 2019
f0ef4b6
Ignore payload processing for diagnostics:ping
Aug 2, 2019
c5bb6ba
Ignore payload processing for diagnostics:ping
Aug 2, 2019
1b92866
Merge branch 'master' of https://github.com/tkrisztian95/bitbucket-pl…
Aug 2, 2019
e8453c7
Endline and formatting reverted
Aug 2, 2019
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
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buildPlugin()
5 changes: 5 additions & 0 deletions findbugs-exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<FindBugsFilter>
<Match>
<Class name="~.+\.Messages" />
</Match>
</FindBugsFilter>
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<artifactId>bitbucket</artifactId>
<version>1.1.6-SNAPSHOT</version>
<version>1.1.11-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Jenkins Bitbucket Plugin</name>
<description>integrate Jenkins with BitBucket.</description>
Expand Down Expand Up @@ -66,6 +66,7 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
<excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>
</configuration>
</plugin>
Expand All @@ -92,7 +93,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>1.40</version>
<version>1.66</version>
<optional>true</optional>
</dependency>

Expand Down Expand Up @@ -150,4 +151,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ private boolean match(SCM scm, URIish url) {
if (scm instanceof GitSCM) {
for (RemoteConfig remoteConfig : ((GitSCM) scm).getRepositories()) {
for (URIish urIish : remoteConfig.getURIs()) {
// needed cause the ssh and https URI differs in Bitbucket Server.
if(urIish.getPath().startsWith("/scm")){
urIish = urIish.setPath(urIish.getPath().substring(4));
}
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
@@ -1,10 +1,13 @@
package com.cloudbees.jenkins.plugins;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class BitbucketPayloadProcessor {
Expand All @@ -25,7 +28,18 @@ 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")) {
if ("repo:push".equals(request.getHeader("x-event-key"))) {
LOGGER.log(Level.INFO, "Processing new Webhooks payload");
processWebhookPayloadBitBucketServer(payload);
}
} else {
if ("diagnostics:ping".equals(request.getHeader("x-event-key"))) {
if (payload.has("test") && payload.getBoolean("test") == true) {
LOGGER.log(Level.INFO, "Bitbucket test connection payload");
return;
}
}
LOGGER.log(Level.INFO, "Processing old POST service payload");
processPostServicePayload(payload);
}
Expand All @@ -36,14 +50,14 @@ private void processWebhookPayload(JSONObject payload) {
JSONObject repo = payload.getJSONObject("repository");
LOGGER.log(Level.INFO, "Received commit hook notification for {0}", repo);

String user = payload.getJSONObject("actor").getString("username");
String user = getUser(payload, "actor");
String url = repo.getJSONObject("links").getJSONObject("html").getString("href");
String scm = repo.has("scm") ? repo.getString("scm") : "git";

probe.triggerMatchingJobs(user, url, scm, payload.toString());
} else if (payload.has("scm")) {
LOGGER.log(Level.INFO, "Received commit hook notification for hg: {0}", payload);
String user = payload.getJSONObject("owner").getString("username");
String user = getUser(payload, "owner");
String url = payload.getJSONObject("links").getJSONObject("html").getString("href");
String scm = payload.has("scm") ? payload.getString("scm") : "hg";

Expand All @@ -52,6 +66,43 @@ private void processWebhookPayload(JSONObject payload) {

}

private String getUser(JSONObject payload, String jsonObject) {
String user;
try {
user = payload.getJSONObject(jsonObject).getString("username");
} catch (JSONException e1) {
try {
user = payload.getJSONObject(jsonObject).getString("nickname");
} catch (JSONException e2) {
user = payload.getJSONObject(jsonObject).getString("display_name");
}
}
return user;
}

/**
* Payload processor for BitBucket server. The plugin Post Webhooks for Bitbucket
* https://marketplace.atlassian.com/plugins/nl.topicus.bitbucket.bitbucket-webhooks/server/overview
* should be installed and configured
*
* @param payload
*/
private void processWebhookPayloadBitBucketServer(JSONObject payload) {
JSONObject repo = payload.getJSONObject("repository");
String user = getUser(payload, "actor");
String url = "";
if (repo.getJSONObject("links").getJSONArray("self").size() != 0) {
try {
URL pushHref = new URL(repo.getJSONObject("links").getJSONArray("self").getJSONObject(0).getString("href"));
url = pushHref.toString().replaceFirst(new String("projects.*"), new String(repo.getString("fullName").toLowerCase()));
String scm = repo.has("scmId") ? repo.getString("scmId") : "git";
probe.triggerMatchingJobs(user, url, scm, payload.toString());
} catch (MalformedURLException e) {
LOGGER.log(Level.WARNING, String.format("URL %s is malformed", url), e);
}
}
}

/*
{
"canon_url": "https://bitbucket.org",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.servlet.http.HttpServletRequest;

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

import org.junit.Before;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void testProcessWebhookPayload() {

JSONObject payload = new JSONObject()
.element("actor", new JSONObject()
.element("username", user))
.element("nickname", user))
.element("repository", new JSONObject()
.element("links", new JSONObject()
.element("html", new JSONObject()
Expand All @@ -60,6 +61,32 @@ public void testProcessWebhookPayload() {
verify(probe).triggerMatchingJobs(user, url, "hg", hgLoad.toString());
}

@Test
public void processWebhookPayloadBitBucketServer() {
when(request.getHeader("user-agent")).thenReturn("Apache-HttpClient/4.5.1 (Java/1.8.0_102)");
when(request.getHeader("x-event-key")).thenReturn("repo:push");

String user = "test_user";
String url = "https://bitbucket.org/ce/test_repo";

JSONObject href = new JSONObject();
href.element("href", "https://bitbucket.org/projects/CE/repos/test_repo/browse");

// Set actor and repository so that payload processor will parse as Bitbucket Server Post Webhook payload
JSONObject payload = new JSONObject()
.element("actor", new JSONObject()
.element("nickname", user))
.element("repository", new JSONObject()
.element("links", new JSONObject()
.element("self", new JSONArray()
.element(href)))
.element("fullName", "CE/test_repo"));

payloadProcessor.processPayload(payload, request);

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

@Test
public void testProcessPostServicePayload() {
// Ensure header isn't set so that payload processor will parse as old POST service payload
Expand All @@ -77,4 +104,38 @@ public void testProcessPostServicePayload() {
verify(probe).triggerMatchingJobs("old_user", "https://staging.bitbucket.org/old_user/old_repo", "git", payload.toString());
}


@Test
public void testProcessWebhookPayload_inCaseOwnerUsernameFieldIsReplacedByNickName() {
// Set headers so that payload processor will parse as new Webhook payload
when(request.getHeader("user-agent")).thenReturn("Bitbucket-Webhooks/2.0");
when(request.getHeader("x-event-key")).thenReturn("repo:push");

String user = "test_user";
String url = "https://bitbucket.org/test_user/test_repo";

JSONObject payload = new JSONObject()
.element("actor", new JSONObject()
.element("nickname", user))
.element("repository", new JSONObject()
.element("links", new JSONObject()
.element("html", new JSONObject()
.element("href", url))));

JSONObject hgLoad = new JSONObject()
.element("scm", "hg")
.element("owner", new JSONObject()
.element("nickname", user))
.element("links", new JSONObject()
.element("html", new JSONObject()
.element("href", url)));

payloadProcessor.processPayload(payload, request);

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

payloadProcessor.processPayload(hgLoad, request);

verify(probe).triggerMatchingJobs(user, url, "hg", hgLoad.toString());
}
}