Skip to content

Commit

Permalink
[JENKINS-43433] Adapt to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jun 16, 2017
1 parent f990e2e commit d8fc1a8
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 33 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Expand Up @@ -8,7 +8,7 @@
<relativePath />
</parent>
<artifactId>github-branch-source</artifactId>
<version>2.0.7-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>GitHub Branch Source Plugin</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Branch+Source+Plugin</url>
Expand All @@ -23,7 +23,7 @@
<properties>
<jenkins.version>1.625.3</jenkins.version>
<workflow.version>1.14.2</workflow.version>
<scm-api.version>2.1.1</scm-api.version>
<scm-api.version>2.2.0-20170410.162230-3</scm-api.version>
</properties>

<scm>
Expand All @@ -41,7 +41,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.6.4</version>
<version>3.2.0</version>
<exclusions>
<!-- To avoid ClassNotFoundException during InjectedTest -->
<exclusion>
Expand Down Expand Up @@ -153,7 +153,7 @@
</execution>
</executions>
<configuration>
<compatibleSinceVersion>2.0.0</compatibleSinceVersion>
<compatibleSinceVersion>2.1.0</compatibleSinceVersion>
</configuration>
</plugin>
</plugins>
Expand Down
Expand Up @@ -56,13 +56,15 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.LogTaskListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -75,6 +77,7 @@
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
Expand Down Expand Up @@ -135,6 +138,13 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
*/
private static /*mostly final*/ int eventDelaySeconds =
Math.min(300, Math.max(0, Integer.getInteger(GitHubSCMSource.class.getName() + ".eventDelaySeconds", 5)));
/**
* Lock to guard access to the {@link #pullRequestSourceMap} field and prevent concurrent GitHub queries during
* a 1.x to 2.1.0+ upgrade.
*
* @since 2.1.0
*/
private static final Object pullRequestSourceMapLock = new Object();

private final String apiUri;

Expand Down Expand Up @@ -199,6 +209,17 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
@NonNull
private transient /*effectively final*/ Map<Integer,ContributorMetadataAction> pullRequestContributorCache;

/**
* Used during upgrade from 1.x to 2.1.0+ only.
*
* @see #retrievePullRequestSource(int)
* @see PullRequestSCMHead.FixMetadata
* @see PullRequestSCMHead.FixMetadataMigration
* @since 2.1.0
*/
@CheckForNull // normally null except during a migration from 1.x
private transient /*effectively final*/ Map<Integer,PullRequestSource> pullRequestSourceMap;

@DataBoundConstructor
public GitHubSCMSource(String id, String apiUri, String checkoutCredentialsId, String scanCredentialsId, String repoOwner, String repository) {
super(id);
Expand Down Expand Up @@ -722,6 +743,12 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
// we did a full scan, so trim the cache entries
this.pullRequestMetadataCache.keySet().retainAll(pullRequestMetadataKeys);
this.pullRequestContributorCache.keySet().retainAll(pullRequestMetadataKeys);
if (Jenkins.getActiveInstance().getInitLevel().compareTo(InitMilestone.JOB_LOADED) > 0) {
// synchronization should be cheap as only writers would be looking for this just to write null
synchronized (pullRequestSourceMapLock) {
this.pullRequestSourceMap = null; // all data has to have been migrated
}
}
}
}

Expand Down Expand Up @@ -974,6 +1001,51 @@ private String repositoryUrl(String owner, String repo) {
return null;
}

@Deprecated // TODO remove once migration from 1.x is no longer supported
PullRequestSource retrievePullRequestSource(int number) {
// we use a big honking great lock to prevent concurrent requests to github during job loading
Map<Integer, PullRequestSource> pullRequestSourceMap;
synchronized (pullRequestSourceMapLock) {
pullRequestSourceMap = this.pullRequestSourceMap;
if (pullRequestSourceMap == null) {
this.pullRequestSourceMap = pullRequestSourceMap = new HashMap<>();
if (repository != null && !repository.isEmpty()) {
String fullName = repoOwner + "/" + repository;
LOGGER.log(Level.INFO, "Getting remote pull requests from {0}", fullName);
StandardCredentials credentials =
Connector.lookupScanCredentials((Item) getOwner(), apiUri, scanCredentialsId);
LogTaskListener listener = new LogTaskListener(LOGGER, Level.INFO);
try {
GitHub github = Connector.connect(apiUri, credentials);
try {
checkApiUrlValidity(github);
Connector.checkApiRateLimit(listener, github);
ghRepository = github.getRepository(fullName);
LOGGER.log(Level.INFO, "Got remote pull requests from {0}", fullName);
int n = 0;
for (GHPullRequest pr: ghRepository.queryPullRequests().state(GHIssueState.OPEN).list()) {
pullRequestSourceMap.put(pr.getNumber(), new PullRequestSource(
pr.getHead().getRepository().getOwnerName(),
pr.getHead().getRepository().getName(),
pr.getHead().getRef()));
n++;
if (n % 30 == 0) { // default page size is 30
Connector.checkApiRateLimit(listener, github);
}
}
} finally {
Connector.release(github);
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING,
"Could not get all pull requests from " + fullName + ", there may be rebuilds", e);
}
}
}
return pullRequestSourceMap.get(number);
}
}

/**
* Similar to {@link PreBuildMerge}, but we cannot use that unmodified: we need to specify the exact base branch hash.
* It is possible to just ask Git to check out {@code refs/pull/123/merge}, but this has two problems:
Expand Down

0 comments on commit d8fc1a8

Please sign in to comment.