Skip to content

Commit

Permalink
[JENKINS-34776] Jobs are removed if the remote is unavailable (#57)
Browse files Browse the repository at this point in the history
* [JENKINS-34776] If an error is found we have to throw an exception
* [JENKINS-34776] Wrong format message
* [JENKINS-34776] AbortException instead of InterruptedException
* [JENKINS-34776] Added a new case for AbortException. Log messages with the same format
* [JENKINS-34776] Replaced an InterruptedException to AbortException
* [JENKINS-34776] checkApiUrlValidity() moved to places where we want to verify the connectivity explicitly
* [JENKINS-34776] Removed all newline characters in the messages of AbortException
* [JENKINS-34776] I forget to catch the FileNotFoundException when we are managing the Owner
  • Loading branch information
recena committed May 13, 2016
1 parent 44edd83 commit 38a28e6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
Expand Up @@ -33,6 +33,8 @@
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -50,11 +52,14 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.HttpException;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import static org.jenkinsci.plugins.github.config.GitHubServerConfig.GITHUB_URL;

public class GitHubSCMNavigator extends SCMNavigator {

private final String repoOwner;
Expand Down Expand Up @@ -119,25 +124,37 @@ public String getApiUri() {

@Override public void visitSources(SCMSourceObserver observer) throws IOException, InterruptedException {
TaskListener listener = observer.getListener();

// Input data validation
if (repoOwner.isEmpty()) {
listener.getLogger().format("Must specify user or organization%n");
return;
throw new AbortException("Must specify user or organization");
}

StandardCredentials credentials = Connector.lookupScanCredentials(observer.getContext(), apiUri, scanCredentialsId);

// Github client and validation
GitHub github = Connector.connect(apiUri, credentials);
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
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()) {
listener.getLogger().format("Invalid scan credentials %s to connect to %s, skipping%n", CredentialsNameProvider.name(credentials), apiUri == null ? "github.com" : apiUri);
return;
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 ? "github.com" : 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
myself = github.getMyself();
} catch (IOException e) {
// Something wrong happened, maybe java.net.ConnectException?
} catch (RateLimitExceededException rle) {
throw new AbortException(rle.getMessage());
}
if (myself != null && repoOwner.equalsIgnoreCase(myself.getLogin())) {
listener.getLogger().format("Looking up repositories of myself %s%n%n", repoOwner);
Expand All @@ -150,17 +167,16 @@ public String getApiUri() {
return;
}
} else {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? "github.com" : apiUri);
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}

GHOrganization org = null;
try {
org = github.getOrganization(repoOwner);
} catch (RateLimitExceededException rle) {
listener.getLogger().format("%n%s%n%n", rle.getMessage());
throw new InterruptedException();
} catch (IOException e) {
// may be a user... ok to ignore
throw new AbortException(rle.getMessage());
} catch (FileNotFoundException fnf) {
// may be an user... ok to ignore
}
if (org != null && repoOwner.equalsIgnoreCase(org.getLogin())) {
listener.getLogger().format("Looking up repositories of organization %s%n%n", repoOwner);
Expand All @@ -174,10 +190,9 @@ public String getApiUri() {
try {
user = github.getUser(repoOwner);
} catch (RateLimitExceededException rle) {
listener.getLogger().format("%n%s%n%n", rle.getMessage());
throw new InterruptedException();
} catch (IOException e) {
// Something wrong happened, maybe java.net.ConnectException?
throw new AbortException(rle.getMessage());
} catch (FileNotFoundException fnf) {
// the user may not exist... ok to ignore
}
if (user != null && repoOwner.equalsIgnoreCase(user.getLogin())) {
listener.getLogger().format("Looking up repositories of user %s%n%n", repoOwner);
Expand Down Expand Up @@ -205,7 +220,7 @@ private void add(TaskListener listener, SCMSourceObserver observer, GHRepository
GitHubSCMSource ghSCMSource = new GitHubSCMSource(null, apiUri, checkoutCredentialsId, scanCredentialsId, repoOwner, name);
ghSCMSource.setExcludes(getExcludes());
ghSCMSource.setIncludes(getIncludes());

projectObserver.addSource(ghSCMSource);
projectObserver.complete();
}
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.AbortException;
import hudson.Extension;
import hudson.Util;
import hudson.console.HyperlinkNote;
Expand Down Expand Up @@ -60,6 +61,7 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.HttpException;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
Expand All @@ -82,6 +84,7 @@
import java.util.logging.Logger;

import static hudson.model.Items.XSTREAM2;
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.GITHUB_URL;

public class GitHubSCMSource extends AbstractGitSCMSource {

Expand Down Expand Up @@ -220,29 +223,40 @@ public String getRemote() {

@Override protected final void retrieve(SCMHeadObserver observer, final TaskListener listener) throws IOException, InterruptedException {
StandardCredentials credentials = Connector.lookupScanCredentials(getOwner(), apiUri, scanCredentialsId);

// Github client and validation
GitHub github = Connector.connect(apiUri, credentials);
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
String message = String.format("It seems %s is unreachable", apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

try {
// Input data validation
if (credentials != null && !github.isCredentialValid()) {
listener.getLogger().format("Invalid scan credentials %s to connect to %s, skipping%n", CredentialsNameProvider.name(credentials), apiUri == null ? "github.com" : apiUri);
return;
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 ? "github.com" : apiUri, CredentialsNameProvider.name(credentials));
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri, CredentialsNameProvider.name(credentials));
} else {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? "github.com" : apiUri);
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}

// Input data validation
if (repository == null || repository.isEmpty()) {
listener.getLogger().println("No repository selected, skip");
return;
throw new AbortException("No repository selected, skipping");
}

String fullName = repoOwner + "/" + repository;
final GHRepository repo = github.getRepository(fullName);
listener.getLogger().format("Looking up %s%n", HyperlinkNote.encodeTo(repo.getHtmlUrl().toString(), fullName));
doRetrieve(observer, listener, repo);
listener.getLogger().format("%nDone examining %s%n%n", fullName);
} catch (RateLimitExceededException rle) {
listener.getLogger().format("%n%s%n%n", rle.getMessage());
throw new InterruptedException();
throw new AbortException(rle.getMessage());
}
}

Expand Down Expand Up @@ -368,24 +382,31 @@ protected SCMSourceCriteria.Probe getProbe(final String branch, final String thi
@CheckForNull
protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOException, InterruptedException {
StandardCredentials credentials = Connector.lookupScanCredentials(getOwner(), apiUri, scanCredentialsId);

// Github client and validation
GitHub github = Connector.connect(apiUri, credentials);
try {
github.checkApiUrlValidity();
} catch (HttpException e) {
String message = String.format("It seems %s is unreachable", apiUri == null ? GITHUB_URL : apiUri);
throw new AbortException(message);
}

try {
if (credentials != null && !github.isCredentialValid()) {
listener.getLogger().format("Invalid scan credentials, skipping%n");
return null;
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", getDescriptor().getDisplayName(),
CredentialsNameProvider.name(credentials));
listener.getLogger().format("Connecting to %s using %s%n", apiUri == null ? GITHUB_URL : apiUri, CredentialsNameProvider.name(credentials));
} else {
listener.getLogger().format("Connecting to %s using anonymous access%n", getDescriptor().getDisplayName());
listener.getLogger().format("Connecting to %s using anonymous access%n", apiUri == null ? GITHUB_URL : apiUri);
}
String fullName = repoOwner + "/" + repository;
GHRepository repo = github.getRepository(fullName);
return doRetrieve(head, listener, repo);
} catch (RateLimitExceededException rle) {
listener.getLogger().format("%n%s%n%n", rle.getMessage());
throw new InterruptedException();
throw new AbortException(rle.getMessage());
}
}

Expand Down

0 comments on commit 38a28e6

Please sign in to comment.