|
| 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 |
| 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 | + demandOption: true, |
| 18 | + }) |
| 19 | + .option("pull_request_number", { |
| 20 | + alias: "pr", |
| 21 | + type: "number", |
| 22 | + description: "Pull request number", |
| 23 | + demandOption: true, |
| 24 | + }) |
| 25 | + .option("token", { |
| 26 | + alias: "t", |
| 27 | + type: "string", |
| 28 | + description: "GitHub personal access token", |
| 29 | + demandOption: true, |
| 30 | + }) |
| 31 | + .option("update_pr", { |
| 32 | + alias: "u", |
| 33 | + type: "boolean", |
| 34 | + description: "Update the PR description with preview links", |
| 35 | + default: false, |
| 36 | + }) |
| 37 | + .help().argv; |
| 38 | + |
| 39 | +const { repo, pull_request_number, token, update_pr } = args; |
| 40 | + |
| 41 | +async function updatePRDescription(markdownContent) { |
| 42 | + if (!update_pr) return; |
| 43 | + |
| 44 | + try { |
| 45 | + // Get current PR description |
| 46 | + const prResponse = await axios.get(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, { |
| 47 | + headers: { |
| 48 | + Authorization: `token ${token}`, |
| 49 | + Accept: 'application/vnd.github.v3+json' |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + const currentBody = prResponse.data.body || ''; |
| 54 | + |
| 55 | + // Remove any existing preview section |
| 56 | + const cleanedBody = currentBody.replace(/🚀 \*\*Netlify Preview\*\*:[\s\S]*?(?=\n\n|\n$|$)/g, '').trim(); |
| 57 | + |
| 58 | + // Create new body with preview links prepended |
| 59 | + const newBody = `🚀 **Netlify Preview**: |
| 60 | +🔄 **Changed Pages**: |
| 61 | +${markdownContent} |
| 62 | +
|
| 63 | +${cleanedBody}`.trim(); |
| 64 | + |
| 65 | + // Update PR description |
| 66 | + await axios.patch(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, { |
| 67 | + body: newBody |
| 68 | + }, { |
| 69 | + headers: { |
| 70 | + Authorization: `token ${token}`, |
| 71 | + Accept: 'application/vnd.github.v3+json' |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + console.log('PR description updated successfully'); |
| 76 | + } catch (error) { |
| 77 | + console.error('Error updating PR description:', error.message); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Define the base URLs |
| 82 | +const previewBaseURL = `https://deploy-preview--${pull_request_number}--wai-aria-.netlify.app`; |
| 83 | +const EDBaseURL = `https://w3c.github.io`; |
| 84 | + |
| 85 | +async function getChangedFiles() { |
| 86 | + try { |
| 87 | + // Build headers conditionally - only include Authorization if token is provided |
| 88 | + const headers = {}; |
| 89 | + if (token) { |
| 90 | + headers.Authorization = `token ${token}`; |
| 91 | + } |
| 92 | + |
| 93 | + // Fetch the list of changed files from the GitHub API |
| 94 | + const response = await axios.get(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}/files`, { |
| 95 | + headers, |
| 96 | + }); |
| 97 | + |
| 98 | + const files = response.data.map((file) => file.filename); |
| 99 | + |
| 100 | + // Filter to only include index.html files |
| 101 | + const specSources = files.filter(file => |
| 102 | + file === 'index.html' || file.endsWith('/index.html') |
| 103 | + ); |
| 104 | + |
| 105 | + // Build the Markdown list with preview URLs and diff links |
| 106 | + const markdownList = specSources.map((file) => { |
| 107 | + const previewUrl = `${previewBaseURL}/${file}`; |
| 108 | + |
| 109 | + // Build ED URL based on file path |
| 110 | + let EDUrl; |
| 111 | + if (file === 'index.html') { |
| 112 | + EDUrl = `${EDBaseURL}/aria/`; |
| 113 | + } else if (file.endsWith('/index.html')) { |
| 114 | + // Extract directory name for subdirectory index.html files |
| 115 | + const dirName = file.split('/').slice(-2, -1)[0]; |
| 116 | + EDUrl = `${EDBaseURL}/${dirName}/`; |
| 117 | + } |
| 118 | + |
| 119 | + const diffUrl = `https://services.w3.org/htmldiff?doc1=${encodeURIComponent(EDUrl)}&doc2=${encodeURIComponent(previewUrl)}`; |
| 120 | + |
| 121 | + // Generate spec name based on file path |
| 122 | + let specName; |
| 123 | + if (file === 'index.html') { |
| 124 | + specName = 'ARIA'; |
| 125 | + } else if (file.endsWith('/index.html')) { |
| 126 | + // Extract directory name for subdirectory index.html files |
| 127 | + const dirName = file.split('/').slice(-2, -1)[0]; |
| 128 | + specName = dirName; |
| 129 | + } |
| 130 | + |
| 131 | + return `- [${specName} preview](${previewUrl}) ([diff](${diffUrl}))`; |
| 132 | + }).join("\n"); |
| 133 | + |
| 134 | + // Output the Markdown list |
| 135 | + console.log(markdownList); |
| 136 | + |
| 137 | + // Optionally, write the output to a file |
| 138 | + const outputPath = path.join(__dirname, "changed_files.md"); |
| 139 | + fs.writeFileSync(outputPath, markdownList, "utf8"); |
| 140 | + console.log(`Markdown list written to ${outputPath}`); |
| 141 | + |
| 142 | + // Update PR description if requested |
| 143 | + await updatePRDescription(markdownList); |
| 144 | + } catch (error) { |
| 145 | + console.error("Error fetching changed files:", error.message); |
| 146 | + process.exit(1); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// Run the script |
| 151 | +getChangedFiles(); |
0 commit comments