@@ -177,7 +177,7 @@ jobs:
177177
178178 pr_cleanup :
179179 name : Clean up documentation previews
180- if : github.event_name == 'pull_request_target'
180+ if : github.event_name == 'pull_request_target' || github.event_name == 'schedule'
181181 runs-on : ubuntu-latest
182182
183183 permissions :
@@ -189,11 +189,59 @@ jobs:
189189 with :
190190 ref : ${{ vars.preview_branch }}
191191
192- - name : Delete preview folder
192+ - name : Delete preview folder for closed PR
193+ if : github.event_name == 'pull_request_target'
193194 run : |
194195 git config --global user.name "cuda-quantum-bot"
195196 git config --global user.email "[email protected] " 196197 git rm -r "pr-${{ github.event.pull_request.number }}" --ignore-unmatch
197198 git commit --allow-empty -m "Cleaning up docs preview for PR #${{ github.event.pull_request.number }}."
198199 git config pull.rebase true
199200 git pull --no-edit && git push
201+
202+ - name : Delete preview folder for stale PRs (open > 5 days)
203+ if : github.event_name == 'schedule'
204+ env :
205+ GH_TOKEN : ${{ secrets.REPO_BOT_ACCESS_TOKEN || github.token }}
206+ run : |
207+ set -euo pipefail
208+
209+ git config --global user.name "cuda-quantum-bot"
210+ git config --global user.email "[email protected] " 211+ git config pull.rebase true
212+
213+ # Keep preview branch up-to-date
214+ git pull --no-edit
215+
216+ # 5 days ago timestamp
217+ cutoff=$(date -u -d '5 days ago' +%s)
218+
219+ # Fetch open PRs created before the cutoff date
220+ stale_prs=$(gh pr list \
221+ --repo "${{ github.repository }}" \
222+ --state open \
223+ -- limit 200 \
224+ --json number,createdAt \
225+ --jq ".[] | select((.createdAt | fromdate) < $cutoff) | .number")
226+
227+ if [ -z "stale_prs" ]; then
228+ echo "No stale PR previews to delete."
229+ exit 0
230+ fi
231+
232+ echo "Stale PRs (open > 5 days): $stale_prs"
233+
234+ for pr in $stale_prs; do
235+ folder="pr-$pr"
236+ echo "Deleting preview folder $folder (PR #$pr)."
237+ git rm -r "$folder" --ignore-unmatch || true
238+ done
239+
240+ # If nothing matches, skip commit/push
241+ if git diff --quiet; then
242+ echo "No preview folders were removed (nothing to commit)."
243+ exit 0
244+ fi
245+
246+ git commit -m "Cleaning up stale docs preview for PRs open > 5 days."
247+ git push
0 commit comments