|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 14 | + * for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +/* eslint-env node */ |
| 22 | + |
| 23 | +const fs = require('fs'); |
| 24 | +const path = require('path'); |
| 25 | +const execSync = require('child_process').execSync; |
| 26 | + |
| 27 | +/** |
| 28 | + * Conditionally clones the phoenix-pro repository if environment variables are set. |
| 29 | + * |
| 30 | + * Behavior: |
| 31 | + * - If env vars not set: Skip clone, build continues (community builds) |
| 32 | + * - If env vars set but clone fails: Build FAILS (credentials configured but clone failed) |
| 33 | + * - If directory exists with correct commit: Skip clone, build continues |
| 34 | + * - If directory exists with wrong commit: Log warning, build continues (respect local changes) |
| 35 | + */ |
| 36 | +function clonePhoenixProRepo() { |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + // this is only expected to be hit in github actions environment. |
| 39 | + // in normal builds, we will bail out as soon as we detect that the environmental vars are note present. |
| 40 | + |
| 41 | + const proRepoUrl = process.env.PRO_REPO_URL; |
| 42 | + const proRepoToken = process.env.PRO_REPO_ACCESS_TOKEN; |
| 43 | + const targetDir = path.resolve(__dirname, '../src/extensionsIntegrated/phoenix-pro'); |
| 44 | + |
| 45 | + // Check if both environment variables are set |
| 46 | + if (!proRepoUrl || !proRepoToken) { |
| 47 | + // this si what will happen in most dev builds. |
| 48 | + console.log('Skipping phoenix-pro clone: PRO_REPO_URL or PRO_REPO_ACCESS_TOKEN not set'); |
| 49 | + console.log('This is expected for community builds'); |
| 50 | + resolve(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // all code below is only likely to be executed in the ci environment |
| 55 | + |
| 56 | + // Check if directory already exists |
| 57 | + if (fs.existsSync(targetDir)) { |
| 58 | + console.log('phoenix-pro directory already exists at:', targetDir); |
| 59 | + |
| 60 | + // Check if it's a git repository |
| 61 | + const gitDir = path.join(targetDir, '.git'); |
| 62 | + if (fs.existsSync(gitDir)) { |
| 63 | + try { |
| 64 | + // Verify current commit |
| 65 | + const trackingRepos = require('../tracking-repos.json'); |
| 66 | + const expectedCommit = trackingRepos.phoenixPro.commitID; |
| 67 | + const currentCommit = execSync('git rev-parse HEAD', { |
| 68 | + cwd: targetDir, |
| 69 | + encoding: 'utf8' |
| 70 | + }).trim(); |
| 71 | + |
| 72 | + if (currentCommit === expectedCommit) { |
| 73 | + console.log(`✓ phoenix-pro is already at the correct commit: ${expectedCommit}`); |
| 74 | + resolve(); |
| 75 | + return; |
| 76 | + } else { |
| 77 | + // this code will only reach in ci envs with teh env variables, so ward if the commit |
| 78 | + // is not what we expect. |
| 79 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 80 | + console.error(`Error: phoenix-pro is at commit ${currentCommit.substring(0, 8)}`); |
| 81 | + console.error(` but tracking-repos.json specifies ${expectedCommit.substring(0, 8)}`); |
| 82 | + console.error('Not building incorrect binary.'); |
| 83 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 84 | + reject(); |
| 85 | + return; |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + console.error(`Error: Could not verify phoenix-pro commit: ${error.message}`); |
| 89 | + console.error('Not building incorrect binary.'); |
| 90 | + reject(); |
| 91 | + return; |
| 92 | + } |
| 93 | + } else { |
| 94 | + console.warn('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 95 | + console.warn('Error: phoenix-pro directory exists but is not a git repository'); |
| 96 | + console.error('Not building incorrect binary as it could not be verified.'); |
| 97 | + console.warn('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 98 | + reject(); |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Perform the clone operation |
| 104 | + try { |
| 105 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 106 | + console.log('Cloning phoenix-pro repository...'); |
| 107 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 108 | + |
| 109 | + // Load target commit from tracking-repos.json |
| 110 | + const trackingRepos = require('../tracking-repos.json'); |
| 111 | + const commitID = trackingRepos.phoenixPro.commitID; |
| 112 | + console.log(`Target commit: ${commitID}`); |
| 113 | + |
| 114 | + // Construct authenticated URL |
| 115 | + const authUrl = proRepoUrl.replace('https://', `https://oauth2:${proRepoToken}@`); |
| 116 | + |
| 117 | + // Step 1: Shallow clone |
| 118 | + console.log('Step 1/3: Cloning repository (shallow clone)...'); |
| 119 | + execSync(`git clone --depth 1 "${authUrl}" "${targetDir}"`, { |
| 120 | + stdio: ['pipe', 'pipe', 'inherit'] // Hide stdout (may contain token), show stderr |
| 121 | + }); |
| 122 | + console.log('✓ Clone completed'); |
| 123 | + |
| 124 | + // Step 2: Fetch specific commit |
| 125 | + console.log(`Step 2/3: Fetching specific commit: ${commitID}...`); |
| 126 | + try { |
| 127 | + execSync(`git fetch --depth 1 origin ${commitID}`, { |
| 128 | + cwd: targetDir, |
| 129 | + stdio: ['pipe', 'pipe', 'inherit'] |
| 130 | + }); |
| 131 | + console.log('✓ Fetch completed'); |
| 132 | + } catch (fetchError) { |
| 133 | + // Commit might already be in shallow clone |
| 134 | + console.log(' (Commit may already be present in shallow clone)'); |
| 135 | + } |
| 136 | + |
| 137 | + // Step 3: Checkout specific commit |
| 138 | + console.log(`Step 3/3: Checking out commit: ${commitID}...`); |
| 139 | + execSync(`git checkout ${commitID}`, { |
| 140 | + cwd: targetDir, |
| 141 | + stdio: ['pipe', 'pipe', 'inherit'] |
| 142 | + }); |
| 143 | + console.log('✓ Checkout completed'); |
| 144 | + |
| 145 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 146 | + console.log('✓ Successfully cloned and checked out phoenix-pro repository'); |
| 147 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 148 | + resolve(); |
| 149 | + |
| 150 | + } catch (error) { |
| 151 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 152 | + console.error('✗ ERROR: Failed to clone phoenix-pro repository'); |
| 153 | + console.error(`Error: ${error.message}`); |
| 154 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 155 | + console.error('Build failed because:'); |
| 156 | + console.error(' - PRO_REPO_URL and PRO_REPO_ACCESS_TOKEN are set (phoenix-pro expected)'); |
| 157 | + console.error(' - Clone operation failed'); |
| 158 | + console.error(''); |
| 159 | + console.error('Possible causes:'); |
| 160 | + console.error(' - Invalid or expired access token'); |
| 161 | + console.error(' - Insufficient token permissions (needs "repo" scope)'); |
| 162 | + console.error(' - Network connectivity issues'); |
| 163 | + console.error(' - Repository URL is incorrect'); |
| 164 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 165 | + |
| 166 | + // Clean up partial clone if it exists |
| 167 | + if (fs.existsSync(targetDir)) { |
| 168 | + try { |
| 169 | + fs.rmSync(targetDir, { recursive: true, force: true }); |
| 170 | + console.log('Cleaned up partial clone directory'); |
| 171 | + } catch (cleanupError) { |
| 172 | + console.warn(`Could not clean up partial clone: ${cleanupError.message}`); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + reject(new Error('Failed to clone phoenix-pro repository')); // FAIL BUILD |
| 177 | + } |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +// Export the function |
| 182 | +exports.clonePhoenixProRepo = clonePhoenixProRepo; |
0 commit comments