Skip to content

Commit 6ddfd68

Browse files
Fix CI for large PRs (#4461)
cli/cli#10712 resulted in our Runtime PR failing. I debated using Git to get the information, but didn't because our `fetch-depth` is 1 and we would need to fetch more, which isn't great with our repository's size. I also don't want to use `tj-actions/changed-files` due to supply chain security and because it also under-the-hood Git fetches more commits. So, we now use the API directly. The REST API has overly verbose information with the actual patch for each file, so we use GraphQL.
1 parent c5daa75 commit 6ddfd68

File tree

7 files changed

+112
-36
lines changed

7 files changed

+112
-36
lines changed

.github/workflows/dev-docs-sync.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Set up Node.js
2626
uses: actions/setup-node@v4
2727
with:
28-
node-version: 18
28+
node-version: 24
2929
- name: Install dependencies
3030
run: |
3131
sudo apt-get update

.github/workflows/main-branch-preview.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- uses: actions/checkout@v4
3737
- uses: actions/setup-node@v4
3838
with:
39-
node-version: 22
39+
node-version: 24
4040
- uses: actions/setup-python@v5
4141
with:
4242
python-version: "3.11"

.github/workflows/main.yml

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,41 @@ jobs:
1616
decide-jobs:
1717
name: Decide which jobs to run
1818
runs-on: ubuntu-latest
19-
# We don't run these tests in the merge queue
20-
if: github.repository_owner == 'Qiskit' && github.event_name == 'pull_request'
19+
if: github.repository_owner == 'Qiskit'
2120
outputs:
21+
all-changed-files: ${{ steps.changed-files.outputs.ALL_CHANGED_FILES }}
2222
run-notebook-tester: ${{ steps.decide.outputs.RUN_NOTEBOOK_TESTER }}
2323
run-api-checks: ${{ steps.decide.outputs.RUN_API_CHECKS }}
2424
steps:
25+
- uses: actions/checkout@v4
26+
- name: Set up Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 24
30+
- name: Determine all changed files
31+
id: changed-files
32+
env:
33+
GITHUB_TOKEN: ${{ github.token }}
34+
run: |
35+
# GitHub API doesn't give PR information in the merge_group event, but
36+
# we can get the PR number from the branch name
37+
if [[ "${{ github.event_name }}" == 'merge_group' ]]; then
38+
BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
39+
PR_NUMBER="$(echo $BRANCH_NAME | grep -oP '(?<=gh-readonly-queue/main/pr-)\d+')"
40+
else
41+
PR_NUMBER="${{ github.event.number }}"
42+
fi
43+
44+
CHANGED_FILES=$(node scripts/ci/determine-changed-files.ts "${PR_NUMBER}")
45+
{
46+
echo "ALL_CHANGED_FILES<<__GH_OUTPUT__"
47+
printf '%s\n' "$CHANGED_FILES"
48+
echo "__GH_OUTPUT__"
49+
} >> "$GITHUB_OUTPUT"
2550
- name: Decide which jobs to run
2651
id: decide
2752
env:
28-
GH_TOKEN: ${{ github.token }}
53+
CHANGED_FILES: ${{ steps.changed-files.outputs.ALL_CHANGED_FILES }}
2954
# If changing these regexes, test them on https://regex101.com/ with the
3055
# following paths (you can paste the block in and uncomment it):
3156
# Add new tests if necessary
@@ -56,13 +81,10 @@ jobs:
5681
# public/learning/image.svg
5782
# .github/workflows/notebook-test-cron.yml
5883
run: |
59-
CHANGED_FILES=$(gh pr diff -R Qiskit/documentation ${{ github.event.number }} --name-only)
60-
if [ "$(echo "${CHANGED_FILES[@]}" | grep -P $NOTEBOOK_TEST_REGEX || true)" != '' ]
61-
then
84+
if [[ "${{ github.event_name }}" == 'pull_request' && -n "$(printf '%s\n' "${CHANGED_FILES}" | grep -P "$NOTEBOOK_TEST_REGEX")" ]]; then
6285
echo "RUN_NOTEBOOK_TESTER=true" >> "$GITHUB_OUTPUT"
6386
fi
64-
if [ "$(echo "${CHANGED_FILES[@]}" | grep -P $API_CHECKS_REGEX || true)" != '' ]
65-
then
87+
if [[ "${{ github.event_name }}" == 'pull_request' && -n "$(printf '%s\n' "${CHANGED_FILES}" | grep -P "$API_CHECKS_REGEX")" ]]; then
6688
echo "RUN_API_CHECKS=true" >> "$GITHUB_OUTPUT"
6789
fi
6890
@@ -75,7 +97,7 @@ jobs:
7597
- name: Set up Node.js
7698
uses: actions/setup-node@v4
7799
with:
78-
node-version: 22
100+
node-version: 24
79101
- name: Install Node.js dependencies
80102
run: npm ci
81103
- name: Install ImageMagick
@@ -110,7 +132,7 @@ jobs:
110132

111133
- name: Get all changed content files
112134
env:
113-
GH_TOKEN: ${{ github.token }}
135+
CHANGED_FILES: ${{needs.decide-jobs.outputs.all-changed-files}}
114136
# If changing these regexes, test them on https://regex101.com/ with the
115137
# following paths (you can paste the block in and uncomment it):
116138
# Add new tests if necessary
@@ -126,18 +148,7 @@ jobs:
126148
# scripts/nb-tester/example-notebook.ipynb
127149
id: changed-content-files
128150
run: |
129-
# GitHub API doesn't give PR information in the merge_group event, but
130-
# we can get the PR number from the branch name
131-
if [ "${{ github.event_name }}" == 'merge_group' ]
132-
then
133-
BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
134-
PR_NUMBER="$(echo $BRANCH_NAME | grep -oP '(?<=gh-readonly-queue/main/pr-)\d+')"
135-
else
136-
PR_NUMBER="${{ github.event.number }}"
137-
fi
138-
139-
CHANGED_FILES=$(gh pr diff -R Qiskit/documentation $PR_NUMBER --name-only)
140-
CHANGED_CONTENT_FILES=$(echo "${CHANGED_FILES[@]}" | grep -P $CONTENT_FILE_REGEX || true)
151+
CHANGED_CONTENT_FILES=$(echo "${CHANGED_FILES}" | grep -P $CONTENT_FILE_REGEX || true)
141152
if [ "$CHANGED_CONTENT_FILES" != '' ]
142153
then
143154
echo "ANY_CHANGED=true" >> "$GITHUB_OUTPUT"
@@ -194,13 +205,12 @@ jobs:
194205
runs-on: ubuntu-latest
195206
steps:
196207
- uses: actions/checkout@v4
197-
198208
- name: Get changed files
199209
env:
200-
GH_TOKEN: ${{ github.token }}
210+
CHANGED_FILES: ${{needs.decide-jobs.outputs.all-changed-files}}
201211
run: |
202212
mkdir -p .github/outputs
203-
gh pr diff -R Qiskit/documentation ${{ github.event.number }} --name-only > .github/outputs/changed-files.txt
213+
echo "${CHANGED_FILES}" > .github/outputs/changed-files.txt
204214
205215
- name: Check if extra linux deps needed
206216
id: check-deps
@@ -265,7 +275,7 @@ jobs:
265275
- name: Set up Node.js
266276
uses: actions/setup-node@v4
267277
with:
268-
node-version: 22
278+
node-version: 24
269279
- name: Install Node.js dependencies
270280
run: npm ci
271281
- name: Check internal links

.github/workflows/observability-sync.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ jobs:
2525
- name: Set up Node.js
2626
uses: actions/setup-node@v4
2727
with:
28-
node-version: 18
28+
node-version: 24
2929
- name: Install dependencies
30-
run: |
31-
run: npm ci
30+
run: npm ci
3231

3332
- name: Run observability script
3433
run: npx tsx scripts/js/commands/generateObservabilityPage.ts
3534

3635
- name: Get all changed docs files
3736
id: changed-docs-files
38-
3937
run: |
4038
echo "CHANGED_FILES<<EOF" >> $GITHUB_OUTPUT
4139
git diff --name-only docs >> $GITHUB_OUTPUT

.github/workflows/pr-preview.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
- uses: actions/checkout@v4
4242
- uses: actions/setup-node@v4
4343
with:
44-
node-version: 18
44+
node-version: 24
4545
- uses: actions/setup-python@v5
4646
with:
4747
python-version: "3.11"

.github/workflows/weekly-checks.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Set up Node.js
2626
uses: actions/setup-node@v4
2727
with:
28-
node-version: 18
28+
node-version: 24
2929
- name: Install Node.js dependencies
3030
run: npm ci
3131
- name: Start local Docker preview
@@ -51,7 +51,7 @@ jobs:
5151
- name: Set up Node.js
5252
uses: actions/setup-node@v4
5353
with:
54-
node-version: 18
54+
node-version: 24
5555
- name: Install Node.js dependencies
5656
run: npm ci
5757
- name: Check external links
@@ -67,7 +67,7 @@ jobs:
6767
- name: Set up Node.js
6868
uses: actions/setup-node@v4
6969
with:
70-
node-version: 18
70+
node-version: 24
7171
- name: Install Node.js dependencies
7272
run: npm ci
7373
- name: Check outdated new pills
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
async function fetchAllFiles(prNumber: number): Promise<string[]> {
2+
const results: string[] = [];
3+
let cursor: string | null = null;
4+
5+
while (true) {
6+
const response = await fetch(`https://api.github.com/graphql`, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
11+
},
12+
signal: AbortSignal.timeout(10_000),
13+
body: JSON.stringify({
14+
query: `
15+
query($pr: Int!, $cursor: String) {
16+
repository(owner: "Qiskit", name: "documentation") {
17+
pullRequest(number: $pr) {
18+
files(first: 100, after: $cursor) {
19+
nodes {
20+
path
21+
}
22+
pageInfo {
23+
endCursor
24+
hasNextPage
25+
}
26+
}
27+
}
28+
}
29+
}
30+
`,
31+
variables: {
32+
pr: prNumber,
33+
cursor,
34+
},
35+
}),
36+
});
37+
if (!response.ok) {
38+
const error = await response.text();
39+
throw new Error(`Failed to fetch changed files: ${error}`);
40+
}
41+
42+
const { data } = await response.json();
43+
const files = data.repository.pullRequest.files;
44+
results.push(...files.nodes.map((n: { path: string }) => n.path));
45+
46+
if (!files.pageInfo.hasNextPage) {
47+
break;
48+
}
49+
cursor = files.pageInfo.endCursor;
50+
}
51+
52+
return results;
53+
}
54+
55+
async function main(): Promise<void> {
56+
const prNumber = process.argv[2];
57+
if (!prNumber) {
58+
throw new Error("Must set the PR number as the first argument.");
59+
}
60+
61+
const changed = await fetchAllFiles(Number(prNumber));
62+
console.log(changed.join("\n"));
63+
}
64+
65+
main().catch((e) => {
66+
console.error(e);
67+
process.exit(1);
68+
});

0 commit comments

Comments
 (0)