-
Notifications
You must be signed in to change notification settings - Fork 162
61 lines (53 loc) · 2.06 KB
/
preview_clear.yml
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
name: preview_clear
on: workflow_dispatch
jobs:
preview_clear:
concurrency:
group: cpprefjp.gh-pages.lock
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: gh-pages
fetch-depth: 0
- name: filter-branch gh-pages
env:
GH_TOKEN: ${{ github.token }}
run: |
set -ex
# Note: 以下の 999 という数は適当な大きな数。もし open PR の数が 999
# を超えるようならば、この数はもっと大きくする必要がある。
CPPREFJP_OPEN_PR_LIST=$(gh pr list --limit=999 --json number -q '.[].number' | tr '\n' ' ')
CPPREFJP_OPEN_PR_LIST=${CPPREFJP_OPEN_PR_LIST% }
export CPPREFJP_OPEN_PR_LIST
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git filter-branch --commit-filter '
commit_message=$(git log -n 1 --pretty=format:%s "$GIT_COMMIT")
if [ "$commit_message" = preview_clear ]; then
# drop a dummy commit by previous preview_clear
skip_commit "$@"
exit
fi
pr=$(echo "$commit_message" | sed -n '\''s/^Preview PR \([0-9]\{1,\}\).*/\1/p'\'')
if [ -n "$pr" ]; then
case " $CPPREFJP_OPEN_PR_LIST " in
(*" $pr "*)
# preserve a commit for an open PR
git commit-tree "$@" ;;
(*)
# drop a commit for a closed PR
skip_commit "$@" ;;
esac
exit
fi
# we preserve all the other commits
git commit-tree "$@"
' HEAD || true
commit_count=$(git rev-list HEAD --count || true)
if [ -z "$commit_count" ] || [ "$commit_count" = 0 ]; then
# When no commits are left, we create a dummy commit
git reset
git commit --allow-empty -m 'preview_clear'
fi
git push -f origin gh-pages