preview_clear #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |