Skip to content

Commit 5a53779

Browse files
authored
feat(comparisons): SHA unifying, legacy-json comparison (#521)
* feat(comparisons): SHA unifying, `legacy-json` comparison * code review; * fixup! * fixup!
1 parent 9ddf8f9 commit 5a53779

File tree

10 files changed

+1022
-259
lines changed

10 files changed

+1022
-259
lines changed

.github/workflows/compare-builds.yml

Lines changed: 0 additions & 130 deletions
This file was deleted.

.github/workflows/generate.yml

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Generate Docs
1+
name: Generate and Compare Docs
22

33
on:
44
push:
@@ -15,8 +15,64 @@ permissions:
1515
contents: read
1616

1717
jobs:
18+
prepare:
19+
runs-on: ubuntu-latest
20+
outputs:
21+
sha: ${{ steps.push.outputs.sha || steps.pr.outputs.sha }}
22+
base-run: ${{ steps.main.outputs.run_id }}
23+
steps:
24+
- name: Harden Runner
25+
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
26+
with:
27+
egress-policy: audit
28+
29+
# If we are running from the main branch (a non-pull_request event), we
30+
# want the latest SHA from nodejs/node
31+
- id: push
32+
if: ${{ github.event_name != 'pull_request' }}
33+
run: |
34+
SHA=$(git ls-remote https://github.com/nodejs/node.git HEAD | awk '{print $1}')
35+
echo "$SHA" > commit
36+
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
37+
38+
- name: Upload metadata artifact
39+
if: ${{ github.event_name != 'pull_request' }}
40+
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
41+
with:
42+
name: commit
43+
path: commit
44+
45+
# If we are running from a PR (a pull_request event), we
46+
# want the SHA used by the most recent `push` run
47+
- name: Get latest `main` run
48+
if: ${{ github.event_name == 'pull_request' }}
49+
id: main
50+
env:
51+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
52+
GH_TOKEN: ${{ github.token }}
53+
run: |
54+
# `174604400` refers to `generate.yml`'s workflow ID, available at https://api.github.com/repos/nodejs/doc-kit/actions/workflows/generate.yml
55+
# The `databaseId` is a given runs run ID (Ref: https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#workflowrun)
56+
ID=$(gh run list --repo $GITHUB_REPOSITORY -c $BASE_SHA -w 174604400 -L 1 --json databaseId --jq ".[].databaseId")
57+
echo "run_id=$ID" >> $GITHUB_OUTPUT
58+
59+
- name: Download metadata artifact
60+
if: ${{ github.event_name == 'pull_request' }}
61+
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
62+
with:
63+
name: commit
64+
run-id: ${{ steps.main.outputs.run_id }}
65+
github-token: ${{ secrets.GITHUB_TOKEN }}
66+
67+
- id: pr
68+
if: ${{ github.event_name == 'pull_request' }}
69+
run: |
70+
SHA=$(cat commit)
71+
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
72+
1873
generate:
1974
runs-on: ubuntu-latest
75+
needs: prepare
2076
strategy:
2177
matrix:
2278
include:
@@ -32,10 +88,12 @@ jobs:
3288
input: './node/doc/api/*.md'
3389
- target: legacy-json
3490
input: './node/doc/api/*.md'
91+
compare: true
3592
- target: legacy-html
3693
input: './node/doc/api/*.md'
3794
- target: web
3895
input: './node/doc/api/*.md'
96+
compare: true
3997
- target: llms-txt
4098
input: './node/doc/api/*.md'
4199
fail-fast: false
@@ -56,6 +114,7 @@ jobs:
56114
with:
57115
persist-credentials: false
58116
repository: nodejs/node
117+
ref: ${{ needs.prepare.outputs.sha }}
59118
sparse-checkout: |
60119
doc/api
61120
lib
@@ -79,13 +138,27 @@ jobs:
79138
node bin/cli.mjs generate \
80139
-t ${{ matrix.target }} \
81140
-i "${{ matrix.input }}" \
82-
-o "out/${{ matrix.target }}" \
141+
-o out \
83142
-c ./node/CHANGELOG.md \
84143
--index ./node/doc/api/index.md \
85144
--log-level debug
86145
146+
- name: Download base branch artifact
147+
if: ${{ matrix.compare && needs.prepare.outputs.base-run }}
148+
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
149+
with:
150+
name: ${{ matrix.target }}
151+
path: base
152+
run-id: ${{ needs.prepare.outputs.base-run }}
153+
github-token: ${{ secrets.GITHUB_TOKEN }}
154+
155+
- name: Compare to base branch
156+
if: ${{ matrix.compare && needs.prepare.outputs.base-run }}
157+
run: |
158+
node scripts/compare-builds/${{ matrix.target }}.mjs > out/comparison.txt
159+
87160
- name: Upload ${{ matrix.target }} artifacts
88161
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
89162
with:
90163
name: ${{ matrix.target }}
91-
path: out/${{ matrix.target }}
164+
path: out
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Leave a comment
2+
3+
on:
4+
workflow_run:
5+
workflows: ['Generate and Compare Docs']
6+
types: [completed]
7+
8+
permissions:
9+
contents: read
10+
actions: read
11+
pull-requests: write
12+
13+
jobs:
14+
aggregate:
15+
name: Aggregate Comparison Results
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Harden Runner
19+
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
20+
with:
21+
egress-policy: audit
22+
23+
- name: Download all comparison artifacts
24+
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
25+
with:
26+
run-id: ${{ github.event.workflow_run.id }}
27+
github-token: ${{ secrets.GITHUB_TOKEN }}
28+
path: results
29+
30+
- name: Combine results
31+
id: combine
32+
# Even if the cat fails (no files found), we don't want to fail the workflow
33+
continue-on-error: true
34+
run: |
35+
{
36+
echo "combined<<EOF"
37+
cat results/*/comparison.txt
38+
echo "EOF"
39+
} >> "$GITHUB_OUTPUT"
40+
41+
- name: Add Comment to PR
42+
if: steps.combine.outputs.combined
43+
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
44+
with:
45+
comment-tag: compared
46+
message: ${{ steps.combine.outputs.combined }}
47+
pr-number: ${{ github.event.workflow_run.pull_requests[0].number }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
node_modules
33
npm-debug.log
44

5-
# Default Output Directory
5+
# Default Output and Comparison Directories
66
out
7+
base
78

89
# Tests
910
coverage

0 commit comments

Comments
 (0)