Skip to content

Commit

Permalink
Add option to protect the build
Browse files Browse the repository at this point in the history
  • Loading branch information
dagio committed Aug 9, 2016
1 parent 64a83f4 commit 3d5cf11
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 87 deletions.
4 changes: 3 additions & 1 deletion application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

require __DIR__.'/vendor/autoload.php';

use DAG\Appetize\Deploy\Command\ProtectAllBuildsCommand;
use DAG\Appetize\Deploy\Command\UploadAppCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

$inputArg = [
basename(__FILE__),
'upload',
isset($_SERVER['command']) ? $_SERVER['command'] : 'upload',
isset($_SERVER['app_path']) ? $_SERVER['app_path'] : null,
isset($_SERVER['platform']) ? $_SERVER['platform'] : null,
isset($_SERVER['appetize_token']) ? $_SERVER['appetize_token'] : null,
Expand All @@ -30,4 +31,5 @@
'@package_version@'
);
$application->add(new UploadAppCommand());
$application->add(new ProtectAllBuildsCommand());
$application->run($input);
150 changes: 150 additions & 0 deletions src/API/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
namespace DAG\Appetize\Deploy\API;

use GuzzleHttp\Client;

/**
* Class Api
*/
final class Api
{
/** @var string */
private $token;

/** @var Client */
private $client;

public function __construct($token)
{
$this->token = $token;
$this->client = new Client();
}

/**
* @param string $appFilePath
* @param string $platform
* @param string $publicKey
* @param false $protectedByAccount
*
* @return UploadResponse
*
* @throws \Exception
*/
public function upload($appFilePath, $platform, $publicKey = null, $protectedByAccount = false)
{
if ($publicKey !== null) {
$url = sprintf('https://%s@api.appetize.io/v1/apps/%s', $this->token, $publicKey);
} else {
$url = sprintf('https://%s@api.appetize.io/v1/apps', $this->token);
}

$response = $this->client->request(
'POST',
$url,
[
'multipart' => [
[
'name' => 'platform',
'contents' => $platform,
],
[
'name' => 'protectedByAccount',
'contents' => $protectedByAccount ? '1' : '0',
],
[
'name' => 'file',
'contents' => fopen($appFilePath, 'r'),
],
],
]
);

if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf('API returned HTTP response %d', $response->getStatusCode()));
}

$responseContent = $response->getBody()->getContents();
$responseData = json_decode($responseContent, true);

if (!isset($responseData['appURL'])) {
throw new \Exception('Missing app URL in response');
}

if (!isset($responseData['publicKey'])) {
throw new \Exception('Missing public key in response');
}

$apiResponse = new UploadResponse($responseData['publicKey'], $responseData['appURL']);

// Set environment variable
$out = $returnValue = null;
exec('envman add --key APPETIZE_APP_URL --value "'.$responseData['appURL'].'"', $out, $returnValue);

if ($returnValue != 0) {
throw new \Exception('Can not set the environement variable');
}

return $apiResponse;
}

/**
* @return array
*
* @throws \Exception
*/
public function fetchAll()
{
$baseUrl = sprintf('https://%s@api.appetize.io/v1/apps', $this->token);

$builds = [];

$nextKey = null;

while (true) {
if ($nextKey) {
$url = $baseUrl.'?'.http_build_query(['nextKey' => $nextKey]);
} else {
$url = $baseUrl;
}

$response = $this->client->request('GET', $url);

$responseContent = $response->getBody()->getContents();

if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf('API returned HTTP response %d', $response->getStatusCode()));
}

$responseData = json_decode($responseContent, true);

$builds = array_merge($builds, $responseData['data']);

if (!$responseData['hasMore']) {
break;
}

$nextKey = $responseData['nextKey'];
}

return $builds;
}

public function protectBuild($publicKey)
{
$url = sprintf('https://%s@api.appetize.io/v1/apps/%s', $this->token, $publicKey);

$response = $this->client->request(
'POST',
$url,
[
'json' => [
'protectedByAccount' => true,
],
]
);

if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf('API returned HTTP response %d', $response->getStatusCode()));
}
}
}
78 changes: 0 additions & 78 deletions src/API/UploadApi.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/API/Response.php → src/API/UploadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace DAG\Appetize\Deploy\API;

/**
* Class Response
* Class UploadResponse
*/
final class Response
final class UploadResponse
{
/** @var string */
private $publicKey;
Expand All @@ -13,7 +13,7 @@ final class Response
private $appURL;

/**
* Response constructor.
* UploadResponse constructor.
*
* @param string $publicKey
* @param string $appURL
Expand Down
30 changes: 30 additions & 0 deletions src/Command/ProtectAllBuildsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace DAG\Appetize\Deploy\Command;

use DAG\Appetize\Deploy\API\Api;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ProtectAllBuildsCommand extends Command
{
protected function configure()
{
$this
->setName('disable-all')
->addArgument('token', InputArgument::REQUIRED, 'The token provided by Appetize.io');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$api = new Api($input->getArgument('token'));
$builds = $api->fetchAll();

foreach ($builds as $build) {
$api->protectBuild($build['publicKey']);
}

return;
}
}
10 changes: 5 additions & 5 deletions src/Command/UploadAppCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace DAG\Appetize\Deploy\Command;

use DAG\Appetize\Deploy\API\UploadApi;
use DAG\Appetize\Deploy\API\Api;
use DAG\Appetize\Deploy\Archive\IOSArchive;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
Expand All @@ -22,7 +22,7 @@ protected function configure()
->addArgument('app-path', InputArgument::REQUIRED, 'The path to the .app or .apk')
->addArgument('platform', InputArgument::REQUIRED, 'The platform. Either "ios" or "android"')
->addArgument('token', InputArgument::REQUIRED, 'The token provided by Appetize.io')
->addOption('protected-by-account', InputOption::VALUE_NONE, 'Protect the build to those who have an account')
->addOption('protected-by-account', null, InputOption::VALUE_NONE, 'Protect the build to those who have an account')
->addOption('public-key', null, InputOption::VALUE_REQUIRED, 'A public key to upload to the same app');
}

Expand All @@ -45,10 +45,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$uploadFilePath = $appPath;
}

$uploadApi = new UploadApi();
$uploadApi = new Api($input->getArgument('token'));

$response = $uploadApi->upload(
$uploadFilePath,
$input->getArgument('token'),
$platform,
$input->getOption('public-key'),
$input->getOption('protected-by-account')
Expand All @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('Upload success');

$table = new Table($output);
$table->setHeaders(['Response info', 'Value']);
$table->setHeaders(['UploadResponse info', 'Value']);
$table->addRow(['Public key', $response->getPublicKey()]);
$table->addRow(['App URL', $response->getAppURL()]);
$table->render();
Expand Down

0 comments on commit 3d5cf11

Please sign in to comment.