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
4 changes: 3 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
buildPlugin()
#!groovy

buildPlugin(checkstyle: [run:true, archive:true, unstableTotalAll: '1'])
10 changes: 10 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="NewlineAtEndOfFile" />
<module name="TreeWalker">
<module name="ImportOrder">
<property name="option" value="bottom" />
</module>
</module>
</module>
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@

<build>
<plugins>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<configLocation>${basedir}/checkstyle.xml</configLocation>
<includeResources>false</includeResources>
<excludes>**/FormTagLib.java</excludes>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMSourceObserver;
import jenkins.scm.api.trait.SCMNavigatorRequest;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMSourceObserver;
import jenkins.scm.api.trait.SCMNavigatorRequest;

/**
* The {@link SCMNavigatorRequest} for bitbucket.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,21 +674,24 @@ class Skip extends IOException {
strategy);
}
if (request.process(head, //
new SCMSourceRequest.IntermediateLambda<String>() {
new SCMSourceRequest.IntermediateLambda<BitbucketCommit>() {
@Nullable
@Override
public String create() throws IOException, InterruptedException {
return pull.getSource().getCommit().getHash();
public BitbucketCommit create() throws IOException, InterruptedException {
// use branch instead of commit to postpone closure initialisation
return new BranchHeadCommit(pull.getSource().getBranch());
}
}, //
new BitbucketProbeFactory<String>(pullBitbucket, request), //
new BitbucketRevisionFactory<String>(pullBitbucket) {
new BitbucketProbeFactory<BitbucketCommit>(pullBitbucket, request), //
new BitbucketRevisionFactory<BitbucketCommit>(pullBitbucket) {
@NonNull
@Override
public SCMRevision create(@NonNull SCMHead head, @Nullable String hash)
public SCMRevision create(@NonNull SCMHead head, @Nullable BitbucketCommit sourceCommit)
throws IOException, InterruptedException {
try {
return super.create(head, hash, pull.getDestination().getCommit().getHash());
// use branch instead of commit to postpone closure initialisation
BranchHeadCommit targetCommit = new BranchHeadCommit(pull.getDestination().getBranch());
return super.create(head, sourceCommit, targetCommit);
} catch (BitbucketRequestException e) {
if (originBitbucket instanceof BitbucketCloudApiClient) {
if (e.getHttpCode() == 403) {
Expand Down Expand Up @@ -897,9 +900,10 @@ private BitbucketCommit findCommit(String branchName, List<? extends BitbucketBr
private BitbucketCommit findPRCommit(String prId, List<? extends BitbucketPullRequest> pullRequests, TaskListener listener) {
for (BitbucketPullRequest pr : pullRequests) {
if (prId.equals(pr.getId())) {
BitbucketCommit commit = pr.getSource().getCommit();
String revision = commit.getHash();
if (revision == null) {
// if I use getCommit() the branch closure is trigger immediately
BitbucketBranch branch = pr.getSource().getBranch();
String hash = branch.getRawNode();
if (hash == null) {
if (BitbucketCloudEndpoint.SERVER_URL.equals(getServerUrl())) {
listener.getLogger().format("Cannot resolve the hash of the revision in PR-%s%n",
prId);
Expand All @@ -910,7 +914,7 @@ private BitbucketCommit findPRCommit(String prId, List<? extends BitbucketPullRe
}
return null;
}
return commit;
return new BranchHeadCommit(branch);
}
}
listener.getLogger().format("Cannot find the PR-%s%n", prId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParsePosition;
import java.util.Date;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.common.base.Charsets;

/**
* Jackson based JSON parser
*/
Expand Down Expand Up @@ -115,4 +113,4 @@ private static ObjectMapper createObjectMapper(){
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ public interface BitbucketCommit {
*/
long getDateMillis();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public interface BitbucketPullRequestEvent {
*/
BitbucketRepository getRepository();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public interface BitbucketPullRequestSource {
*/
BitbucketCommit getCommit();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public interface BitbucketRepositoryOwner {
*/
String getDisplayName();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ public interface BitbucketWebHook {
*/
String getUuid();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMFile;

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -112,7 +108,29 @@
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;

public class BitbucketCloudApiClient implements BitbucketApi {

/**
* Make available commit informations in a lazy way.
*
* @author Nikolas Falco
*/
private class CommitClosure implements Callable<BitbucketCommit> {
private final String hash;

public CommitClosure(@NonNull String hash) {
this.hash = hash;
}

@Override
public BitbucketCommit call() throws Exception {
return resolveCommit(hash);
}
}

private static final Logger LOGGER = Logger.getLogger(BitbucketCloudApiClient.class.getName());
private static final HttpHost API_HOST = HttpHost.create("https://api.bitbucket.org");
private static final String V2_API_BASE_URL = "https://api.bitbucket.org/2.0/repositories";
Expand Down Expand Up @@ -253,38 +271,44 @@ public String getRepositoryUri(@NonNull BitbucketRepositoryType type,
@Override
public List<BitbucketPullRequestValue> getPullRequests() throws InterruptedException, IOException {
List<BitbucketPullRequestValue> pullRequests = new ArrayList<BitbucketPullRequestValue>();
int pageNumber = 1;

UriTemplate template = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/pullrequests{?page,pagelen}")
.set("owner", owner)
.set("repo", repositoryName)
.set("page", pageNumber)
.set("pagelen", 50);
String url = template.expand();

String response = getRequest(url);
BitbucketPullRequests page;
try {
page = JsonParser.toJava(response, BitbucketPullRequests.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
pullRequests.addAll(page.getValues());
while (page.getNext() != null) {
int pageNumber = 1;
do {
if (Thread.interrupted()) {
throw new InterruptedException();
}
pageNumber++;
response = getRequest(url = template.set("page", pageNumber).expand());
String url = template //
.set("page", pageNumber++) //
.expand();
String response = getRequest(url);
try {
page = JsonParser.toJava(response, BitbucketPullRequests.class);
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
pullRequests.addAll(page.getValues());
} while (page.getNext() != null);

for (BitbucketPullRequestValue pullRequest : pullRequests) {
setupClosureForPRBranch(pullRequest);
}

return pullRequests;
}

private void setupClosureForPRBranch(BitbucketPullRequestValue pullRequest) {
BitbucketCloudBranch branch = pullRequest.getSource().getBranch();
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
branch = pullRequest.getDestination().getBranch();
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
}

@Deprecated
@CheckForNull
public String getLogin() {
Expand All @@ -307,7 +331,9 @@ public BitbucketPullRequest getPullRequestById(@NonNull Integer id) throws IOExc
.expand();
String response = getRequest(url);
try {
return JsonParser.toJava(response, BitbucketPullRequestValue.class);
BitbucketPullRequestValue pr = JsonParser.toJava(response, BitbucketPullRequestValue.class);
setupClosureForPRBranch(pr);
return pr;
} catch (IOException e) {
throw new IOException("I/O error when parsing response from URL: " + url, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket.client;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -34,6 +32,8 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

public class Cache<K, V> {

private static final int MAX_ENTRIES_DEFAULT = 100;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cloudbees.jenkins.plugins.bitbucket.client;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

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

public class ClosingConnectionInputStream extends InputStream {

private final CloseableHttpResponse response;
Expand Down Expand Up @@ -74,4 +73,4 @@ public synchronized void reset() throws IOException {
public long skip(final long n) throws IOException {
return delegate.skip(n);
}
}
}
Loading