Skip to content

Commit d313015

Browse files
seismanweiji14
andauthored
Add slash command "/format" to automatically format PRs (#646)
This PR adds a special github action to enable slash commands in comments. With the feature enabled, anyone, who have written permission to a pull request, can write /format at the first line of a comment to trigger the "format" action, which can format the codes automatically. Co-authored-by: Wei Ji <[email protected]>
1 parent 75ec6d0 commit d313015

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ Fixes #
1515
- [ ] Add new public functions/methods/classes to `doc/api/index.rst`.
1616
- [ ] Write detailed docstrings for all functions/methods.
1717
- [ ] If adding new functionality, add an example to docstrings or tutorials.
18+
19+
**Notes**
20+
21+
- You can write `/format` in the first line of a comment to lint the code automatically

.github/workflows/format-command.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: format-command
2+
on:
3+
repository_dispatch:
4+
types: [format-command]
5+
jobs:
6+
format:
7+
runs-on: ubuntu-latest
8+
steps:
9+
# Generate token from GenericMappingTools bot
10+
- uses: tibdex/github-app-token@v1
11+
id: generate-token
12+
with:
13+
app_id: ${{ secrets.APP_ID }}
14+
private_key: ${{ secrets.APP_PRIVATE_KEY }}
15+
16+
# Checkout the pull request branch
17+
- uses: actions/checkout@v2
18+
with:
19+
token: ${{ steps.generate-token.outputs.token }}
20+
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
21+
ref: ${{ github.event.client_payload.pull_request.head.ref }}
22+
23+
# Setup Python environment
24+
- uses: actions/setup-python@v1
25+
26+
# Install formatting tools
27+
- name: Install formatting tools
28+
run: pip install black blackdoc flake8
29+
30+
# Run "make format" and commit the change to the PR branch
31+
- name: Commit to the PR branch if any changes
32+
run: |
33+
make format
34+
if [[ $(git ls-files -m) ]]; then
35+
git config --global user.name 'actions-bot'
36+
git config --global user.email '[email protected]'
37+
git commit -am "[format-command] fixes"
38+
git push
39+
fi
40+
41+
- name: Add reaction
42+
uses: peter-evans/create-or-update-comment@v1
43+
with:
44+
token: ${{ steps.generate-token.outputs.token }}
45+
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
46+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
47+
reaction-type: hooray
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Slash Command Dispatch
2+
on:
3+
issue_comment:
4+
types: [created]
5+
# Add "edited" type for test purposes. Where possible, avoid using to prevent processing unnecessary events.
6+
# types: [created, edited]
7+
jobs:
8+
slashCommandDispatch:
9+
runs-on: ubuntu-latest
10+
steps:
11+
# Generate token from GenericMappingTools bot
12+
- uses: tibdex/github-app-token@v1
13+
id: generate-token
14+
with:
15+
app_id: ${{ secrets.APP_ID }}
16+
private_key: ${{ secrets.APP_PRIVATE_KEY }}
17+
18+
- name: Slash Command Dispatch
19+
uses: peter-evans/slash-command-dispatch@v2
20+
with:
21+
token: ${{ steps.generate-token.outputs.token }}
22+
commands: |
23+
format
24+
issue-type: pull-request

CONTRIBUTING.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,10 @@ Before committing, run it to automatically format your code:
255255
make format
256256
```
257257

258-
Don't worry if you forget to do it.
259-
Our continuous integration systems will warn us and you can make a new commit with the
260-
formatted code.
258+
Don't worry if you forget to do it. Our continuous integration systems will
259+
warn us and you can make a new commit with the formatted code.
260+
Even better, you can just write `/format` in the first line of any comment in a
261+
Pull Request to lint the code automatically.
261262

262263
We also use [flake8](http://flake8.pycqa.org/en/latest/) and
263264
[pylint](https://www.pylint.org/) to check the quality of the code and quickly catch

0 commit comments

Comments
 (0)