import { mkdirSync, writeFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { buildConsumerSupportBundleMarkdown } from './consumer-startup-failure-support-bundle-lib'; import type { ConsumerSupportBundleCliOptions } from './consumer-support-bundle-contract'; import { collectConsumerSupportBundleRunDiagnostics, ensureConsumerSupportBundleAuth, loadConsumerSupportBundleActionsPermissions, loadConsumerSupportBundleBillingInfo, loadConsumerSupportBundleRepoInfo, loadConsumerSupportBundleWorkflowRuns, } from './consumer-support-bundle-gh-lib'; export const buildConsumerSupportBundleReport = ( options: ConsumerSupportBundleCliOptions ): string => { const authStatus = ensureConsumerSupportBundleAuth(); const repoInfoResult = loadConsumerSupportBundleRepoInfo(options.repo); const actionsPermissionsResult = loadConsumerSupportBundleActionsPermissions(options.repo); const billingResult = loadConsumerSupportBundleBillingInfo(options.repo); const runs = loadConsumerSupportBundleWorkflowRuns(options); const diagnostics = collectConsumerSupportBundleRunDiagnostics({ repo: options.repo, runs, }); return buildConsumerSupportBundleMarkdown({ generatedAtIso: new Date().toISOString(), options, authStatus, repoInfo: repoInfoResult.ok ? repoInfoResult.data : undefined, actionsPermissions: actionsPermissionsResult.ok ? actionsPermissionsResult.data : undefined, actionsPermissionsError: actionsPermissionsResult.ok ? undefined : actionsPermissionsResult.error, billingInfo: billingResult.ok ? billingResult.data : undefined, billingError: billingResult.ok ? undefined : billingResult.error, runs, diagnostics, }); }; export const writeConsumerSupportBundleReport = (params: { markdown: string; outFile: string; }): string => { const outputPath = resolve(process.cwd(), params.outFile); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, params.markdown, 'utf8'); return outputPath; };