Skip to content

Commit

Permalink
[FIXED JENKINS-41246] Guard against PRs from deleted forks
Browse files Browse the repository at this point in the history
Cherry-pick forward of a0c20fb
  • Loading branch information
stephenc committed Jul 4, 2017
1 parent 9ec1b73 commit 2c8d31f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Expand Up @@ -821,7 +821,7 @@ protected final void retrieve(@CheckForNull SCMSourceCriteria criteria,
Connector.checkConnectionValidity(apiUri, listener, credentials, github);

// Input data validation
if (repository == null || repository.isEmpty()) {
if (StringUtils.isBlank(repository)) {
throw new AbortException("No repository selected, skipping");
}

Expand Down Expand Up @@ -898,12 +898,6 @@ public SCMSourceCriteria.Probe create(@NonNull BranchSCMHead head,
} else {
branchName = "PR-" + number + "-" + strategy.name().toLowerCase(Locale.ENGLISH);
}
// TODO move trusted decision to traits
final boolean trusted = collaboratorNames != null
&& collaboratorNames.contains(pr.getHead().getRepository().getOwnerName());
if (!trusted) {
listener.getLogger().format(" (not from a trusted source)%n");
}
count++;
if (request.process(new PullRequestSCMHead(
pr, branchName, strategy == ChangeRequestCheckoutStrategy.MERGE
Expand All @@ -915,6 +909,10 @@ public SCMSourceCriteria.Probe create(@NonNull BranchSCMHead head,
public SCMSourceCriteria.Probe create(@NonNull PullRequestSCMHead head,
@Nullable Void revisionInfo)
throws IOException, InterruptedException {
boolean trusted = request.isTrusted(head);
if (!trusted) {
listener.getLogger().format(" (not from a trusted source)%n");
}
return GitHubSCMSource.this
.createProbe(trusted ? head : head.getTarget(), null);
}
Expand Down Expand Up @@ -1121,9 +1119,11 @@ PullRequestSource retrievePullRequestSource(int number) {
LOGGER.log(Level.INFO, "Got remote pull requests from {0}", fullName);
int n = 0;
for (GHPullRequest pr: ghRepository.queryPullRequests().state(GHIssueState.OPEN).list()) {
GHRepository repository = pr.getHead().getRepository();
// JENKINS-41246 repository may be null for deleted forks
pullRequestSourceMap.put(pr.getNumber(), new PullRequestSource(
pr.getHead().getRepository().getOwnerName(),
pr.getHead().getRepository().getName(),
repository == null ? null : repository.getOwnerName(),
repository == null ? null : repository.getName(),
pr.getHead().getRef()));
n++;
if (n % 30 == 0) { // default page size is 30
Expand Down
Expand Up @@ -39,6 +39,7 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;

/**
* Head corresponding to a pull request.
Expand Down Expand Up @@ -82,8 +83,9 @@ public class PullRequestSCMHead extends SCMHead implements ChangeRequestSCMHead2
this.number = pr.getNumber();
this.target = new BranchSCMHead(pr.getBase().getRef());
// the source stuff is immutable for a pull request on github, so safe to store here
this.sourceOwner = pr.getHead().getRepository().getOwnerName();
this.sourceRepo = pr.getHead().getRepository().getName();
GHRepository repository = pr.getHead().getRepository(); // may be null for deleted forks JENKINS-41246
this.sourceOwner = repository == null ? null : repository.getOwnerName();
this.sourceRepo = repository == null ? null : repository.getName();
this.sourceBranch = pr.getHead().getRef();
this.origin = pr.getRepository().getOwnerName().equalsIgnoreCase(sourceOwner)
? SCMHeadOrigin.DEFAULT
Expand Down

0 comments on commit 2c8d31f

Please sign in to comment.