|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Continuous\Cli\Command\Package; |
| 4 | + |
| 5 | +use Continuous\Cli\Command\CommandAbstract; |
| 6 | +use Continuous\Sdk\Collection; |
| 7 | +use Continuous\Sdk\Entity\Build; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +class PackageDownloadCommand extends CommandAbstract |
| 14 | +{ |
| 15 | + const PACKAGE_TYPES = ['deploy', 'test']; |
| 16 | + |
| 17 | + protected function configure() |
| 18 | + { |
| 19 | + $this |
| 20 | + ->setName('package:download') |
| 21 | + ->setDescription('Download package of continuousphp build.') |
| 22 | + ->setHelp('This command download package for latest build of pipeline the one you specify by build ID.') |
| 23 | + ->addArgument('provider', InputArgument::REQUIRED, 'The repository provider') |
| 24 | + ->addArgument('repository', InputArgument::REQUIRED, 'The repository name') |
| 25 | + ->addArgument('destination', InputArgument::OPTIONAL, 'The destination path of package file, by default current workdir') |
| 26 | + ; |
| 27 | + |
| 28 | + $this |
| 29 | + ->addOption( |
| 30 | + 'ref', |
| 31 | + 'r', |
| 32 | + InputOption::VALUE_OPTIONAL, |
| 33 | + 'The git reference' |
| 34 | + ) |
| 35 | + ->addOption( |
| 36 | + 'id', |
| 37 | + 'i', |
| 38 | + InputOption::VALUE_OPTIONAL, |
| 39 | + 'The build ID' |
| 40 | + ) |
| 41 | + ->addOption( |
| 42 | + 'type', |
| 43 | + 't', |
| 44 | + InputOption::VALUE_OPTIONAL, |
| 45 | + 'The package type (deploy|test)', |
| 46 | + 'deploy' |
| 47 | + ) |
| 48 | + ->addOption( |
| 49 | + 'pr', |
| 50 | + 'p', |
| 51 | + InputOption::VALUE_OPTIONAL, |
| 52 | + 'The Pull Request ID' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param InputInterface $input |
| 58 | + * @param OutputInterface $output |
| 59 | + * @return int|null|void |
| 60 | + * @throws \Continuous\Sdk\Exception |
| 61 | + */ |
| 62 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 63 | + { |
| 64 | + parent::execute($input, $output); |
| 65 | + |
| 66 | + $packageType = $input->getOption('type'); |
| 67 | + $buildId = $input->getOption('id'); |
| 68 | + $destination = getcwd() . '/' . $input->getArgument('destination'); |
| 69 | + |
| 70 | + if (false === file_exists($destination)) { |
| 71 | + return $output->writeln( |
| 72 | + "<error>ERROR : directory $destination not exist</error>" |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + if (!$buildId && $latestBuild = $this->findLastBuildId($input, $output)) { |
| 77 | + $buildId = $latestBuild->get('buildId'); |
| 78 | + } |
| 79 | + |
| 80 | + if (false === in_array($packageType, static::PACKAGE_TYPES)) { |
| 81 | + return $output->writeln( |
| 82 | + "<error>ERROR : package type option must be <b>deploy</b> or <b>test</b> only</error>" |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if (!$buildId) { |
| 87 | + return $output->writeln( |
| 88 | + "<error>ERROR : no build ID has been found</error>" |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + $package = $this->continuousClient->downloadPackage([ |
| 93 | + 'provider' => static::mapProviderToSdk($input->getArgument('provider')), |
| 94 | + 'repository' => $input->getArgument('repository'), |
| 95 | + 'buildId' => $buildId, |
| 96 | + 'packageType' => $packageType, |
| 97 | + 'destination' => getcwd() . '/' . $input->getArgument('destination') |
| 98 | + ]); |
| 99 | + |
| 100 | + $output->writeln('Package downloaded at ' . $package['path']); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Find the latest build of repository |
| 105 | + * @param InputInterface $input |
| 106 | + * @param OutputInterface $output |
| 107 | + * @return mixed|null |
| 108 | + */ |
| 109 | + private function findLastBuildId(InputInterface $input, OutputInterface $output): ?Build |
| 110 | + { |
| 111 | + $pr = $input->getOption('pr'); |
| 112 | + $filters = [ |
| 113 | + 'provider' => static::mapProviderToSdk($input->getArgument('provider')), |
| 114 | + 'repository' => $input->getArgument('repository'), |
| 115 | + 'result' => ['success', 'warning'], |
| 116 | + 'state' => ['complete'], |
| 117 | + 'exclude_pull_requests' => !$pr ? 1 : 0, |
| 118 | + 'page_size' => 1, |
| 119 | + ]; |
| 120 | + |
| 121 | + if ($input->hasOption('ref')) { |
| 122 | + $filters['pipeline_id'] = $input->getOption('ref'); |
| 123 | + } |
| 124 | + |
| 125 | + if ($pr) { |
| 126 | + $filters['pull_request_id'] = (int)$pr; |
| 127 | + } |
| 128 | + |
| 129 | + /** @var Collection $builds $builds */ |
| 130 | + $builds = $this->continuousClient->getBuilds($filters); |
| 131 | + |
| 132 | + return 0 === $builds->count() ? null : $builds->getIterator()->current(); |
| 133 | + } |
| 134 | +} |
0 commit comments