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 : ' action@github.com'
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