1-
21import axios from "axios" ;
32import fs from "fs" ;
43import path from "path" ;
54import yargs from "yargs" ;
65import { hideBin } from "yargs/helpers" ;
76import { 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
930const __filename = fileURLToPath ( import . meta. url ) ;
1031const __dirname = path . dirname ( __filename ) ;
@@ -47,7 +68,14 @@ const { repo, pull_request_number, token, update_pr, build_specs } = args;
4768
4869async 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 ( / 🚀 \* \* N e t l i f y P r e v i e w \* \* : [ \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` ;
90120const EDBaseURL = `https://w3c.github.io` ;
91121
92122async function getChangedFiles ( ) {
0 commit comments