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
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:
+87
-165
@@ -12,110 +12,125 @@
|
||||
* INGROUP: moko-platform
|
||||
* REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform
|
||||
* PATH: /cli/bulk_workflow_push.php
|
||||
* VERSION: 09.21.00
|
||||
* VERSION: 09.21.07
|
||||
* BRIEF: Push a workflow file to all governed repos via the Gitea Contents API
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class BulkWorkflowPush
|
||||
{
|
||||
private string $giteaUrl = 'https://git.mokoconsulting.tech';
|
||||
private string $token = '';
|
||||
private string $org = '';
|
||||
private string $workflowFile = '';
|
||||
private string $destPath = '';
|
||||
private string $branch = 'main';
|
||||
private bool $dryRun = false;
|
||||
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
|
||||
|
||||
use MokoEnterprise\CliFramework;
|
||||
|
||||
class BulkWorkflowPushCli extends CliFramework
|
||||
{
|
||||
private int $updated = 0;
|
||||
private int $created = 0;
|
||||
private int $skipped = 0;
|
||||
private int $errors = 0;
|
||||
|
||||
public function run(): int
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->parseArgs();
|
||||
$this->setDescription('Push a workflow file to all governed repos via the Gitea Contents API');
|
||||
$this->addArgument('--gitea-url', 'Gitea URL (default: https://git.mokoconsulting.tech)', 'https://git.mokoconsulting.tech');
|
||||
$this->addArgument('--token', 'Gitea API token', '');
|
||||
$this->addArgument('--org', 'Target organization', '');
|
||||
$this->addArgument('--file', 'Local workflow file to push', '');
|
||||
$this->addArgument('--dest', 'Destination path in repos (default: .mokogitea/workflows/<filename>)', '');
|
||||
$this->addArgument('--branch', 'Target branch (default: main)', 'main');
|
||||
}
|
||||
|
||||
if ($this->token === '') {
|
||||
$this->log('ERROR: --token is required.');
|
||||
$this->printUsage();
|
||||
protected function run(): int
|
||||
{
|
||||
$giteaUrl = rtrim($this->getArgument('--gitea-url'), '/');
|
||||
$token = $this->getArgument('--token');
|
||||
$org = $this->getArgument('--org');
|
||||
$workflowFile = $this->getArgument('--file');
|
||||
$destPath = $this->getArgument('--dest');
|
||||
$branch = $this->getArgument('--branch');
|
||||
|
||||
if ($token === '') {
|
||||
$this->log('ERROR', '--token is required.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ($this->workflowFile === '') {
|
||||
$this->log('ERROR: --file is required.');
|
||||
$this->printUsage();
|
||||
if ($workflowFile === '') {
|
||||
$this->log('ERROR', '--file is required.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!file_exists($this->workflowFile)) {
|
||||
$this->log("ERROR: File not found: {$this->workflowFile}");
|
||||
if (!file_exists($workflowFile)) {
|
||||
$this->log('ERROR', "File not found: {$workflowFile}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ($this->org === '') {
|
||||
$this->log('ERROR: --org is required.');
|
||||
$this->printUsage();
|
||||
if ($org === '') {
|
||||
$this->log('ERROR', '--org is required.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ($this->destPath === '') {
|
||||
$this->destPath = '.mokogitea/workflows/' . basename($this->workflowFile);
|
||||
if ($destPath === '') {
|
||||
$destPath = '.mokogitea/workflows/' . basename($workflowFile);
|
||||
}
|
||||
|
||||
$localContent = file_get_contents($this->workflowFile);
|
||||
$localContent = file_get_contents($workflowFile);
|
||||
|
||||
if ($localContent === false) {
|
||||
$this->log("ERROR: Could not read file: {$this->workflowFile}");
|
||||
$this->log('ERROR', "Could not read file: {$workflowFile}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->log("Pushing: {$this->workflowFile}");
|
||||
$this->log(" -> {$this->destPath} (branch: {$this->branch})");
|
||||
$this->log(" -> Org: {$this->org} @ {$this->giteaUrl}");
|
||||
$this->log('INFO', "Pushing: {$workflowFile}");
|
||||
$this->log('INFO', " -> {$destPath} (branch: {$branch})");
|
||||
$this->log('INFO', " -> Org: {$org} @ {$giteaUrl}");
|
||||
|
||||
if ($this->dryRun) {
|
||||
$this->log('[DRY RUN] No changes will be made.');
|
||||
$this->log('INFO', '[DRY RUN] No changes will be made.');
|
||||
}
|
||||
|
||||
$this->log('');
|
||||
echo "\n";
|
||||
|
||||
$repos = $this->fetchOrgRepos();
|
||||
$repos = $this->fetchOrgRepos($giteaUrl, $token, $org);
|
||||
|
||||
if ($repos === null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->log("Found " . count($repos) . " repo(s) in \"{$this->org}\".");
|
||||
$this->log('');
|
||||
$this->log(sprintf('%-45s | %s', 'Repo', 'Status'));
|
||||
$this->log(str_repeat('-', 70));
|
||||
$this->log('INFO', "Found " . count($repos) . " repo(s) in \"{$org}\".");
|
||||
echo "\n";
|
||||
fprintf(STDERR, "%-45s | %s\n", 'Repo', 'Status');
|
||||
fprintf(STDERR, "%s\n", str_repeat('-', 70));
|
||||
|
||||
$encodedContent = base64_encode($localContent);
|
||||
|
||||
foreach ($repos as $repo) {
|
||||
$this->pushToRepo($repo, $encodedContent, $localContent);
|
||||
$this->pushToRepo($giteaUrl, $token, $repo, $encodedContent, $localContent, $destPath, $branch);
|
||||
}
|
||||
|
||||
$this->log('');
|
||||
$this->log("Done: {$this->created} created, {$this->updated} updated, "
|
||||
echo "\n";
|
||||
$this->log('INFO', "Done: {$this->created} created, {$this->updated} updated, "
|
||||
. "{$this->skipped} skipped, {$this->errors} error(s).");
|
||||
|
||||
return $this->errors > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
private function pushToRepo(
|
||||
string $giteaUrl,
|
||||
string $token,
|
||||
string $repoFullName,
|
||||
string $encodedContent,
|
||||
string $localContent
|
||||
string $localContent,
|
||||
string $destPath,
|
||||
string $branch
|
||||
): void {
|
||||
[$owner, $repoName] = explode('/', $repoFullName, 2);
|
||||
|
||||
$existing = $this->apiRequest(
|
||||
$giteaUrl,
|
||||
$token,
|
||||
'GET',
|
||||
"/api/v1/repos/{$owner}/{$repoName}/contents/"
|
||||
. "{$this->destPath}?ref={$this->branch}"
|
||||
. "{$destPath}?ref={$branch}"
|
||||
);
|
||||
|
||||
if ($existing['code'] === 200) {
|
||||
@@ -124,21 +139,13 @@ final class BulkWorkflowPush
|
||||
$remoteContent = base64_decode($data['content'] ?? '');
|
||||
|
||||
if ($remoteContent === $localContent) {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
'IDENTICAL (skipped)'
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, 'IDENTICAL (skipped)');
|
||||
$this->skipped++;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->dryRun) {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
'WOULD UPDATE'
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, 'WOULD UPDATE');
|
||||
$this->updated++;
|
||||
return;
|
||||
}
|
||||
@@ -146,100 +153,82 @@ final class BulkWorkflowPush
|
||||
$payload = json_encode([
|
||||
'content' => $encodedContent,
|
||||
'sha' => $remoteSha,
|
||||
'message' => "chore: sync {$this->destPath} "
|
||||
'message' => "chore: sync {$destPath} "
|
||||
. "from moko-platform [skip ci]",
|
||||
'branch' => $this->branch,
|
||||
'branch' => $branch,
|
||||
]);
|
||||
|
||||
$response = $this->apiRequest(
|
||||
$giteaUrl,
|
||||
$token,
|
||||
'PUT',
|
||||
"/api/v1/repos/{$owner}/{$repoName}/contents/"
|
||||
. $this->destPath,
|
||||
. $destPath,
|
||||
$payload
|
||||
);
|
||||
|
||||
if ($response['code'] === 200) {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
'UPDATED'
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, 'UPDATED');
|
||||
$this->updated++;
|
||||
} else {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
"ERROR (HTTP {$response['code']})"
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, "ERROR (HTTP {$response['code']})");
|
||||
$this->errors++;
|
||||
}
|
||||
} elseif ($existing['code'] === 404) {
|
||||
if ($this->dryRun) {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
'WOULD CREATE'
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, 'WOULD CREATE');
|
||||
$this->created++;
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = json_encode([
|
||||
'content' => $encodedContent,
|
||||
'message' => "chore: add {$this->destPath} "
|
||||
'message' => "chore: add {$destPath} "
|
||||
. "from moko-platform [skip ci]",
|
||||
'branch' => $this->branch,
|
||||
'branch' => $branch,
|
||||
]);
|
||||
|
||||
$response = $this->apiRequest(
|
||||
$giteaUrl,
|
||||
$token,
|
||||
'POST',
|
||||
"/api/v1/repos/{$owner}/{$repoName}/contents/"
|
||||
. $this->destPath,
|
||||
. $destPath,
|
||||
$payload
|
||||
);
|
||||
|
||||
if ($response['code'] === 201) {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
'CREATED'
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, 'CREATED');
|
||||
$this->created++;
|
||||
} else {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
"ERROR (HTTP {$response['code']})"
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, "ERROR (HTTP {$response['code']})");
|
||||
$this->errors++;
|
||||
}
|
||||
} else {
|
||||
$this->log(sprintf(
|
||||
'%-45s | %s',
|
||||
$repoFullName,
|
||||
"ERROR (HTTP {$existing['code']})"
|
||||
));
|
||||
fprintf(STDERR, "%-45s | %s\n", $repoFullName, "ERROR (HTTP {$existing['code']})");
|
||||
$this->errors++;
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchOrgRepos(): ?array
|
||||
private function fetchOrgRepos(string $giteaUrl, string $token, string $org): ?array
|
||||
{
|
||||
$this->log("Fetching repos from org: {$this->org}");
|
||||
$this->log('INFO', "Fetching repos from org: {$org}");
|
||||
|
||||
$page = 1;
|
||||
$repos = [];
|
||||
|
||||
while (true) {
|
||||
$response = $this->apiRequest(
|
||||
$giteaUrl,
|
||||
$token,
|
||||
'GET',
|
||||
"/api/v1/orgs/{$this->org}/repos?"
|
||||
"/api/v1/orgs/{$org}/repos?"
|
||||
. "limit=50&page={$page}"
|
||||
);
|
||||
|
||||
if ($response['code'] < 200 || $response['code'] >= 300) {
|
||||
if ($page === 1) {
|
||||
$this->log("ERROR: Could not fetch repos "
|
||||
$this->log('ERROR', "Could not fetch repos "
|
||||
. "(HTTP {$response['code']}).");
|
||||
return null;
|
||||
}
|
||||
@@ -271,76 +260,14 @@ final class BulkWorkflowPush
|
||||
return $repos;
|
||||
}
|
||||
|
||||
private function parseArgs(): void
|
||||
{
|
||||
$args = $_SERVER['argv'] ?? [];
|
||||
$count = count($args);
|
||||
|
||||
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 '--org':
|
||||
$this->org = $args[++$i] ?? '';
|
||||
break;
|
||||
case '--file':
|
||||
$this->workflowFile = $args[++$i] ?? '';
|
||||
break;
|
||||
case '--dest':
|
||||
$this->destPath = $args[++$i] ?? '';
|
||||
break;
|
||||
case '--branch':
|
||||
$this->branch = $args[++$i] ?? 'main';
|
||||
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 printUsage(): void
|
||||
{
|
||||
$this->log(
|
||||
'Usage: bulk_workflow_push.php '
|
||||
. '--token <token> --file <path> --org <org> [options]'
|
||||
);
|
||||
$this->log('');
|
||||
$this->log(
|
||||
'Push a workflow file from moko-platform '
|
||||
. 'to all governed repos.'
|
||||
);
|
||||
$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(' --org <org> Target organization');
|
||||
$this->log(' --file <path> Local workflow file to push');
|
||||
$this->log(' --dest <path> Destination path in repos '
|
||||
. '(default: .mokogitea/workflows/<filename>)');
|
||||
$this->log(' --branch <branch> Target branch (default: main)');
|
||||
$this->log(' --dry-run Show what would be done');
|
||||
$this->log(' --help, -h Show this help');
|
||||
}
|
||||
|
||||
private function apiRequest(
|
||||
string $giteaUrl,
|
||||
string $token,
|
||||
string $method,
|
||||
string $endpoint,
|
||||
?string $body = null
|
||||
): array {
|
||||
$url = $this->giteaUrl . $endpoint;
|
||||
$url = $giteaUrl . $endpoint;
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
@@ -349,7 +276,7 @@ final class BulkWorkflowPush
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
"Authorization: token {$this->token}",
|
||||
"Authorization: token {$token}",
|
||||
]);
|
||||
|
||||
if ($body !== null) {
|
||||
@@ -376,12 +303,7 @@ final class BulkWorkflowPush
|
||||
|
||||
return ['code' => $httpCode, 'body' => $responseBody];
|
||||
}
|
||||
|
||||
private function log(string $message): void
|
||||
{
|
||||
fwrite(STDERR, $message . PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
$app = new BulkWorkflowPush();
|
||||
exit($app->run());
|
||||
$app = new BulkWorkflowPushCli();
|
||||
exit($app->execute());
|
||||
|
||||
Reference in New Issue
Block a user