From 139853bdb682912d0cdaf9e6bc6fc9dc3fc69412 Mon Sep 17 00:00:00 2001 From: Anand Roy <86306690+andycandy@users.noreply.github.com> Date: Tue, 17 Jun 2025 13:15:34 +0530 Subject: [PATCH] auto format script (tests) (#805) --- .github/workflows/notebooks.yaml | 103 +++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/.github/workflows/notebooks.yaml b/.github/workflows/notebooks.yaml index 49da8b808..eb9239567 100644 --- a/.github/workflows/notebooks.yaml +++ b/.github/workflows/notebooks.yaml @@ -1,46 +1,87 @@ # Notebook-related checks - name: Notebooks on: - # Relevant PRs pull_request: paths: - - "**.ipynb" - # Allow manual runs + - "**.ipynb" workflow_dispatch: jobs: - # Format all notebooks. nbfmt: - name: Notebook format + name: Notebook format and testing runs-on: ubuntu-latest + + # Only push when the PR branch lives in the same repository + if: github.event.pull_request.head.repo.full_name == github.repository + steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 # Get *full* history - - 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: Check notebook formatting - run: | - if [ "${{ github.event_name }}" == "pull_request" ]; then - # Only check notebooks modified in this pull request - base_commit=$(git merge-base HEAD ${{ github.event.pull_request.base.sha }}) - readarray -t changed_notebooks < <(git diff --name-only "${base_commit}...HEAD" "*.ipynb") - 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 + # Checkout the PR branch with a PAT so the follow-up commit re-triggers all workflows + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + token: ${{ secrets.BOT_PAT }} # PAT for the bot account + fetch-depth: 0 # merge-base needs full history + + # Python and tooling + - uses: actions/setup-python@v4 + + - name: Install tensorflow-docs (provides nbfmt) + run: python -m pip install -qU git+https://github.com/tensorflow/docs + + # Identify as the bot for commits + - name: Configure Git user + run: | + git config --local user.email "kasyapanand7+1@github.com" + git config --local user.name "andyservice" + + # Main loop: test each notebook, format if needed, single commit at the end + - name: Check notebooks one-by-one, format if required + run: | + set -e + + # 1. Collect notebooks touched in this PR + if [ "${{ github.event_name }}" = "pull_request" ]; then + base=$(git merge-base HEAD origin/${{ github.event.pull_request.base.ref }}) + readarray -t notebooks < <(git diff --name-only "${base}...HEAD" "*.ipynb") + else + readarray -t notebooks < <(find . -name '*.ipynb') + fi + + if [[ ${#notebooks[@]} -eq 0 ]]; then + echo "No notebooks modified in this pull request." + exit 0 + fi + + echo "Notebook list: ${notebooks[*]}" + + # 2. Loop: run --test, format when necessary + reformatted=false + for nb in "${notebooks[@]}"; do + echo "::group::${nb}" + set +e + python -m tensorflow_docs.tools.nbfmt --test "${nb}" + test_rc=$? + set -e + + if [[ $test_rc -eq 0 ]]; then + echo "✔ ${nb} already formatted" + else + echo "✘ ${nb} needs formatting → applying formatter" + python -m tensorflow_docs.tools.nbfmt "${nb}" + reformatted=true + fi + echo "::endgroup::" + done + + # 3. One commit if anything changed + if [[ "$reformatted" == "true" ]]; then + git add "${notebooks[@]}" + git commit -m "style: auto-format notebooks with nbfmt" + git push + else + echo "All notebooks already in canonical format – nothing to commit." + fi nblint: name: Notebook lint