Skip to content

Commit f53e7f4

Browse files
Copilotmmcky
andcommitted
Add complete workflow template and update examples for proper setup
Co-authored-by: mmcky <[email protected]>
1 parent 4e88411 commit f53e7f4

File tree

2 files changed

+264
-4
lines changed

2 files changed

+264
-4
lines changed

.github/actions/code-style-checker/examples.md

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ jobs:
6969
7070
### Complete PR Comment Workflow
7171
72+
Copy this complete workflow to your repository as `.github/workflows/code-style-formatter.yml`:
73+
7274
```yaml
7375
name: Code Style Formatter
7476
on:
@@ -95,6 +97,8 @@ jobs:
9597
core.setOutput('head-sha', pullRequest.head.sha);
9698
core.setOutput('head-ref', pullRequest.head.ref);
9799
core.setOutput('base-sha', pullRequest.base.sha);
100+
101+
return pullRequest;
98102
99103
- name: Checkout PR branch
100104
uses: actions/checkout@v4
@@ -111,17 +115,118 @@ jobs:
111115
base_sha: ${{ steps.pr.outputs.base-sha }}
112116
sha: ${{ steps.pr.outputs.head-sha }}
113117
114-
- name: Format changed markdown files
115-
if: steps.changed-files.outputs.any_changed == 'true'
118+
- name: Check if any markdown files changed
119+
id: check-files
120+
run: |
121+
if [ -z "${{ steps.changed-files.outputs.all_changed_files }}" ]; then
122+
echo "no-files=true" >> $GITHUB_OUTPUT
123+
echo "No markdown files were changed in this PR"
124+
else
125+
echo "no-files=false" >> $GITHUB_OUTPUT
126+
echo "Changed markdown files:"
127+
echo "${{ steps.changed-files.outputs.all_changed_files }}"
128+
fi
129+
130+
- name: Format MyST markdown files
131+
if: steps.check-files.outputs.no-files == 'false'
132+
id: format
116133
uses: QuantEcon/meta/.github/actions/code-style-checker@main
117134
with:
118135
files: ${{ steps.changed-files.outputs.all_changed_files }}
136+
check-myst-code-cells: 'true'
137+
check-markdown-blocks: 'true'
138+
python-languages: 'python,python3,ipython,ipython3'
139+
black-args: '--line-length=88'
140+
commit-files: 'true'
141+
git-user-name: 'GitHub Action'
142+
git-user-email: '[email protected]'
119143
120144
- name: Push changes
121-
if: steps.changed-files.outputs.any_changed == 'true'
122-
run: git push
145+
if: steps.check-files.outputs.no-files == 'false' && steps.format.outputs.changes-made == 'true'
146+
run: |
147+
git push
148+
echo "Successfully pushed formatting changes"
149+
150+
- name: Post comment with results
151+
uses: actions/github-script@v7
152+
with:
153+
script: |
154+
const noFiles = '${{ steps.check-files.outputs.no-files }}';
155+
const changesMade = '${{ steps.format.outputs.changes-made }}';
156+
const filesProcessed = '${{ steps.format.outputs.files-processed }}';
157+
const filesChanged = '${{ steps.format.outputs.files-changed }}';
158+
const blocksFormatted = '${{ steps.format.outputs.total-blocks-formatted }}';
159+
160+
let body;
161+
162+
if (noFiles === 'true') {
163+
body = [
164+
'## 🔍 Code Style Check Results',
165+
'',
166+
'✅ **No markdown files were changed in this PR.**',
167+
'',
168+
'The code style checker found no markdown files to process.',
169+
'',
170+
'---',
171+
'',
172+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
173+
].join('\n');
174+
} else if (changesMade === 'true') {
175+
body = [
176+
'## ✅ Code Style Formatting Applied',
177+
'',
178+
`🎉 **Successfully applied black formatting to ${blocksFormatted} code block(s) across ${filesChanged} file(s).**`,
179+
'',
180+
'**Summary:**',
181+
`- **Files processed:** ${filesProcessed}`,
182+
`- **Files modified:** ${filesChanged}`,
183+
`- **Code blocks formatted:** ${blocksFormatted}`,
184+
'',
185+
'**Changes committed:**',
186+
'- Each modified file has been committed separately with a descriptive commit message',
187+
'- The formatting follows PEP8 standards using black',
188+
'',
189+
'**Languages processed:**',
190+
'- \`python\`, \`python3\`, \`ipython\`, \`ipython3\` code blocks',
191+
'- Both MyST \`{code-cell}\` directives and standard markdown fenced code blocks',
192+
'',
193+
'---',
194+
'',
195+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
196+
].join('\n');
197+
} else {
198+
body = [
199+
'## ✅ Code Style Check Completed',
200+
'',
201+
`📝 **Processed ${filesProcessed} markdown file(s) - no formatting changes needed.**`,
202+
'',
203+
'All Python code blocks in the changed markdown files are already properly formatted according to PEP8 standards.',
204+
'',
205+
'**Summary:**',
206+
`- **Files processed:** ${filesProcessed}`,
207+
'- **Files modified:** 0',
208+
'- **Code blocks formatted:** 0',
209+
'',
210+
'**Languages checked:**',
211+
'- \`python\`, \`python3\`, \`ipython\`, \`ipython3\` code blocks',
212+
'- Both MyST \`{code-cell}\` directives and standard markdown fenced code blocks',
213+
'',
214+
'---',
215+
'',
216+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
217+
].join('\n');
218+
}
219+
220+
await github.rest.issues.createComment({
221+
owner: context.repo.owner,
222+
repo: context.repo.repo,
223+
issue_number: context.issue.number,
224+
body: body
225+
});
123226
```
124227

228+
After adding this workflow to your repository, simply comment `@quantecon-code-style` on any PR to trigger automatic formatting of Python code in changed markdown files.
229+
125230
### Custom Comment Triggers
126231

127232
You can customize the trigger phrase:
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Template workflow for repositories using the Code Style Checker Action
2+
# Copy this file to your repository as .github/workflows/code-style-formatter.yml
3+
# and it will trigger when someone comments '@quantecon-code-style' on a PR
4+
5+
name: Code Style Formatter
6+
on:
7+
issue_comment:
8+
types: [created]
9+
10+
jobs:
11+
format-code:
12+
if: github.event.issue.pull_request && contains(github.event.comment.body, '@quantecon-code-style')
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Get PR information
17+
id: pr
18+
uses: actions/github-script@v7
19+
with:
20+
script: |
21+
const { data: pullRequest } = await github.rest.pulls.get({
22+
owner: context.repo.owner,
23+
repo: context.repo.repo,
24+
pull_number: context.issue.number,
25+
});
26+
27+
core.setOutput('head-sha', pullRequest.head.sha);
28+
core.setOutput('head-ref', pullRequest.head.ref);
29+
core.setOutput('base-sha', pullRequest.base.sha);
30+
31+
return pullRequest;
32+
33+
- name: Checkout PR branch
34+
uses: actions/checkout@v4
35+
with:
36+
token: ${{ secrets.GITHUB_TOKEN }}
37+
ref: ${{ steps.pr.outputs.head-ref }}
38+
fetch-depth: 0
39+
40+
- name: Get changed files
41+
id: changed-files
42+
uses: tj-actions/changed-files@v40
43+
with:
44+
files: '**/*.md'
45+
base_sha: ${{ steps.pr.outputs.base-sha }}
46+
sha: ${{ steps.pr.outputs.head-sha }}
47+
48+
- name: Check if any markdown files changed
49+
id: check-files
50+
run: |
51+
if [ -z "${{ steps.changed-files.outputs.all_changed_files }}" ]; then
52+
echo "no-files=true" >> $GITHUB_OUTPUT
53+
echo "No markdown files were changed in this PR"
54+
else
55+
echo "no-files=false" >> $GITHUB_OUTPUT
56+
echo "Changed markdown files:"
57+
echo "${{ steps.changed-files.outputs.all_changed_files }}"
58+
fi
59+
60+
- name: Format MyST markdown files
61+
if: steps.check-files.outputs.no-files == 'false'
62+
id: format
63+
uses: QuantEcon/meta/.github/actions/code-style-checker@main
64+
with:
65+
files: ${{ steps.changed-files.outputs.all_changed_files }}
66+
check-myst-code-cells: 'true'
67+
check-markdown-blocks: 'true'
68+
python-languages: 'python,python3,ipython,ipython3'
69+
black-args: '--line-length=88'
70+
commit-files: 'true'
71+
git-user-name: 'GitHub Action'
72+
git-user-email: '[email protected]'
73+
74+
- name: Push changes
75+
if: steps.check-files.outputs.no-files == 'false' && steps.format.outputs.changes-made == 'true'
76+
run: |
77+
git push
78+
echo "Successfully pushed formatting changes"
79+
80+
- name: Post comment with results
81+
uses: actions/github-script@v7
82+
with:
83+
script: |
84+
const noFiles = '${{ steps.check-files.outputs.no-files }}';
85+
const changesMade = '${{ steps.format.outputs.changes-made }}';
86+
const filesProcessed = '${{ steps.format.outputs.files-processed }}';
87+
const filesChanged = '${{ steps.format.outputs.files-changed }}';
88+
const blocksFormatted = '${{ steps.format.outputs.total-blocks-formatted }}';
89+
90+
let body;
91+
92+
if (noFiles === 'true') {
93+
body = [
94+
'## 🔍 Code Style Check Results',
95+
'',
96+
'✅ **No markdown files were changed in this PR.**',
97+
'',
98+
'The code style checker found no markdown files to process.',
99+
'',
100+
'---',
101+
'',
102+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
103+
].join('\n');
104+
} else if (changesMade === 'true') {
105+
body = [
106+
'## ✅ Code Style Formatting Applied',
107+
'',
108+
`🎉 **Successfully applied black formatting to ${blocksFormatted} code block(s) across ${filesChanged} file(s).**`,
109+
'',
110+
'**Summary:**',
111+
`- **Files processed:** ${filesProcessed}`,
112+
`- **Files modified:** ${filesChanged}`,
113+
`- **Code blocks formatted:** ${blocksFormatted}`,
114+
'',
115+
'**Changes committed:**',
116+
'- Each modified file has been committed separately with a descriptive commit message',
117+
'- The formatting follows PEP8 standards using black',
118+
'',
119+
'**Languages processed:**',
120+
'- \`python\`, \`python3\`, \`ipython\`, \`ipython3\` code blocks',
121+
'- Both MyST \`{code-cell}\` directives and standard markdown fenced code blocks',
122+
'',
123+
'---',
124+
'',
125+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
126+
].join('\n');
127+
} else {
128+
body = [
129+
'## ✅ Code Style Check Completed',
130+
'',
131+
`📝 **Processed ${filesProcessed} markdown file(s) - no formatting changes needed.**`,
132+
'',
133+
'All Python code blocks in the changed markdown files are already properly formatted according to PEP8 standards.',
134+
'',
135+
'**Summary:**',
136+
`- **Files processed:** ${filesProcessed}`,
137+
'- **Files modified:** 0',
138+
'- **Code blocks formatted:** 0',
139+
'',
140+
'**Languages checked:**',
141+
'- \`python\`, \`python3\`, \`ipython\`, \`ipython3\` code blocks',
142+
'- Both MyST \`{code-cell}\` directives and standard markdown fenced code blocks',
143+
'',
144+
'---',
145+
'',
146+
'🤖 *This comment was automatically generated by the [Code Style Formatter](https://github.com/QuantEcon/meta/.github/actions/code-style-checker).*'
147+
].join('\n');
148+
}
149+
150+
await github.rest.issues.createComment({
151+
owner: context.repo.owner,
152+
repo: context.repo.repo,
153+
issue_number: context.issue.number,
154+
body: body
155+
});

0 commit comments

Comments
 (0)