Repository navigation
156 lines (143 loc) · 8.42 KB
/
Copy pathmutation-testing.yml
File metadata and controls
156 lines (143 loc) · 8.42 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
name: Changed-code mutation testing
on:
pull_request:
types: [edited, opened, reopened, ready_for_review, synchronize]
merge_group:
types: [checks_requested]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
# The open pull request map is read in a job that never checks out or runs pull-request
# code, so the token cannot reach anything the contributor controls. Only the filtered map
# crosses into the job that runs the gate. Each entry carries the draft flag and the base ref: a
# draft pull request is not gated, and a pull request whose base is another branch is not the
# branch this unit lands on, so either one makes the gate measure against the event base instead.
stacked_map:
name: Resolve the stacked parent
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
pull-requests: read
# The map is published as an artifact for the gate job, which is the only artifact this
# job writes; it never sees pull-request code because this job checks out nothing.
actions: write
steps:
- name: Read the open pull request heads
id: read_map
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# The fallback must itself be a command: this step runs bash with -e, so a bare '[]'
# is executed as a program name and an API failure would abort the job instead of
# measuring against the event base.
# --jq runs once per page, so a paginated call emits one JSON array per page; the
# gate reads that stream, not a single array.
# The base ref travels with every entry: the gate only measures a unit against a parent
# that targets the branch the unit itself targets.
STACKED_MAP="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/pulls?state=open&per_page=100" --jq '[.[] | {number: .number, headSha: .head.sha, draft: (.draft == true), baseRef: .base.ref}]')" || STACKED_MAP="$(printf '[]')"
# Publish the map as a file in an artifact rather than as a job output that the gate
# step then receives as an argument: Linux caps one argument or environment string at
# 128 KiB (MAX_ARG_STRLEN), and a map past that cap makes the gate step unstartable
# instead of merely large. A failed write aborts this job: a truncated or missing map
# would leave the gate measuring against the event base while still reporting success.
case "$STACKED_MAP" in
"["*) ;;
*) echo "::error title=Stacked unit base::The stacked map is not a JSON array" >&2; exit 1 ;;
esac
printf '%s\n' "$STACKED_MAP" > stacked-map.json
# A failed upload must not skip the gate: the download side already treats a missing
# artifact as "measure against the event base" (stricter, never looser), and that
# fallback can only run if this job still succeeds.
- name: Publish the stacked map
continue-on-error: true
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: mutation-stacked-map
path: stacked-map.json
if-no-files-found: error
retention-days: 1
mutation-diff:
name: mutation-diff
needs: stacked_map
if: github.event_name == 'merge_group' || github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 30
# Only the filtered map crosses into this job, and it crosses as a file: the token stays in the
# job that read it. actions:write covers reading that artifact and publishing the reports below.
permissions:
contents: read
actions: write
steps:
- name: Record merge-queue enforcement
if: github.event_name == 'merge_group'
run: |
{
echo "## Changed-code mutation testing"
echo "Mutation testing was enforced on each pull request before it entered the merge queue."
} >> "$GITHUB_STEP_SUMMARY" || echo "::warning title=Mutation test advisory::Could not write the job summary"
- name: Checkout pull request merge result
if: github.event_name == 'pull_request'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
fetch-depth: 0
persist-credentials: false
- name: Setup Node.js and pnpm
if: github.event_name == 'pull_request'
uses: ./.github/actions/setup-node-pnpm
with:
install-args: "--frozen-lockfile"
- name: Test mutation gate logic
if: github.event_name == 'pull_request'
run: pnpm test:mutation-ci
- name: Read the stacked map
if: github.event_name == 'pull_request'
# Downloaded outside the checkout on purpose: a pull request can commit .mutation/stacked-map.json
# itself, and with continue-on-error a missing artifact would leave that committed file in place for
# the gate to read as if it were the map. Under RUNNER_TEMP a missing artifact means no file, which
# degrades to the event base - stricter than the intended measurement, never looser - and the gate
# logs it.
continue-on-error: true
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: mutation-stacked-map
path: ${{ runner.temp }}/mutation-stacked-map
- name: Enforce executable-line scope and run advisory mutation testing
if: github.event_name == 'pull_request'
env:
HEAD_SHA: ${{ github.sha }}
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
BASE_SHA="$(git rev-parse "$HEAD_SHA^1")"
# The merge commit's second parent is the pull request head. When that head's own
# parent is another open pull request's head, this PR is a stacked unit, so the gate
# measures only this unit's delta instead of every unmerged ancestor.
# The event already carries that head for provenance, so it wins here; deriving it from
# the merge commit stays as the fallback for a manual run with no pull_request payload.
PR_HEAD_SHA="${PR_HEAD_SHA:-$(git rev-parse "$HEAD_SHA^2" 2>/dev/null || git rev-parse "$HEAD_SHA")}"
node scripts/stryker-diff.mjs ci --base "$BASE_SHA" --head "$HEAD_SHA" --pr-head "$PR_HEAD_SHA" --pr-base-ref "$PR_BASE_REF" --stacked-map-file "${RUNNER_TEMP}/mutation-stacked-map/stacked-map.json"
- name: Upload mutation reports
id: mutation_report
if: always() && github.event_name == 'pull_request'
continue-on-error: true
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: changed-code-mutation-report
path: reports/mutation/
if-no-files-found: ignore
retention-days: 7
- name: Link mutation report artifact
if: always() && github.event_name == 'pull_request' && steps.mutation_report.outputs.artifact-url != ''
env:
ARTIFACT_URL: ${{ steps.mutation_report.outputs.artifact-url }}
run: |
{
echo ""
echo "### Download mutation reports"
echo "[Open the changed-code-mutation-report artifact]($ARTIFACT_URL), then open the package's mutation.html file."
} >> "$GITHUB_STEP_SUMMARY" || echo "::warning title=Mutation test advisory::Could not write the job summary"