* Execute the integrations pull command.
(options: IntegrationsPullOptions)
| 84 | * Execute the integrations pull command. |
| 85 | */ |
| 86 | async function pullIntegrations(options: IntegrationsPullOptions): Promise<void> { |
| 87 | const token = resolveToken(options) |
| 88 | const baseUrl = options.url ?? DEFAULT_API_URL |
| 89 | const filePath = options.file ?? DEFAULT_INTEGRATIONS_FILE |
| 90 | const envFilePath = options.envFile ?? DEFAULT_ENV_FILE |
| 91 | |
| 92 | log(chalk.dim(`Fetching integrations from ${baseUrl}...`)) |
| 93 | |
| 94 | // Fetch integrations from API |
| 95 | const fetchedIntegrations = await fetchIntegrations(baseUrl, token) |
| 96 | |
| 97 | if (fetchedIntegrations.length === 0) { |
| 98 | log(chalk.yellow('No integrations found in your workspace.')) |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | log(chalk.dim(`Found ${fetchedIntegrations.length} integration(s).`)) |
| 103 | |
| 104 | // Read existing document (if any) - preserves comments and formatting |
| 105 | const existingDoc = await readIntegrationsDocument(filePath) |
| 106 | debug(`Read existing document from ${filePath}: ${existingDoc ? 'found' : 'not found'}`) |
| 107 | const doc = existingDoc ?? createNewDocument() |
| 108 | |
| 109 | // Merge API integrations into document and extract secrets |
| 110 | const { secrets, stats, skipped } = mergeApiIntegrationsIntoDocument(doc, fetchedIntegrations) |
| 111 | |
| 112 | // Log any invalid or unsupported integrations that were skipped (debug mode only) |
| 113 | for (const skippedIntegration of skipped) { |
| 114 | debug( |
| 115 | `Skipping invalid or unsupported integration "${skippedIntegration.name}" (${skippedIntegration.type}) [${skippedIntegration.id}]:` |
| 116 | ) |
| 117 | for (const issue of skippedIntegration.issues) { |
| 118 | debug(` ${issue.code} [${issue.path.join('.')}]: ${issue.message}`) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Ensure schema comment is set |
| 123 | if (doc.commentBefore == null || !doc.commentBefore.includes('yaml-language-server')) { |
| 124 | doc.commentBefore = SCHEMA_COMMENT |
| 125 | } |
| 126 | |
| 127 | const secretCount = Object.keys(secrets).length |
| 128 | if (secretCount > 0) { |
| 129 | debug(`Extracted ${secretCount} secret(s) to store in ${envFilePath}`) |
| 130 | } |
| 131 | |
| 132 | // Write secrets to .env file |
| 133 | if (secretCount > 0) { |
| 134 | await updateDotEnv(envFilePath, secrets) |
| 135 | log(chalk.dim(`Updated ${envFilePath} with ${secretCount} secret(s)`)) |
| 136 | } |
| 137 | |
| 138 | // Write YAML file (with comments and formatting preserved) |
| 139 | await writeIntegrationsFile(filePath, doc) |
| 140 | |
| 141 | // Get final count of integrations in the document |
| 142 | const finalIntegrations = doc.get('integrations') |
| 143 | const finalCount = isSeq(finalIntegrations) ? finalIntegrations.items.length : 0 |
no test coverage detected