Skip to content

Commit

Permalink
Ignore application not found errors during metadata processing (#442)
Browse files Browse the repository at this point in the history
* ignore application not found errors during metadata processing

Fixes #441

* Small nits

Signed-off-by: Alex Saveau <[email protected]>

* Generalize JSON error handling

Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
bhurling authored Oct 21, 2018
1 parent 1deba3c commit f686d08
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.triplet.gradle.play.internal

import com.android.builder.model.Version
import com.github.triplet.gradle.play.PlayPublisherExtension
import com.google.api.client.googleapis.json.GoogleJsonResponseException
import org.gradle.util.GradleVersion

private val MIN_GRADLE_VERSION: GradleVersion = GradleVersion.version("4.1")
Expand Down Expand Up @@ -48,3 +49,6 @@ internal fun PlayPublisherExtension.validate() {
}
}
}

internal infix fun GoogleJsonResponseException.has(error: String) =
details?.errors.orEmpty().any { it.reason == error }
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class ProcessPackageMetadata : PlayPublishTaskBase() {
progressLogger.completed()
}

private fun processVersionCodes() = read { editId ->
private fun processVersionCodes() = read(true) { editId ->
progressLogger.progress("Downloading active version codes")
val maxVersionCode = tracks().list(variant.applicationId, editId).execute().tracks
?.map { it.releases ?: emptyList() }?.flatten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ open class PublishApk : PlayPublishPackageBase() {
.trackUploadProgress(progressLogger, "APK")
.execute()
} catch (e: GoogleJsonResponseException) {
return e.handleUploadFailures(content.file)
return handleUploadFailures(e, content.file)
}

handlePackageDetails(editId, apk.versionCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class PublishBundle : PlayPublishPackageBase() {
.trackUploadProgress(progressLogger, "App Bundle")
.execute()
} catch (e: GoogleJsonResponseException) {
return e.handleUploadFailures(content.file)
return handleUploadFailures(e, content.file)
}

handlePackageDetails(editId, bundle.versionCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.triplet.gradle.play.internal.ImageType
import com.github.triplet.gradle.play.internal.LISTINGS_PATH
import com.github.triplet.gradle.play.internal.ListingDetail
import com.github.triplet.gradle.play.internal.climbUpTo
import com.github.triplet.gradle.play.internal.has
import com.github.triplet.gradle.play.internal.isDirectChildOf
import com.github.triplet.gradle.play.internal.orNull
import com.github.triplet.gradle.play.internal.playPath
Expand Down Expand Up @@ -123,7 +124,7 @@ open class PublishListing : PlayPublishTaskBase() {
try {
listings().update(variant.applicationId, editId, locale, listing).execute()
} catch (e: GoogleJsonResponseException) {
if (e.details?.errors.orEmpty().any { it.reason == "unsupportedListingLanguage" }) {
if (e has "unsupportedListingLanguage") {
// Rethrow for clarity
throw IllegalArgumentException("Unsupported locale $locale", e)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.triplet.gradle.play.internal.RELEASE_NOTES_MAX_LENGTH
import com.github.triplet.gradle.play.internal.RELEASE_NOTES_PATH
import com.github.triplet.gradle.play.internal.ReleaseStatus
import com.github.triplet.gradle.play.internal.ResolutionStrategy
import com.github.triplet.gradle.play.internal.has
import com.github.triplet.gradle.play.internal.orNull
import com.github.triplet.gradle.play.internal.readProcessed
import com.github.triplet.gradle.play.internal.trackUploadProgress
Expand Down Expand Up @@ -93,27 +94,25 @@ abstract class PlayPublishPackageBase : PlayPublishTaskBase() {
return this
}

protected fun GoogleJsonResponseException.handleUploadFailures(file: File): Nothing? {
val isConflict = details?.errors.orEmpty().all {
it.reason == "apkUpgradeVersionConflict" || it.reason == "apkNoUpgradePath"
}
protected fun handleUploadFailures(e: GoogleJsonResponseException, file: File): Nothing? {
val isConflict = e has "apkUpgradeVersionConflict" || e has "apkNoUpgradePath"
if (isConflict) {
when (extension._resolutionStrategy) {
ResolutionStrategy.AUTO -> throw IllegalStateException(
"Concurrent uploads for variant ${variant.name}. Make sure to " +
"synchronously upload your APKs such that they don't conflict.",
this
e
)
ResolutionStrategy.FAIL -> throw IllegalStateException(
"Version code ${variant.versionCode} is too low for variant ${variant.name}.",
this
e
)
ResolutionStrategy.IGNORE -> logger.warn(
"Ignoring APK ($file) for version code ${variant.versionCode}")
}
return null
} else {
throw this
throw e
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.gradle.api.ApplicationVariant
import com.github.triplet.gradle.play.PlayPublisherExtension
import com.github.triplet.gradle.play.internal.AccountConfig
import com.github.triplet.gradle.play.internal.PLUGIN_NAME
import com.github.triplet.gradle.play.internal.has
import com.github.triplet.gradle.play.internal.transport
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.json.GoogleJsonResponseException
Expand Down Expand Up @@ -54,19 +55,26 @@ abstract class PlayPublishTaskBase : DefaultTask(), ExtensionOptions {
}.setApplicationName(PLUGIN_NAME).build()
}

protected fun read(block: AndroidPublisher.Edits.(editId: String) -> Unit) {
protected fun read(
skipIfNotFound: Boolean = false,
block: AndroidPublisher.Edits.(editId: String) -> Unit
) {
val edits = publisher.edits()
val request = edits.insert(variant.applicationId, null)

val id = try {
request.execute().id
} catch (e: GoogleJsonResponseException) {
// Rethrow for clarity
if (e.details?.errors.orEmpty().any { it.reason == "applicationNotFound" }) {
throw IllegalArgumentException(
"No application found for the package name ${variant.applicationId}. " +
"The first version of your app must be uploaded via the " +
"Play Store console.", e)
if (e has "applicationNotFound") {
if (skipIfNotFound) {
return
} else {
// Rethrow for clarity
throw IllegalArgumentException(
"No application found for the package name ${variant.applicationId}. " +
"The first version of your app must be uploaded via the " +
"Play Store console.", e)
}
} else if (e.statusCode == 401) {
throw IllegalArgumentException("Invalid service account credentials.", e)
} else {
Expand Down

0 comments on commit f686d08

Please sign in to comment.