Skip to content

Commit

Permalink
Merge pull request #113 from stephenc/jenkins-41453-follow-up
Browse files Browse the repository at this point in the history
[JENKINS-41453] sourceOwner != pull request user and add ContributorMetadataAction for PRs
  • Loading branch information
stephenc committed Jan 26, 2017
2 parents 1e6f8f6 + df17212 commit d775b67
Show file tree
Hide file tree
Showing 4 changed files with 92 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 All @@ -191,6 +198,7 @@ public GitHubSCMSource(String id, String apiUri, String checkoutCredentialsId, S
this.scanCredentialsId = Util.fixEmpty(scanCredentialsId);
this.checkoutCredentialsId = checkoutCredentialsId;
pullRequestMetadataCache = new ConcurrentHashMap<>();
pullRequestContributorCache = new ConcurrentHashMap<>();
}

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

Expand Down Expand Up @@ -603,6 +614,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 +666,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 +1096,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
32 changes: 32 additions & 0 deletions src/test/resources/api/__files/body-users-stephenc-JRfTU.json
@@ -0,0 +1,32 @@
{
"login": "stephenc",
"id": 209336,
"avatar_url": "https://avatars.githubusercontent.com/u/209336?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/stephenc",
"html_url": "https://github.com/stephenc",
"followers_url": "https://api.github.com/users/stephenc/followers",
"following_url": "https://api.github.com/users/stephenc/following{/other_user}",
"gists_url": "https://api.github.com/users/stephenc/gists{/gist_id}",
"starred_url": "https://api.github.com/users/stephenc/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/stephenc/subscriptions",
"organizations_url": "https://api.github.com/users/stephenc/orgs",
"repos_url": "https://api.github.com/users/stephenc/repos",
"events_url": "https://api.github.com/users/stephenc/events{/privacy}",
"received_events_url": "https://api.github.com/users/stephenc/received_events",
"type": "User",
"site_admin": false,
"name": "Stephen Connolly",
"company": "CloudBees Inc.",
"blog": "http://javaadventure.blogspot.com/",
"location": "Dublin, Ireland",
"email": null,
"hireable": null,
"bio": null,
"public_repos": 139,
"public_gists": 10,
"followers": 73,
"following": 13,
"created_at": "2010-02-23T17:38:28Z",
"updated_at": "2017-01-21T10:29:10Z"
}
34 changes: 34 additions & 0 deletions src/test/resources/api/mappings/mapping-users-stephenc-JRfTU.json
@@ -0,0 +1,34 @@
{
"request" : {
"url" : "/users/stephenc",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "body-users-stephenc-JRfTU.json",
"headers" : {
"Server" : "GitHub.com",
"Date" : "Tue, 06 Dec 2016 15:06:25 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Transfer-Encoding" : "chunked",
"Status" : "200 OK",
"X-RateLimit-Limit" : "60",
"X-RateLimit-Remaining" : "60",
"X-RateLimit-Reset" : "1481039662",
"Cache-Control" : "public, max-age=60, s-maxage=60",
"Vary" : [ "Accept", "Accept-Encoding" ],
"ETag" : "W/\"91482e12d1cde431b38e0238d74dfa7b\"",
"Last-Modified" : "Sat, 27 Feb 2016 07:57:23 GMT",
"X-GitHub-Media-Type" : "github.v3; format=json",
"Access-Control-Expose-Headers" : "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin" : "*",
"Content-Security-Policy" : "default-src 'none'",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options" : "nosniff",
"X-Frame-Options" : "deny",
"X-XSS-Protection" : "1; mode=block",
"X-Served-By" : "d256f86292c6dde5d09d15d926ec67a3",
"X-GitHub-Request-Id" : "B2A7FE77:629C:AF96C1F:5846D3F0"
}
}
}

0 comments on commit d775b67

Please sign in to comment.