1- name : Lint Auto-fix
1+ name : Lint and Format Auto-fix
22
33on :
44 workflow_run :
@@ -16,7 +16,7 @@ permissions:
1616
1717jobs :
1818 autofix :
19- # Only run on pull requests where lint failed
19+ # Only run on pull requests where lint or format failed
2020 if : >
2121 github.event.workflow_run.event == 'pull_request' &&
2222 github.event.workflow_run.conclusion == 'failure'
@@ -39,19 +39,19 @@ jobs:
3939 repo: context.repo.repo,
4040 commit_sha: '${{ github.event.workflow_run.head_sha }}'
4141 });
42-
42+
4343 if (response.data.length === 0) {
4444 core.info('No pull request found for this commit');
4545 return;
4646 }
47-
47+
4848 const pr = response.data[0];
4949 core.setOutput('number', pr.number);
5050 core.setOutput('head_ref', pr.head.ref);
5151 core.setOutput('head_repo', pr.head.repo.full_name);
5252
53- - name : Check if lint job failed
54- id : check_lint
53+ - name : Check which jobs failed
54+ id : check_jobs
5555 uses : actions/github-script@5c56fde4671bc2d3592fb0f2c5b5bab9ddae03b1
5656 with :
5757 script : |
@@ -60,25 +60,29 @@ jobs:
6060 repo: context.repo.repo,
6161 run_id: ${{ github.event.workflow_run.id }}
6262 });
63-
63+
6464 const lintJob = jobs.data.jobs.find(job => job.name === 'lint');
65-
66- if (!lintJob) {
67- core.info('No lint job found');
68- core.setOutput('should_fix', 'false');
69- return;
70- }
71-
72- if (lintJob.conclusion === 'failure') {
65+ const formatJob = jobs.data.jobs.find(job => job.name === 'format');
66+
67+ const lintFailed = lintJob && lintJob.conclusion === 'failure';
68+ const formatFailed = formatJob && formatJob.conclusion === 'failure';
69+
70+ core.setOutput('lint_failed', lintFailed ? 'true' : 'false');
71+ core.setOutput('format_failed', formatFailed ? 'true' : 'false');
72+ core.setOutput('should_fix', (lintFailed || formatFailed) ? 'true' : 'false');
73+
74+ if (lintFailed) {
7375 core.info('Lint job failed, will attempt auto-fix');
74- core.setOutput('should_fix', 'true');
75- } else {
76- core.info('Lint job did not fail');
77- core.setOutput('should_fix', 'false');
76+ }
77+ if (formatFailed) {
78+ core.info('Format job failed, will attempt auto-fix');
79+ }
80+ if (!lintFailed && !formatFailed) {
81+ core.info('Neither lint nor format jobs failed');
7882 }
7983
8084 - name : Checkout PR branch
81- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
85+ if : steps.check_jobs .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
8286 uses : actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
8387 with :
8488 repository : ${{ steps.pr.outputs.head_repo }}
@@ -87,40 +91,55 @@ jobs:
8791 fetch-depth : 0
8892
8993 - name : Set up Node.js
90- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
94+ if : steps.check_jobs .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
9195 uses : actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f
9296 with :
9397 node-version-file : ' .nvmrc'
9498 cache : ' npm'
9599
96100 - name : Install dependencies
97- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
101+ if : steps.check_jobs .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
98102 run : npm ci
99103
104+ - name : Run format auto-fix
105+ if : steps.check_jobs.outputs.format_failed == 'true' && steps.pr.outputs.head_ref != ''
106+ run : npm run format
107+
100108 - name : Run lint auto-fix
101- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
109+ if : steps.check_jobs .outputs.lint_failed == 'true' && steps.pr.outputs.head_ref != ''
102110 run : |
103111 npm run lint:fix || true
104112 npm run lint:css:fix || true
105- npm run format || true
106113
107114 - name : Commit and push changes
108- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
115+ if : steps.check_jobs .outputs.should_fix == 'true' && steps.pr.outputs.head_ref != ''
109116 uses : stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9
110117 with :
111- commit_message : ' chore: auto-fix lint issues'
118+ commit_message : ' chore: auto-fix lint and formatting issues'
112119 commit_user_name : ' github-actions[bot]'
113120 commit_user_email : ' github-actions[bot]@users.noreply.github.com'
114121
115122 - name : Comment on PR
116- if : steps.check_lint .outputs.should_fix == 'true' && steps.pr.outputs.number != ''
123+ if : steps.check_jobs .outputs.should_fix == 'true' && steps.pr.outputs.number != ''
117124 uses : actions/github-script@5c56fde4671bc2d3592fb0f2c5b5bab9ddae03b1
118125 with :
119126 github-token : ${{ steps.generate_token.outputs.token }}
120127 script : |
128+ const lintFailed = '${{ steps.check_jobs.outputs.lint_failed }}' === 'true';
129+ const formatFailed = '${{ steps.check_jobs.outputs.format_failed }}' === 'true';
130+
131+ let message = '🤖 ';
132+ if (lintFailed && formatFailed) {
133+ message += 'Lint and formatting issues have been automatically fixed and committed to this PR.';
134+ } else if (lintFailed) {
135+ message += 'Lint issues have been automatically fixed and committed to this PR.';
136+ } else if (formatFailed) {
137+ message += 'Formatting issues have been automatically fixed and committed to this PR.';
138+ }
139+
121140 await github.rest.issues.createComment({
122141 owner: context.repo.owner,
123142 repo: context.repo.repo,
124143 issue_number: ${{ steps.pr.outputs.number }},
125- body: '🤖 Lint issues have been automatically fixed and committed to this PR.'
144+ body: message
126145 });
0 commit comments