Skip to content

Commit

Permalink
trying to auto-format notbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Giom-V committed Dec 30, 2024
1 parent 46d681f commit f301f30
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions .github/workflows/notebooks.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Notebook-related checks

name: Notebooks

on:
Expand All @@ -21,49 +19,66 @@ jobs:
- name: Install tensorflow-docs
run: python3 -m pip install -U git+https://github.com/tensorflow/docs
- name: Fetch main branch
if: ${{ github.event_name == 'pull_request' }} # Only need to fetch main on PRs
run: git fetch -u origin main:main
- name: Check notebook formatting
- name: Get changed notebooks
id: changed-notebooks
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
# Only check notebooks modified in this pull request
readarray -t changed_notebooks < <(git diff --name-only main | grep '\.ipynb$' || true)
git diff --name-only origin/main | grep '\.ipynb$' > changed_notebooks.txt
else
# Manual run, check everything
readarray -t changed_notebooks < <(find -name '*.ipynb')
find . -name '*.ipynb' > changed_notebooks.txt
fi
if [[ ${#changed_notebooks[@]} == 0 ]]; then
echo "No notebooks modified in this pull request."
exit 0
echo "changed_notebooks=$(cat changed_notebooks.txt)" >> $GITHUB_OUTPUT
- name: Format notebooks
run: |
if [ -s "${{ steps.changed-notebooks.outputs.changed_notebooks }}" ]; then
echo "Formatting notebooks with nbfmt:"
python3 -m tensorflow_docs.tools.nbfmt ${{ steps.changed-notebooks.outputs.changed_notebooks }}
else
echo "Check formatting with nbfmt:"
python3 -m tensorflow_docs.tools.nbfmt --test "${changed_notebooks[@]}"
echo "No notebooks to format."
fi
- name: Commit and push changes (if any)
if: ${{ success() }}
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git diff --quiet && git diff --staged --quiet || (git commit -a -m "Format notebooks" && git push)
nblint:
name: Notebook lint
runs-on: ubuntu-latest
needs: nbfmt # Ensure nblint runs after nbfmt
steps:
- uses: actions/checkout@v3
with:
# Checkout PR branch for PR events, main branch for manual runs
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || 'main' }}
- uses: actions/setup-python@v4
- name: Install tensorflow-docs
run: python3 -m pip install -U git+https://github.com/tensorflow/docs
- name: Fetch main branch
if: ${{ github.event_name == 'pull_request' }} # Only need to fetch main on PRs
run: git fetch -u origin main:main

# Lint for all notebooks
- name: Lint notebooks
- name: Get changed notebooks
id: changed-notebooks
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
# Only check notebooks modified in this pull request
readarray -t changed_notebooks < <(git diff --name-only main |grep '\.ipynb$' || true)
git diff --name-only origin/main | grep '\.ipynb$' > changed_notebooks.txt
else
# Manual run, check everything
readarray -t changed_notebooks < <(find . -name '*.ipynb')
find . -name '*.ipynb' > changed_notebooks.txt
fi
if [[ ${#changed_notebooks[@]} == 0 ]]; then
echo "No website notebooks modified in this pull request."
exit 0
else
echo "changed_notebooks=$(cat changed_notebooks.txt)" >> $GITHUB_OUTPUT
# Lint for all notebooks
- name: Lint notebooks
run: |
if [ -s "${{ steps.changed-notebooks.outputs.changed_notebooks }}" ]; then
echo "WARNING: If the button_colab check fails for you, make sure you have <table class=\"tfo-notebook-buttons\"...>"
echo "Lint check with nblint:"
python3 -m tensorflow_docs.tools.nblint \
Expand All @@ -74,6 +89,7 @@ jobs:
--exclude_lint=tensorflow::button_website \
--arg=base_url:https://ai.google.dev/ \
--exclude_lint=tensorflow::button_github \
"${changed_notebooks[@]}"
fi
${{ steps.changed-notebooks.outputs.changed_notebooks }}
else
echo "No notebooks to lint."
fi

0 comments on commit f301f30

Please sign in to comment.