Skip to content

Commit dfe3904

Browse files
committed
better output
1 parent 78bd648 commit dfe3904

File tree

1 file changed

+40
-72
lines changed

1 file changed

+40
-72
lines changed

dev/src/Command/RepoComplianceCommand.php

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ protected function configure()
4747
->setDescription('ensure all github repositories meet compliance')
4848
->addOption('component', 'c', InputOption::VALUE_REQUIRED, 'If specified, display repo info for this component only', '')
4949
->addOption('token', 't', InputOption::VALUE_REQUIRED, 'Github token to use for authentication', '')
50-
->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'page to start from', '1')
51-
->addOption('results-per-page', 'r', InputOption::VALUE_REQUIRED, 'results to display per page (0 for all)', '10')
5250
->addOption('new-packagist-token', '', InputOption::VALUE_REQUIRED, 'update the packagist token')
5351
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'can be "ci" or "table"', 'table')
5452
;
@@ -61,58 +59,44 @@ protected function execute(InputInterface $input, OutputInterface $output)
6159
$this->github = new GitHub(new RunShell(), $http, $input->getOption('token'), $output);
6260
$this->packagist = new Packagist($http, self::PACKAGIST_USERNAME, $input->getOption('new-packagist-token') ?? '');
6361

64-
$nextPageQuestion = new ConfirmationQuestion('Next Page (enter), "n" to quit: ', true);
65-
$table = (new Table($output))->setHeaders([
66-
'name' => 'Name',
67-
'repo_config' => 'Repo Config',
68-
'packagist_config' => 'Packagist Config',
69-
'teams' => 'Teams',
70-
'compliant' => 'Compliant?'
71-
]);
72-
if ($componentName = $input->getOption('component')) {
73-
$table->setVertical();
74-
}
75-
$page = (int) $input->getOption('page');
76-
$resultsPerPage = (int) $input->getOption('results-per-page');
7762
$format = $input->getOption('format');
7863
if (!in_array($format, ['ci', 'table'])) {
7964
throw new InvalidArgumentException('Invalid format "' . $format . '", must be "table" or "ci"');
8065
}
66+
67+
$table = (new Table($output));
68+
$table->setColumnWidths([55, 20, 20, 25, 50]);
69+
$table->setStyle('compact');
70+
$headers = $format == 'ci' ? ['Name', 'Compliance'] : [
71+
'Name',
72+
'Repo Config',
73+
'Packagist Config',
74+
'Teams',
75+
'Compliance'
76+
];
77+
(clone $table)->setHeaders($headers)->render();
78+
79+
$componentName = $input->getOption('component');
8180
$components = $componentName ? [new Component($componentName)] : Component::getComponents();
8281

83-
if (!$input->getOption('token')) {
84-
$output->writeln('<error>No token provided - please provide token to update compliance</error>');
85-
}
82+
$isCompliant = true;
8683
foreach ($components as $i => $component) {
87-
if ($i < (($page-1) * $resultsPerPage)) {
88-
continue;
89-
}
90-
if ($format == 'table' && 0 !== $resultsPerPage && $i >= ($page * $resultsPerPage)) {
91-
$table->render();
92-
if (!$this->getHelper('question')->ask($input, $output, $nextPageQuestion)) {
93-
return 0;
94-
}
95-
$table->setRows([]);
96-
$page++;
97-
}
9884
$details = $this->getRepoDetails($component);
99-
$compliance = [
100-
'settings' => true,
101-
'packagist' => true,
102-
'teams' => true,
103-
];
85+
$settingsCheck = true;
86+
$packagistCheck = true;
87+
$teamCheck = true;
10488

10589
$refreshDetails = false;
10690
if (!$this->checkSettingsCompliance($details)) {
107-
$compliance['settings'] = false;
91+
$settingsCheck = false;
10892
$refreshDetails |= $this->askFixSettingsCompliance($input, $output, $details);
10993
}
11094
if (!$this->checkPackagistCompliance($details)) {
111-
$compliance['packagist'] = false;
95+
$packagistCheck = false;
11296
$refreshDetails |= $this->askFixPackagistCompliance($input, $output, $component->getRepoName());
11397
}
11498
if (!$this->checkTeamCompliance($details)) {
115-
$compliance['teams'] = $this->github->token ? false : null;
99+
$teamCheck = $this->github->token ? false : null;
116100
$refreshDetails |= $this->askFixTeamCompliance($input, $output, $component->getRepoName());
117101
}
118102
if ($refreshDetails) {
@@ -129,39 +113,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
129113
$output->writeln(sprintf('<comment>%s</comment>: Packagist webhook token updated.', $repoName));
130114
}
131115
}
132-
$isCompliant = true;
133-
$details['compliant'] = '<info>REPO IS COMPLIANT</info>';
134-
foreach ($compliance as $key => $val) {
135-
if ($val === false) {
136-
$isCompliant = false;
137-
$details['compliant'] = '<error>NOT COMPLIANT</error>';
138-
} elseif ($isCompliant && $val === null) {
139-
$details['compliant'] = '<error>???</error> (token required)';
140-
$isCompliant = null;
141-
}
142-
}
143-
144-
if ($format == 'ci' ) {
145-
$output->writeln($component->getName() . ': ' . $details['compliant']);
146-
if (!$isCompliant) {
147-
return 1;
148-
}
149-
}
150116

151-
if (!$isCompliant) {
152-
$details['compliant'] .= PHP_EOL . implode("\n", array_map(
153-
fn ($k, $v) => $k . ': ' . (is_null($v) ? '???' : var_export($v, true)),
154-
array_keys($compliance),
155-
array_values($compliance)
156-
));
117+
$emoji = fn (?bool $check) => match ($check) { null => '', true => '', false => ''};
118+
$details['compliant'] = implode("\n", [
119+
sprintf('%s Issues, Projects, Wiki, Pages, and Discussion are disabled', $emoji($settingsCheck)),
120+
sprintf('%s Packagist maintainer is "google-cloud"', $emoji($packagistCheck)),
121+
sprintf('%s Github teams permissions are configured correctly', $emoji($teamCheck)),
122+
'',
123+
]);
124+
125+
$isCompliant = $isCompliant && $settingsCheck && $packagistCheck && $teamCheck;
126+
if ($format == 'ci') {
127+
unset($details['repo_config'], $details['packagist_config'], $details['teams']);
157128
}
158-
$table->addRow($details);
159-
}
160-
if ($format == 'table') {
161-
$table->render();
129+
(clone $table)->addRow($details)->render();
162130
}
163131

164-
return 0;
132+
return $isCompliant ? Command::SUCCESS : Command::FAILURE;
165133
}
166134

167135
private function checkSettingsCompliance(array $details)
@@ -183,8 +151,8 @@ private function checkTeamCompliance(array $details)
183151

184152
private function askFixSettingsCompliance(InputInterface $input, OutputInterface $output, array $details)
185153
{
186-
if (!$this->github->token) {
187-
// without a token, don't ask to fix compliance
154+
if (!$this->github->token || $input->getOption('format') == 'ci') {
155+
// without a token, or in CI mode, don't ask to fix compliance
188156
return false;
189157
}
190158
$explodedConfig = array_map(fn ($line) => explode(': ', $line), explode("\n", $details['repo_config']));
@@ -218,17 +186,17 @@ private function checkPackagistCompliance(array $details)
218186

219187
private function askFixPackagistCompliance(InputInterface $input, OutputInterface $output, array $details)
220188
{
221-
if (!$this->github->token) {
222-
// without a token, don't ask to fix compliance
189+
if (!$this->github->token || $input->getOption('format') == 'ci') {
190+
// without a token, or in CI mode, don't ask to fix compliance
223191
return false;
224192
}
225193
throw new \Exception('not implemented');
226194
}
227195

228196
private function askFixTeamCompliance(InputInterface $input, OutputInterface $output, string $repoName)
229197
{
230-
if (!$this->github->token) {
231-
// without a token, don't ask to fix compliance
198+
if (!$this->github->token || $input->getOption('format') == 'ci') {
199+
// without a token, or in CI mode, don't ask to fix compliance
232200
return false;
233201
}
234202
$question = new ConfirmationQuestion(sprintf(

0 commit comments

Comments
 (0)