-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Jetpack REST connection: install plugin #22119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
da347ce
Skip wp.com login if access token exists
nbradbury c95fa12
First pass at installing Jetpack
nbradbury f9dba6f
Second pass at installing Jetpack
nbradbury 03f85e7
First pass at handling error response
nbradbury 74f5f45
Return a pair for installJetpack and restored launch wpcom login from…
nbradbury a298f39
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury d15a2bb
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury 8d426ca
refresh the site before starting jetpack install
nbradbury ae6eaa3
Fixed detekt warnings, fetch site directly
nbradbury f72cc1f
Added/updated comments
nbradbury b2f520b
Merge branch 'trunk' of https://github.com/wordpress-mobile/WordPress…
nbradbury fe56a2e
Handle inactive Jetpack install, refresh site in init
nbradbury 1ee7396
Tweaked comments
nbradbury 1cc674f
Restored check for access token
nbradbury 2c1a58a
Check if we need to install or simply activate the jp plugin
nbradbury 1237bc9
Simplified JetpackInstaller to always install the plugin
nbradbury c171b36
Increased timeout
nbradbury d1b59a1
Fixed Detekt warnings
nbradbury 974941a
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury b5b7cbc
Simplified JetpackInstaller.kt
nbradbury a5ac0b4
Simplified JetpackInstaller.kt, p2
nbradbury 5e4aa19
Added log message
nbradbury fe48169
Get plugin status before activating
nbradbury 6af0b6c
Use correct names for slugs
nbradbury 6354253
Removed iOS commented code
nbradbury 8c4dca1
Reduced timeout from 60 to 45 seconds
nbradbury e19e769
Simplified installer
nbradbury ca4efc5
Code cleanup
nbradbury 2a163dd
Removed refreshSite
nbradbury 89ac854
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury d705d4c
Pass WpApiClient as a parameter
nbradbury f7f49ff
Added missing space
nbradbury 7d7d38b
Use a Result<PluginStatus> for installJetpack
nbradbury 4baf46d
Add a delay to wp.com login if already logged in
nbradbury bbccb48
Removed unnecessary launch
nbradbury b601a0c
Suppress TooGenericExceptionCaught
nbradbury b59b354
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury 67d9bd6
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury 77eaae8
Merge branch 'trunk' into feature/jetpack-connect-install
adalpari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
WordPress/src/main/java/org/wordpress/android/ui/jetpackrestconnection/JetpackInstaller.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package org.wordpress.android.ui.jetpackrestconnection | ||
|
||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.utils.AppLogWrapper | ||
import org.wordpress.android.util.AppLog | ||
import rs.wordpress.api.kotlin.WpApiClient | ||
import rs.wordpress.api.kotlin.WpRequestResult | ||
import uniffi.wp_api.PluginCreateParams | ||
import uniffi.wp_api.PluginListParams | ||
import uniffi.wp_api.PluginSlug | ||
import uniffi.wp_api.PluginStatus | ||
import uniffi.wp_api.PluginUpdateParams | ||
import uniffi.wp_api.PluginWpOrgDirectorySlug | ||
import uniffi.wp_api.WpAuthenticationProvider | ||
import java.net.URL | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Installs the Jetpack plugin on the given site using wordpress-rs | ||
*/ | ||
class JetpackInstaller @Inject constructor( | ||
private val appLogWrapper: AppLogWrapper, | ||
) { | ||
@Suppress("TooGenericExceptionCaught") | ||
suspend fun installJetpack(site: SiteModel): Result<PluginStatus> { | ||
if (!validateCredentials(site)) { | ||
return Result.failure(IllegalArgumentException("Missing credentials for Jetpack installation")) | ||
} | ||
|
||
val apiClient = initApiClient(site) | ||
|
||
return try { | ||
val info = getPluginInfo(apiClient) | ||
when (info?.status) { | ||
PluginStatus.ACTIVE, PluginStatus.NETWORK_ACTIVE -> { | ||
logDebug("Jetpack is already installed and activated") | ||
Result.success(info.status) | ||
} | ||
PluginStatus.INACTIVE -> { | ||
logDebug("Jetpack is installed but inactive") | ||
val targetStatus = if (info.isNetworkOnly) { | ||
PluginStatus.NETWORK_ACTIVE | ||
} else { | ||
PluginStatus.ACTIVE | ||
} | ||
activatePlugin(apiClient, targetStatus) | ||
} | ||
null -> { | ||
logDebug("Jetpack is not installed") | ||
createAndActivatePlugin(apiClient) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
logError("Failed to install Jetpack: ${e.message}") | ||
Result.failure(e) | ||
} | ||
} | ||
|
||
private fun validateCredentials(site: SiteModel): Boolean { | ||
return !site.apiRestUsernamePlain.isNullOrBlank() && !site.apiRestPasswordPlain.isNullOrBlank() | ||
} | ||
|
||
private fun initApiClient(site: SiteModel): WpApiClient { | ||
return WpApiClient( | ||
wpOrgSiteApiRootUrl = URL(site.wpApiRestUrl), | ||
authProvider = WpAuthenticationProvider.staticWithUsernameAndPassword( | ||
site.apiRestUsernamePlain!!, | ||
site.apiRestPasswordPlain!! | ||
) | ||
) | ||
} | ||
|
||
private suspend fun getPluginInfo(apiClient: WpApiClient): PluginInfo? { | ||
val response = apiClient.request { requestBuilder -> | ||
requestBuilder.plugins().listWithEditContext( | ||
params = PluginListParams(search = JETPACK_SLUG.slug) | ||
) | ||
} | ||
|
||
return when (response) { | ||
is WpRequestResult.Success -> { | ||
response.response.data.firstOrNull { | ||
it.plugin.slug == JETPACK_SLUG.slug | ||
}?.let { | ||
PluginInfo( | ||
status = it.status, | ||
isNetworkOnly = it.networkOnly | ||
) | ||
} | ||
} | ||
else -> { | ||
logError("Failed to get plugin info") | ||
null | ||
} | ||
} | ||
} | ||
|
||
private suspend fun activatePlugin(apiClient: WpApiClient, targetStatus: PluginStatus): Result<PluginStatus> { | ||
logDebug("Activating Jetpack plugin with status: $targetStatus") | ||
|
||
val response = apiClient.request { requestBuilder -> | ||
requestBuilder.plugins().update( | ||
pluginSlug = JETPACK_SLUG, | ||
params = PluginUpdateParams(status = targetStatus) | ||
) | ||
} | ||
|
||
return when (response) { | ||
is WpRequestResult.Success -> Result.success(response.response.data.status) | ||
is WpRequestResult.WpError<*> -> { | ||
val error = Exception("Activation failed - ${response.errorCode}") | ||
logError(error.message ?: "Activation failed") | ||
Result.failure(error) | ||
} | ||
else -> { | ||
val error = Exception("Activation failed") | ||
logError(error.message ?: "Activation failed") | ||
Result.failure(error) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun createAndActivatePlugin(apiClient: WpApiClient): Result<PluginStatus> { | ||
logDebug("Installing and activating Jetpack plugin") | ||
|
||
val response = apiClient.request { requestBuilder -> | ||
requestBuilder.plugins().create( | ||
PluginCreateParams( | ||
slug = JETPACK_SLUG_WPORG_DIRECTORY, | ||
status = PluginStatus.ACTIVE | ||
) | ||
) | ||
} | ||
|
||
return when (response) { | ||
is WpRequestResult.Success -> { | ||
logDebug("Installation successful") | ||
Result.success(response.response.data.status) | ||
} | ||
is WpRequestResult.WpError<*> -> { | ||
val error = Exception("Installation failed - ${response.errorCode}") | ||
logError(error.message ?: "Installation failed") | ||
Result.failure(error) | ||
} | ||
else -> { | ||
val error = Exception("Installation failed") | ||
logError(error.message ?: "Installation failed") | ||
Result.failure(error) | ||
} | ||
} | ||
} | ||
|
||
private fun logDebug(message: String) { | ||
appLogWrapper.d(AppLog.T.API, "$TAG: $message") | ||
} | ||
|
||
private fun logError(message: String) { | ||
appLogWrapper.e(AppLog.T.API, "$TAG: $message") | ||
} | ||
|
||
private data class PluginInfo( | ||
val status: PluginStatus, | ||
val isNetworkOnly: Boolean | ||
) | ||
|
||
companion object { | ||
private const val TAG = "JetpackInstaller" | ||
private val JETPACK_SLUG = PluginSlug("jetpack/jetpack") | ||
private val JETPACK_SLUG_WPORG_DIRECTORY = PluginWpOrgDirectorySlug("jetpack") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⛏️ What about moving this
WpApiClient
creation to.WpApiClientProvider
, so we are sure every instance creation is done there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I can look into this for this subsequent PR which moves the
WpApiClient
initialization to a helper class.