Skip to content
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

Adding app upload functionality #19

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ plugins {
id 'com.diffplug.spotless' version '6.25.0'
}

version = '1.0.5'
version = '1.0.6'
group = 'io.github.lambdatest'

repositories {
Original file line number Diff line number Diff line change
@@ -5,18 +5,21 @@

/**
* The main plugin class that integrates LambdaTest functionality into the Gradle build system. This
* plugin adds the 'runLambdaTest' task to the project's task container.
* plugin adds the 'runLambdaTest' and 'uploadApkToLambdaTest' tasks to the project's task
* container.
*/
public class LambdaTestPlugin implements Plugin<Project> {

/**
* Applies the plugin to the specified Gradle project, registering the LambdaTest task.
* Applies the plugin to the specified Gradle project, registering the LambdaTest and
* uploadApkToLambdaTest task.
*
* @param project The Gradle project to which this plugin is being applied
*/
@Override
public void apply(Project project) {
// Register a new task named "runLambdaTest" and associate it with the LambdaTestTask class
project.getTasks().create("runLambdaTest", LambdaTestTask.class);
project.getTasks().create("uploadApkToLambdaTest", LambdaUploaderTask.class);
}
}
83 changes: 83 additions & 0 deletions src/main/java/io/github/lambdatest/gradle/LambdaUploaderTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.github.lambdatest.gradle;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.TaskAction;

/**
* Uploader task class for the LambdaTest Gradle plugin that handles uploading of APK . This task
* manages the upload of applications and test suites.
*
* <p>This task coordinates between {@link AppUploader} and {@link TestSuiteUploader}, to upload the
* apps to Lmabdatest.
*/
public class LambdaUploaderTask extends DefaultTask {

private static final Logger logger = Logging.getLogger(LambdaUploaderTask.class);
private String username;
private String accessKey;
private String appFilePath;
private String testSuiteFilePath;

@TaskAction
public void uploadApkToLambdaTest() {
// Generated after upload of app and test suite
String appId;
String testSuiteId;
CompletableFuture<String> appIdFuture = null;
CompletableFuture<String> testSuiteIdFuture = null;
logger.lifecycle("Starting LambdaTest APK Uploader task...");

if (appFilePath != null) {
logger.lifecycle("Uploading app ...");
AppUploader appUploader = new AppUploader(username, accessKey, appFilePath);
appIdFuture = appUploader.uploadAppAsync();
}

if (testSuiteFilePath != null) {
logger.lifecycle("Uploading test suite ...");
TestSuiteUploader testSuiteUploader =
new TestSuiteUploader(username, accessKey, testSuiteFilePath);
testSuiteIdFuture = testSuiteUploader.uploadTestSuiteAsync();
}

try {
if (appIdFuture != null) {
appId = appIdFuture.join();
logger.lifecycle("\u001B[32mApp uploaded successfully with ID: {}\u001B[0m", appId);
}

if (testSuiteIdFuture != null) {
testSuiteId = testSuiteIdFuture.join();
logger.lifecycle(
"\u001B[32mTest suite uploaded successfully with ID: {}\u001B[0m",
testSuiteId);
}
} catch (CompletionException e) {
logger.error("Failed to execute LambdaTest APK Uploader task : {}", e);
throw new RuntimeException(e);
}

logger.lifecycle("Completed LambdaTest APK Uploader task ...");
}

// Setter functions for the task
public void setUsername(String username) {
this.username = username;
}

public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}

public void setAppFilePath(String appFilePath) {
this.appFilePath = appFilePath;
}

public void setTestSuiteFilePath(String testSuiteFilePath) {
this.testSuiteFilePath = testSuiteFilePath;
}
}
9 changes: 7 additions & 2 deletions src/main/java/io/github/lambdatest/gradle/UploaderUtil.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import com.google.gson.JsonParser;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
@@ -39,8 +40,12 @@ private UploaderUtil() {
*/
public static String uploadAndGetId(String username, String accessKey, String filePath)
throws IOException {

OkHttpClient client = new OkHttpClient();
OkHttpClient client =
new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES) // Increase connection timeout
.readTimeout(0, TimeUnit.MILLISECONDS) // Increase read timeout
.writeTimeout(0, TimeUnit.MILLISECONDS) // Increase write timeout
.build();

MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body =