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\API\DataService; |
12
|
|
|
|
13
|
|
|
class SSHDataService extends BaseDataService |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Version of the Gerrit Server |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $version; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param \Gerrie\Component\Connection\SSH $connector |
27
|
|
|
* @param array $config |
28
|
|
|
* @return \Gerrie\API\DataService\SSHDataService |
|
|
|
|
29
|
|
|
*/ |
30
|
8 |
|
public function __construct(\Gerrie\Component\Connection\SSH $connector, array $config) |
31
|
|
|
{ |
32
|
8 |
|
$this->setConnector($connector); |
33
|
8 |
|
$this->setConfig($config); |
34
|
8 |
|
$this->setName('SSH'); |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Transforms a JSON string into an array. |
39
|
|
|
* Regular, the json is the content from the response. |
40
|
|
|
* |
41
|
|
|
* @param string $json The json string |
42
|
|
|
* @return array|null |
43
|
|
|
*/ |
44
|
2 |
|
public function transformJsonResponse($json) |
45
|
|
|
{ |
46
|
2 |
|
return json_decode($json, true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Requests projects at the Gerrit server |
51
|
|
|
* |
52
|
|
|
* @return array|null |
53
|
|
|
*/ |
54
|
|
|
public function getProjects() |
55
|
|
|
{ |
56
|
|
|
$connector = $this->getBaseQuery(); |
57
|
|
|
|
58
|
|
|
$connector->addCommandPart('ls-projects'); |
59
|
|
|
$connector->addArgument('--format', 'JSON', ' '); |
60
|
|
|
$connector->addArgument('--description', '', ''); |
61
|
|
|
$connector->addArgument('--tree', '', ''); |
62
|
|
|
$connector->addArgument('--type', 'all', ' '); |
63
|
|
|
$connector->addArgument('--all', '', ''); |
64
|
|
|
// TODO |
65
|
|
|
// The ls-projects command supports a "limit" argument |
66
|
|
|
// The default limit from Gerrit is 500. |
67
|
|
|
// What happen when the Gerrit system got more then 500 projects? |
68
|
|
|
// I don`t see a "resume_sortkey" option here :( |
69
|
|
|
// Does anyone know this? |
70
|
|
|
|
71
|
|
|
$content = $connector->execute(); |
72
|
|
|
$content = $this->transformJsonResponse($content); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $content; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the base ssh query object for all SSH requests. |
79
|
|
|
* |
80
|
|
|
* @return \Gerrie\Component\Connection\SSH |
81
|
|
|
*/ |
82
|
|
|
protected function getBaseQuery() |
83
|
|
|
{ |
84
|
|
|
$config = $this->getConfig(); |
85
|
|
|
$connector = $this->getConnector(); |
86
|
|
|
/** @var \Gerrie\Component\Connection\SSH $connector */ |
87
|
|
|
|
88
|
|
|
$connector->reset(); |
89
|
|
|
|
90
|
|
|
$host = $config['host']; |
91
|
|
|
if (isset($config['user']) === true) { |
92
|
|
|
$host = $config['user'] . '@' . $host; |
93
|
|
|
} |
94
|
|
|
$connector->addCommandPart($host); |
95
|
|
|
$connector->addCommandPart('gerrit'); |
96
|
|
|
|
97
|
|
|
return $connector; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Requests changesets at the Gerrit server. |
102
|
|
|
* |
103
|
|
|
* @param string $projectName The project name |
104
|
|
|
* @param string $resumeKey The key where the request will be resumed |
105
|
|
|
* @param integer $start Number of changesets which will be skipped |
106
|
|
|
* @return array |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function getChangesets($projectName, $resumeKey = null, $start = 0) |
110
|
|
|
{ |
111
|
|
|
if (!$this->version) { |
112
|
|
|
$this->version = $this->getVersion(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$connector = $this->getBaseQuery(); |
116
|
|
|
|
117
|
|
|
$connector->addCommandPart('query'); |
118
|
|
|
$connector->addArgument('--format', 'JSON', ' '); |
119
|
|
|
$connector->addArgument('--current-patch-set', '', ' '); |
120
|
|
|
$connector->addArgument('--all-approvals', '', ''); |
121
|
|
|
$connector->addArgument('--files', '', ''); |
122
|
|
|
$connector->addArgument('--comments', '', ''); |
123
|
|
|
$connector->addArgument('--commit-message', '', ''); |
124
|
|
|
$connector->addArgument('--dependencies', '', ''); |
125
|
|
|
$connector->addArgument('--submit-records', '', ''); |
126
|
|
|
$connector->addArgument('', 'project:' . $projectName, ''); |
127
|
|
|
$connector->addArgument('limit', $this->getQueryLimit(), ':'); |
128
|
|
|
|
129
|
|
|
// TODO Implement "--all-reviewers" |
130
|
|
|
// Show the name and email of all reviewers which are added to a change (irrespective of whether they have been voting on that change or not). |
131
|
|
|
// See https://review.typo3.org/Documentation/cmd-query.html |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* We have to compare the current version, because Gerrit got |
135
|
|
|
* a breaking change in v2.9.0. They removed the "resume_sortkey" parameter, |
136
|
|
|
* without offering a alternative for SSH API. |
137
|
|
|
* "--start" was added in v2.9.1 |
138
|
|
|
* |
139
|
|
|
* = v2.9.0 is not supported, because resume_sortkey was removed and --start not implemented |
140
|
|
|
* >= v2.9.1 is needed for --start parameter |
141
|
|
|
* |
142
|
|
|
* @link https://github.com/andygrunwald/Gerrie/issues/4 |
143
|
|
|
* @link https://groups.google.com/forum/#!searchin/repo-discuss/Andy$20Grunwald/repo-discuss/yQgRR5hlS3E/xTAZWXOSklsJ |
144
|
|
|
*/ |
145
|
|
|
if ((version_compare($this->version, '2.9.1') >= 0) && $start > 0) { |
146
|
|
|
$connector->addArgument('--start', $start, ' '); |
147
|
|
|
|
148
|
|
|
} elseif (version_compare($this->version, '2.9.0') == 0) { |
149
|
|
|
throw new \RuntimeException('Version v2.9.0 of Gerrit is not supported with SSH API. See #4 in andygrunwald/Gerrie on Github.', 1412027367); |
150
|
|
|
|
151
|
|
|
} elseif ((version_compare($this->version, '2.9.0') == -1) && $resumeKey) { |
|
|
|
|
152
|
|
|
$connector->addArgument('resume_sortkey', $resumeKey, ':'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$content = $connector->execute(false); |
156
|
|
|
return $content; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Initiales the query limit |
161
|
|
|
* |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
protected function initQueryLimit() |
165
|
|
|
{ |
166
|
|
|
// @todo implement! Idea: Config OR try to get query limit over HTTP with HTTP dataservice |
167
|
|
|
return 500; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the version of the Gerrit server |
172
|
|
|
* |
173
|
|
|
* @link https://review.typo3.org/Documentation/cmd-version.html |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getVersion() |
178
|
|
|
{ |
179
|
|
|
$connector = $this->getBaseQuery(); |
180
|
|
|
$connector->addCommandPart('version'); |
181
|
|
|
$content = $connector->execute(false); |
182
|
|
|
|
183
|
|
|
if (is_array($content) && count($content) > 0) { |
184
|
|
|
$content = array_shift($content); |
185
|
|
|
$content = str_replace('gerrit version ', '', $content); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $content; |
189
|
|
|
} |
190
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.