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

Fixing a typo, adding message re 'Test connection' #61

Closed
wants to merge 17 commits into from
Closed
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
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>
5 changes: 3 additions & 2 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.9-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 Down Expand Up @@ -150,4 +151,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void doIndex(StaplerRequest req) throws IOException {

payloadProcessor.processPayload(payload, req);
} else {
LOGGER.log(Level.WARNING, "The Jenkins job cannot be triggered. You might no have configured correctly the WebHook on BitBucket with the last slash `http://<JENKINS-URL>/bitbucket-hook/`");
LOGGER.log(Level.WARNING, "The Jenkins job cannot be triggered. You might not have configured correctly the WebHook on BitBucket with the last slash `http://<JENKINS-URL>/bitbucket-hook/` or a 'Test connection' invocation of the hook was triggered.");
}

}
Expand Down
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,5 +1,7 @@
package com.cloudbees.jenkins.plugins;

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

Expand All @@ -25,6 +27,11 @@ 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 {
LOGGER.log(Level.INFO, "Processing old POST service payload");
processPostServicePayload(payload);
Expand Down Expand Up @@ -52,6 +59,29 @@ private void processWebhookPayload(JSONObject payload) {

}

/**
* 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 = payload.getJSONObject("actor").getString("username");
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 @@ -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("username", 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 Down