1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softius\JenkinsJobMonitor; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PushCommand |
14
|
|
|
* @package Softius\JenkinsJobMonitor |
15
|
|
|
*/ |
16
|
|
|
class PushCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var GuzzleHttp\ClientInterface |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configures the push command. |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('push') |
30
|
|
|
->setDescription('Sets external job result') |
31
|
|
|
->addArgument('url', InputArgument::REQUIRED, 'Jenkins home URL') |
32
|
|
|
->addArgument('job', InputArgument::REQUIRED, 'Job name as configured in Jenkins') |
33
|
|
|
->addOption('display', null, InputOption::VALUE_OPTIONAL, '') |
34
|
|
|
->addOption('description', null, InputOption::VALUE_OPTIONAL, '') |
35
|
|
|
->addOption('duration', null, InputOption::VALUE_OPTIONAL, '') |
36
|
|
|
->addOption('log', null, InputOption::VALUE_OPTIONAL, ''); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Executes the monitor command. |
41
|
|
|
* |
42
|
|
|
* @param InputInterface $input An InputInterface instance |
43
|
|
|
* @param OutputInterface $output An OutputInterface instance |
44
|
|
|
* |
45
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$monitor = new JobMonitor( |
50
|
|
|
$input->getArgument('job'), |
51
|
|
|
$input->getOption('display'), |
52
|
|
|
$input->getOption('description') |
53
|
|
|
); |
54
|
|
|
$monitor->setLog($this->getOptionLog($input)); |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
$this->getClient()->send($monitor->getRequest(), ['base_uri' => $input->getArgument('url')]); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
60
|
|
|
return 1; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the value for log option. |
68
|
|
|
* |
69
|
|
|
* If the log option is not provided, STDIN is being used instead. |
70
|
|
|
* |
71
|
|
|
* @param InputInterface $input |
72
|
|
|
* @throws \RuntimeException |
73
|
|
|
* @return mixed|string |
74
|
|
|
*/ |
75
|
|
|
public function getOptionLog(InputInterface $input) |
76
|
|
|
{ |
77
|
|
|
$log = $input->getOption('log'); |
78
|
|
|
if (!$log && 0 === ftell(STDIN)) { |
79
|
|
|
$log = ''; |
80
|
|
|
while (!feof(STDIN)) { |
81
|
|
|
$log .= fread(STDIN, 1024); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $log; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param GuzzleHttp\ClientInterface $client |
90
|
|
|
*/ |
91
|
|
|
public function setClient(ClientInterface $client) |
92
|
|
|
{ |
93
|
|
|
$this->client = $client; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Client |
98
|
|
|
*/ |
99
|
|
|
public function getClient() |
100
|
|
|
{ |
101
|
|
|
return $this->client; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..