All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.carousell.aws.UploadAppPackage Maven / Gradle / Ivy

package io.github.carousell.aws;

import com.amazonaws.services.devicefarm.model.AWSDeviceFarmException;
import com.amazonaws.services.devicefarm.model.UploadType;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Mojo for uploading application packages (APK, IPA) to AWS Device Farm */
@Mojo(name = "uploadAppPackage")
public class UploadAppPackage extends AbstractMojo {

  private static final Logger LOG = LoggerFactory.getLogger(UploadAppPackage.class);

  @Parameter(defaultValue = "${project}", readonly = true, required = true)
  private MavenProject project;

  @Parameter(required = true)
  private String awsDeviceFarmAccessKey;

  @Parameter(required = true)
  private String awsDeviceFarmSecretKey;
  
  @Parameter(required = true)
  private String awsDynamoAccessKey;

  @Parameter(required = true)
  private String awsDynamoSecretKey;

  @Parameter(defaultValue = "us-east-1")
  private String awsRegion;

  @Parameter(required = true)
  private String projectArn;

  @Parameter(required = true)
  private String appPackage;
  
  // e.g. android-latest, ios-release etc.
  @Parameter(required = true)
  private String appPackageName;

  @Parameter(required = true)
  private String platform;

  public void execute() throws MojoExecutionException, MojoFailureException {
    try {
      AWSDeviceFarmService awsService = new AWSDeviceFarmService(awsDeviceFarmAccessKey, awsDeviceFarmSecretKey, awsRegion);

      // upload app
      LOG.info("Uploading app package from {}", appPackage);
      UploadType uploadType;
      if (platform.equalsIgnoreCase("IOS")) {
        uploadType = UploadType.IOS_APP;
      } else if (platform.equalsIgnoreCase("Android")) {
        uploadType = UploadType.ANDROID_APP;
      } else {
        throw new MojoExecutionException("Unknown platform " + platform);
      }
      String appArn =
          awsService.upload(new File(appPackage), projectArn, uploadType, true).getArn();

      LOG.info("Setting appArn {} as Maven property", appArn);
      project.getProperties().put("appArn", appArn);
      
      LOG.info("Publishing appArn {} to DynamoDB", appArn);
      updateDynamoValue(appPackageName, appArn);

    } catch (AWSDeviceFarmException | InterruptedException | IOException exception) {
      LOG.error("Error while triggering AWS test", exception);
    }
  }
  
  private void updateDynamoValue(String key, String arn) {
    AWSDynamoService awsDynamoService =
        new AWSDynamoService(awsDynamoAccessKey, awsDynamoSecretKey, awsRegion);
    HashMap keyMap = new HashMap();
    keyMap.put("key", new AttributeValue().withS(key));

    Map expressionAttributeValues = new HashMap();
    expressionAttributeValues.put(":arn", new AttributeValue().withS(arn));

    UpdateItemRequest updateItemRequest =
        new UpdateItemRequest()
            .withTableName("appPackages")
            .withKey(keyMap)
            .withUpdateExpression("set arn = :arn")
            .withExpressionAttributeValues(expressionAttributeValues);

    awsDynamoService.getAwsDynamo().updateItem(updateItemRequest);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy