|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Gerrie package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gerrie\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Gerrie\Gerrie; |
|
14
|
|
|
use Gerrie\Component\Configuration\ConfigurationFactory; |
|
15
|
|
|
use Gerrie\Component\Configuration\CommandConfiguration; |
|
16
|
|
|
use Gerrie\Component\Database\Database; |
|
17
|
|
|
use Gerrie\API\DataService\DataServiceFactory; |
|
18
|
|
|
use Gerrie\Component\Console\InputExtendedInterface; |
|
19
|
|
|
use Gerrie\Service\DatabaseService; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
|
|
23
|
|
|
class CrawlCommand extends GerrieBaseCommand |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Command name |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const COMMAND_NAME = 'Crawl'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Database object |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Gerrie\Component\Database\Database |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $database = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Configuration object |
|
42
|
|
|
* |
|
43
|
|
|
* @var \Gerrie\Component\Configuration\Configuration |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $configuration = null; |
|
46
|
|
|
|
|
47
|
|
|
protected function configure() |
|
48
|
|
|
{ |
|
49
|
|
|
$this |
|
50
|
|
|
->setName('gerrie:crawl') |
|
51
|
|
|
->setDescription('Crawls a Gerrit review system and stores the into a database'); |
|
52
|
|
|
|
|
53
|
|
|
$this->addConfigFileOption(); |
|
54
|
|
|
$this->addDatabaseOptions(); |
|
55
|
|
|
$this->addSSHKeyOption(); |
|
56
|
|
|
$this->addSetupDatabaseOption(); |
|
57
|
|
|
$this->addDebugOption(); |
|
58
|
|
|
$this->addInstancesArgument(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var InputExtendedInterface $input */ |
|
64
|
|
|
|
|
65
|
|
|
$configFile = $input->getOption('config-file'); |
|
66
|
|
|
$this->configuration = ConfigurationFactory::getConfigurationByConfigFileAndCommandOptionsAndArguments($configFile, $input); |
|
67
|
|
|
|
|
68
|
|
|
$databaseConfig = $this->configuration->getConfigurationValue('Database'); |
|
69
|
|
|
$this->database = new Database($databaseConfig); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
73
|
|
|
{ |
|
74
|
|
|
/** @var InputExtendedInterface $input */ |
|
75
|
|
|
$this->outputStartMessage($output); |
|
76
|
|
|
|
|
77
|
|
|
// If we enable the "setup-database-tables" setting, we will check if the necessary tables |
|
78
|
|
|
// are already there. If not we will try to setup / create them. |
|
79
|
|
|
// Try, because this process can fail due to missing access rights of the database user. |
|
80
|
|
|
// If the user got the needed rights, everything will work fine ;) |
|
81
|
|
|
if ($input->getOption('setup-database-tables') === true) { |
|
82
|
|
|
$databaseService = new DatabaseService($this->database, $output); |
|
83
|
|
|
$databaseService->setupDatabaseTables(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Start the importer for each configured project |
|
87
|
|
|
$gerritSystems = $this->configuration->getConfigurationValue('Gerrit'); |
|
88
|
|
|
$defaultSSHKeyFile = $this->configuration->getConfigurationValue('SSH.KeyFile'); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($gerritSystems as $name => $gerrieProject) { |
|
91
|
|
|
$gerritSystem = [ |
|
92
|
|
|
'Name' => $name |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($gerrieProject as $gerritInstance) { |
|
96
|
|
|
|
|
97
|
|
|
// TODO Extract this Instance Key part here. This is the same as in "ListProjectsCommand". |
|
98
|
|
|
// Get instance url |
|
99
|
|
|
// If the instance is a string, we only got a url path like scheme://user@url:port/ |
|
100
|
|
|
if (is_string($gerritInstance)) { |
|
101
|
|
|
$instanceConfig = [ |
|
102
|
|
|
'Instance' => $gerritInstance, |
|
103
|
|
|
'KeyFile' => $defaultSSHKeyFile |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
// If the instance is an array, we get a key => value structure with an Instance key |
|
107
|
|
|
} elseif (is_array($gerritInstance) && isset($gerritInstance['Instance'])) { |
|
108
|
|
|
$instanceConfig = [ |
|
109
|
|
|
'Instance' => $gerritInstance['Instance'], |
|
110
|
|
|
'KeyFile' => $defaultSSHKeyFile |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
if (array_key_exists('KeyFile', $gerritInstance) === true) { |
|
114
|
|
|
$instanceConfig['KeyFile'] = $gerritInstance['KeyFile']; |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
throw new \RuntimeException('No Gerrit instance config given', 1415451921); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$dataService = DataServiceFactory::getDataService($instanceConfig); |
|
121
|
|
|
|
|
122
|
|
|
// Bootstrap the importer |
|
123
|
|
|
$gerrie = new Gerrie($this->database, $dataService, $gerritSystem); |
|
124
|
|
|
$gerrie->setOutput($output); |
|
125
|
|
|
if ($input->getOption('debug') === true) { |
|
126
|
|
|
$gerrie->enableDebugFunctionality(); |
|
127
|
|
|
} else { |
|
128
|
|
|
$gerrie->disableDebugFunctionality(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Start the crawling action |
|
132
|
|
|
$gerrie->crawl(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->outputEndMessage($output); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function outputStartMessage(OutputInterface $output) |
|
140
|
|
|
{ |
|
141
|
|
|
$output->writeln(''); |
|
142
|
|
|
$output->writeln('<comment>Starting application "' . self::COMMAND_NAME . '"</comment>'); |
|
143
|
|
|
$output->writeln(''); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
protected function outputEndMessage(OutputInterface $output) |
|
147
|
|
|
{ |
|
148
|
|
|
$output->writeln(''); |
|
149
|
|
|
$output->writeln('<comment>Application "' . self::COMMAND_NAME . '" finished</comment>'); |
|
150
|
|
|
$output->writeln(''); |
|
151
|
|
|
} |
|
152
|
|
|
} |