66 - main
77 paths :
88 - ' examples/**'
9- - ' docs/v1/examples/**'
10- - ' .github/workflows/add-notebook-examples-to-docs.yml'
11-
9+ - ' docs/v2/examples/**'
1210 workflow_dispatch :
1311
1412permissions :
@@ -21,49 +19,114 @@ jobs:
2119 steps :
2220 - name : Checkout repository
2321 uses : actions/checkout@v3
22+ with :
23+ fetch-depth : 0
2424
2525 - name : Set up Python
2626 uses : actions/setup-python@v4
2727 with :
2828 python-version : ' 3.x'
2929
3030 - name : Install dependencies
31- run : |
32- pip install jupyter nbconvert
31+ run : pip install jupyter nbconvert
3332
34- - name : Convert notebooks to markdown and add to docs
33+ - name : Detect and process notebooks
34+ id : process
3535 run : |
36- set -x # Enable debug mode
37- for file in docs/v1/examples/*.mdx; do
38- echo "Processing file: $file"
39- source_file=$(grep -oP '(?<=\{/\* SOURCE_FILE: ).*(?= \*/\})' "$file" || true)
40- if [[ -z "$source_file" ]]; then
41- continue
36+ # Determine comparison range
37+ case "${{ github.event_name }}" in
38+ "push")
39+ BASE_SHA="${{ github.event.before }}"
40+ [[ "$BASE_SHA" == "0000000000000000000000000000000000000000" ]] && BASE_SHA="$(git hash-object -t tree /dev/null)"
41+ HEAD_SHA="${{ github.event.after }}"
42+ ;;
43+ "pull_request")
44+ BASE_SHA="${{ github.event.pull_request.base.sha }}"
45+ HEAD_SHA="${{ github.event.pull_request.head.sha }}"
46+ ;;
47+ "workflow_dispatch")
48+ git fetch origin "${{ github.event.repository.default_branch }}" --depth=50
49+ BASE_SHA="origin/${{ github.event.repository.default_branch }}"
50+ HEAD_SHA="HEAD"
51+ ;;
52+ *)
53+ exit 1
54+ ;;
55+ esac
56+
57+ # Get changed files and filter in one pass
58+ CHANGED_FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
59+
60+ # Use arrays for better performance
61+ declare -A notebooks_set
62+
63+ # Process notebooks directly from changed files
64+ while IFS= read -r file; do
65+ if [[ "$file" =~ ^examples/.*\.ipynb$ ]]; then
66+ notebooks_set["$file"]=1
67+ elif [[ "$file" =~ ^docs/v2/examples/.*\.mdx$ ]] && [[ -f "$file" ]]; then
68+ # Extract source notebook from MDX file
69+ source_notebook=$(grep -oP '(?<=SOURCE_FILE: )[^ ]+' "$file" 2>/dev/null || true)
70+ if [[ -n "$source_notebook" && -f "$source_notebook" ]]; then
71+ notebooks_set["$source_notebook"]=1
72+ fi
4273 fi
43- echo "Source file: $source_file "
44- if [[ -f "$source_file" ]]; then
45- echo "Converting notebook to markdown"
46- jupyter nbconvert --to markdown "$source_file" || { echo "Error: Failed to convert $source_file" >&2; continue; }
47- markdown_file="${source_file%.ipynb}.md"
48- echo "Removing existing content after {/* SOURCE_FILE: ... */}"
49- sed -i '\#{/\* SOURCE_FILE:#,$d' "$file"
50- echo "Appending markdown to $file "
51- echo -e "{/* SOURCE_FILE: $source_file */}\n" >> "$file"
52- cat "$markdown_file" >> "$file" || { echo "Error: Failed to append markdown to $file" >&2; continue; }
53- rm "$markdown_file" || { echo "Error: Failed to remove $markdown_file" >&2; continue; }
74+ done <<< "$CHANGED_FILES "
75+
76+ # Exit if no notebooks found
77+ [[ ${#notebooks_set[@]} -eq 0 ]] && exit 0
78+
79+ # Process each notebook and track results
80+ failed_count=0
81+ processed_notebooks=" "
82+ for notebook in "${!notebooks_set[@]}"; do
83+ if python examples/generate_documentation.py "$notebook"; then
84+ processed_notebooks="$processed_notebooks$notebook"$'\n'
5485 else
55- echo "Error: Source file not found: $source_file" >&2
86+ ((failed_count++))
5687 fi
5788 done
89+
90+ [[ $failed_count -gt 0 ]] && exit 1
91+
92+ # Get modified files for PR description
93+ modified_files=$(git status --porcelain | grep -E '^\s*[AM]' | awk '{print $2}' | sort)
94+
95+ # Save outputs for PR
96+ echo "processed_notebooks<<EOF" >> $GITHUB_OUTPUT
97+ echo "$processed_notebooks" >> $GITHUB_OUTPUT
98+ echo "EOF" >> $GITHUB_OUTPUT
99+
100+ echo "modified_files<<EOF" >> $GITHUB_OUTPUT
101+ echo "$modified_files" >> $GITHUB_OUTPUT
102+ echo "EOF" >> $GITHUB_OUTPUT
58103
59104 - name : Create Pull Request
60105 uses : peter-evans/create-pull-request@v7
61106 with :
62- committer : Howard Gil <howardbgil@gmail.com>
63- commit-message : GitHub Action - Update examples in docs from notebooks
64- title : GitHub Action - Update examples in docs from notebooks
65- body : Changes detected in examples/** or docs/v1/examples/** triggered an update of the docs/v1/examples/**.mdx files to incorporate markdown from the corresponding notebook in examples/**.
66- branch : update-examples-in-docs-from-notebooks
107+ token : ${{ secrets.GITHUB_TOKEN }}
108+ committer : github-actions[bot] <github-actions[bot]@users.noreply.github.com>
109+ author : github-actions[bot] <github-actions[bot]@users.noreply.github.com>
110+ commit-message : " docs: update examples from notebooks"
111+ title : " docs: update examples from notebooks"
112+ body : |
113+ Updates documentation examples from their source notebooks.
114+
115+ **Triggered by:** @${{ github.actor }}
116+ **Event:** ${{ github.event_name }}
117+ ${{ steps.process.outputs.processed_notebooks && format('
118+
119+ ## Processed Notebooks
120+ ```
121+ {0}
122+ ```', steps.process.outputs.processed_notebooks) || '' }}
123+ ${{ steps.process.outputs.modified_files && format('
124+
125+ ## Modified Files
126+ ```
127+ {0}
128+ ```', steps.process.outputs.modified_files) || '' }}
129+ branch : update-examples-docs
67130 delete-branch : true
68- assignees : HowieG,siyangqiu ,bboynton97,areibman
69- reviewers : HowieG,siyangqiu ,bboynton97,areibman
131+ assignees : the-praxs ,bboynton97,areibman
132+ reviewers : the-praxs ,bboynton97,areibman
0 commit comments