|
| 1 | +/** |
| 2 | + * Asset Synchronization Script |
| 3 | + * Main script for downloading and organizing release assets |
| 4 | + */ |
| 5 | +const path = require('path'); |
| 6 | +const { ensureDir, fileExists, writeFile } = require('./file-utils'); |
| 7 | +const { generateHashFiles } = require('./hash-utils'); |
| 8 | +const { downloadAssetWithRetry } = require('./download-utils'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Process a single repository and download its release assets |
| 12 | + */ |
| 13 | +async function processRepository(github, context, repo, repositoryData, totalAssets, isPullRequest = false, releaseLimit = null) { |
| 14 | + console.log(`Processing repository: ${repo.name}`); |
| 15 | + |
| 16 | + let processedReleasesWithAssets = 0; |
| 17 | + |
| 18 | + try { |
| 19 | + // Get releases for the repository with pagination |
| 20 | + const releases = await github.paginate(github.rest.repos.listReleases, { |
| 21 | + owner: context.repo.owner, |
| 22 | + repo: repo.name, |
| 23 | + per_page: 100 |
| 24 | + }); |
| 25 | + |
| 26 | + // Filter out draft and prerelease |
| 27 | + const publishedReleases = releases.filter(release => !release.draft && !release.prerelease); |
| 28 | + |
| 29 | + if (publishedReleases.length === 0) { |
| 30 | + console.log(`No published releases found for ${repo.name}`); |
| 31 | + return { totalAssets, processedReleases: 0 }; |
| 32 | + } |
| 33 | + |
| 34 | + const repoData = { |
| 35 | + name: repo.name, |
| 36 | + releases: [] |
| 37 | + }; |
| 38 | + |
| 39 | + for (const release of publishedReleases) { |
| 40 | + // For pull requests, stop after processing the specified number of releases with assets |
| 41 | + if (isPullRequest && releaseLimit && processedReleasesWithAssets >= releaseLimit) { |
| 42 | + console.log(`PR mode: Reached limit of ${releaseLimit} releases with assets for ${repo.name}`); |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + const assetCount = await processRelease(repo.name, release); |
| 47 | + totalAssets += assetCount; |
| 48 | + |
| 49 | + if (assetCount > 0) { |
| 50 | + repoData.releases.push({ |
| 51 | + tag: release.tag_name, |
| 52 | + assetCount: assetCount |
| 53 | + }); |
| 54 | + processedReleasesWithAssets++; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (repoData.releases.length > 0) { |
| 59 | + repositoryData.push(repoData); |
| 60 | + } |
| 61 | + |
| 62 | + } catch (error) { |
| 63 | + console.error(`Error processing repository ${repo.name}: ${error.message}`); |
| 64 | + } |
| 65 | + |
| 66 | + return { totalAssets, processedReleases: processedReleasesWithAssets }; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Process a single release and download its assets |
| 71 | + */ |
| 72 | +async function processRelease(repoName, release) { |
| 73 | + console.log(`Processing release: ${release.tag_name}`); |
| 74 | + |
| 75 | + if (release.assets.length === 0) { |
| 76 | + console.log(`No assets found for release ${release.tag_name}`); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + // Create directory structure |
| 81 | + const releaseDir = path.join(repoName, release.tag_name); |
| 82 | + ensureDir(releaseDir); |
| 83 | + |
| 84 | + let assetCount = 0; |
| 85 | + |
| 86 | + for (const asset of release.assets) { |
| 87 | + const downloaded = await processAsset(releaseDir, asset); |
| 88 | + if (downloaded) { |
| 89 | + assetCount++; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return assetCount; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Process a single asset - download and generate hashes if not exists |
| 98 | + */ |
| 99 | +async function processAsset(releaseDir, asset) { |
| 100 | + const assetPath = path.join(releaseDir, asset.name); |
| 101 | + |
| 102 | + // Skip if asset already exists |
| 103 | + if (fileExists(assetPath)) { |
| 104 | + console.log(`Asset already exists: ${assetPath}`); |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + console.log(`Downloading: ${asset.name}`); |
| 109 | + |
| 110 | + try { |
| 111 | + await downloadAssetWithRetry( |
| 112 | + asset.browser_download_url, |
| 113 | + assetPath, |
| 114 | + process.env.GITHUB_TOKEN |
| 115 | + ); |
| 116 | + |
| 117 | + console.log(`Successfully downloaded: ${assetPath}`); |
| 118 | + |
| 119 | + // Generate hash files |
| 120 | + const hashSuccess = generateHashFiles(assetPath); |
| 121 | + |
| 122 | + return hashSuccess; |
| 123 | + |
| 124 | + } catch (error) { |
| 125 | + console.error(`Failed to download ${asset.name}: ${error.message}`); |
| 126 | + return false; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Generate repository data JSON for the web interface |
| 132 | + */ |
| 133 | +function generateRepositoryDataJson(repositoryData, totalAssets) { |
| 134 | + const dataJson = { |
| 135 | + repositories: repositoryData, |
| 136 | + lastUpdated: new Date().toISOString(), |
| 137 | + totalRepositories: repositoryData.length, |
| 138 | + totalReleases: repositoryData.reduce((sum, repo) => sum + repo.releases.length, 0), |
| 139 | + totalAssets: totalAssets |
| 140 | + }; |
| 141 | + |
| 142 | + writeFile('repository-data.json', JSON.stringify(dataJson, null, 2)); |
| 143 | + console.log('Generated repository-data.json'); |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Main function to sync all release assets |
| 148 | + */ |
| 149 | +async function syncReleaseAssets(github, context, isPullRequest = false) { |
| 150 | + console.log('Getting repositories from organization...'); |
| 151 | + |
| 152 | + if (isPullRequest) { |
| 153 | + console.log('Running in pull request mode - limiting to 2 releases with assets per repository'); |
| 154 | + } |
| 155 | + |
| 156 | + // Get all repositories with pagination |
| 157 | + const repos = await github.paginate(github.rest.repos.listForOrg, { |
| 158 | + org: context.repo.owner, |
| 159 | + type: 'all', |
| 160 | + per_page: 100 |
| 161 | + }); |
| 162 | + |
| 163 | + console.log(`Found ${repos.length} repositories`); |
| 164 | + |
| 165 | + const repositoryData = []; |
| 166 | + let totalAssets = 0; |
| 167 | + let totalProcessedReleases = 0; |
| 168 | + |
| 169 | + // Process each repository |
| 170 | + for (const repo of repos) { |
| 171 | + const result = await processRepository( |
| 172 | + github, |
| 173 | + context, |
| 174 | + repo, |
| 175 | + repositoryData, |
| 176 | + totalAssets, |
| 177 | + isPullRequest, |
| 178 | + isPullRequest ? 2 : null |
| 179 | + ); |
| 180 | + totalAssets = result.totalAssets; |
| 181 | + totalProcessedReleases += result.processedReleases; |
| 182 | + } |
| 183 | + |
| 184 | + // Generate repository data JSON for the index.html |
| 185 | + generateRepositoryDataJson(repositoryData, totalAssets); |
| 186 | + |
| 187 | + if (isPullRequest) { |
| 188 | + console.log(`PR mode: Processed ${repositoryData.length} repositories with ${totalProcessedReleases} releases containing assets`); |
| 189 | + } else { |
| 190 | + console.log(`Processed ${repositoryData.length} repositories with assets`); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = { |
| 195 | + syncReleaseAssets |
| 196 | +}; |
0 commit comments