1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Jira-CLI library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/jira-cli |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\JiraCLI\Command; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use ConsoleHelpers\ConsoleKit\Exception\CommandException; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class DownloadAttachmentCommand extends AbstractCommand |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('download-attachment') |
30
|
|
|
->setDescription('Downloads issue attachments') |
31
|
|
|
->addArgument('issue', InputArgument::REQUIRED, 'Issue key or url') |
32
|
|
|
->addOption( |
33
|
|
|
'index', |
34
|
|
|
'i', |
35
|
|
|
InputOption::VALUE_IS_ARRAY + InputOption::VALUE_OPTIONAL, |
36
|
|
|
'Attachment index to download' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$issue_key = $this->getIssueKey(); |
46
|
|
|
$issue_data = $this->jiraApi->getIssue($issue_key)->getResult(); |
47
|
|
|
|
48
|
|
|
$attachments = $issue_data['fields']['attachment']; |
49
|
|
|
|
50
|
|
|
// Show attachments. |
51
|
|
|
$this->io->writeln('<info>Issue ' . $issue_key . ' attachments:</info>'); |
52
|
|
|
|
53
|
|
|
foreach ( $attachments as $index => $attachment_data ) { |
54
|
|
|
$this->io->writeln($index . '. ' . $attachment_data['filename']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$indexes = $this->io->getOption('index'); |
58
|
|
|
|
59
|
|
|
if ( count($indexes) === 0 ) { |
|
|
|
|
60
|
|
|
$indexes = array_keys($attachments); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Download attachments. |
64
|
|
|
$this->io->writeln(''); |
65
|
|
|
$this->io->writeln('Downloading all attachments to <info>' . getcwd() . '</info>'); |
66
|
|
|
|
67
|
|
|
foreach ( $attachments as $index => $attachment_data ) { |
68
|
|
|
if ( !in_array($index, $indexes) ) { |
|
|
|
|
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->io->write('- ' . $attachment_data['filename'] . ' ... '); |
73
|
|
|
file_put_contents( |
74
|
|
|
$attachment_data['filename'], |
75
|
|
|
$this->jiraApi->downloadAttachment($attachment_data['content']) |
76
|
|
|
); |
77
|
|
|
$this->io->writeln('done'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->showStatistics(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns issue key. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
* @throws CommandException When issue key is invalid. |
88
|
|
|
*/ |
89
|
|
|
protected function getIssueKey() |
90
|
|
|
{ |
91
|
|
|
$issue = $this->io->getArgument('issue'); |
92
|
|
|
|
93
|
|
|
if ( $this->isValidIssueKey($issue) ) { |
|
|
|
|
94
|
|
|
return $issue; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new CommandException('The issue key is invalid'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|