1
- name : Pre-commit Bot - Trigger
1
+ name : Pre-commit Bot
2
2
3
- run-name : Pre-commit bot trigger
3
+ run-name : Pre-commit bot for PR # ${{ github.event.issue.number }}
4
4
5
5
on :
6
6
issue_comment :
7
7
types : [created]
8
8
9
9
jobs :
10
- trigger :
10
+ pre-commit :
11
11
# Only run on pull request comments
12
12
if : github.event.issue.pull_request && contains(github.event.comment.body, '@github-actions run precommit')
13
13
runs-on : ubuntu-latest
14
14
permissions :
15
- contents : read
15
+ contents : write
16
16
pull-requests : write
17
17
18
18
steps :
19
- - name : Check comment author
19
+ - name : Check comment author and get PR details
20
20
id : check_author
21
21
uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
22
22
with :
@@ -67,12 +67,13 @@ jobs:
67
67
return;
68
68
}
69
69
70
- // Save PR info for the execution workflow
70
+ // Save PR info for later steps
71
71
core.setOutput('pr_number', context.issue.number);
72
72
core.setOutput('pr_head_ref', pr.data.head.ref);
73
73
core.setOutput('pr_head_sha', pr.data.head.sha);
74
74
core.setOutput('pr_head_repo', pr.data.head.repo.full_name);
75
75
core.setOutput('pr_base_ref', pr.data.base.ref);
76
+ core.setOutput('is_fork', pr.data.head.repo.full_name !== context.payload.repository.full_name);
76
77
core.setOutput('authorized', 'true');
77
78
78
79
- name : React to comment
@@ -88,29 +89,139 @@ jobs:
88
89
content: 'rocket'
89
90
});
90
91
91
- - name : Trigger execution workflow
92
+ - name : Comment starting
92
93
if : steps.check_author.outputs.authorized == 'true'
93
94
uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
94
95
with :
95
96
github-token : ${{ secrets.GITHUB_TOKEN }}
96
97
script : |
97
- await github.rest.actions.createWorkflowDispatch ({
98
+ await github.rest.issues.createComment ({
98
99
owner: context.repo.owner,
99
100
repo: context.repo.repo,
100
- workflow_id: 'precommit-execute.yml',
101
- ref: context.payload.repository.default_branch,
102
- inputs: {
103
- pr_number: '${{ steps.check_author.outputs.pr_number }}',
104
- pr_head_ref: '${{ steps.check_author.outputs.pr_head_ref }}',
105
- pr_head_sha: '${{ steps.check_author.outputs.pr_head_sha }}',
106
- pr_head_repo: '${{ steps.check_author.outputs.pr_head_repo }}',
107
- pr_base_ref: '${{ steps.check_author.outputs.pr_base_ref }}'
108
- }
101
+ issue_number: ${{ steps.check_author.outputs.pr_number }},
102
+ body: `⏳ Running pre-commit hooks on PR #${{ steps.check_author.outputs.pr_number }}...`
103
+ });
104
+
105
+ - name : Checkout PR branch (same-repo)
106
+ if : steps.check_author.outputs.authorized == 'true' && steps.check_author.outputs.is_fork == 'false'
107
+ uses : actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
108
+ with :
109
+ ref : ${{ steps.check_author.outputs.pr_head_ref }}
110
+ fetch-depth : 0
111
+ token : ${{ secrets.GITHUB_TOKEN }}
112
+
113
+ - name : Checkout PR branch (fork)
114
+ if : steps.check_author.outputs.authorized == 'true' && steps.check_author.outputs.is_fork == 'true'
115
+ uses : actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
116
+ with :
117
+ repository : ${{ steps.check_author.outputs.pr_head_repo }}
118
+ ref : ${{ steps.check_author.outputs.pr_head_ref }}
119
+ fetch-depth : 0
120
+ token : ${{ secrets.GITHUB_TOKEN }}
121
+
122
+ - name : Verify checkout
123
+ if : steps.check_author.outputs.authorized == 'true'
124
+ run : |
125
+ echo "Current SHA: $(git rev-parse HEAD)"
126
+ echo "Expected SHA: ${{ steps.check_author.outputs.pr_head_sha }}"
127
+ if [[ "$(git rev-parse HEAD)" != "${{ steps.check_author.outputs.pr_head_sha }}" ]]; then
128
+ echo "::error::Checked out SHA does not match expected SHA"
129
+ exit 1
130
+ fi
131
+
132
+ - name : Set up Python
133
+ if : steps.check_author.outputs.authorized == 'true'
134
+ uses : actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
135
+ with :
136
+ python-version : ' 3.12'
137
+ cache : pip
138
+ cache-dependency-path : |
139
+ **/requirements*.txt
140
+ .pre-commit-config.yaml
141
+
142
+ - name : Set up Node.js
143
+ if : steps.check_author.outputs.authorized == 'true'
144
+ uses : actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
145
+ with :
146
+ node-version : ' 20'
147
+ cache : ' npm'
148
+ cache-dependency-path : ' llama_stack/ui/'
149
+
150
+ - name : Install npm dependencies
151
+ if : steps.check_author.outputs.authorized == 'true'
152
+ run : npm ci
153
+ working-directory : llama_stack/ui
154
+
155
+ - name : Run pre-commit
156
+ if : steps.check_author.outputs.authorized == 'true'
157
+ id : precommit
158
+ uses : pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
159
+ continue-on-error : true
160
+ env :
161
+ SKIP : no-commit-to-branch
162
+ RUFF_OUTPUT_FORMAT : github
163
+
164
+ - name : Check for changes
165
+ if : steps.check_author.outputs.authorized == 'true'
166
+ id : changes
167
+ run : |
168
+ if ! git diff --exit-code || [ -n "$(git ls-files --others --exclude-standard)" ]; then
169
+ echo "has_changes=true" >> $GITHUB_OUTPUT
170
+ echo "Changes detected after pre-commit"
171
+ else
172
+ echo "has_changes=false" >> $GITHUB_OUTPUT
173
+ echo "No changes after pre-commit"
174
+ fi
175
+
176
+ - name : Commit and push changes
177
+ if : steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'true'
178
+ run : |
179
+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
180
+ git config --local user.name "github-actions[bot]"
181
+
182
+ git add -A
183
+ git commit -m "style: apply pre-commit fixes
184
+
185
+ 🤖 Applied by @github-actions bot via pre-commit workflow"
186
+
187
+ # Push changes
188
+ git push origin HEAD:${{ steps.check_author.outputs.pr_head_ref }}
189
+
190
+ - name : Comment success with changes
191
+ if : steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'true'
192
+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
193
+ with :
194
+ github-token : ${{ secrets.GITHUB_TOKEN }}
195
+ script : |
196
+ await github.rest.issues.createComment({
197
+ owner: context.repo.owner,
198
+ repo: context.repo.repo,
199
+ issue_number: ${{ steps.check_author.outputs.pr_number }},
200
+ body: `✅ Pre-commit hooks completed successfully!\n\n🔧 Changes have been committed and pushed to the PR branch.`
109
201
});
110
202
203
+ - name : Comment success without changes
204
+ if : steps.check_author.outputs.authorized == 'true' && steps.changes.outputs.has_changes == 'false' && steps.precommit.outcome == 'success'
205
+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
206
+ with :
207
+ github-token : ${{ secrets.GITHUB_TOKEN }}
208
+ script : |
209
+ await github.rest.issues.createComment({
210
+ owner: context.repo.owner,
211
+ repo: context.repo.repo,
212
+ issue_number: ${{ steps.check_author.outputs.pr_number }},
213
+ body: `✅ Pre-commit hooks passed!\n\n✨ No changes needed - your code is already formatted correctly.`
214
+ });
215
+
216
+ - name : Comment failure
217
+ if : failure()
218
+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
219
+ with :
220
+ github-token : ${{ secrets.GITHUB_TOKEN }}
221
+ script : |
111
222
await github.rest.issues.createComment({
112
223
owner: context.repo.owner,
113
224
repo: context.repo.repo,
114
- issue_number: context.issue.number ,
115
- body: `🚀 Pre-commit workflow triggered! Check the [Actions tab ](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/workflows/precommit-execute.yml ) for progress .`
225
+ issue_number: ${{ steps.check_author.outputs.pr_number }} ,
226
+ body: `❌ Pre-commit workflow failed!\n\nPlease check the [workflow logs ](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId} ) for details .`
116
227
});
0 commit comments