|
| 1 | +import axios from "axios"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import yargs from "yargs"; |
| 5 | +import { hideBin } from "yargs/helpers"; |
| 6 | +import { fileURLToPath } from "url"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +// Parse command-line arguments with fallbacks to GitHub Actions environment variables |
| 12 | +const args = yargs(hideBin(process.argv)) |
| 13 | + .option("repo", { |
| 14 | + alias: "r", |
| 15 | + type: "string", |
| 16 | + description: "GitHub repository in the format owner/repo", |
| 17 | + default: process.env.GITHUB_REPOSITORY, |
| 18 | + }) |
| 19 | + .option("pull_request_number", { |
| 20 | + alias: "pr", |
| 21 | + type: "number", |
| 22 | + description: "Pull request number", |
| 23 | + default: process.env.PR_NUMBER ? parseInt(process.env.PR_NUMBER) : undefined, |
| 24 | + }) |
| 25 | + .option("token", { |
| 26 | + alias: "t", |
| 27 | + type: "string", |
| 28 | + description: "GitHub personal access token", |
| 29 | + default: process.env.GITHUB_TOKEN, |
| 30 | + }) |
| 31 | + .option("build_specs", { |
| 32 | + alias: "b", |
| 33 | + type: "boolean", |
| 34 | + description: "Build specs before generating links", |
| 35 | + default: true, |
| 36 | + }) |
| 37 | + .option("update_pr", { |
| 38 | + alias: "u", |
| 39 | + type: "boolean", |
| 40 | + description: "Update the PR description with preview links", |
| 41 | + default: false, |
| 42 | + }) |
| 43 | + .option("base_ref", { |
| 44 | + type: "string", |
| 45 | + description: "Base branch reference for git diff", |
| 46 | + default: process.env.GITHUB_BASE_REF || "main", |
| 47 | + }) |
| 48 | + .help().argv; |
| 49 | + |
| 50 | +const { repo, pull_request_number, token, update_pr, build_specs, base_ref } = args; |
| 51 | + |
| 52 | +async function updatePRDescription(markdownContent) { |
| 53 | + if (!update_pr) return; |
| 54 | + |
| 55 | + try { |
| 56 | + // Get current PR description |
| 57 | + const prResponse = await axios.get(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, { |
| 58 | + headers: { |
| 59 | + Authorization: `token ${token}`, |
| 60 | + Accept: 'application/vnd.github.v3+json' |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + const currentBody = prResponse.data.body || ''; |
| 65 | + |
| 66 | + // Remove any existing preview section |
| 67 | + const cleanedBody = currentBody.replace(/🚀 \*\*Netlify Preview\*\*:[\s\S]*?(?=\n\n|\n$|$)/g, '').trim(); |
| 68 | + |
| 69 | + // Create new body with preview links prepended |
| 70 | + const newBody = `🚀 **Netlify Preview**: |
| 71 | +🔄 **this PR updates the following sspecs**: |
| 72 | +${markdownContent} |
| 73 | +
|
| 74 | +${cleanedBody}`.trim(); |
| 75 | + |
| 76 | + // Update PR description |
| 77 | + await axios.patch(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, { |
| 78 | + body: newBody |
| 79 | + }, { |
| 80 | + headers: { |
| 81 | + Authorization: `token ${token}`, |
| 82 | + Accept: 'application/vnd.github.v3+json' |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + console.log('PR description updated successfully'); |
| 87 | + } catch (error) { |
| 88 | + console.error('Error updating PR description:', error.message); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Define the base URLs |
| 93 | +// Use Netlify site and context to build preview URL |
| 94 | +// Get Netlify site name from environment variable or default to 'wai-aria' |
| 95 | +const netlifySite = process.env.SITE_NAME || 'wai-aria'; |
| 96 | +// Get Netlify context from environment variable or default to 'deploy-preview' |
| 97 | +const netlifyContext = process.env.CONTEXT || process.env.NETLIFY_CONTEXT || 'deploy-preview'; |
| 98 | +const previewBaseURL = `https://${netlifyContext}-${pull_request_number}--${netlifySite}.netlify.app`; |
| 99 | +// Extract repo owner and name from the repo parameter (format: owner/repo) |
| 100 | +const repoOwner = repo.split('/')[0]; |
| 101 | +const repoName = repo.split('/')[1]; |
| 102 | +const EDBaseURL = `https://${repoOwner}.github.io/${repoName}`; |
| 103 | + |
| 104 | +async function getChangedFiles() { |
| 105 | + const { execSync } = await import('child_process'); |
| 106 | + try { |
| 107 | + // Use git diff to get changed files |
| 108 | + const diffOutput = execSync(`git diff --name-only origin/${base_ref}...HEAD`, { encoding: 'utf-8' }); |
| 109 | + const files = diffOutput.split('\n').filter(Boolean); |
| 110 | + |
| 111 | + // Filter to only include index.html files |
| 112 | + const specSources = files.filter(file => |
| 113 | + file === 'index.html' || file.endsWith('/index.html') |
| 114 | + ); |
| 115 | + |
| 116 | + // Skip if no index files were changed |
| 117 | + if (specSources.length === 0) { |
| 118 | + console.log('No index.html files changed in this PR. Skipping preview generation.'); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // Build the Markdown list with preview URLs and diff links |
| 123 | + const markdownList = specSources.map((file) => { |
| 124 | + const previewUrl = `${previewBaseURL}/${file}`; |
| 125 | + |
| 126 | + // Build ED URL based on file path |
| 127 | + let EDUrl; |
| 128 | + if (file === 'index.html') { |
| 129 | + // Main spec: https://w3c.github.io/aria/ |
| 130 | + EDUrl = `${EDBaseURL}/`; |
| 131 | + } else if (file.endsWith('/index.html')) { |
| 132 | + // Child specs: https://w3c.github.io/core-aam/ (not /aria/core-aam/) |
| 133 | + const dirName = file.split('/').slice(-2, -1)[0]; |
| 134 | + EDUrl = `https://w3c.github.io/${dirName}/`; |
| 135 | + } |
| 136 | + |
| 137 | + const diffUrl = `https://services.w3.org/htmldiff?doc1=${encodeURIComponent(EDUrl)}&doc2=${encodeURIComponent(previewUrl)}`; |
| 138 | + |
| 139 | + // Generate spec name based on file path |
| 140 | + let specName; |
| 141 | + if (file === 'index.html') { |
| 142 | + specName = 'ARIA'; |
| 143 | + } else if (file.endsWith('/index.html')) { |
| 144 | + // Extract directory name for subdirectory index.html files |
| 145 | + const dirName = file.split('/').slice(-2, -1)[0]; |
| 146 | + specName = dirName; |
| 147 | + } |
| 148 | + |
| 149 | + return `- [${specName} preview](${previewUrl}) — [${specName} diff](${diffUrl})`; |
| 150 | + }).join("\n"); |
| 151 | + |
| 152 | + // Output the Markdown list |
| 153 | + console.log(markdownList); |
| 154 | + |
| 155 | + // Optionally, write the output to a file |
| 156 | + const outputPath = path.join(__dirname, "changed_files.md"); |
| 157 | + fs.writeFileSync(outputPath, markdownList, "utf8"); |
| 158 | + console.log(`Markdown list written to ${outputPath}`); |
| 159 | + |
| 160 | + // Update PR description if requested |
| 161 | + await updatePRDescription(markdownList); |
| 162 | + } catch (error) { |
| 163 | + console.error("Error fetching changed files:", error.message); |
| 164 | + process.exit(1); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +(async () => { |
| 169 | + await getChangedFiles(); |
| 170 | +})(); |
0 commit comments