-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Code Style Formatter Action for Python code in MyST markdown files with complete workflow setup #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: mmcky <[email protected]>
Co-authored-by: mmcky <[email protected]>
@copilot for the use case when using directly in workflows can
accept patterns such as |
Co-authored-by: mmcky <[email protected]>
... Yes! The action now supports glob patterns like # All markdown files in lecture directory and subdirectories
files: 'lecture/**/*.md'
# Multiple directories
files: 'lecture/**/*.md,examples/**/*.md'
# Mix patterns with explicit files
files: 'lecture/**/*.md,specific-file.md' The implementation maintains full backward compatibility with comma-separated explicit file paths and includes smart filtering to only process Commit: e1c0394 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@copilot I just tested this in QuantEcon/lecture-python.myst#615 and it doesn't appear to run after making a comment QuantEcon/lecture-python.myst#615 (comment) that should activate the style checker workflow. Can you please take a look and let me know what I've done wrong or is this a bug? |
The issue is that the workflow file you added in the test PR is incomplete and has some problems:
The correct workflow should be: name: Code Style Formatter
on:
issue_comment:
types: [created]
jobs:
format-code:
if: github.event.issue.pull_request && contains(github.event.comment.body, '@quantecon-code-style')
runs-on: ubuntu-latest
steps:
- name: Get PR information
id: pr
uses: actions/github-script@v7
with:
script: |
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
core.setOutput('head-sha', pullRequest.head.sha);
core.setOutput('head-ref', pullRequest.head.ref);
core.setOutput('base-sha', pullRequest.base.sha);
- name: Checkout PR branch
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ steps.pr.outputs.head-ref }}
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v40
with:
files: '**/*.md'
base_sha: ${{ steps.pr.outputs.base-sha }}
sha: ${{ steps.pr.outputs.head-sha }}
- name: Format MyST markdown files
if: steps.changed-files.outputs.any_changed == 'true'
uses: QuantEcon/meta/.github/actions/code-style-checker@main
with:
files: ${{ steps.changed-files.outputs.all_changed_files }}
check-myst-code-cells: 'true'
check-markdown-blocks: 'true'
python-languages: 'python,python3,ipython,ipython3'
black-args: '--line-length=88'
commit-files: 'true'
git-user-name: 'GitHub Action'
git-user-email: '[email protected]'
- name: Push changes
if: steps.changed-files.outputs.any_changed == 'true'
run: git push Update your workflow file with this complete version and try commenting again. |
Co-authored-by: mmcky <[email protected]>
Co-authored-by: mmcky <[email protected]>
This PR implements a new GitHub Action that automatically formats Python code in MyST markdown files and standard markdown code blocks using black, addressing the need for consistent code formatting in QuantEcon documentation projects.
Key Features
Smart Markdown Parsing: The action uses a robust line-by-line parser that correctly handles both MyST
{code-cell}
directives and standard markdown fenced code blocks without interference between the two formats.Language Detection: Automatically identifies Python code by language identifiers (
python
,python3
,ipython
,ipython3
) and skips non-Python code blocks (e.g., Julia, JavaScript) with informative logging.Black Integration: Formats Python code using black with configurable arguments, ensuring PEP8 compliance and consistent styling across all code blocks.
Glob Pattern Support: Accepts both explicit file paths and shell glob patterns for flexible file selection. Supports patterns like
lecture/**/*.md
to process all markdown files in a directory tree recursively.PR Comment Trigger: Activates when PR comments contain
@quantecon-code-style
, automatically finding changed markdown files and applying formatting with individual commit messages per file.Complete Workflow Setup: Provides production-ready workflow templates and comprehensive documentation to ensure proper integration in target repositories.
Usage Example
Setup Instructions
To enable PR comment triggers, copy the complete workflow template from
.github/workflows/code-style-formatter-template.yml
to your repository as.github/workflows/code-style-formatter.yml
. The template includes all necessary steps for proper PR integration:File Input Formats
The action supports multiple input formats for maximum flexibility:
'file1.md,file2.md'
'lecture/**/*.md'
(recursive),'lecture/*.md'
(single level)'lecture/**/*.md,specific-file.md'
Implementation Details
The action processes files in two phases:
For each Python code block found, the action:
Testing
Comprehensive test suite includes:
unformatted-code.md
: Mixed MyST and markdown blocks with poor formattingformatted-code.md
: Well-formatted code to ensure no unnecessary changesno-python-code.md
: Non-Python languages to verify selective processingManual testing confirms the action correctly formats 5 code blocks (2 standard markdown + 3 MyST) while properly skipping non-Python content. Additional tests validate glob pattern expansion and file filtering functionality.
Documentation
Fixes #221.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.