Skip to content

Commit 5780a69

Browse files
Merge pull request #6 from daniel-montalvo/accname-core-aam
Try preview link updates
2 parents 0654668 + 1bbdd69 commit 5780a69

File tree

3 files changed

+157
-76
lines changed

3 files changed

+157
-76
lines changed

.github/workflows/update-preview-links.yml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,17 @@ jobs:
1717
- name: Set up Node.js
1818
uses: actions/setup-node@v3
1919
with:
20-
node-version: '16'
20+
node-version: 'latest'
2121

2222
# Step 3: Install dependencies
2323
- name: Install dependencies
2424
run: npm install axios yargs
2525

26-
# Step 4: Run the script
27-
- name: Generate preview links
26+
# Step 4: Run the script and update PR
27+
- name: Generate preview links and update PR
2828
run: |
29-
node common/script/updatePreviewLinks.js \
29+
node common/script/updatePreviewLinks.mjs \
3030
--repo ${{ github.repository }} \
3131
--pull_request_number ${{ github.event.pull_request.number }} \
32-
--token ${{ secrets.GITHUB_TOKEN }}
33-
34-
# Step 5: Update the pull request body
35-
- name: Update pull request body
36-
uses: peter-evans/create-or-update-comment@v4
37-
with:
38-
issue-number: ${{ github.event.pull_request.number }}
39-
body: |
40-
🚀 **Netlify Preview**:
41-
🔄 **Changed Pages**:
42-
$(cat .github/scripts/changed_files.md)
43-
edit-mode: replace
32+
--token ${{ secrets.GITHUB_TOKEN }} \
33+
--update_pr

common/script/updatePreviewLinks.js

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)