-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathadd-notebook-examples-to-docs.yml
More file actions
132 lines (113 loc) · 4.56 KB
/
add-notebook-examples-to-docs.yml
File metadata and controls
132 lines (113 loc) · 4.56 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Add Notebook Examples to Docs
on:
push:
branches:
- main
paths:
- 'examples/**'
- 'docs/v2/examples/**'
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
add-notebook-examples-to-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: pip install jupyter nbconvert
- name: Detect and process notebooks
id: process
run: |
# Determine comparison range
case "${{ github.event_name }}" in
"push")
BASE_SHA="${{ github.event.before }}"
[[ "$BASE_SHA" == "0000000000000000000000000000000000000000" ]] && BASE_SHA="$(git hash-object -t tree /dev/null)"
HEAD_SHA="${{ github.event.after }}"
;;
"pull_request")
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
;;
"workflow_dispatch")
git fetch origin "${{ github.event.repository.default_branch }}" --depth=50
BASE_SHA="origin/${{ github.event.repository.default_branch }}"
HEAD_SHA="HEAD"
;;
*)
exit 1
;;
esac
# Get changed files and filter in one pass
CHANGED_FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
# Use arrays for better performance
declare -A notebooks_set
# Process notebooks directly from changed files
while IFS= read -r file; do
if [[ "$file" =~ ^examples/.*\.ipynb$ ]]; then
notebooks_set["$file"]=1
elif [[ "$file" =~ ^docs/v2/examples/.*\.mdx$ ]] && [[ -f "$file" ]]; then
# Extract source notebook from MDX file
source_notebook=$(grep -oP '(?<=SOURCE_FILE: )[^ ]+' "$file" 2>/dev/null || true)
if [[ -n "$source_notebook" && -f "$source_notebook" ]]; then
notebooks_set["$source_notebook"]=1
fi
fi
done <<< "$CHANGED_FILES"
# Exit if no notebooks found
[[ ${#notebooks_set[@]} -eq 0 ]] && exit 0
# Process each notebook and track results
failed_count=0
processed_notebooks=""
for notebook in "${!notebooks_set[@]}"; do
if python examples/generate_documentation.py "$notebook"; then
processed_notebooks="$processed_notebooks$notebook"$'\n'
else
((failed_count++))
fi
done
[[ $failed_count -gt 0 ]] && exit 1
# Get modified files for PR description
modified_files=$(git status --porcelain | grep -E '^\s*[AM]' | awk '{print $2}' | sort)
# Save outputs for PR
echo "processed_notebooks<<EOF" >> $GITHUB_OUTPUT
echo "$processed_notebooks" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "modified_files<<EOF" >> $GITHUB_OUTPUT
echo "$modified_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
commit-message: "docs: update examples from notebooks"
title: "docs: update examples from notebooks"
body: |
Updates documentation examples from their source notebooks.
**Triggered by:** @${{ github.actor }}
**Event:** ${{ github.event_name }}
${{ steps.process.outputs.processed_notebooks && format('
## Processed Notebooks
```
{0}
```', steps.process.outputs.processed_notebooks) || '' }}
${{ steps.process.outputs.modified_files && format('
## Modified Files
```
{0}
```', steps.process.outputs.modified_files) || '' }}
branch: update-examples-docs
delete-branch: true
assignees: the-praxs,bboynton97,areibman
reviewers: the-praxs,bboynton97,areibman