-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
140 lines (125 loc) · 5 KB
/
check-component-index.yml
File metadata and controls
140 lines (125 loc) · 5 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
name: Check Component Index Order
on:
pull_request_target:
types: [opened, reopened, synchronize]
paths:
- 'src/content/docs/components/index.mdx'
permissions:
pull-requests: write
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
BOT_NAME: "github-actions[bot]"
REVIEW_MARKER: "ImgTable blocks are not in alphabetical order"
jobs:
check:
name: Component Index Ordering
runs-on: ubuntu-latest
steps:
- name: Checkout PR head
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Set up Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: '20'
- name: Check ordering
id: check
run: |
set +e
OUTPUT=$(node script/check_component_index.mjs --suggestions 2>&1)
EXIT_CODE=$?
set -e
if [ "$EXIT_CODE" -eq 0 ]; then
echo "sorted=true" >> "$GITHUB_OUTPUT"
else
echo "sorted=false" >> "$GITHUB_OUTPUT"
# Write the JSON output to a file for the next step
echo "$OUTPUT" > /tmp/suggestions.json
fi
- name: Post or dismiss review
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
SORTED: ${{ steps.check.outputs.sorted }}
PR_NUMBER: ${{ github.event.pull_request.number }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
with:
script: |
const fs = require('fs');
const { owner, repo } = context.repo;
const pr_number = parseInt(process.env.PR_NUMBER);
const sorted = process.env.SORTED === 'true';
// Helper: find the most recent bot review for this check
async function findBotReview() {
const { data: reviews } = await github.rest.pulls.listReviews({
owner,
repo,
pull_number: pr_number
});
return reviews
.filter(r =>
r.user.login === process.env.BOT_NAME &&
r.state === 'CHANGES_REQUESTED' &&
r.body && r.body.includes(process.env.REVIEW_MARKER)
)
.sort((a, b) => new Date(b.submitted_at) - new Date(a.submitted_at))[0];
}
if (sorted) {
console.log('All ImgTable blocks are correctly sorted.');
// Dismiss any previous review from this bot
const botReview = await findBotReview();
if (botReview) {
console.log('Dismissing previous review', botReview.id);
await github.rest.pulls.dismissReview({
owner,
repo,
pull_number: pr_number,
review_id: botReview.id,
message: 'Component index ordering has been fixed — dismissing review.'
});
}
return;
}
// Read the suggestions JSON produced by the script
const data = JSON.parse(fs.readFileSync('/tmp/suggestions.json', 'utf-8'));
console.log(`Found ${data.suggestions.length} unsorted table(s).`);
// Build inline suggestion comments
const comments = data.suggestions.map(s => ({
path: data.file,
start_line: s.startLine,
line: s.endLine,
side: 'RIGHT',
body: [
`**${s.section}**: items are not in alphabetical order.`,
'',
'```suggestion',
s.body,
'```'
].join('\n')
}));
// Create REQUEST_CHANGES review with inline suggestions
await github.rest.pulls.createReview({
owner,
repo,
pull_number: pr_number,
commit_id: process.env.HEAD_SHA,
event: 'REQUEST_CHANGES',
body: [
`### ${process.env.REVIEW_MARKER}`,
'',
`Found ${data.suggestions.length} ImgTable block(s) with incorrect ordering below **Network Protocols**.`,
'Each table has at most one Core item (the first name ending with " Core"), pinned first, followed by Template items (names starting with "Template "), then all remaining items sorted alphabetically.',
'',
'You can fix this automatically by running:',
'```',
'node script/check_component_index.mjs --fix',
'```',
'',
'See the inline suggestions below for the correct order in each section.'
].join('\n'),
comments
});
console.log('Posted REQUEST_CHANGES review with inline suggestions.');