Skip to content

Commit e185a19

Browse files
committed
update
1 parent d23c084 commit e185a19

3 files changed

Lines changed: 75 additions & 30 deletions

File tree

src/lib/__tests__/repoSiteBootstrap.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PAGES_WORKFLOW } from '../blogPaths'
33
import {
44
buildPagesWorkflowYaml,
55
formatBootstrapStatusMessage,
6+
workflowNeedsEnablementUpdate,
67
} from '../repoSiteBootstrap'
78
import { READ_ONLY_TEMPLATES } from '../readOnlyTemplates'
89

@@ -12,12 +13,22 @@ describe('buildPagesWorkflowYaml', () => {
1213
expect(yml).toContain('path: ./blog')
1314
expect(yml).toContain('branches: [main]')
1415
expect(yml).toContain('configure-pages@v5')
16+
expect(yml).toContain('enablement: true')
1517
expect(yml).toContain('name: Upload blog artifact')
1618
expect(READ_ONLY_TEMPLATES.githubPagesWorkflowYaml).toContain('./blog')
1719
expect(PAGES_WORKFLOW).toBe('.github/workflows/deploy-blog-pages.yml')
1820
})
1921
})
2022

23+
describe('workflowNeedsEnablementUpdate', () => {
24+
it('flags workflows without enablement: true', () => {
25+
expect(workflowNeedsEnablementUpdate('uses: actions/configure-pages@v5')).toBe(true)
26+
expect(
27+
workflowNeedsEnablementUpdate('configure-pages@v5\n with:\n enablement: true'),
28+
).toBe(false)
29+
})
30+
})
31+
2132
describe('formatBootstrapStatusMessage', () => {
2233
it('lists created and found paths', () => {
2334
const msg = formatBootstrapStatusMessage([

src/lib/repoSiteBootstrap.js

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,71 @@ export function formatBootstrapStatusMessage(fileLog) {
3535
.join(' · ')
3636
}
3737

38+
const WORKFLOW_ENABLEMENT_RE = /enablement:\s*true\b/
39+
3840
export function buildPagesWorkflowYaml(branch = 'main') {
3941
const b = String(branch ?? '').trim() || 'main'
4042
const branchRef = /^[a-zA-Z0-9._/-]+$/.test(b) ? b : `"${b.replace(/"/g, '\\"')}"`
4143
return READ_ONLY_TEMPLATES.githubPagesWorkflowYaml.replace(/\{\{BRANCH\}\}/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+
44103
async 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,

templates/github-pages-blog.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
uses: actions/checkout@v4
2929
- name: Setup Pages
3030
uses: actions/configure-pages@v5
31+
with:
32+
enablement: true
3133
- name: Upload blog artifact
3234
uses: actions/upload-pages-artifact@v3
3335
with:

0 commit comments

Comments
 (0)