Skip to content

preview_clear

preview_clear #7

Workflow file for this run

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% }
# Note: 以下は closed PR に関する commit を squash する。原理的にはこ
# のステップは不要だが、後の --index-filter の処理コストを軽減するた
# めに commit 数を減らしておく。
export CPPREFJP_OPEN_PR_LIST
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_non_empty_tree "$@" ;;
(*)
# drop a commit for a closed PR
skip_commit "$@" ;;
esac
exit
fi
# we preserve all the other commits
git_commit_non_empty_tree "$@"
'
CPPREFJP_CLOSED_PR_LIST=
for dir in gen/pull/*; do
pr=${dir##*/}
case " $CPPREFJP_OPEN_PR_LIST " in
(*" $pr "*)
;;
(*)
CPPREFJP_CLOSED_PR_LIST="$CPPREFJP_CLOSED_PR_LIST $pr" ;;
esac
done
if [ -z "$CPPREFJP_CLOSED_PR_LIST" ]; then
exit 0
fi
export CPPREFJP_CLOSED_PR_LIST
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git filter-branch --prune-empty -f --index-filter '
for pr in $CPPREFJP_CLOSED_PR_LIST; do
git rm -rf "gen/pull/$pr" 2>/dev/null || true
done
' 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