Skip to content

Commit 41f1d20

Browse files
Copilotpuehringer
andcommitted
Add auto-fix functionality to build-node-python action and workflow
Co-authored-by: puehringer <[email protected]>
1 parent 579fedb commit 41f1d20

File tree

2 files changed

+133
-6
lines changed

2 files changed

+133
-6
lines changed

.github/actions/build-node-python/action.yml

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ inputs:
8585
run_python_build:
8686
default: true
8787
required: false
88+
auto_fix_lint:
89+
description: "automatically fix linting errors when possible and commit changes"
90+
default: false
91+
required: false
8892

8993
runs:
9094
using: "composite"
@@ -161,6 +165,33 @@ runs:
161165
run: |
162166
# Run node and python in parallel
163167
168+
# Function to setup git for committing fixes
169+
setup_git_for_fixes() {
170+
git config --global user.name "github-actions[bot]"
171+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
172+
}
173+
174+
# Function to commit lint fixes
175+
commit_lint_fixes() {
176+
local changes_made="$1"
177+
if [ "$changes_made" = "true" ]; then
178+
if git diff --quiet && git diff --cached --quiet; then
179+
echo "No changes to commit after running fixes"
180+
return 0
181+
fi
182+
183+
echo "Committing lint fixes..."
184+
git add .
185+
if git diff --cached --quiet; then
186+
echo "No staged changes to commit"
187+
return 0
188+
fi
189+
git commit -m "Auto-fix: Apply linting and formatting fixes [skip ci]" || true
190+
git push || true
191+
echo "✓ Lint fixes committed and pushed"
192+
fi
193+
}
194+
164195
# Define the node sequence of commands
165196
node_job() {
166197
set -e
@@ -172,10 +203,34 @@ runs:
172203
}
173204
done
174205
175-
parallel_jobs=()
206+
# Handle node linting with auto-fix capability
176207
if [ "$RUN_NODE_LINT" = "true" ]; then
177-
parallel_jobs+=("yarn run lint --quiet")
208+
echo "Running node lint..."
209+
if ! yarn run lint --quiet; then
210+
if [ "$AUTO_FIX_LINT" = "true" ]; then
211+
echo "Node lint failed. Attempting to auto-fix..."
212+
setup_git_for_fixes
213+
yarn run lint:fix || echo "Warning: yarn lint:fix command failed or not available"
214+
215+
echo "Re-running node lint after fixes..."
216+
if yarn run lint --quiet; then
217+
echo "✓ Node lint passed after auto-fix"
218+
commit_lint_fixes "true"
219+
else
220+
echo "✗ Node lint still failing after auto-fix"
221+
exit 1
222+
fi
223+
else
224+
echo "✗ Node lint failed and auto-fix is disabled"
225+
exit 1
226+
fi
227+
else
228+
echo "✓ Node lint passed"
229+
fi
178230
fi
231+
232+
# Run other node tasks
233+
parallel_jobs=()
179234
if [ "$RUN_NODE_TEST" = "true" ]; then
180235
parallel_jobs+=("yarn run test")
181236
fi
@@ -189,31 +244,61 @@ runs:
189244
parallel_jobs+=("yarn playwright install --with-deps chromium")
190245
fi
191246
192-
parallel --jobs $1 --lb --halt-on-error 2 --verbose ::: "${parallel_jobs[@]}"
247+
if [ ${#parallel_jobs[@]} -gt 0 ]; then
248+
parallel --jobs $1 --lb --halt-on-error 2 --verbose ::: "${parallel_jobs[@]}"
249+
fi
193250
}
194251
195252
# Define the python sequence of commands
196253
python_job() {
197254
set -e
198255
make develop
199256
200-
parallel_jobs=()
257+
# Handle python linting with auto-fix capability
201258
if [ "$RUN_PYTHON_LINT" = "true" ]; then
202-
parallel_jobs+=("make lint check-format")
259+
echo "Running python lint..."
260+
if ! make lint check-format; then
261+
if [ "$AUTO_FIX_LINT" = "true" ]; then
262+
echo "Python lint failed. Attempting to auto-fix..."
263+
setup_git_for_fixes
264+
make format || echo "Warning: make format command failed or not available"
265+
266+
echo "Re-running python lint after fixes..."
267+
if make lint check-format; then
268+
echo "✓ Python lint passed after auto-fix"
269+
commit_lint_fixes "true"
270+
else
271+
echo "✗ Python lint still failing after auto-fix"
272+
exit 1
273+
fi
274+
else
275+
echo "✗ Python lint failed and auto-fix is disabled"
276+
exit 1
277+
fi
278+
else
279+
echo "✓ Python lint passed"
280+
fi
203281
fi
282+
283+
# Run other python tasks
284+
parallel_jobs=()
204285
if [ "$RUN_PYTHON_TEST" = "true" ]; then
205286
parallel_jobs+=("make test")
206287
fi
207288
if [ "$RUN_PYTHON_BUILD" = "true" ]; then
208289
parallel_jobs+=("make build")
209290
fi
210291
211-
parallel --jobs $1 --lb --halt-on-error 2 --verbose ::: "${parallel_jobs[@]}"
292+
if [ ${#parallel_jobs[@]} -gt 0 ]; then
293+
parallel --jobs $1 --lb --halt-on-error 2 --verbose ::: "${parallel_jobs[@]}"
294+
fi
212295
}
213296
214297
# Export the functions so they can be used by GNU parallel
215298
export -f node_job
216299
export -f python_job
300+
export -f setup_git_for_fixes
301+
export -f commit_lint_fixes
217302
218303
# If RUN_PARALLEL is set, set --jobs to 0, otherwise to 1
219304
N_JOBS=1
@@ -243,6 +328,7 @@ runs:
243328
RUN_PYTHON_LINT: ${{ inputs.run_python_lint }}
244329
RUN_PYTHON_TEST: ${{ inputs.run_python_test }}
245330
RUN_PYTHON_BUILD: ${{ inputs.run_python_build }}
331+
AUTO_FIX_LINT: ${{ inputs.auto_fix_lint }}
246332
# Node
247333
- name: Save yarn cache
248334
uses: actions/cache/save@v4

.github/workflows/build-node-python.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ on:
88
required: false
99
# When using github.ref || github.head_ref, it would contain the full path, including /, which breaks the postgres hostname
1010
default: ${{ github.sha }}
11+
auto_fix_lint:
12+
description: "Automatically fix linting errors when possible and commit changes"
13+
type: boolean
14+
required: false
15+
default: false
1116
cypress_enable:
1217
description: "Global enable for cypress"
1318
type: boolean
@@ -100,6 +105,38 @@ on:
100105
required: false
101106
CHROMATIC_PROJECT_TOKEN:
102107
required: false
108+
workflow_dispatch:
109+
inputs:
110+
branch:
111+
type: string
112+
required: false
113+
description: "Branch to build"
114+
default: "main"
115+
auto_fix_lint:
116+
description: "Automatically fix linting errors when possible and commit changes"
117+
type: boolean
118+
required: false
119+
default: false
120+
cypress_enable:
121+
description: "Global enable for cypress"
122+
type: boolean
123+
required: false
124+
default: false
125+
playwright_enable:
126+
description: "Global enable for playwright"
127+
type: boolean
128+
required: false
129+
default: false
130+
node_run_webpack:
131+
description: "Flag to always run the webpack production build."
132+
type: boolean
133+
required: false
134+
default: false
135+
run_parallel:
136+
description: "Flag to ensure that node and python jobs are run in parallel."
137+
type: boolean
138+
required: false
139+
default: true
103140

104141
env:
105142
NPM_REGISTRY: "https://registry.npmjs.org/"
@@ -156,6 +193,7 @@ jobs:
156193
enable_python_cache: ${{ inputs.runs_on != 'self-hosted' }}
157194
chromatic_enable: ${{ inputs.chromatic_enable }}
158195
chromatic_project_token: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
196+
auto_fix_lint: ${{ inputs.auto_fix_lint }}
159197

160198
build-python:
161199
name: Python
@@ -193,6 +231,7 @@ jobs:
193231
run_node_bundle: ${{ inputs.node_run_webpack }}
194232
enable_node_cache: ${{ inputs.runs_on != 'self-hosted' }}
195233
enable_python_cache: ${{ inputs.runs_on != 'self-hosted' }}
234+
auto_fix_lint: ${{ inputs.auto_fix_lint }}
196235

197236
# If cypress is used, build node and python sequentially as it is avoiding the duplicate install overhead
198237
build-node-python-cypress:
@@ -285,6 +324,7 @@ jobs:
285324
enable_python_cache: ${{ inputs.cypress_runs_on != 'self-hosted' && inputs.runs_on != 'self-hosted' }}
286325
chromatic_enable: ${{ inputs.chromatic_enable }}
287326
chromatic_project_token: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
327+
auto_fix_lint: ${{ inputs.auto_fix_lint }}
288328
- name: Decrypt .env.enc and <app>/.env.enc
289329
run: |
290330
yarn run env:decrypt -pass env:ENV_PASSWORD || true
@@ -428,6 +468,7 @@ jobs:
428468
enable_python_cache: ${{ inputs.playwright_runs_on != 'self-hosted' && inputs.runs_on != 'self-hosted' }}
429469
chromatic_enable: false # Set to false as we run chromatic below w/ playwright integration
430470
chromatic_project_token: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
471+
auto_fix_lint: ${{ inputs.auto_fix_lint }}
431472
- name: Decrypt .env.enc and <app>/.env.enc
432473
run: |
433474
yarn run env:decrypt -pass env:ENV_PASSWORD || true

0 commit comments

Comments
 (0)