@@ -35,12 +35,71 @@ export function formatBootstrapStatusMessage(fileLog) {
3535 . join ( ' · ' )
3636}
3737
38+ const WORKFLOW_ENABLEMENT_RE = / e n a b l e m e n t : \s * t r u e \b /
39+
3840export function buildPagesWorkflowYaml ( branch = 'main' ) {
3941 const b = String ( branch ?? '' ) . trim ( ) || 'main'
4042 const branchRef = / ^ [ a - z A - Z 0 - 9 . _ / - ] + $ / . test ( b ) ? b : `"${ b . replace ( / " / g, '\\"' ) } "`
4143 return READ_ONLY_TEMPLATES . githubPagesWorkflowYaml . replace ( / \{ \{ B R A N C H \} \} / g, branchRef )
4244}
4345
46+ export function workflowNeedsEnablementUpdate ( existingYaml ) {
47+ return ! WORKFLOW_ENABLEMENT_RE . test ( String ( existingYaml ?? '' ) )
48+ }
49+
50+ async function ensurePagesWorkflow ( { token, owner, repo, branch, fileLog } ) {
51+ const content = buildPagesWorkflowYaml ( branch )
52+ const existingSha = await getFileSha ( { token, owner, repo, path : PAGES_WORKFLOW , branch } )
53+
54+ if ( existingSha ) {
55+ try {
56+ const existing = await fetchRepoFileText ( { token, owner, repo, path : PAGES_WORKFLOW , branch } )
57+ if ( ! workflowNeedsEnablementUpdate ( existing . text ) ) {
58+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'found' } )
59+ return { workflowOk : true , workflowWarning : null }
60+ }
61+ await upsertFile ( {
62+ token,
63+ owner,
64+ repo,
65+ path : PAGES_WORKFLOW ,
66+ branch,
67+ content,
68+ message : 'Update GitHub Pages workflow (enable Pages)' ,
69+ sha : existing . sha ,
70+ } )
71+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'created' } )
72+ return { workflowOk : true , workflowWarning : null }
73+ } catch {
74+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'failed' } )
75+ return { workflowOk : false , workflowWarning : WORKFLOW_PERMISSION_WARNING }
76+ }
77+ }
78+
79+ try {
80+ await upsertFile ( {
81+ token,
82+ owner,
83+ repo,
84+ path : PAGES_WORKFLOW ,
85+ branch,
86+ content,
87+ message : 'Add GitHub Pages workflow for blog/' ,
88+ sha : null ,
89+ } )
90+ const verified = await getFileSha ( { token, owner, repo, path : PAGES_WORKFLOW , branch } )
91+ if ( verified ) {
92+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'created' } )
93+ return { workflowOk : true , workflowWarning : null }
94+ }
95+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'failed' } )
96+ return { workflowOk : false , workflowWarning : WORKFLOW_PERMISSION_WARNING }
97+ } catch {
98+ fileLog . push ( { path : PAGES_WORKFLOW , status : 'failed' } )
99+ return { workflowOk : false , workflowWarning : WORKFLOW_PERMISSION_WARNING }
100+ }
101+ }
102+
44103async function recordFile ( { token, owner, repo, branch, path, content, message, fileLog } ) {
45104 const sha = await getFileSha ( { token, owner, repo, path, branch } )
46105 if ( sha ) {
@@ -152,36 +211,9 @@ export async function bootstrapBlogSite({ token, owner, repo, branch }) {
152211 indexModified = prep . modified
153212 }
154213
155- // Workflow lives at repo root (.github/workflows/…), not under blog/
156- const workflowSha = await getFileSha ( { token, owner, repo, path : PAGES_WORKFLOW , branch } )
157- if ( workflowSha ) {
158- fileLog . push ( { path : PAGES_WORKFLOW , status : 'found' } )
159- workflowOk = true
160- } else {
161- try {
162- await upsertFile ( {
163- token,
164- owner,
165- repo,
166- path : PAGES_WORKFLOW ,
167- branch,
168- content : buildPagesWorkflowYaml ( branch ) ,
169- message : 'Add GitHub Pages workflow for blog/' ,
170- sha : null ,
171- } )
172- const verified = await getFileSha ( { token, owner, repo, path : PAGES_WORKFLOW , branch } )
173- if ( verified ) {
174- fileLog . push ( { path : PAGES_WORKFLOW , status : 'created' } )
175- workflowOk = true
176- } else {
177- fileLog . push ( { path : PAGES_WORKFLOW , status : 'failed' } )
178- workflowWarning = WORKFLOW_PERMISSION_WARNING
179- }
180- } catch {
181- fileLog . push ( { path : PAGES_WORKFLOW , status : 'failed' } )
182- workflowWarning = WORKFLOW_PERMISSION_WARNING
183- }
184- }
214+ const workflowResult = await ensurePagesWorkflow ( { token, owner, repo, branch, fileLog } )
215+ workflowOk = workflowResult . workflowOk
216+ workflowWarning = workflowResult . workflowWarning
185217
186218 return {
187219 indexText,
0 commit comments