-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (73 loc) · 2.5 KB
/
validate_and_fix_notebook.yml
File metadata and controls
87 lines (73 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: Validate and Fix Notebook
on:
pull_request:
branches:
- main
concurrency:
group: pr-branch-maintenance-${{ github.event.pull_request.head.ref || github.ref_name }}
cancel-in-progress: false
permissions:
contents: write
pull-requests: write
jobs:
validate-and-fix-notebook:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install Jupyter and nbformat
run: pip install jupyter nbformat
- name: Validate and fix notebooks
run: |
python - <<'PY'
import glob
import sys
import nbformat
files = glob.glob('**/*.ipynb', recursive=True)
if not files:
print('No notebooks found.')
sys.exit(0)
for file_path in files:
with open(file_path, 'r', encoding='utf-8') as handle:
notebook = nbformat.read(handle, as_version=4)
nbformat.validate(notebook)
widgets = notebook.metadata.setdefault('widgets', {})
widget_state = widgets.setdefault(
'application/vnd.jupyter.widget-state+json',
{'version': '1.0', 'state': {}},
)
widget_state.setdefault('state', {})
with open(file_path, 'w', encoding='utf-8') as handle:
nbformat.write(notebook, handle)
PY
- name: Configure Git
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Commit changes
env:
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git add -A
if git diff --staged --quiet; then
echo "No changes to commit"
exit 0
fi
git commit -m "Fix notebook format issues"
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}
for attempt in 1 2 3; do
git fetch origin "$PR_BRANCH"
git rebase "origin/$PR_BRANCH"
if git push origin HEAD:"$PR_BRANCH"; then
exit 0
fi
done
echo "Failed to push branch updates after 3 attempts."
exit 1