Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

pr.yml

150 lines6.8 KB

1name: PR
2​
3on:
4 pull_request:
5​
6concurrency:
7 group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
8 cancel-in-progress: true
9​
10env:
11 NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
12​
13permissions:
14 contents: read
15​
16jobs:
17 test:
18 name: Test
19 runs-on: ubuntu-latest
20 steps:
21 - name: Checkout
22 uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
23 with:
24 fetch-depth: 0
25 persist-credentials: false
26 - name: Setup Tools
27 uses: TanStack/config/.github/setup@190f659075ff0845850e330883eb26d7ffd0671f # main
28 - name: Get base and head commits for `nx affected`
29 uses: nrwl/nx-set-shas@3e9ad7370203c1e93d109be57f3b72eb0eb511b1 # v4.4.0
30 with:
31 main-branch-name: main
32 - name: Run Checks
33 run: pnpm run test:pr
34 coverage:
35 name: Coverage
36 runs-on: ubuntu-latest
37 permissions:
38 contents: read
39 pull-requests: write # the last step posts the table as a PR comment; only it reads the token
40 steps:
41 - name: Checkout
42 uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
43 with:
44 fetch-depth: 0
45 persist-credentials: false
46 - name: Setup Tools
47 uses: TanStack/config/.github/setup@190f659075ff0845850e330883eb26d7ffd0671f # main
48 - name: Get base and head commits for `nx affected`
49 uses: nrwl/nx-set-shas@3e9ad7370203c1e93d109be57f3b72eb0eb511b1 # v4.4.0
50 with:
51 main-branch-name: main
52 # Stashed outside the working tree because checking out the merge base
53 # would otherwise remove it — the script may be added or changed by the
54 # very PR under test. Both runs and the comparison use this copy, so all
55 # three agree on the version.
56 - name: Stage the coverage script
57 run: cp scripts/coverage-check.mjs "$RUNNER_TEMP/coverage-check.mjs"
58 # The set of packages to measure is decided once, here, so that the base
59 # run measures exactly what the head run does. `--with-target` keeps out
60 # projects that have no test:coverage script, so a docs-only PR resolves
61 # to an empty list rather than erroring in run-many.
62 - name: Resolve affected packages
63 id: affected
64 run: |
65 list=$(pnpm exec nx show projects --affected --with-target=test:coverage --exclude='examples/**,testing/**' --json | jq -r 'sort | join(",")')
66 echo "list=$list" >> "$GITHUB_OUTPUT"
67 echo "Measuring: ${list:-<none>}"
68 - name: Coverage for this PR
69 if: steps.affected.outputs.list != ''
70 # Via env, not `${{ }}` inside run: project names come from the PR's own
71 # files, so interpolating them into the shell would be injectable.
72 env:
73 PROJECTS: ${{ steps.affected.outputs.list }}
74 run: |
75 rm -rf packages/*/coverage
76 pnpm exec nx run-many --targets=test:coverage --projects="$PROJECTS" --outputStyle=static -- --coverage.reporter=json-summary --coverage.include='src/**'
77 node "$RUNNER_TEMP/coverage-check.mjs" --collect "$RUNNER_TEMP/cov-head" --expect "$PROJECTS"
78 # Re-measuring the merge-base is normally a set of Nx Cloud cache
79 # restores seeded by the Coverage workflow on main: the task hashes are
80 # identical, and `test:coverage` declares its coverage dir as an output,
81 # so the summaries come back without the tests re-running.
82 - name: Coverage for the merge base
83 if: steps.affected.outputs.list != ''
84 env:
85 PROJECTS: ${{ steps.affected.outputs.list }}
86 run: |
87 git checkout --force --detach "$NX_BASE"
88 # coverage/ is gitignored, so the checkout leaves this PR's results in
89 # place — without this the base collection silently re-reads head's
90 # numbers and every delta comes out as 0.00.
91 rm -rf packages/*/coverage
92 pnpm install --frozen-lockfile
93 # Intersect with projects that have test:coverage on the base commit
94 # (added packages, or packages that gained the script in this PR).
95 printf '%s\n' "$PROJECTS" | tr ',' '\n' | sort > /tmp/wanted
96 pnpm exec nx show projects --with-target=test:coverage --json | jq -r '.[]' | sort > /tmp/available
97 base_list=$(comm -12 /tmp/wanted /tmp/available | paste -sd, -)
98 if [ -z "$base_list" ]; then
99 echo "No affected package has test:coverage on the base commit; treating all as new."
100 mkdir -p "$RUNNER_TEMP/cov-base"
101 exit 0
102 fi
103 # Extra reporters/include so a merge-base whose scripts are still
104 # `vitest run --coverage` still writes coverage-summary.json.
105 pnpm exec nx run-many --targets=test:coverage --projects="$base_list" --outputStyle=static -- --coverage.reporter=json-summary --coverage.include='src/**'
106 node "$RUNNER_TEMP/coverage-check.mjs" --collect "$RUNNER_TEMP/cov-base" --expect "$base_list"
107 - name: Compare
108 if: steps.affected.outputs.list != ''
109 run: |
110 node "$RUNNER_TEMP/coverage-check.mjs" \
111 --base "$RUNNER_TEMP/cov-base" \
112 --head "$RUNNER_TEMP/cov-head" \
113 --report "$RUNNER_TEMP/coverage-report.md"
114 # One comment per PR, updated on each push. Runs when Compare fails too.
115 # A fork's token cannot write comments, so fork PRs skip this step; the
116 # table is in the job summary either way.
117 - name: Post the coverage report
118 if: ${{ !cancelled() && steps.affected.outputs.list != '' && github.event.pull_request.head.repo.full_name == github.repository }}
119 env:
120 GH_TOKEN: ${{ github.token }}
121 REPO: ${{ github.repository }}
122 PR_NUMBER: ${{ github.event.pull_request.number }}
123 run: |
124 report="$RUNNER_TEMP/coverage-report.md"
125 [ -f "$report" ] || exit 0
126 ids=$(gh api "repos/$REPO/issues/$PR_NUMBER/comments" --paginate \
127 --jq '.[] | select(.user.login == "github-actions[bot]" and (.body | startswith("<!-- coverage-report -->"))) | .id')
128 read -r id <<< "$ids" || true
129 if [ -n "$id" ]; then
130 gh api -X PATCH "repos/$REPO/issues/comments/$id" -F "body=@$report" > /dev/null
131 else
132 gh api -X POST "repos/$REPO/issues/$PR_NUMBER/comments" -F "body=@$report" > /dev/null
133 fi
134 preview:
135 name: Preview
136 runs-on: ubuntu-latest
137 permissions:
138 contents: read
139 pull-requests: write
140 steps:
141 - name: Checkout
142 uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
143 with:
144 persist-credentials: false
145 - name: Setup Tools
146 uses: TanStack/config/.github/setup@190f659075ff0845850e330883eb26d7ffd0671f # main
147 - name: Build Packages
148 run: pnpm run build:all
149 - name: Publish Previews
150 run: pnpx pkg-pr-new publish --pnpm './packages/*'