Auto format notebooks #484
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Notebooks | |
on: | |
# Relevant PRs | |
pull_request: | |
paths: | |
- "**.ipynb" | |
# Allow manual runs | |
workflow_dispatch: | |
jobs: | |
# Format all notebooks. | |
nbfmt: | |
name: Notebook format | |
runs-on: ubuntu-latest | |
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.sha || 'main' }} | |
fetch-depth: 0 # Fetch all history to avoid issues with rebasing | |
- 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 | |
run: git fetch -u origin main:main | |
- name: Get changed notebooks | |
id: changed-notebooks | |
run: | | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
# Only check notebooks modified in this pull request | |
git diff --name-only origin/main | grep '\.ipynb$' > changed_notebooks.txt | |
else | |
# Manual run, check everything | |
find . -name '*.ipynb' > changed_notebooks.txt | |
fi | |
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 "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" | |
# Check if there are changes | |
if ! git diff --quiet; then | |
# Commit on the current branch | |
git add . | |
git commit -m "Format notebooks" | |
# Push directly to the PR branch | |
git push origin ${{ github.event.pull_request.head.ref }} | |
else | |
echo "No changes to commit." | |
fi | |
- name: Check notebook formatting | |
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) | |
else | |
# Manual run, check everything | |
readarray -t changed_notebooks < <(find -name '*.ipynb') | |
fi | |
if [[ ${#changed_notebooks[@]} == 0 ]]; then | |
echo "No notebooks modified in this pull request." | |
exit 0 | |
else | |
echo "Check formatting with nbfmt:" | |
python3 -m tensorflow_docs.tools.nbfmt --test "${changed_notebooks[@]}" | |
fi | |
nblint: | |
name: Notebook lint | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- 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 | |
run: git fetch -u origin main:main | |
# Lint for all notebooks | |
- name: Lint 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) | |
else | |
# Manual run, check everything | |
readarray -t changed_notebooks < <(find . -name '*.ipynb') | |
fi | |
if [[ ${#changed_notebooks[@]} == 0 ]]; then | |
echo "No website notebooks modified in this pull request." | |
exit 0 | |
else | |
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 \ | |
--styles=google,tensorflow \ | |
--arg=repo:google-gemini/cookbook \ | |
--arg=branch:main \ | |
--exclude_lint=tensorflow::button_download \ | |
--exclude_lint=tensorflow::button_website \ | |
--arg=base_url:https://ai.google.dev/ \ | |
--exclude_lint=tensorflow::button_github \ | |
"${changed_notebooks[@]}" | |
fi |