Navigation Menu

Skip to content

Commit

Permalink
[JENKINS-25933] Improve errors when credentials are missing or miscon…
Browse files Browse the repository at this point in the history
…figured.
  • Loading branch information
orrc committed Jun 22, 2015
1 parent 19ba74e commit d22f9a0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
Expand Up @@ -96,7 +96,7 @@ protected Boolean execute() throws IOException, InterruptedException, UploadExce
// Check whether this APK already exists on the server (i.e. uploading it would fail)
for (Apk apk : existingApks) {
if (apk.getBinary().getSha1().toLowerCase(Locale.ENGLISH).equals(apkSha1Hash)) {
logger.println("This APK already exists on the server; it cannot be uploaded again");
logger.println("This APK already exists in the Google Play account; it cannot be uploaded again");
return false;
}
}
Expand Down
@@ -0,0 +1,10 @@
package org.jenkinsci.plugins.googleplayandroidpublisher;

/** Thrown when there's a local configuration error with the Google Play credentials. */
public class CredentialsException extends UploadException {

public CredentialsException(String message) {
super(message);
}

}
Expand Up @@ -2,17 +2,25 @@

import com.google.jenkins.plugins.credentials.oauth.GoogleOAuth2ScopeRequirement;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
import org.apache.commons.lang.exception.ExceptionUtils;

import java.io.FileNotFoundException;
import java.security.GeneralSecurityException;

import static hudson.Util.fixEmptyAndTrim;

public class CredentialsHandler {

private final String googleCredentialsId;

public CredentialsHandler(String googleCredentialsId) {
this.googleCredentialsId = googleCredentialsId;
public CredentialsHandler(String googleCredentialsId) throws CredentialsException {
String id = fixEmptyAndTrim(googleCredentialsId);
if (id == null) {
throw new CredentialsException("No credentials have been specified: You must add a Google Account via the "
+ "Jenkins Credentials page, then configure this job to use those credentials");
}
this.googleCredentialsId = id;
}

/** @return The Google API credentials configured for this job. */
Expand All @@ -21,15 +29,25 @@ public final GoogleRobotCredentials getServiceAccountCredentials() throws Upload
GoogleOAuth2ScopeRequirement req = new AndroidPublisherScopeRequirement();
GoogleRobotCredentials credentials = GoogleRobotCredentials.getById(googleCredentialsId);
if (credentials == null) {
throw new UploadException("Credentials for the configured Google Account could not be found");
throw new CredentialsException(String.format("The configured Google Service Account credential '%s' "
+ "could not be found.\n\tIf you renamed the credential since configuring this job, you must "
+ "re-configure this job, choosing the new credential name", googleCredentialsId));
}
return credentials.forRemote(req);
} catch (GoogleRobotPrivateKeyCredentials.AccountIdNotSetException e) {
throw new CredentialsException(String.format("The configured Google Service Account credential '%s' "
+ "has not been configured correctly.\n\tUpdate the credential, ensuring that the required data "
+ "have been entered and try again", googleCredentialsId));
} catch (GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException e) {
throw new CredentialsException(String.format("The configured Google Service Account credential '%s' "
+ "has not been configured correctly.\n\tUpdate the credential, ensuring that the required data "
+ "have been entered and try again", googleCredentialsId));
} catch (NullPointerException e) {
// This should really be handled by the Google OAuth plugin
throw new UploadException("Failed to get Google service account info.\n" +
"\tCheck that the correct 'Client Secrets JSON' file has been uploaded for the " +
"'"+ googleCredentialsId +"' credential.\n" +
"\tThe correct JSON file can be obtained by visiting the *old* Google APIs Console, selecting "+
"'" + googleCredentialsId + "' credential.\n" +
"\tThe correct JSON file can be obtained by visiting the *old* Google APIs Console, selecting " +
"'API Access' and then clicking 'Download JSON' for the appropriate service account.\n" +
"\tSee: https://code.google.com/apis/console/?noredirect", e);
} catch (IllegalStateException e) {
Expand Down
Expand Up @@ -13,7 +13,7 @@ public abstract class GooglePlayBuilder extends Builder {
@DataBoundSetter
private String googleCredentialsId;

protected CredentialsHandler getCredentialsHandler() {
protected CredentialsHandler getCredentialsHandler() throws CredentialsException {
if (credentialsHandler == null) {
credentialsHandler = new CredentialsHandler(googleCredentialsId);
}
Expand Down
Expand Up @@ -13,7 +13,7 @@ public abstract class GooglePlayPublisher extends Recorder {
@DataBoundSetter
private String googleCredentialsId;

protected CredentialsHandler getCredentialsHandler() {
protected CredentialsHandler getCredentialsHandler() throws CredentialsException {
if (credentialsHandler == null) {
credentialsHandler = new CredentialsHandler(googleCredentialsId);
}
Expand Down
Expand Up @@ -75,6 +75,9 @@ static String expand(EnvVars env, String value) {

/** @return A user-friendly(ish) Google Play API error message, if one could be found in the given exception. */
static String getPublisherErrorMessage(UploadException e) {
if (e instanceof CredentialsException) {
return e.getMessage();
}
if (e instanceof PublisherApiException) {
// TODO: Here we could map error reasons like "apkUpgradeVersionConflict" to better (and localised) text
String message = ((PublisherApiException) e).getDetailsMessage();
Expand Down

0 comments on commit d22f9a0

Please sign in to comment.