-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
110 lines (97 loc) · 3.59 KB
/
action.yml
File metadata and controls
110 lines (97 loc) · 3.59 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
name: 'Find Impacted Tests'
description: 'Find test files impacted by code changes using static dependency analysis'
author: 'sozua'
branding:
icon: 'filter'
color: 'blue'
inputs:
base:
description: 'Base ref to compare against (commit SHA, branch, or tag)'
required: false
default: ${{ github.event.pull_request.base.sha || 'HEAD~1' }}
head:
description: 'Head ref (commit SHA, branch, or tag)'
required: false
default: 'HEAD'
pattern:
description: 'Glob pattern for test files'
required: false
default: '**/*.{test,spec}.{js,mjs,cjs,jsx,ts,mts,cts,tsx}'
working-directory:
description: 'Working directory'
required: false
default: '.'
outputs:
files:
description: 'Space-separated list of impacted test files (for xargs/node --test)'
value: ${{ steps.impacted.outputs.files }}
files-json:
description: 'JSON array of impacted test files'
value: ${{ steps.impacted.outputs.files_json }}
count:
description: 'Number of impacted test files'
value: ${{ steps.impacted.outputs.count }}
has-impacted:
description: 'Whether there are any impacted tests (true/false)'
value: ${{ steps.impacted.outputs.has_impacted }}
runs:
using: 'composite'
steps:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Get changed files
id: changed
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
# Get changed files between base and head
CHANGED=$(git diff --name-only ${{ inputs.base }} ${{ inputs.head }} -- '*.js' '*.mjs' '*.cjs' '*.jsx' '*.ts' '*.mts' '*.cts' '*.tsx' || true)
if [ -z "$CHANGED" ]; then
echo "No source files changed"
echo "changed=" >> $GITHUB_OUTPUT
else
# Escape newlines for GitHub Actions
CHANGED_ESCAPED="${CHANGED//$'\n'/'%0A'}"
echo "changed<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGED" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Find impacted tests
id: impacted
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
if [ -z "${{ steps.changed.outputs.changed }}" ]; then
echo "files=" >> $GITHUB_OUTPUT
echo "files_json=[]" >> $GITHUB_OUTPUT
echo "count=0" >> $GITHUB_OUTPUT
echo "has_impacted=false" >> $GITHUB_OUTPUT
exit 0
fi
# Install impacted
npm install --no-save impacted 2>/dev/null || npx impacted --help >/dev/null 2>&1 || {
echo "Installing impacted..."
npm install -g impacted
}
# Run impacted with changed files
IMPACTED=$(echo "${{ steps.changed.outputs.changed }}" | npx impacted -p "${{ inputs.pattern }}" 2>/dev/null || true)
if [ -z "$IMPACTED" ]; then
echo "files=" >> $GITHUB_OUTPUT
echo "files_json=[]" >> $GITHUB_OUTPUT
echo "count=0" >> $GITHUB_OUTPUT
echo "has_impacted=false" >> $GITHUB_OUTPUT
else
# Space-separated for node --test
FILES_SPACE=$(echo "$IMPACTED" | tr '\n' ' ' | sed 's/ $//')
echo "files=$FILES_SPACE" >> $GITHUB_OUTPUT
# JSON array
FILES_JSON=$(echo "$IMPACTED" | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "files_json=$FILES_JSON" >> $GITHUB_OUTPUT
# Count
COUNT=$(echo "$IMPACTED" | wc -l | tr -d ' ')
echo "count=$COUNT" >> $GITHUB_OUTPUT
echo "has_impacted=true" >> $GITHUB_OUTPUT
echo "Found $COUNT impacted test files"
fi