Skip to content

Commit

Permalink
[JENKINS-41453] sourceOwner != pull request user and add ContributorM…
Browse files Browse the repository at this point in the history
…etadataAction for PRs
  • Loading branch information
stephenc committed Jan 26, 2017
1 parent 1e6f8f6 commit e05ea33
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -75,6 +75,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLHandshakeException;
import jenkins.management.ConfigureLink;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
Expand All @@ -86,6 +87,7 @@
import jenkins.scm.api.SCMSourceDescriptor;
import jenkins.scm.api.SCMSourceEvent;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.api.metadata.PrimaryInstanceMetadataAction;
import jenkins.scm.impl.ChangeRequestSCMHeadCategory;
Expand Down Expand Up @@ -181,6 +183,11 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
*/
@NonNull
private transient /*effectively final*/ Map<Integer,ObjectMetadataAction> pullRequestMetadataCache;
/**
* The cache of {@link ObjectMetadataAction} instances for each open PR.
*/
@NonNull
private transient /*effectively final*/ Map<Integer,ContributorMetadataAction> pullRequestContributorCache;

@DataBoundConstructor
public GitHubSCMSource(String id, String apiUri, String checkoutCredentialsId, String scanCredentialsId, String repoOwner, String repository) {
Expand Down Expand Up @@ -217,6 +224,9 @@ private Object readResolve() {
if (pullRequestMetadataCache == null) {
pullRequestMetadataCache = new ConcurrentHashMap<>();
}
if (pullRequestContributorCache == null) {
pullRequestContributorCache = new ConcurrentHashMap<>();
}
return this;
}

Expand Down Expand Up @@ -603,6 +613,12 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
ghPullRequest.getHtmlUrl().toExternalForm()
)
);
GHUser user = ghPullRequest.getUser();
pullRequestContributorCache.put(number, new ContributorMetadataAction(
user.getLogin(),
user.getName(),
user.getEmail()
));
pullRequestMetadataKeys.add(number);
PullRequestSCMHead head = new PullRequestSCMHead(ghPullRequest, branchName, merge);
if (includes != null && !includes.contains(head)) {
Expand Down Expand Up @@ -649,6 +665,7 @@ private void doRetrieve(SCMSourceCriteria criteria, SCMHeadObserver observer, Ta
if (includes == null) {
// we did a full scan, so trim the cache entries
this.pullRequestMetadataCache.keySet().retainAll(pullRequestMetadataKeys);
this.pullRequestContributorCache.keySet().retainAll(pullRequestMetadataKeys);
}
}

Expand Down Expand Up @@ -1078,12 +1095,17 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
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());
int number = ((PullRequestSCMHead) head).getNumber();
url = repoLink.getUrl() + "/pull/" + number;
metadataAction = pullRequestMetadataCache.get(number);
if (metadataAction == null) {
// best effort
metadataAction = new ObjectMetadataAction(null, null, url);
}
ContributorMetadataAction contributor = pullRequestContributorCache.get(number);
if (contributor != null) {
result.add(contributor);
}
} else {
// branch in this repository
url = repoLink.getUrl() + "/tree/" + head.getName();
Expand Down
Expand Up @@ -106,7 +106,7 @@ private Object readResolve() {
merge,
metadata.getNumber(),
new BranchSCMHead(metadata.getBaseRef()),
metadata.getUserLogin(),
null,
null,
null
);
Expand Down

0 comments on commit e05ea33

Please sign in to comment.