Skip to content

Commit

Permalink
[FIXED JENKINS-40876] Retrieve the ObjectMetadataAction instances for…
Browse files Browse the repository at this point in the history
… heads
  • Loading branch information
stephenc committed Jan 12, 2017
1 parent 0f11bca commit e1f132e
Showing 1 changed file with 33 additions and 0 deletions.
Expand Up @@ -64,7 +64,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLHandshakeException;
Expand Down Expand Up @@ -160,6 +162,12 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
@CheckForNull
private transient Set<String> collaboratorNames;

/**
* The cache of {@link ObjectMetadataAction} instances for each open PR.
*/
@NonNull
private transient /*effectively final*/ Map<Integer,ObjectMetadataAction> pullRequestMetadataCache;

@DataBoundConstructor
public GitHubSCMSource(String id, String apiUri, String checkoutCredentialsId, String scanCredentialsId, String repoOwner, String repository) {
super(id);
Expand All @@ -168,6 +176,7 @@ public GitHubSCMSource(String id, String apiUri, String checkoutCredentialsId, S
this.repository = repository;
this.scanCredentialsId = Util.fixEmpty(scanCredentialsId);
this.checkoutCredentialsId = checkoutCredentialsId;
pullRequestMetadataCache = new ConcurrentHashMap<>();
}

/** Use defaults for old settings. */
Expand All @@ -191,6 +200,9 @@ private Object readResolve() {
if (buildForkPRHead == null) {
buildForkPRHead = DescriptorImpl.defaultBuildForkPRHead;
}
if (pullRequestMetadataCache == null) {
pullRequestMetadataCache = new ConcurrentHashMap<>();
}
return this;
}

Expand Down Expand Up @@ -500,6 +512,7 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
// we use a paged iterable so that if the observer is finished observing we stop making API calls
pullRequests = repo.queryPullRequests().state(GHIssueState.OPEN).list();
}
Set<Integer> pullRequestMetadataKeys = new HashSet<>();
for (GHPullRequest ghPullRequest : pullRequests) {
checkInterrupt();
int number = ghPullRequest.getNumber();
Expand Down Expand Up @@ -568,6 +581,14 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
branchName += "-head";
}
}
pullRequestMetadataCache.put(number,
new ObjectMetadataAction(
ghPullRequest.getTitle(),
ghPullRequest.getBody(),
ghPullRequest.getHtmlUrl().toExternalForm()
)
);
pullRequestMetadataKeys.add(number);
PullRequestSCMHead head = new PullRequestSCMHead(ghPullRequest, branchName, merge);
if (includes != null && !includes.contains(head)) {
// don't waste rate limit testing a head we are not interested in
Expand Down Expand Up @@ -610,6 +631,10 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
pullrequests++;
}
listener.getLogger().format("%n %d pull requests were processed%n", pullrequests);
if (includes == null) {
// we did a full scan, so trim the cache entries
this.pullRequestMetadataCache.keySet().retainAll(pullRequestMetadataKeys);
}
}

if (wantBranches && (buildOriginBranch || buildOriginBranchWithPR)) {
Expand Down Expand Up @@ -941,14 +966,22 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
GitHubLink repoLink = ((Actionable) owner).getAction(GitHubLink.class);
if (repoLink != null) {
String url;
ObjectMetadataAction metadataAction = null;
if (head instanceof PullRequestSCMHead) {
// pull request to this repository
url = repoLink.getUrl() + "/pull/" + ((PullRequestSCMHead) head).getNumber();
metadataAction = pullRequestMetadataCache.get(((PullRequestSCMHead) head).getNumber());
if (metadataAction == null) {
// best effort
metadataAction = new ObjectMetadataAction(null, null, url);
}
} else {
// branch in this repository
url = repoLink.getUrl() + "/tree/" + head.getName();
metadataAction = new ObjectMetadataAction(head.getName(), null, url);
}
result.add(new GitHubLink("icon-github-branch", url));
result.add(metadataAction);
}
if (head instanceof BranchSCMHead) {
for (GitHubDefaultBranch p : ((Actionable) owner).getActions(GitHubDefaultBranch.class)) {
Expand Down

0 comments on commit e1f132e

Please sign in to comment.