Skip to content

Commit

Permalink
feat(package) Command package:download (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierozi authored and Frédéric Dewinne committed May 14, 2018
1 parent f28213b commit 0194b2c
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Continuous\Cli\Command\Company\CompanyListCommand;
use Continuous\Cli\Command\ConfigureCommand;
use Continuous\Cli\Command\Pipeline\PipelineExportCommand;
use Continuous\Cli\Command\Package\PackageDownloadCommand;
use Continuous\Cli\Command\Project\ProjectListCommand;
use Continuous\Cli\Command\Repository\RepositoryListCommand;
use Symfony\Component\Console\Application;
Expand Down Expand Up @@ -36,6 +37,7 @@ public function create()
$application->add(new BuildStartCommand());
$application->add(new BuildStopCommand());
$application->add(new PipelineExportCommand());
$application->add(new PackageDownloadCommand());

return $application;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Build/BuildListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configure()
's',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'the build status',
Build::STATE
Build::STATES
)
->addOption(
'noPr',
Expand Down
134 changes: 134 additions & 0 deletions src/Command/Package/PackageDownloadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Continuous\Cli\Command\Package;

use Continuous\Cli\Command\CommandAbstract;
use Continuous\Sdk\Collection;
use Continuous\Sdk\Entity\Build;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PackageDownloadCommand extends CommandAbstract
{
const PACKAGE_TYPES = ['deploy', 'test'];

protected function configure()
{
$this
->setName('package:download')
->setDescription('Download package of continuousphp build.')
->setHelp('This command download package for latest build of pipeline the one you specify by build ID.')
->addArgument('provider', InputArgument::REQUIRED, 'The repository provider')
->addArgument('repository', InputArgument::REQUIRED, 'The repository name')
->addArgument('destination', InputArgument::OPTIONAL, 'The destination path of package file, by default current workdir')
;

$this
->addOption(
'ref',
'r',
InputOption::VALUE_OPTIONAL,
'The git reference'
)
->addOption(
'id',
'i',
InputOption::VALUE_OPTIONAL,
'The build ID'
)
->addOption(
'type',
't',
InputOption::VALUE_OPTIONAL,
'The package type (deploy|test)',
'deploy'
)
->addOption(
'pr',
'p',
InputOption::VALUE_OPTIONAL,
'The Pull Request ID'
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Continuous\Sdk\Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$packageType = $input->getOption('type');
$buildId = $input->getOption('id');
$destination = getcwd() . '/' . $input->getArgument('destination');

if (false === file_exists($destination)) {
return $output->writeln(
"<error>ERROR : directory $destination not exist</error>"
);
}

if (!$buildId && $latestBuild = $this->findLastBuildId($input, $output)) {
$buildId = $latestBuild->get('buildId');
}

if (false === in_array($packageType, static::PACKAGE_TYPES)) {
return $output->writeln(
"<error>ERROR : package type option must be <b>deploy</b> or <b>test</b> only</error>"
);
}

if (!$buildId) {
return $output->writeln(
"<error>ERROR : no build ID has been found</error>"
);
}

$package = $this->continuousClient->downloadPackage([
'provider' => static::mapProviderToSdk($input->getArgument('provider')),
'repository' => $input->getArgument('repository'),
'buildId' => $buildId,
'packageType' => $packageType,
'destination' => getcwd() . '/' . $input->getArgument('destination')
]);

$output->writeln('Package downloaded at ' . $package['path']);
}

/**
* Find the latest build of repository
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed|null
*/
private function findLastBuildId(InputInterface $input, OutputInterface $output): ?Build
{
$pr = $input->getOption('pr');
$filters = [
'provider' => static::mapProviderToSdk($input->getArgument('provider')),
'repository' => $input->getArgument('repository'),
'result' => ['success', 'warning'],
'state' => ['complete'],
'exclude_pull_requests' => !$pr ? 1 : 0,
'page_size' => 1,
];

if ($input->hasOption('ref')) {
$filters['pipeline_id'] = $input->getOption('ref');
}

if ($pr) {
$filters['pull_request_id'] = (int)$pr;
}

/** @var Collection $builds $builds */
$builds = $this->continuousClient->getBuilds($filters);

return 0 === $builds->count() ? null : $builds->getIterator()->current();
}
}

0 comments on commit 0194b2c

Please sign in to comment.