Repository navigation
278 lines (258 loc) · 12.3 KB
/
Copy pathci-detect-changes.yml
File metadata and controls
278 lines (258 loc) · 12.3 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: CI / Detect Changes
on:
workflow_call:
inputs:
runs-on:
description: "Runner for every job, as JSON: a label such as \"ubuntu-latest\" or a label list such as [\"self-hosted\"]. rtCamp private repos pass [\"self-hosted\"]."
type: string
default: '"ubuntu-latest"'
node-version:
description: "Node.js version used to run the wp-tooling CLI."
type: string
default: "22"
ignore-paths:
description: "Regex of paths to exclude from the counts and file lists. Empty uses the CLI's built-in ignore set."
type: string
default: ""
base-ref:
description: "Explicit git ref to diff HEAD against. Empty resolves it from the pull request base or the push event."
type: string
default: ""
working-dir:
description: >-
Directory the project lives in, relative to the repo root. A path scope, not a working
directory: the diff is always taken from the repo root. Restricts the css/js/php outputs
to it and makes those file lists relative to it; total/ignored/gha stay repo-wide.
type: string
default: "."
wp-tooling-ref:
description: "Version or range of @rtcamp/wp-tooling to run from npm. Leave empty for ^2."
type: string
default: ""
outputs:
total-count:
description: >-
Number of changed files after the ignore filter. Always repo-wide, never restricted by
working-dir: the CLI buckets only CSS/JS/PHP/GHA, so a scoped total would drop every other
file type.
value: ${{ jobs.detect.outputs.total-count }}
ignored-count:
description: "Number of changed files dropped by the ignore filter. Always repo-wide — NOT restricted by working-dir."
value: ${{ jobs.detect.outputs.ignored-count }}
css-count:
description: "Number of changed CSS/SCSS files under working-dir."
value: ${{ jobs.detect.outputs.css-count }}
js-count:
description: "Number of changed JS files under working-dir."
value: ${{ jobs.detect.outputs.js-count }}
php-count:
description: "Number of changed PHP files under working-dir."
value: ${{ jobs.detect.outputs.php-count }}
gha-count:
description: "Number of changed GitHub Actions workflow files. Always repo-wide: workflow files only exist at .github/workflows/ in the repo root."
value: ${{ jobs.detect.outputs.gha-count }}
css-files:
description: "Newline-separated changed CSS/SCSS files, relative to working-dir. Pass straight into ci-lint-css.yml's changed-files, provided that workflow is given the same working-dir."
value: ${{ jobs.detect.outputs.css-files }}
js-files:
description: "Newline-separated changed JS files, relative to working-dir. Pass straight into ci-lint-js.yml's changed-files, provided that workflow is given the same working-dir."
value: ${{ jobs.detect.outputs.js-files }}
php-files:
description: "Newline-separated changed PHP files, relative to working-dir. Pass straight into ci-lint-php.yml's changed-files, provided that workflow is given the same working-dir."
value: ${{ jobs.detect.outputs.php-files }}
gha-files:
description: "Newline-separated changed GitHub Actions workflow files, repo-root-relative. Always repo-wide — NOT restricted by working-dir."
value: ${{ jobs.detect.outputs.gha-files }}
permissions:
contents: read
jobs:
detect:
name: Detect changes
runs-on: ${{ fromJSON(inputs.runs-on) }}
outputs:
# css/js/php come from the scope step; total/ignored/gha stay repo-wide on detect.
total-count: ${{ steps.detect.outputs.total-count }}
ignored-count: ${{ steps.detect.outputs.ignored-count }}
css-count: ${{ steps.scope.outputs.css-count }}
js-count: ${{ steps.scope.outputs.js-count }}
php-count: ${{ steps.scope.outputs.php-count }}
# Workflow files only exist at the repo root, so scoping gha would always yield 0.
gha-count: ${{ steps.detect.outputs.gha-count }}
css-files: ${{ steps.scope.outputs.css-files }}
js-files: ${{ steps.scope.outputs.js-files }}
php-files: ${{ steps.scope.outputs.php-files }}
gha-files: ${{ steps.detect.outputs.gha-files }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 2
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: ${{ inputs.node-version }}
- name: Install wp-tooling
shell: bash
env:
WP_TOOLING_VERSION: ${{ inputs.wp-tooling-ref || '^2' }}
run: |
set -euo pipefail
npm install --ignore-scripts --no-audit --no-fund --prefix "$RUNNER_TEMP/wp-tooling" "@rtcamp/wp-tooling@$WP_TOOLING_VERSION"
npm ls --prefix "$RUNNER_TEMP/wp-tooling" @rtcamp/wp-tooling
echo "$RUNNER_TEMP/wp-tooling/node_modules/.bin" >> "$GITHUB_PATH"
# The CLI fetches its own base unless given --base, so resolving it here keeps the token out
# of the CLI's environment.
- name: Resolve diff base
id: base
shell: bash
env:
BASE_REF: ${{ inputs.base-ref }}
EVENT_NAME: ${{ github.event_name }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PUSH_BEFORE: ${{ github.event.before }}
GITHUB_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
# Keyed on the server URL so GHES and ghe.com hosts match too.
fetch() {
local auth
auth="$(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 | tr -d '\n')"
echo "::add-mask::$auth"
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraheader" \
GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $auth" \
git fetch --no-tags --depth=1 origin "$1"
}
if [ -n "$BASE_REF" ]; then
if ! base="$(git rev-parse --verify -q "${BASE_REF}^{commit}")"; then
fetch "${BASE_REF#origin/}" ||
{ echo "::error::base-ref '$BASE_REF' could not be fetched from origin"; exit 1; }
base="$(git rev-parse FETCH_HEAD)"
fi
elif [ -n "${GITHUB_BASE_REF:-}" ]; then
# Two parents alone prove nothing: pull_request_target checks out the base branch,
# whose tip can be a merge commit too.
head_parent="$(git rev-parse -q --verify 'HEAD^2^{commit}' || true)"
if [[ "$EVENT_NAME" != "pull_request" || "$GITHUB_REF" != refs/pull/*/merge ||
-z "$PR_HEAD_SHA" || "$head_parent" != "$PR_HEAD_SHA" ]]; then
echo "::error::Not a pull_request merge checkout (event '${EVENT_NAME}', ref '${GITHUB_REF}'); no PR changes to detect."
exit 1
fi
base="$(git rev-parse --verify 'HEAD^1^{commit}')"
elif [ -n "$PUSH_BEFORE" ] && [[ ! "$PUSH_BEFORE" =~ ^0+$ ]]; then
if ! git cat-file -e "${PUSH_BEFORE}^{commit}" 2>/dev/null; then
fetch "$PUSH_BEFORE" ||
{ echo "::error::Could not fetch the push's previous commit ${PUSH_BEFORE}; pass base-ref."; exit 1; }
fi
base="$PUSH_BEFORE"
elif ! base="$(git rev-parse --verify -q 'HEAD~1^{commit}')"; then
# A shallow checkout hides parents, so read them from the raw commit.
commit="$(git cat-file commit HEAD)"
if [[ "${commit%%$'\n\n'*}" == *$'\n'parent\ * ]]; then
echo "::error::HEAD's parent is missing from the checkout; pass base-ref."
exit 1
fi
base="$(git hash-object -t tree /dev/null)"
echo "::notice::HEAD is a root commit, so every file in it counts as changed."
fi
echo "sha=$base" >> "$GITHUB_OUTPUT"
- name: Detect changed files
id: detect
shell: bash
env:
IGNORE_PATHS: ${{ inputs.ignore-paths }}
BASE: ${{ steps.base.outputs.sha }}
run: |
set -euo pipefail
args=(--strict --output github --include-files --base "$BASE")
if [ -n "$IGNORE_PATHS" ]; then
args+=(--ignore "$IGNORE_PATHS")
fi
wp-tooling detect-changes "${args[@]}"
# The detector emits repo-root-relative paths but the linters run with cwd = working-dir,
# so an unscoped list resolves to <dir>/<dir>/<file>. At working-dir '.' this is a pure
# pass-through, counts included.
- name: Scope results to working-dir
id: scope
shell: bash
env:
WORKING_DIR: ${{ inputs.working-dir }}
TOTAL_COUNT: ${{ steps.detect.outputs.total-count }}
CSS_FILES: ${{ steps.detect.outputs.css-files }}
JS_FILES: ${{ steps.detect.outputs.js-files }}
PHP_FILES: ${{ steps.detect.outputs.php-files }}
CSS_COUNT: ${{ steps.detect.outputs.css-count }}
JS_COUNT: ${{ steps.detect.outputs.js-count }}
PHP_COUNT: ${{ steps.detect.outputs.php-count }}
run: |
set -euo pipefail
dir="${WORKING_DIR:-.}"
dir="${dir#./}"
dir="${dir%/}"
[ -n "$dir" ] || dir="."
case "$dir" in
/*) echo "::error::working-dir must be relative to the repo root (got '$WORKING_DIR')"; exit 1 ;;
*..*) echo "::error::working-dir must not contain '..' (got '$WORKING_DIR')"; exit 1 ;;
esac
if [ "$dir" != "." ] && [ ! -d "$dir" ]; then
echo "::error::working-dir '${WORKING_DIR}' does not exist in this repository."
exit 1
fi
# Non-empty lines only, so an empty list counts 0 rather than 1.
count() { printf '%s' "${1-}" | awk 'NF{n++} END{print n+0}'; }
# Random delimiter so a crafted filename cannot forge a step output.
emit() {
local name="$1" val="${2-}" delim
delim="ghadelim_$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')"
{
printf '%s<<%s\n' "$name" "$delim"
if [ -n "$val" ]; then
printf '%s\n' "$val"
fi
printf '%s\n' "$delim"
} >> "$GITHUB_OUTPUT"
}
# Prefix stripped so a consumer with cwd=$dir can open them. String matching, not
# regex, so metacharacters in a path need no escaping.
scope() {
local p out=""
while IFS= read -r p; do
[ -n "$p" ] || continue
case "$p" in
"$dir"/*) out="${out}${p#"$dir"/}"$'\n' ;;
esac
done <<< "${1-}"
# No trailing newline: the lint workflows xargs this, so one would pass an extra
# empty path to the linter.
printf '%s' "${out%$'\n'}"
}
if [ "$dir" = "." ]; then
# Pure pass-through — the CLI's own counts are authoritative, so do not recount.
emit css-files "$CSS_FILES"
emit js-files "$JS_FILES"
emit php-files "$PHP_FILES"
{
echo "css-count=${CSS_COUNT}"
echo "js-count=${JS_COUNT}"
echo "php-count=${PHP_COUNT}"
} >> "$GITHUB_OUTPUT"
exit 0
fi
css="$(scope "$CSS_FILES")"
js="$(scope "$JS_FILES")"
php="$(scope "$PHP_FILES")"
css_n="$(count "$css")"; js_n="$(count "$js")"; php_n="$(count "$php")"
echo "::notice::Scoped to '${dir}/': css=${css_n} js=${js_n} php=${php_n}."
if [ "${TOTAL_COUNT:-0}" -gt 0 ] && [ "$css_n" -eq 0 ] && [ "$js_n" -eq 0 ] && [ "$php_n" -eq 0 ]; then
echo "::warning::${TOTAL_COUNT} file(s) changed in this repository but none are in scope" \
"for '${dir}', so no lint, test or build job ran for it. If this unit depends on code" \
"outside '${dir}', that dependency is not covered by this call."
fi
emit css-files "$css"
emit js-files "$js"
emit php-files "$php"
{
echo "css-count=${css_n}"
echo "js-count=${js_n}"
echo "php-count=${php_n}"
} >> "$GITHUB_OUTPUT"