Skip to content

Commit

Permalink
[FIXED JENKINS-40875] Catch and diagnose permissions issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jan 12, 2017
1 parent 0f11bca commit fa3c8e7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 18 deletions.
Expand Up @@ -74,6 +74,8 @@
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import static org.jenkinsci.plugins.github_branch_source.GitHubSCMSource.GITHUB_URL;

public class GitHubSCMNavigator extends SCMNavigator {

private final String repoOwner;
Expand Down Expand Up @@ -243,7 +245,7 @@ public void setPattern(String pattern) {
@NonNull
@Override
protected String id() {
return StringUtils.defaultIfBlank(apiUri, GitHubSCMSource.GITHUB_URL) + "::" + repoOwner;
return StringUtils.defaultIfBlank(apiUri, GITHUB_URL) + "::" + repoOwner;
}

@Override
Expand All @@ -262,18 +264,18 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
String message = String.format("It seems %s is unreachable", apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
String message = String.format("It seems %s is unreachable", apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

// Input data validation
if (credentials != null && !github.isCredentialValid()) {
String message = String.format("Invalid scan credentials %s to connect to %s, skipping", CredentialsNameProvider.name(credentials), apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
String message = String.format("Invalid scan credentials %s to connect to %s, skipping", CredentialsNameProvider.name(credentials), apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

if (!github.isAnonymous()) {
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri, CredentialsNameProvider.name(credentials));
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri, CredentialsNameProvider.name(credentials));
GHMyself myself = null;
try {
// Requires an authenticated access
Expand All @@ -296,7 +298,7 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
return;
}
} else {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}

GHOrganization org = null;
Expand Down Expand Up @@ -360,19 +362,19 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
String message = String.format("It seems %s is unreachable", apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
String message = String.format("It seems %s is unreachable", apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

// Input data validation
if (credentials != null && !github.isCredentialValid()) {
String message = String.format("Invalid scan credentials %s to connect to %s, skipping",
CredentialsNameProvider.name(credentials), apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
CredentialsNameProvider.name(credentials), apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

if (!github.isAnonymous()) {
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri,
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri,
CredentialsNameProvider.name(credentials));
GHMyself myself = null;
try {
Expand All @@ -391,7 +393,7 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
}
} else {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n",
apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
apiUri == null ? GITHUB_URL : apiUri);
}

GHOrganization org = null;
Expand Down Expand Up @@ -469,8 +471,33 @@ public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
listener.getLogger().printf("Looking up details of %s...%n", getRepoOwner());
List<Action> result = new ArrayList<>();
StandardCredentials credentials = Connector.lookupScanCredentials(owner, getApiUri(), getScanCredentialsId());
GitHub hub = Connector.connect(getApiUri(), credentials);
GHUser u = hub.getUser(getRepoOwner());
GitHub hub = Connector.connect(apiUri, credentials);
try {
hub.checkApiUrlValidity();
} catch (HttpException e) {
listener.getLogger().format("It seems %s is unreachable%n",
apiUri == null ? GITHUB_URL : apiUri);
return result;
}
String credentialsName = credentials == null ? "anonymous access" : CredentialsNameProvider.name(credentials);
if (credentials != null && !hub.isCredentialValid()) {
throw new AbortException(String.format("Invalid scan credentials %s to connect to %s, skipping",
credentialsName, apiUri == null ? GITHUB_URL : apiUri));
}
if (!hub.isAnonymous()) {
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri,
credentialsName);
} else {
listener.getLogger()
.format("Connecting to %s using anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}
GHUser u = null;
try {
u = hub.getUser(getRepoOwner());
} catch (FileNotFoundException e) {
throw new AbortException(String.format("Invalid scan credentials when using %s to connect to %s on %s",
credentialsName, getRepoOwner(), apiUri == null ? GITHUB_URL : apiUri));
}
String objectUrl = u.getHtmlUrl() == null ? null : u.getHtmlUrl().toExternalForm();
result.add(new ObjectMetadataAction(
Util.fixEmpty(u.getName()),
Expand Down
Expand Up @@ -705,7 +705,23 @@ protected SCMProbe createProbe(@NonNull SCMHead head, @CheckForNull final SCMRev
// Github client and validation
GitHub github = Connector.connect(apiUri, credentials);
String fullName = repoOwner + "/" + repository;
final GHRepository repo = github.getRepository(fullName);
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
throw new IOException(String.format("It seems %s is unreachable", apiUri == null ? GITHUB_URL : apiUri), e);
}
String credentialsName = credentials == null ? "anonymous access" : CredentialsNameProvider.name(credentials);
if (credentials != null && !github.isCredentialValid()) {
throw new AbortException(String.format("Invalid scan credentials %s to connect to %s, skipping",
credentialsName, apiUri == null ? GITHUB_URL : apiUri));
}
GHRepository repo;
try {
repo = github.getRepository(fullName);
} catch (FileNotFoundException e) {
throw new AbortException(String.format("Invalid scan credentials when using %s to connect to %s on %s",
credentialsName, fullName, apiUri == null ? GITHUB_URL : apiUri));
}
return new GitHubSCMProbe(repo, head, revision);
}

Expand Down Expand Up @@ -872,16 +888,18 @@ public SCMRevision getTrustedRevision(SCMRevision revision, TaskListener listene
}
if (collaboratorNames == null) {
// Input data validation
String credentialsName =
credentials == null ? "anonymous access" : CredentialsNameProvider.name(credentials);
if (credentials != null && !github.isCredentialValid()) {
listener.getLogger().format("Invalid scan credentials %s to connect to %s, "
+ "assuming no trusted collaborators%n",
CredentialsNameProvider.name(credentials), apiUri == null ? GITHUB_URL : apiUri);
credentialsName, apiUri == null ? GITHUB_URL : apiUri);
collaboratorNames = Collections.singleton(repoOwner);
} else {
if (!github.isAnonymous()) {
listener.getLogger()
.format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri,
CredentialsNameProvider.name(credentials));
credentialsName);
} else {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n",
apiUri == null ? GITHUB_URL : apiUri);
Expand All @@ -892,8 +910,17 @@ public SCMRevision getTrustedRevision(SCMRevision revision, TaskListener listene
collaboratorNames = Collections.singleton(repoOwner);
} else {
String fullName = repoOwner + "/" + repository;
final GHRepository repo = github.getRepository(fullName);
collaboratorNames = new HashSet<>(repo.getCollaboratorNames());
final GHRepository repo;
try {
repo = github.getRepository(fullName);
collaboratorNames = new HashSet<>(repo.getCollaboratorNames());
} catch (FileNotFoundException e) {
listener.getLogger().format("Invalid scan credentials %s to connect to %s, "
+ "assuming no trusted collaborators%n",
credentialsName,
apiUri == null ? GITHUB_URL : apiUri);
collaboratorNames = Collections.singleton(repoOwner);
}
}
}
}
Expand Down Expand Up @@ -973,11 +1000,36 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
@NonNull TaskListener listener) throws IOException {
// TODO when we have support for trusted events, use the details from event if event was from trusted source
List<Action> result = new ArrayList<>();
result.add(new GitHubRepoMetadataAction());
StandardCredentials credentials = Connector.lookupScanCredentials(getOwner(), apiUri, scanCredentialsId);
GitHub hub = Connector.connect(apiUri, credentials);
GHRepository repo = hub.getRepository(getRepoOwner() + '/' + repository);
try {
hub.checkApiUrlValidity();
} catch (HttpException e) {
listener.getLogger().format("It seems %s is unreachable%n",
apiUri == null ? GITHUB_URL : apiUri);
return result;
}
String credentialsName = credentials == null ? "anonymous access" : CredentialsNameProvider.name(credentials);
if (credentials != null && !hub.isCredentialValid()) {
throw new AbortException(String.format("Invalid scan credentials %s to connect to %s, skipping",
credentialsName, apiUri == null ? GITHUB_URL : apiUri));
}
if (!hub.isAnonymous()) {
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri,
credentialsName);
} else {
listener.getLogger()
.format("Connecting to %s using anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}
GHRepository repo;
try {
repo = hub.getRepository(getRepoOwner() + '/' + repository);
} catch (FileNotFoundException e) {
throw new AbortException(String.format("Invalid scan credentials when using %s to connect to %s/%s on %s",
credentialsName, repoOwner, repository, apiUri == null ? GITHUB_URL : apiUri));
}
result.add(new ObjectMetadataAction(null, repo.getDescription(), Util.fixEmpty(repo.getHomepage())));
result.add(new GitHubRepoMetadataAction());
result.add(new GitHubLink("icon-github-repo", repo.getHtmlUrl()));
if (StringUtils.isNotBlank(repo.getDefaultBranch())) {
result.add(new GitHubDefaultBranch(getRepoOwner(), repository, repo.getDefaultBranch()));
Expand Down

0 comments on commit fa3c8e7

Please sign in to comment.