Skip to content

Commit 800f969

Browse files
committed
Add workflow to auto-format notebooks in a pull request
1 parent 1fae782 commit 800f969

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

.github/workflows/bot-pr-fix.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Automatically add commits to fix pull requests. This workflow must initiate
2+
# from an authenticated bot repo collaborator. Check for opt-out label.
3+
# Webhook events: Pull requests
4+
name: Auto-fix pull request
5+
on:
6+
repository_dispatch:
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
nbfmt:
11+
# Check for opt-out label.
12+
if: >-
13+
${{ github.actor == 'tfdocsbot' &&
14+
!contains(github.event.client_payload.pull_request.labels.*.name, 'nbfmt-disable') }}
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Set up Python
18+
uses: actions/setup-python@v2
19+
- name: Install tensorflow-docs
20+
run: python3 -m pip install -U git+https://github.com/tensorflow/docs
21+
- name: Fetch pull request branch
22+
uses: actions/checkout@v2
23+
with:
24+
# Head repo is the user's fork. Ref is the branch name.
25+
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
26+
ref: ${{ github.event.client_payload.pull_request.head.ref }}
27+
- name: Fetch base master branch
28+
run: git fetch -u "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" master:master
29+
- name: Format notebooks
30+
run: |
31+
# Only want notebooks modified in this pull request.
32+
readarray -t changed_files < <(git diff --name-only master | grep '\.ipynb$' || true)
33+
if [[ ${#changed_files[@]} == 0 ]]; then
34+
echo "No notebooks modified in this pull request."
35+
exit 0
36+
fi
37+
python3 -m tensorflow_docs.tools.nbfmt "${changed_files[@]}"
38+
39+
if [[ -z $(git ls-files --modified) ]]; then
40+
echo "Notebooks already formatted."
41+
exit 0
42+
fi
43+
# Set author and commit.
44+
git config --global user.name "$GITHUB_ACTOR"
45+
git config --global user.email "[email protected]"
46+
git commit -am "nbfmt"
47+
# Push to the pull request branch submitted by head.
48+
git push

0 commit comments

Comments
 (0)