Skip to content

Commit

Permalink
feat: start trial option if not started (#2288)
Browse files Browse the repository at this point in the history
* feat: start trial option if not started

* fix: feedbacks for cli start trial

* fix: added company name input to maestro
  • Loading branch information
proksh authored Feb 11, 2025
1 parent 1e71f42 commit 0d9f6b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
69 changes: 68 additions & 1 deletion maestro-cli/src/main/java/maestro/cli/api/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import okio.IOException
import okio.buffer
import java.io.File
import java.nio.file.Path
import java.util.Scanner
import kotlin.io.path.absolutePathString
import kotlin.io.path.exists
import kotlin.time.Duration.Companion.minutes
Expand Down Expand Up @@ -331,7 +332,7 @@ class ApiClient(
}

val url = if (projectId != null) {
"$baseUrl/runMaestroTest"
"$baseUrl/v2/project/$projectId/runMaestroTest"
} else {
"$baseUrl/v2/upload"
}
Expand All @@ -351,6 +352,50 @@ class ApiClient(
if (!response.isSuccessful) {
val errorMessage = response.body?.string().takeIf { it?.isNotEmpty() == true } ?: "Unknown"

if (response.code == 403 && errorMessage.contains("Your trial has not started yet", ignoreCase = true)) {
println("\n\u001B[31;1m[ERROR]\u001B[0m Your trial has not started yet.")
print("\u001B[34;1m[INPUT]\u001B[0m Please enter your company name to start the trial: ")

val scanner = Scanner(System.`in`)
val companyName = scanner.nextLine().trim()

if (companyName.isNotEmpty()) {
println("\u001B[33;1m[INFO]\u001B[0m Starting your trial for company: \u001B[36;1m$companyName\u001B[0m...")

val isTrialStarted = startTrial(authToken, companyName);
if(isTrialStarted) {
println("\u001B[32;1m[SUCCESS]\u001B[0m Trial successfully started. Enjoy your 7-day free trial!\n")
return upload(
authToken = authToken,
appFile = appFile,
workspaceZip = workspaceZip,
uploadName = uploadName,
mappingFile = mappingFile,
repoOwner = repoOwner,
repoName = repoName,
branch = branch,
commitSha = commitSha,
pullRequestId = pullRequestId,
env = env,
androidApiLevel = androidApiLevel,
iOSVersion = iOSVersion,
includeTags = includeTags,
excludeTags = excludeTags,
maxRetryCount = maxRetryCount,
completedRetries = completedRetries + 1,
progressListener = progressListener,
appBinaryId = appBinaryId,
disableNotifications = disableNotifications,
deviceLocale = deviceLocale,
)
} else {
println("\u001B[31;1m[ERROR]\u001B[0m Failed to start trial. Please check your details and try again.")
}
} else {
println("\u001B[31;1m[ERROR]\u001B[0m Company name is required for starting a trial.")
}
}

if (response.code >= 500) {
return retry("Upload failed with status code ${response.code}: $errorMessage")
} else {
Expand All @@ -368,6 +413,28 @@ class ApiClient(
}
}

private fun startTrial(authToken: String, companyName: String): Boolean {
println("Starting your trial...")
val url = "$baseUrl/v2/start-trial"

val jsonBody = """{ "companyName": "$companyName" }""".toRequestBody("application/json".toMediaType())
val trialRequest = Request.Builder()
.header("Authorization", "Bearer $authToken")
.url(url)
.post(jsonBody)
.build()

try {
val response = client.newCall(trialRequest).execute()
if (response.isSuccessful) return true;
println("\u001B[31m${response.body?.string()}\u001B[0m");
return false
} catch (e: IOException) {
println("\u001B[31;1m[ERROR]\u001B[0m We're experiencing connectivity issues, please try again in sometime, reach out to the slack channel in case if this doesn't work.")
return false
}
}

private fun parseRobinUploadResponse(responseBody: Map<*, *>): UploadResponse {
@Suppress("UNCHECKED_CAST")
val orgId = responseBody["orgId"] as String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class CloudCommand : Callable<Int> {
// Upload
val apiUrl = apiUrl ?: run {
if (projectId != null) {
"https://api.copilot.mobile.dev/v2/project/$projectId"
"https://api.copilot.mobile.dev"
} else {
throw CliError("You need to specify a Robin project with --projectId")
}
Expand Down

0 comments on commit 0d9f6b9

Please sign in to comment.