Skip to content

Commit

Permalink
[JENKINS-34395] Update to 1.89 of github-api and guard against 404 in…
Browse files Browse the repository at this point in the history
… repos without any tags
  • Loading branch information
stephenc committed Sep 25, 2017
1 parent 51fc6ef commit 4d47488
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.87-20170901.115941-1</version>
<version>1.89</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -65,6 +65,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -112,6 +113,7 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHOrganization;
Expand Down Expand Up @@ -2000,8 +2002,8 @@ public void observe(GHPullRequest pr) {
pr.getHtmlUrl().toExternalForm()
)
);
GHUser user = pr.getUser();
try {
GHUser user = pr.getUser();
if (users.containsKey(user.getLogin())) {
// looked up this user already
user = users.get(user.getLogin());
Expand Down Expand Up @@ -2083,7 +2085,58 @@ protected Iterable<GHRef> create() {
return Collections.singletonList(repo.getRef("tags/" + tagName));
}
request.listener().getLogger().format("%n Getting remote tags...%n");
return repo.listRefs("tags");
// GitHub will give a 404 if the repository does not have any tags
// we could rework the code that iterates to expect the 404, but that
// would mean leaking the strange behaviour in every trait that consults the list
// of tags. (And GitHub API is probably correct in throwing the GHFileNotFoundException
// from a PagedIterable, so we don't want to fix that)
//
// Instead we just return a wrapped iterator that does the right thing.
final Iterable<GHRef> iterable = repo.listRefs("tags");
return new Iterable<GHRef>() {
@Override
public Iterator<GHRef> iterator() {
final Iterator<GHRef> iterator;
try {
iterator = iterable.iterator();
} catch (Error e) {
if (e.getCause() instanceof GHFileNotFoundException) {
return Collections.<GHRef>emptyList().iterator();
}
throw e;
}
return new Iterator<GHRef>() {
@Override
public boolean hasNext() {
try {
return iterator.hasNext();
} catch (Error e) {
if (e.getCause() instanceof GHFileNotFoundException) {
return false;
}
throw e;
}
}

@Override
public GHRef next() {
try {
return iterator.next();
} catch (Error e) {
if (e.getCause() instanceof GHFileNotFoundException) {
throw new NoSuchElementException();
}
throw e;
}
}

@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
}
};
} catch (IOException | InterruptedException e) {
throw new GitHubSCMSource.WrappedException(e);
}
Expand Down
Expand Up @@ -26,6 +26,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.InvisibleAction;
import java.io.IOException;
import java.net.URL;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.actions.ChangeRequestAction;
Expand All @@ -47,7 +48,7 @@ final class PullRequestAction extends InvisibleAction {
private final String userLogin;
private final String baseRef;

PullRequestAction(GHPullRequest pr) {
PullRequestAction(GHPullRequest pr) throws IOException {
number = pr.getNumber();
url = pr.getHtmlUrl();
title = pr.getTitle();
Expand Down
Expand Up @@ -60,6 +60,7 @@
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;

import static com.google.common.collect.Sets.immutableEnumSet;
Expand Down Expand Up @@ -266,7 +267,14 @@ && isApiMatch(((GitHubSCMSource) source).getApiUri())
// fake repository name
return Collections.emptyMap();
}
String prOwnerName = ghPullRequest.getHead().getUser().getLogin();
GHUser user;
try {
user = ghPullRequest.getHead().getUser();
} catch (IOException e) {
// fake owner name
return Collections.emptyMap();
}
String prOwnerName = user.getLogin();
if (!prOwnerName.matches(GitHubSCMSource.VALID_GITHUB_USER_NAME)) {
// fake owner name
return Collections.emptyMap();
Expand Down

0 comments on commit 4d47488

Please sign in to comment.