Skip to content

Commit 20c3170

Browse files
Wait for Netlify
1 parent d2acae1 commit 20c3170

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

common/script/prLinks.mjs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
21
import axios from "axios";
32
import fs from "fs";
43
import path from "path";
54
import yargs from "yargs";
65
import { hideBin } from "yargs/helpers";
76
import { fileURLToPath } from "url";
7+
// Check and wait for Netlify
8+
const netlifySite = "staging-aria"; // Netlify site name
9+
async function isNetlifyDeploySuccessful(prNumber) {
10+
const apiUrl = `https://api.netlify.com/api/v1/sites/${site}/deploys`;
11+
12+
try {
13+
const response = await axios.get(apiUrl);
14+
// Find the deploy for this PR
15+
const prDeploy = response.data.find(deploy => {
16+
// Netlify context for PR deploys is 'deploy-preview' and branch is 'deploy-preview-<prNumber>'
17+
return (
18+
deploy.context === "deploy-preview" &&
19+
deploy.branch === `deploy-preview-${prNumber}`
20+
);
21+
});
22+
if (!prDeploy) return false;
23+
return prDeploy.state === "ready";
24+
} catch (e) {
25+
console.error("Error checking Netlify deploy status:", e.message);
26+
return false;
27+
}
28+
}
829

930
const __filename = fileURLToPath(import.meta.url);
1031
const __dirname = path.dirname(__filename);
@@ -47,7 +68,14 @@ const { repo, pull_request_number, token, update_pr, build_specs } = args;
4768

4869
async function updatePRDescription(markdownContent) {
4970
if (!update_pr) return;
50-
71+
72+
// Check Netlify deploy status first
73+
const deployOk = await isNetlifyDeploySuccessful(pull_request_number);
74+
if (!deployOk) {
75+
console.log("Netlify deploy not successful. PR description will not be updated.");
76+
return;
77+
}
78+
5179
try {
5280
// Get current PR description
5381
const prResponse = await axios.get(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, {
@@ -56,19 +84,19 @@ async function updatePRDescription(markdownContent) {
5684
Accept: 'application/vnd.github.v3+json'
5785
}
5886
});
59-
87+
6088
const currentBody = prResponse.data.body || '';
61-
89+
6290
// Remove any existing preview section
6391
const cleanedBody = currentBody.replace(/🚀 \*\*Netlify Preview\*\*:[\s\S]*?(?=\n\n|\n$|$)/g, '').trim();
64-
92+
6593
// Create new body with preview links prepended
6694
const newBody = `🚀 **Netlify Preview**:
6795
🔄 **Changed Pages**:
6896
${markdownContent}
6997
7098
${cleanedBody}`.trim();
71-
99+
72100
// Update PR description
73101
await axios.patch(`https://api.github.com/repos/${repo}/pulls/${pull_request_number}`, {
74102
body: newBody
@@ -78,15 +106,17 @@ ${cleanedBody}`.trim();
78106
Accept: 'application/vnd.github.v3+json'
79107
}
80108
});
81-
109+
82110
console.log('PR description updated successfully');
83111
} catch (error) {
84112
console.error('Error updating PR description:', error.message);
85113
}
86114
}
87115

88116
// Define the base URLs
89-
const previewBaseURL = `https://deploy-preview-${pull_request_number}--staging-aria.netlify.app`;
117+
// Use Netlify site and context to build preview URL
118+
const netlifyContext = "deploy-preview";
119+
const previewBaseURL = `https://${netlifyContext}-${pull_request_number}--${netlifySite}.netlify.app`;
90120
const EDBaseURL = `https://w3c.github.io`;
91121

92122
async function getChangedFiles() {

0 commit comments

Comments
 (0)