Skip to content
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 @@ -111,10 +111,8 @@ public interface BitbucketApi {

/**
* Register a webhook on the repository.
*
* @param hook the webhook object
*/
void registerCommitWebHook(BitbucketWebHook hook);
void registerCommitWebHook();

/**
* Remove the webhook (ID field required) from the repository.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import java.net.Proxy;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.cloudbees.jenkins.plugins.bitbucket.hooks.BitbucketSCMSourcePushHookReceiver;
import com.cloudbees.jenkins.plugins.bitbucket.hooks.HookEventType;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
Expand Down Expand Up @@ -270,7 +273,7 @@ public BitbucketCommit resolveCommit(String hash) {
@Override
@CheckForNull
public String resolveSourceFullHash(BitbucketPullRequest pull) {
String response = getRequest(V2_API_BASE_URL + pull.getSource().getRepository().getOwnerName() + "/" +
String response = getRequest(V2_API_BASE_URL + pull.getSource().getRepository().getOwnerName() + "/" +
pull.getSource().getRepository().getRepositoryName() + "/commit/" + pull.getSource().getCommit().getHash());
try {
return parse(response, BitbucketCloudCommit.class).getHash();
Expand All @@ -282,14 +285,24 @@ public String resolveSourceFullHash(BitbucketPullRequest pull) {

/** {@inheritDoc} */
@Override
public void registerCommitWebHook(BitbucketWebHook hook) {
public void registerCommitWebHook() {
try {
postRequest(V2_API_BASE_URL + owner + "/" + repositoryName + "/hooks", asJson(hook));
postRequest(V2_API_BASE_URL + owner + "/" + repositoryName + "/hooks", asJson(getHook()));
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "cannot register webhook", e);
}
}

private BitbucketWebHook getHook() {
BitbucketRepositoryHook hooks = new BitbucketRepositoryHook();
hooks.setActive(true);
hooks.setDescription("Jenkins hooks");
hooks.setUrl(Jenkins.getActiveInstance().getRootUrl() + BitbucketSCMSourcePushHookReceiver.FULL_PATH);
hooks.setEvents(Arrays.asList(HookEventType.PUSH.getKey(),
HookEventType.PULL_REQUEST_CREATED.getKey(), HookEventType.PULL_REQUEST_UPDATED.getKey()));
return hooks;
}

/** {@inheritDoc} */
@Override
public void removeCommitWebHook(BitbucketWebHook hook) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void doRun() {

// synchronized just to avoid duplicated webhooks in case SCMSourceOwner is updated repeteadly and quickly
private synchronized void registerHooks(SCMSourceOwner owner) {
List<BitbucketSCMSource> sources = getBitucketSCMSources(owner);
List<BitbucketSCMSource> sources = getBitbucketSCMSources(owner);
for (BitbucketSCMSource source : sources) {
if (source.isAutoRegisterHook()) {
BitbucketApi bitbucket = source.buildBitbucketClient();
Expand All @@ -123,7 +123,7 @@ private synchronized void registerHooks(SCMSourceOwner owner) {
String rootUrl = Jenkins.getActiveInstance().getRootUrl();
if (rootUrl != null && !rootUrl.startsWith("http://localhost")) {
LOGGER.info(String.format("Registering hook for %s/%s", source.getRepoOwner(), source.getRepository()));
bitbucket.registerCommitWebHook(getHook());
bitbucket.registerCommitWebHook();
} else {
LOGGER.warning(String.format("Can not register hook. Jenkins root URL is not valid: %s", rootUrl));
}
Expand All @@ -133,7 +133,7 @@ private synchronized void registerHooks(SCMSourceOwner owner) {
}

private void removeHooks(SCMSourceOwner owner) {
List<BitbucketSCMSource> sources = getBitucketSCMSources(owner);
List<BitbucketSCMSource> sources = getBitbucketSCMSources(owner);
for (BitbucketSCMSource source : sources) {
if (source.isAutoRegisterHook()) {
BitbucketApi bitbucket = source.buildBitbucketClient();
Expand All @@ -150,7 +150,7 @@ private void removeHooks(SCMSourceOwner owner) {
LOGGER.info(String.format("Removing hook for %s/%s", source.getRepoOwner(), source.getRepository()));
bitbucket.removeCommitWebHook(hook);
} else {
LOGGER.log(Level.FINE, String.format("NOT removing hook for %s/%s because does not exists or its used in other project",
LOGGER.log(Level.FINE, String.format("NOT removing hook for %s/%s because does not exists or its used in other project",
source.getRepoOwner(), source.getRepository()));
}
}
Expand All @@ -173,7 +173,7 @@ private boolean isUsedSomewhereElse(SCMSourceOwner owner, String repoOwner, Stri
return false;
}

private List<BitbucketSCMSource> getBitucketSCMSources(SCMSourceOwner owner) {
private List<BitbucketSCMSource> getBitbucketSCMSources(SCMSourceOwner owner) {
List<BitbucketSCMSource> sources = new ArrayList<BitbucketSCMSource>();
for (SCMSource source : owner.getSCMSources()) {
if (source instanceof BitbucketSCMSource) {
Expand All @@ -183,17 +183,6 @@ private List<BitbucketSCMSource> getBitucketSCMSources(SCMSourceOwner owner) {
return sources;
}

private BitbucketWebHook getHook() {
// TODO: generalize this for BB server
BitbucketRepositoryHook hooks = new BitbucketRepositoryHook();
hooks.setActive(true);
hooks.setDescription("Jenkins hooks");
hooks.setUrl(Jenkins.getActiveInstance().getRootUrl() + BitbucketSCMSourcePushHookReceiver.FULL_PATH);
hooks.setEvents(Arrays.asList(HookEventType.PUSH.getKey(),
HookEventType.PULL_REQUEST_CREATED.getKey(), HookEventType.PULL_REQUEST_UPDATED.getKey()));
return hooks;
}

/**
* We need a single thread executor to run webhooks operations in background but in order.
* Registrations and removals need to be done in the same order than they were called by the item listener.
Expand Down
Loading