Spaces:
Sleeping
Sleeping
| import { execFile } from 'node:child_process'; | |
| import { promisify } from 'node:util'; | |
| const execFileAsync = promisify(execFile); | |
| function parseArgs(argv) { | |
| const out = {}; | |
| for (let i = 0; i < argv.length; i += 1) { | |
| const token = argv[i]; | |
| if (!token.startsWith('--')) continue; | |
| const key = token.slice(2); | |
| const next = argv[i + 1]; | |
| if (!next || next.startsWith('--')) { | |
| out[key] = 'true'; | |
| continue; | |
| } | |
| out[key] = next; | |
| i += 1; | |
| } | |
| return out; | |
| } | |
| async function safeGh(args) { | |
| try { | |
| const { stdout } = await execFileAsync('gh', args, { cwd: process.cwd() }); | |
| return { ok: true, stdout }; | |
| } catch (error) { | |
| return { ok: false, error: error instanceof Error ? error.message : String(error) }; | |
| } | |
| } | |
| async function main() { | |
| const args = parseArgs(process.argv.slice(2)); | |
| const repo = args.repo || 'Delqhi/sin-github-issues'; | |
| const workflow = args.workflow || '.github/workflows/deploy-huggingface-space.yml'; | |
| const repoMeta = await safeGh(['repo', 'view', repo, '--json', 'name,isPrivate,url,defaultBranchRef']); | |
| const secrets = await safeGh(['secret', 'list', '-R', repo, '--json', 'name,updatedAt,visibility']); | |
| const workflows = await safeGh(['workflow', 'list', '-R', repo, '--json', 'name,path,state,id']); | |
| const runs = await safeGh(['run', 'list', '-R', repo, '--workflow', workflow, '--limit', '3', '--json', 'databaseId,status,conclusion,headSha,createdAt,name,workflowName,url,jobs']); | |
| const parsedSecrets = secrets.ok ? JSON.parse(secrets.stdout || '[]') : []; | |
| const parsedWorkflows = workflows.ok ? JSON.parse(workflows.stdout || '[]') : []; | |
| const parsedRuns = runs.ok ? JSON.parse(runs.stdout || '[]') : []; | |
| const hasHfTokenSecret = parsedSecrets.some((entry) => entry.name === 'HF_TOKEN'); | |
| const deployWorkflow = parsedWorkflows.find((entry) => entry.path === workflow || entry.name === workflow || entry.name === 'Deploy Hugging Face Space'); | |
| const latestRun = parsedRuns[0] || null; | |
| const failures = []; | |
| if (!repoMeta.ok) failures.push('repo_view_failed'); | |
| if (!secrets.ok) failures.push('secret_list_failed'); | |
| if (!workflows.ok) failures.push('workflow_list_failed'); | |
| if (!deployWorkflow) failures.push('deploy_workflow_missing'); | |
| if (!hasHfTokenSecret) failures.push('hf_token_secret_missing'); | |
| if (latestRun && latestRun.conclusion === 'failure' && Array.isArray(latestRun.jobs) && latestRun.jobs.length === 0) { | |
| failures.push('latest_deploy_run_failed_without_jobs'); | |
| } | |
| const output = { | |
| ok: failures.length === 0, | |
| repo, | |
| workflow, | |
| repoMeta: repoMeta.ok ? JSON.parse(repoMeta.stdout) : { error: repoMeta.error }, | |
| hasHfTokenSecret, | |
| deployWorkflow: deployWorkflow || null, | |
| latestRun, | |
| failures, | |
| }; | |
| console.log(JSON.stringify(output, null, 2)); | |
| if (failures.length > 0) process.exitCode = 1; | |
| } | |
| main().catch((error) => { | |
| console.error(error instanceof Error ? error.message : String(error)); | |
| process.exit(1); | |
| }); | |