Skip to content

Commit 2fc717e

Browse files
authored
delete every sunday at midnight (#1088)
1 parent 9d08d5e commit 2fc717e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Doc Preview Cleanup
2+
3+
permissions:
4+
contents: write
5+
6+
on:
7+
workflow_dispatch:
8+
schedule:
9+
- cron: "0 0 * * 0"
10+
11+
jobs:
12+
doc-preview-cleanup:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout gh-pages branch
16+
uses: actions/checkout@v4
17+
with:
18+
ref: gh-pages
19+
- uses: julia-actions/setup-julia@v2
20+
with:
21+
version: '1'
22+
- name: Check for stale PR previews
23+
shell: julia {0}
24+
run: |
25+
using Pkg
26+
pkg"activate --temp"
27+
pkg"add HTTP JSON3"
28+
29+
using HTTP
30+
using JSON3
31+
using Dates
32+
33+
repo = ENV["GITHUB_REPOSITORY"]
34+
retention_days = 14
35+
36+
pr_previews = map(filter(startswith("PR"), readdir("previews"))) do dir
37+
parse(Int, match(r"PR(\d*)", dir)[1])
38+
end
39+
40+
function all_prs()
41+
query_prs(page) = JSON3.read(HTTP.get("https://api.github.com/repos/$repo/pulls?per_page=100;page=$(page)").body)
42+
prs = []
43+
page = 1
44+
while true
45+
page_prs = query_prs(page)
46+
isempty(page_prs) && break
47+
append!(prs, page_prs)
48+
page += 1
49+
end
50+
return prs
51+
end
52+
prs = all_prs()
53+
open_within_threshold = map(x -> x.number, filter(prs) do pr
54+
time = DateTime(pr.updated_at[1:19], ISODateTimeFormat)
55+
return pr.state == "open" && Dates.days(now() - time) <= retention_days
56+
end)
57+
58+
stale_previews = setdiff(pr_previews, open_within_threshold)
59+
@info "Found $(length(stale_previews)) stale previews"
60+
61+
if isempty(stale_previews)
62+
@info "No stale previews"
63+
exit(1)
64+
end
65+
66+
for pr in stale_previews
67+
path = joinpath("previews", "PR$pr")
68+
@info "Removing $path"
69+
run(`git rm -rf $path`)
70+
end
71+
- name: Push changes
72+
run: |
73+
git config user.name "Documenter.jl"
74+
git config user.email "documenter@juliadocs.github.io"
75+
git commit -m "delete preview"
76+
git branch gh-pages-new $(echo "delete history" | git commit-tree HEAD^{tree})
77+
git push --force origin gh-pages-new:gh-pages

0 commit comments

Comments
 (0)