-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathRepoComplianceCommand.php
More file actions
281 lines (253 loc) · 11.3 KB
/
RepoComplianceCommand.php
File metadata and controls
281 lines (253 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Dev\Command;
use Google\Cloud\Dev\Component;
use Google\Cloud\Dev\GitHub;
use Google\Cloud\Dev\Packagist;
use Google\Cloud\Dev\RunShell;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use GuzzleHttp\Client;
use InvalidArgumentException;
/**
* List repo details
* @internal
*/
class RepoComplianceCommand extends Command
{
private const PACKAGIST_USERNAME = 'google-cloud';
public const PHP_TEAM = 'cloud-sdk-php-team';
private GitHub $github;
private Packagist $packagist;
protected function configure()
{
$this->setName('repo:compliance')
->setDescription('ensure all github repositories meet compliance')
->addOption('component', 'c', InputOption::VALUE_REQUIRED, 'If specified, display repo info for this component only', '')
->addOption('token', 't', InputOption::VALUE_REQUIRED, 'Github token to use for authentication', '')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'can be "ci" or "table"', 'table')
->addOption('packagist-token', 'p', InputOption::VALUE_REQUIRED, 'Packagist token for the webhook')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Create github client wrapper
$http = new Client();
$this->github = new GitHub(new RunShell(), $http, $input->getOption('token'), $output);
$this->packagist = new Packagist($http, self::PACKAGIST_USERNAME, $input->getOption('packagist-token') ?? '');
$format = $input->getOption('format');
if (!in_array($format, ['ci', 'table'])) {
throw new InvalidArgumentException('Invalid format "' . $format . '", must be "table" or "ci"');
}
$table = (new Table($output));
$table->setColumnWidths([55, 20, 20, 25, 50]);
$table->setStyle('compact');
$headers = $format == 'ci' ? ['Name', 'Compliance'] : [
'Name',
'Repo Config',
'Packagist Config',
'Teams',
'Compliance'
];
(clone $table)->setHeaders($headers)->render();
$componentName = $input->getOption('component');
$components = $componentName ? [new Component($componentName)] : Component::getComponents();
$isCompliant = true;
foreach ($components as $i => $component) {
do {
$details = $this->getRepoDetails($component);
$settingsCheck = $packagistCheck = $webhookCheck = $teamCheck = true;
$refreshDetails = false;
if (!$this->checkSettingsCompliance($details)) {
$settingsCheck = false;
$refreshDetails |= $this->askFixSettingsCompliance($input, $output, $details);
}
if (!$this->checkWebhookCompliance($details)) {
$webhookCheck = false;
$refreshDetails |= $this->askFixWebhookCompliance($input, $output, $details);
}
if (!$this->checkPackagistCompliance($details)) {
$packagistCheck = false;
$refreshDetails |= $this->askFixPackagistCompliance($input, $output, $component->getRepoName());
}
if (!$this->checkTeamCompliance($details)) {
$teamCheck = $this->github->token ? false : null;
$refreshDetails |= $this->askFixTeamCompliance($input, $output, $component->getRepoName());
}
} while ($refreshDetails);
$emoji = fn (?bool $check) => match ($check) { null => '❓', true => '✅', false => '❌'};
$details['compliant'] = implode("\n", [
sprintf('%s Issues, Projects, Wiki, Pages, and Discussion are disabled', $emoji($settingsCheck)),
sprintf('%s Packagist webhook is configured', $emoji($webhookCheck)),
sprintf('%s Packagist maintainer is "google-cloud"', $emoji($packagistCheck)),
sprintf('%s Github teams permissions are configured correctly', $emoji($teamCheck)),
'',
]);
$isCompliant &= $settingsCheck && $webhookCheck && $packagistCheck && $teamCheck;
if ($format == 'ci') {
unset($details['repo_config'], $details['packagist_config'], $details['teams']);
}
(clone $table)->addRow($details)->render();
}
return $isCompliant ? Command::SUCCESS : Command::FAILURE;
}
private function checkSettingsCompliance(array $details)
{
return $details['repo_config'] === "issues: false
projects: false
wiki: false
pages: false
discussions: false";
}
private function checkTeamCompliance(array $details)
{
return !empty(array_filter(
explode("\n", $details['teams']),
fn ($team) => $team === (self::PHP_TEAM . ': admin')
));
}
private function askFixSettingsCompliance(InputInterface $input, OutputInterface $output, array $details)
{
if (!$this->github->token || $input->getOption('format') == 'ci') {
// without a token, or in CI mode, don't ask to fix compliance
return false;
}
$explodedConfig = array_map(fn ($line) => explode(': ', $line), explode("\n", $details['repo_config']));
$fields = array_combine(array_column($explodedConfig, 0), array_column($explodedConfig, 1));
$fieldsToUpdate = array_keys(array_filter($fields, fn ($value) => $value === 'true'));
$question = new ConfirmationQuestion(sprintf(
'Repo %s has the following configuration enabled: %s. Would you like to disable them? (Y/n)',
$details['name'],
implode(', ', $fieldsToUpdate)
), true);
if ($this->getHelper('question')->ask($input, $output, $question)) {
$this->github->updateRepoDetails(
'googleapis/' . $details['name'],
array_fill_keys(array_map(
fn ($key) => 'has_' . $key,
array_keys($fields)
), false)
);
return true;
}
return false;
}
private function checkWebhookCompliance(array $details): bool
{
$repoName = 'googleapis/' . $details['name'];
$webhookUrl = $this->packagist->getWebhookUrl();
return null !== $this->github->getWebhook($repoName, $webhookUrl);
}
private function askFixWebhookCompliance(InputInterface $input, OutputInterface $output, array $details)
{
if (!$this->github->token || $input->getOption('format') == 'ci') {
// without a token, or in CI mode, don't ask to fix compliance
return false;
}
$question = new ConfirmationQuestion(sprintf(
'Repo %s does not have the packagist webhook configured. Would you like to configure it? (Y/n)',
$details['name'],
), true);
if ($this->getHelper('question')->ask($input, $output, $question)) {
if (!$packagistToken = $this->packagist->getApiToken()) {
throw new \Exception('Packagist token required to update webhook compliance');
}
$repoName = 'googleapis/' . $details['name'];
$webhookUrl = $this->packagist->getWebhookUrl();
if (!$this->github->addWebhook($repoName, $webhookUrl, $packagistToken)) {
$output->writeln(sprintf('<error>%s</error>: Unable to create Packagist webhook.', $repoName));
return false;
}
$output->writeln(sprintf('<comment>%s</comment>: Packagist webhook created.', $repoName));
return true;
}
return false;
}
private function checkPackagistCompliance(array $details)
{
return !empty(array_filter(
explode("\n", $details['packagist_config']),
fn ($team) => $team === self::PACKAGIST_USERNAME
));
}
private function askFixPackagistCompliance(InputInterface $input, OutputInterface $output, array $details)
{
if (!$this->github->token || $input->getOption('format') == 'ci') {
// without a token, or in CI mode, don't ask to fix compliance
return false;
}
throw new \Exception('not implemented');
}
private function askFixTeamCompliance(InputInterface $input, OutputInterface $output, string $repoName)
{
if (!$this->github->token || $input->getOption('format') == 'ci') {
// without a token, or in CI mode, don't ask to fix compliance
return false;
}
$question = new ConfirmationQuestion(sprintf(
'Repo %s does not have "%s" as an admin. Would you like to add it? (Y/n)',
$repoName,
self::PHP_TEAM,
), true);
if ($this->getHelper('question')->ask($input, $output, $question)) {
return $this->github->updateTeamPermission('googleapis', self::PHP_TEAM, $repoName, 'admin');
}
return false;
}
private function getRepoDetails(Component $component): array
{
$repoDetails = (array) $this->github->getRepoDetails($component->getRepoName());
// use "array_intersect_key" to filter out fields that were not requested.
$fields = array_map(
fn ($field) => var_export($field, true),
array_intersect_key(
$repoDetails,
array_flip(['has_issues', 'has_projects', 'has_wiki', 'has_pages', 'has_discussions'])
)
);
return [
'name' => $repoDetails['name'],
'repo_config' => implode("\n", array_map(
fn ($v, $k) => sprintf('%s: %s', str_replace('has_', '', $k), $v),
$fields,
array_keys($fields),
)),
'packagist_config' => implode("\n", $this->packagist->getMaintainers($component->getPackageName())),
'teams' => $this->getRepoTeamDetails($component),
];
}
private function getRepoTeamDetails(Component $component)
{
if (!$this->github->token) {
return '**Token Required**';
}
// get team fields
$teams = $this->github->getTeams($component->getRepoName());
if (is_null($teams)) {
return '**ACCESS DENIED**';
}
return implode("\n", array_map(
fn ($team) => $team['name'] . ': ' . $team['permission'],
$teams
)) . "\n";
}
}