-
Notifications
You must be signed in to change notification settings - Fork 61
341 lines (309 loc) · 14.6 KB
/
pr.yml
File metadata and controls
341 lines (309 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright (c) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
name: Build HTML preview of PR
on:
# /!\ Warning: using the pull_request_target event to be able to read secrets. But using this event without the cautionary measures described below
# may allow unauthorized GitHub users to open a “pwn request” and exfiltrate secrets.
# As recommended in https://iterative.ai/blog/testing-external-contributions-using-github-actions-secrets,
# we are adding an 'authorize' job that checks if the workflow was triggered from a fork PR. In that case, the "external" environment
# will prevent the job from running until it's approved manually by human intervention.
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]
branches:
- main
- release-1.**
- release-2.**
concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.event.pull_request.head.ref }}
cancel-in-progress: true
env:
GH_TEAM: rhdh
GH_ORGANIZATION: redhat-developer
jobs:
check-commit-author:
runs-on: ubuntu-latest
outputs:
is_authorized: ${{ steps.check-team-membership.outputs.is_active_member }}
steps:
- name: Generate GitHub App Token
id: app-token
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
with:
app-id: ${{ secrets.RHDH_GITHUB_APP_ID }}
private-key: ${{ secrets.RHDH_GITHUB_APP_PRIVATE_KEY }}
- name: Check team membership
uses: redhat-developer/rhdh/.github/actions/check-author@main
id: check-team-membership
with:
team: ${{ env.GH_TEAM }}
organization: ${{ env.GH_ORGANIZATION }}
gh_token: ${{ steps.app-token.outputs.token }}
author: ${{ github.event.pull_request.user.login }}
authorize:
# The 'external' environment is configured with the rhdh-content team as required reviewers.
# All the subsequent jobs in this workflow 'need' this job, which will require manual approval for PRs coming from external forks outside of the rhdh team
needs: check-commit-author
environment:
${{ (needs.check-commit-author.outputs.is_authorized == 'true' || github.event.pull_request.head.repo.full_name == github.repository) && 'internal' || 'external' }}
runs-on: ubuntu-latest
steps:
- name: Check if internal PR
id: check
run: |
if [[ "${{ needs.check-commit-author.outputs.is_authorized }}" == "true" ]]; then
echo "✓ Commit author is in rhdh team - using internal environment"
elif [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then
echo "✓ Internal PR (not from fork) - using internal environment"
else
echo "✓ External PR from fork from non-rhdh team member - using external environment for security"
fi
adoc_build:
name: Ccutil Build For PR branch preview
runs-on: ubuntu-latest
needs: authorize
permissions:
contents: read
packages: write
pull-requests: write
steps:
- name: Checkout trusted build scripts from ${{ github.event.pull_request.base.ref }} branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
path: trusted-scripts
sparse-checkout: build/scripts
- name: Checkout PR branch for content to build
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
path: pr-content
- name: Setup environment
run: |
# update
sudo apt-get update -y || true
# install
sudo apt-get -y -q install podman rsync && podman --version
echo "GIT_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_ENV
- name: Install lychee
run: |
LYCHEE_VERSION="v0.23.0"
curl -sSfL "https://github.com/lycheeverse/lychee/releases/download/lychee-${LYCHEE_VERSION}/lychee-x86_64-unknown-linux-gnu.tar.gz" \
| sudo tar xz -C /usr/local/bin lychee
lychee --version
- name: Install Vale
run: |
wget -q https://github.com/errata-ai/vale/releases/download/v3.9.5/vale_3.9.5_Linux_64-bit.tar.gz
tar -xzf vale_3.9.5_Linux_64-bit.tar.gz -C /usr/local/bin vale
vale --version
- name: Sync Vale styles
run: |
cd pr-content
vale sync
- name: Restore lychee cache
uses: actions/cache@v4
with:
path: pr-content/.lycheecache
key: lychee-${{ github.event.number }}-${{ github.sha }}
restore-keys: |
lychee-${{ github.event.number }}-
lychee-
- name: Build guides and indexes
id: build
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.RHDH_BOT_TOKEN }}
# Used by CQA-15 (redirects check) to diff against the base branch
CQA_BASE_REF: base/${{ github.event.pull_request.base.ref }}
run: |
echo "Building PR ${{ github.event.pull_request.number }}"
rm -rf pr-content/build/scripts
rsync -az trusted-scripts/build/scripts pr-content/build/
# some files are new in main/1.10, so check if they exist before copying
for f in trusted-scripts/.lycheeignore trusted-scripts/lychee.toml; do
if [[ -f $f ]]; then rsync -az $f pr-content/; fi
done
touch pr-content/.lycheecache
cd pr-content
# Add base branch as remote so CQA checks can diff PR content against it
git remote add base https://github.com/${{ github.event.pull_request.base.repo.full_name }}.git
git fetch base ${{ github.event.pull_request.base.ref }}
# Orchestrator runs ccutil + lychee + CQA; older branches fall back to ccutil only
if [[ -f "build/scripts/build-orchestrator.js" ]]; then
node build/scripts/build-orchestrator.js -b "pr-${{ github.event.number }}"
else
build/scripts/build-ccutil.sh -b "pr-${{ github.event.number }}"
fi
# Determine if HTML was generated, so deploy runs even when only CQA/lychee fail.
# Sets html_built=true when all titles built, false when any title failed or no report exists.
- name: Check if HTML was built
id: check_html
if: always() && steps.build.outcome != 'skipped'
run: |
# Older branches (release-1.8/1.9) use build-ccutil.sh and produce no report;
# fall back to the build step outcome
if [[ ! -f "pr-content/build-report.json" ]]; then
if [[ "${{ steps.build.outcome }}" == "success" ]]; then
echo "html_built=true" >> "$GITHUB_OUTPUT"
else
echo "html_built=false" >> "$GITHUB_OUTPUT"
fi
exit 0
fi
# Parse the report to check for title build failures specifically;
# CQA/lychee failures do NOT prevent deployment
title_failures=$(node -e "
const r = JSON.parse(require('fs').readFileSync('pr-content/build-report.json','utf8'));
console.log(r.results.some(t => t.status === 'failed') ? 'true' : 'false');
" 2>&1) || { echo "html_built=false" >> "$GITHUB_OUTPUT"; exit 0; }
if [[ "$title_failures" == "true" ]]; then
echo "html_built=false" >> "$GITHUB_OUTPUT"
else
echo "html_built=true" >> "$GITHUB_OUTPUT"
fi
- name: Deploy to the gh-pages branch
if: steps.check_html.outputs.html_built == 'true'
env:
GITHUB_TOKEN: ${{ secrets.RHDH_BOT_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: bash trusted-scripts/build/scripts/deploy-gh-pages.sh ./pr-content/titles-generated --message "Deploy PR ${{ github.event.number }} preview"
# Post one consolidated PR comment with build results, preview link, and CQA checklist.
# Preview link is always shown; marked stale when title build failed (HTML not generated).
# CQA section is absent on older branches without CQA.
# Detects existing comments from both old (two-comment) and new (consolidated) formats.
- name: Post or update PR comment with doc preview link
if: always() && steps.build.outcome != 'skipped'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.RHDH_BOT_TOKEN }}
script: |
const fs = require('fs');
const prNum = context.issue.number;
const previewUrl = `https://redhat-developer.github.io/red-hat-developers-documentation-rhdh/pr-${prNum}/`;
const now = new Date().toISOString().replace('T', ' ').replace(/\.\d+Z$/, ' UTC');
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
let report;
try {
report = JSON.parse(fs.readFileSync('pr-content/build-report.json', 'utf8'));
} catch { report = null; }
// ── Section 1: Build status + preview ──
let body = '## PR Build Results\n\n';
if (report) {
const titlesFailed = report.results.filter(r => r.status === 'failed');
const hasTitleFailure = titlesFailed.length > 0;
const overallFailed = hasTitleFailure
|| (report.lychee && report.lychee.status === 'failed')
|| (report.cqa && report.cqa.status === 'failed');
if (overallFailed) {
body += `**Build failed** -- ${report.titles.passed}/${report.titles.total} titles | ${report.duration}s\n`;
} else {
body += `**Build passed** -- ${report.titles.passed}/${report.titles.total} titles | ${report.duration}s\n`;
}
if (hasTitleFailure) {
body += `Preview: ${previewUrl} (stale -- title build failed, showing previous version)\n\n`;
} else {
body += `Preview: ${previewUrl}\n\n`;
}
// ── Section 2: Build error details (title failures only) ──
if (hasTitleFailure) {
const details = titlesFailed.map(r => {
const errs = (r.errors || []).map(e =>
` **Error:** \`${e.line}\`\n **Cause:** ${e.cause}\n **Fix:** ${e.fix}`
).join('\n\n');
return `### ${r.title}\n${errs}`;
}).join('\n\n');
body += `${details}\n\n`;
}
// ── Section 2b: Lychee link validation errors ──
if (report.lychee && report.lychee.status === 'failed' && report.lychee.errors && report.lychee.errors.length > 0) {
const s = report.lychee.stats || {};
body += `### Link Validation (lychee)\n\n`;
body += `Total: ${s.total || '?'} | OK: ${s.successful || 0} | Errors: ${s.errors || 0} | Excluded: ${s.excludes || 0} | Timeouts: ${s.timeouts || 0}\n\n`;
for (const e of report.lychee.errors) {
if (e.sources && e.sources.length > 0) {
for (const src of e.sources) {
body += `- [ ] \`${src}\` \u2192 ${e.line}\n`;
}
} else {
body += `- [ ] ${e.line}\n`;
}
}
body += '\n';
}
if (overallFailed) {
body += `[View full logs](${runUrl})\n\n`;
}
body += `---\n\n`;
// ── Section 3: CQA checklist ──
if (report.cqa && report.cqa.output) {
body += `## Content Quality Assessment\n\n`;
body += report.cqa.output + '\n\n';
const s = report.cqa.stats || {};
if (s.total) {
body += `${s.total} checks: ${s.pass} pass, ${s.fail} fail\n\n`;
}
body += `Run \`node build/scripts/cqa/index.js --all --fix\` locally to review and auto-fix issues.\n\n`;
body += `---\n\n`;
}
} else {
// No report -- early crash
body += `**Build failed** -- build crashed before producing a report.\n`;
body += `Preview: ${previewUrl} (stale -- build crashed, showing previous version)\n\n`;
body += `[View full logs](${runUrl})\n\n`;
body += `---\n\n`;
}
body += `*Updated ${now}*`;
// ── Upsert comment ──
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const existing = comments.find(c =>
c.body.includes('## PR Build Results') ||
c.body.includes('preview: https://redhat-developer.github.io/') ||
c.body.includes('Build failed')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body
});
}
// ── Clean up old standalone CQA comment ──
const oldCqa = comments.find(c =>
c.body.includes('Content Quality Assessment Results') &&
(!existing || c.id !== existing.id)
);
if (oldCqa) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: oldCqa.id,
});
}
- name: Fail job if build failed
if: steps.build.outcome == 'failure'
run: exit 1