Skip to content

Commit 0194b2c

Browse files
PieroziFrédéric Dewinne
authored andcommitted
feat(package) Command package:download (#15)
1 parent f28213b commit 0194b2c

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

src/ApplicationFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Continuous\Cli\Command\Company\CompanyListCommand;
99
use Continuous\Cli\Command\ConfigureCommand;
1010
use Continuous\Cli\Command\Pipeline\PipelineExportCommand;
11+
use Continuous\Cli\Command\Package\PackageDownloadCommand;
1112
use Continuous\Cli\Command\Project\ProjectListCommand;
1213
use Continuous\Cli\Command\Repository\RepositoryListCommand;
1314
use Symfony\Component\Console\Application;
@@ -36,6 +37,7 @@ public function create()
3637
$application->add(new BuildStartCommand());
3738
$application->add(new BuildStopCommand());
3839
$application->add(new PipelineExportCommand());
40+
$application->add(new PackageDownloadCommand());
3941

4042
return $application;
4143
}

src/Command/Build/BuildListCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function configure()
3535
's',
3636
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
3737
'the build status',
38-
Build::STATE
38+
Build::STATES
3939
)
4040
->addOption(
4141
'noPr',
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)