refactor(cli): migrate 64 legacy scripts to CliFramework (#235)
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (push) Blocked by required conditions
Platform: moko-platform CI / Gate 5: Template Integrity (push) Blocked by required conditions
Platform: moko-platform CI / CI Summary (push) Blocked by required conditions
Generic: Repo Health / Release configuration (push) Blocked by required conditions
Generic: Repo Health / Scripts governance (push) Blocked by required conditions
Generic: Repo Health / Repository health (push) Blocked by required conditions
Generic: Repo Health / Site Health (push) Has been skipped
Generic: Repo Health / Access control (push) Successful in 2s
Platform: moko-platform CI / Gate 1: Code Quality (push) Failing after 36s

Wrap all CLI tools in cli/, automation/, maintenance/, deploy/, and
release/ in classes extending CliFramework. Replaces manual $argv
parsing with configure()/addArgument(), moves logic into run(): int,
and converts fwrite(STDERR,...) to $this->log(). Two CLIApp subclasses
(generate_dolibarr_version_txt, generate_joomla_update_xml) converted
to extend CliFramework directly.

Every script now gets free --help, --verbose, --quiet, --dry-run,
--json, --no-color, banners, coloured logging, and progress bars.

Authored-by: Moko Consulting
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan Miller
2026-05-31 11:39:10 -05:00
parent af77e9d361
commit b3d9ee8255
64 changed files with 9792 additions and 11200 deletions
+174 -249
View File
@@ -11,309 +11,234 @@
* INGROUP: moko-platform
* REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform
* PATH: /cli/bulk_workflow_trigger.php
* VERSION: 09.21.00
* VERSION: 09.21.07
* BRIEF: Trigger a workflow across multiple repos at once
*/
declare(strict_types=1);
final class BulkWorkflowTrigger
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
use MokoEnterprise\CliFramework;
class BulkWorkflowTriggerCli extends CliFramework
{
private string $giteaUrl = 'https://git.mokoconsulting.tech';
private string $token = '';
private string $reposFile = '';
private string $org = '';
private string $workflow = '';
private string $ref = 'main';
private string $inputs = '';
private bool $dryRun = false;
private string $giteaUrl = 'https://git.mokoconsulting.tech';
private string $token = '';
private string $reposFile = '';
private string $org = '';
private string $workflow = '';
private string $ref = 'main';
private string $inputs = '';
public function run(): int
{
$this->parseArgs();
protected function configure(): void
{
$this->setDescription('Trigger a workflow across multiple repos at once');
$this->addArgument('--gitea-url', 'Gitea URL (default: https://git.mokoconsulting.tech)', 'https://git.mokoconsulting.tech');
$this->addArgument('--token', 'Gitea API token', '');
$this->addArgument('--repos', 'File with newline-separated owner/repo list', '');
$this->addArgument('--org', 'Trigger on all repos in an org', '');
$this->addArgument('--workflow', 'Workflow file (e.g., "sync-servers.yml")', '');
$this->addArgument('--ref', 'Branch ref (default: "main")', 'main');
$this->addArgument('--inputs', 'Workflow inputs as JSON string', '');
}
if ($this->token === '')
{
$this->log('ERROR: --token is required.');
$this->printUsage();
return 1;
}
protected function run(): int
{
$this->giteaUrl = rtrim($this->getArgument('--gitea-url'), '/');
$this->token = $this->getArgument('--token');
$this->reposFile = $this->getArgument('--repos');
$this->org = $this->getArgument('--org');
$this->workflow = $this->getArgument('--workflow');
$this->ref = $this->getArgument('--ref');
$this->inputs = $this->getArgument('--inputs');
if ($this->workflow === '')
{
$this->log('ERROR: --workflow is required.');
$this->printUsage();
return 1;
}
if ($this->token === '') {
$this->log('ERROR', '--token is required.');
return 1;
}
if ($this->reposFile === '' && $this->org === '')
{
$this->log('ERROR: Either --repos <file> or --org <org> is required.');
$this->printUsage();
return 1;
}
if ($this->workflow === '') {
$this->log('ERROR', '--workflow is required.');
return 1;
}
// Build repo list
$repos = $this->buildRepoList();
if ($this->reposFile === '' && $this->org === '') {
$this->log('ERROR', 'Either --repos <file> or --org <org> is required.');
return 1;
}
if ($repos === null || count($repos) === 0)
{
$this->log('ERROR: No repos found to process.');
return 1;
}
// Build repo list
$repos = $this->buildRepoList();
$this->log("Triggering workflow \"{$this->workflow}\" on ref \"{$this->ref}\" across " . count($repos) . " repo(s).");
$this->log("Gitea URL: {$this->giteaUrl}");
if ($repos === null || count($repos) === 0) {
$this->log('ERROR', 'No repos found to process.');
return 1;
}
if ($this->dryRun)
{
$this->log('[DRY RUN] No requests will be sent.');
}
$this->log('INFO', "Triggering workflow \"{$this->workflow}\" on ref \"{$this->ref}\" across " . count($repos) . " repo(s).");
$this->log('INFO', "Gitea URL: {$this->giteaUrl}");
$this->log('');
if ($this->dryRun) {
$this->log('INFO', '[DRY RUN] No requests will be sent.');
}
// Parse inputs
$inputsDecoded = null;
$this->log('INFO', '');
if ($this->inputs !== '')
{
$inputsDecoded = json_decode($this->inputs, true);
// Parse inputs
$inputsDecoded = null;
if (!is_array($inputsDecoded))
{
$this->log('ERROR: --inputs must be valid JSON.');
return 1;
}
}
if ($this->inputs !== '') {
$inputsDecoded = json_decode($this->inputs, true);
// Print header
$this->log(sprintf('%-40s | %s', 'Repo', 'Status'));
$this->log(str_repeat('-', 60));
if (!is_array($inputsDecoded)) {
$this->log('ERROR', '--inputs must be valid JSON.');
return 1;
}
}
$failCount = 0;
// Print header
$this->log('INFO', sprintf('%-40s | %s', 'Repo', 'Status'));
$this->log('INFO', str_repeat('-', 60));
foreach ($repos as $repo)
{
$repo = trim($repo);
$failCount = 0;
if ($repo === '' || strpos($repo, '/') === false)
{
continue;
}
foreach ($repos as $repo) {
$repo = trim($repo);
[$owner, $repoName] = explode('/', $repo, 2);
if ($repo === '' || strpos($repo, '/') === false) {
continue;
}
if ($this->dryRun)
{
$this->log(sprintf('%-40s | %s', $repo, 'DRY RUN (skipped)'));
continue;
}
[$owner, $repoName] = explode('/', $repo, 2);
$payload = ['ref' => $this->ref];
if ($this->dryRun) {
$this->log('INFO', sprintf('%-40s | %s', $repo, 'DRY RUN (skipped)'));
continue;
}
if ($inputsDecoded !== null)
{
$payload['inputs'] = $inputsDecoded;
}
$payload = ['ref' => $this->ref];
$response = $this->apiRequest(
'POST',
"/api/v1/repos/{$owner}/{$repoName}/actions/workflows/{$this->workflow}/dispatches",
json_encode($payload)
);
if ($inputsDecoded !== null) {
$payload['inputs'] = $inputsDecoded;
}
if ($response['code'] >= 200 && $response['code'] < 300)
{
$status = 'TRIGGERED';
}
elseif ($response['code'] === 404)
{
$status = 'FAILED (not found)';
$failCount++;
}
elseif ($response['code'] === 422)
{
$status = 'SKIPPED (unprocessable)';
}
else
{
$status = "FAILED (HTTP {$response['code']})";
$failCount++;
}
$response = $this->apiRequest(
'POST',
"/api/v1/repos/{$owner}/{$repoName}/actions/workflows/{$this->workflow}/dispatches",
json_encode($payload)
);
$this->log(sprintf('%-40s | %s', $repo, $status));
}
if ($response['code'] >= 200 && $response['code'] < 300) {
$status = 'TRIGGERED';
} elseif ($response['code'] === 404) {
$status = 'FAILED (not found)';
$failCount++;
} elseif ($response['code'] === 422) {
$status = 'SKIPPED (unprocessable)';
} else {
$status = "FAILED (HTTP {$response['code']})";
$failCount++;
}
$this->log('');
$this->log('Done. ' . ($failCount > 0 ? "{$failCount} failure(s)." : 'All succeeded.'));
$this->log('INFO', sprintf('%-40s | %s', $repo, $status));
}
return $failCount > 0 ? 1 : 0;
}
$this->log('INFO', '');
$this->log('INFO', 'Done. ' . ($failCount > 0 ? "{$failCount} failure(s)." : 'All succeeded.'));
private function parseArgs(): void
{
$args = $_SERVER['argv'] ?? [];
$count = count($args);
return $failCount > 0 ? 1 : 0;
}
for ($i = 1; $i < $count; $i++)
{
switch ($args[$i])
{
case '--gitea-url':
$this->giteaUrl = rtrim($args[++$i] ?? '', '/');
break;
case '--token':
$this->token = $args[++$i] ?? '';
break;
case '--repos':
$this->reposFile = $args[++$i] ?? '';
break;
case '--org':
$this->org = $args[++$i] ?? '';
break;
case '--workflow':
$this->workflow = $args[++$i] ?? '';
break;
case '--ref':
$this->ref = $args[++$i] ?? 'main';
break;
case '--inputs':
$this->inputs = $args[++$i] ?? '';
break;
case '--dry-run':
$this->dryRun = true;
break;
case '--help':
case '-h':
$this->printUsage();
exit(0);
default:
$this->log("WARNING: Unknown argument: {$args[$i]}");
break;
}
}
}
private function buildRepoList(): ?array
{
if ($this->reposFile !== '') {
if (!file_exists($this->reposFile)) {
$this->log('ERROR', "Repos file not found: {$this->reposFile}");
return null;
}
private function printUsage(): void
{
$this->log('Usage: bulk_workflow_trigger.php --token <token> --workflow <file> [options]');
$this->log('');
$this->log('Options:');
$this->log(' --gitea-url <url> Gitea URL (default: https://git.mokoconsulting.tech)');
$this->log(' --token <token> Gitea API token');
$this->log(' --repos <file> File with newline-separated owner/repo list');
$this->log(' --org <org> Trigger on all repos in an org');
$this->log(' --workflow <filename> Workflow file (e.g., "sync-servers.yml")');
$this->log(' --ref <branch> Branch ref (default: "main")');
$this->log(' --inputs <json> Workflow inputs as JSON string');
$this->log(' --dry-run Show what would be done without triggering');
$this->log(' --help, -h Show this help');
}
$content = file_get_contents($this->reposFile);
$lines = array_filter(array_map('trim', explode("\n", $content)), function (string $line): bool {
return $line !== '' && $line[0] !== '#';
});
private function buildRepoList(): ?array
{
if ($this->reposFile !== '')
{
if (!file_exists($this->reposFile))
{
$this->log("ERROR: Repos file not found: {$this->reposFile}");
return null;
}
return array_values($lines);
}
$content = file_get_contents($this->reposFile);
$lines = array_filter(array_map('trim', explode("\n", $content)), function (string $line): bool {
return $line !== '' && $line[0] !== '#';
});
// Fetch all repos from org
$this->log('INFO', "Fetching repos from org: {$this->org}");
return array_values($lines);
}
$page = 1;
$repos = [];
// Fetch all repos from org
$this->log("Fetching repos from org: {$this->org}");
while (true) {
$response = $this->apiRequest('GET', "/api/v1/orgs/{$this->org}/repos?limit=50&page={$page}");
$page = 1;
$repos = [];
if ($response['code'] < 200 || $response['code'] >= 300) {
if ($page === 1) {
$this->log('ERROR', "Could not fetch repos for org (HTTP {$response['code']}).");
return null;
}
while (true)
{
$response = $this->apiRequest('GET', "/api/v1/orgs/{$this->org}/repos?limit=50&page={$page}");
break;
}
if ($response['code'] < 200 || $response['code'] >= 300)
{
if ($page === 1)
{
$this->log("ERROR: Could not fetch repos for org (HTTP {$response['code']}).");
return null;
}
$data = json_decode($response['body'], true);
break;
}
if (!is_array($data) || count($data) === 0) {
break;
}
$data = json_decode($response['body'], true);
foreach ($data as $repo) {
$fullName = $repo['full_name'] ?? '';
if (!is_array($data) || count($data) === 0)
{
break;
}
if ($fullName !== '') {
$repos[] = $fullName;
}
}
foreach ($data as $repo)
{
$fullName = $repo['full_name'] ?? '';
$page++;
}
if ($fullName !== '')
{
$repos[] = $fullName;
}
}
$this->log('INFO', 'Found ' . count($repos) . " repo(s) in org \"{$this->org}\".");
$page++;
}
return $repos;
}
$this->log('Found ' . count($repos) . " repo(s) in org \"{$this->org}\".");
private function apiRequest(string $method, string $endpoint, ?string $body = null): array
{
$url = $this->giteaUrl . $endpoint;
return $repos;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
"Authorization: token {$this->token}",
]);
private function apiRequest(string $method, string $endpoint, ?string $body = null): array
{
$url = $this->giteaUrl . $endpoint;
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
"Authorization: token {$this->token}",
]);
$responseBody = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($body !== null)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
$responseBody = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
return ['code' => 0, 'body' => "cURL error: {$error}"];
}
if (curl_errno($ch))
{
$error = curl_error($ch);
curl_close($ch);
curl_close($ch);
return ['code' => 0, 'body' => "cURL error: {$error}"];
}
curl_close($ch);
return ['code' => $httpCode, 'body' => $responseBody];
}
private function log(string $message): void
{
fwrite(STDERR, $message . PHP_EOL);
}
return ['code' => $httpCode, 'body' => $responseBody];
}
}
$app = new BulkWorkflowTrigger();
exit($app->run());
$app = new BulkWorkflowTriggerCli();
exit($app->execute());