|
| 1 | +name: Extract todos from code |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpull_requestpull_request_targetbranchesbranches-ignore |
| 6 | + branches: |
| 7 | + - master |
| 8 | + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-excluding-paths |
| 9 | + paths-ignore: |
| 10 | + - '**.json' |
| 11 | + - '**.jpg' |
| 12 | + - '**.png' |
| 13 | + - '**.ico' |
| 14 | + - '**.md' |
| 15 | + |
| 16 | +# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions |
| 17 | +permissions: |
| 18 | + contents: write # for "git push" |
| 19 | + |
| 20 | +defaults: |
| 21 | + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun |
| 22 | + run: |
| 23 | + # Enable fail-fast behavior using set -eo pipefail |
| 24 | + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference |
| 25 | + shell: bash |
| 26 | + |
| 27 | +jobs: |
| 28 | + extract-pdd-puzzles: |
| 29 | + name: Extract todos from code |
| 30 | + # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on |
| 31 | + runs-on: ubuntu-20.04 |
| 32 | + steps: |
| 33 | + - name: Clone source code |
| 34 | + uses: actions/[email protected] # https://github.com/actions/checkout |
| 35 | + with: |
| 36 | + # Whether to configure the token or SSH key with the local git config. Default: true |
| 37 | + persist-credentials: true |
| 38 | + |
| 39 | + - name: Install pdd |
| 40 | + run: sudo gem install pdd:0.23.1 --no-document |
| 41 | + |
| 42 | + - name: Checkout existing todos to another directory |
| 43 | + run: | |
| 44 | + git fetch --depth=1 origin generated-todos |
| 45 | + git worktree add generated-todos generated-todos |
| 46 | +
|
| 47 | + - name: Extract todos |
| 48 | + run: | |
| 49 | + pdd \ |
| 50 | + --skip-gitignore \ |
| 51 | + --exclude '**/*.json' \ |
| 52 | + --exclude '**/*.jpg' \ |
| 53 | + --exclude '**/*.png' \ |
| 54 | + --exclude '**/*.ico' \ |
| 55 | + --exclude '**/*.md' \ |
| 56 | + --exclude '**/*.enc' \ |
| 57 | + --exclude 'src/test/wiremock/**/*' \ |
| 58 | + --exclude 'generated-todos/**' \ |
| 59 | + --verbose \ |
| 60 | + --file pdd.xml |
| 61 | +
|
| 62 | + - name: Remove the current date from pdd.xml |
| 63 | + run: sed -i -E '/<time>[-0-9T:Z]+<\/time>/d;/<puzzles/s| date="[-0-9T:Z]+"||' pdd.xml |
| 64 | + |
| 65 | + - name: Generate todos-in-code.tsv |
| 66 | + run: ./src/main/scripts/ci/pdd-xml-to-json.sh pdd.xml | ./src/main/scripts/ci/pdd-json-to-tsv.sh > todos-in-code.tsv |
| 67 | + |
| 68 | + - name: Check whether there are new todos |
| 69 | + run: | |
| 70 | + CREATE_COMMIT=no |
| 71 | +
|
| 72 | + if [ ! -f generated-todos/pdd.xml ]; then |
| 73 | + echo 'pdd.xml has just been created' |
| 74 | + CREATE_COMMIT=yes |
| 75 | + elif ! diff --brief generated-todos/pdd.xml pdd.xml >/dev/null; then |
| 76 | + echo 'pdd.xml has been modified' |
| 77 | + CREATE_COMMIT=yes |
| 78 | + fi |
| 79 | +
|
| 80 | + if [ ! -f generated-todos/todos-in-code.tsv ]; then |
| 81 | + echo 'todos-in-code.tsv has just been created' |
| 82 | + CREATE_COMMIT=yes |
| 83 | + elif ! diff --brief generated-todos/todos-in-code.tsv todos-in-code.tsv >/dev/null; then |
| 84 | + echo 'todos-in-code.tsv has been modified' |
| 85 | + CREATE_COMMIT=yes |
| 86 | + fi |
| 87 | +
|
| 88 | + if [ "$CREATE_COMMIT" = 'yes' ]; then |
| 89 | + printf 'pdd.xml: %d puzzles\n' "$(grep -c '<puzzle>' pdd.xml)" |
| 90 | + printf 'todos-in-code.tsv: %d todos\n' "$(sed 1d todos-in-code.tsv | wc -l)" |
| 91 | + else |
| 92 | + echo 'neither pdd.xml nor todos-in-code.tsv have been modified' |
| 93 | + fi |
| 94 | +
|
| 95 | + # Make variable available for the next steps |
| 96 | + echo "CREATE_COMMIT=$CREATE_COMMIT" | tee -a "$GITHUB_ENV" |
| 97 | +
|
| 98 | + - name: Collect information about the original commit |
| 99 | + if: env.CREATE_COMMIT == 'yes' |
| 100 | + run: | |
| 101 | + # Make variables available for the next steps |
| 102 | + ( |
| 103 | + echo "COMMIT_MSG=$(git show "$GITHUB_SHA" --no-patch --format=oneline --abbrev-commit)" |
| 104 | + echo "AUTHOR_EMAIL=$(git show "$GITHUB_SHA" --no-patch --format=%aE)" |
| 105 | + echo "AUTHOR_NAME=$(git show "$GITHUB_SHA" --no-patch --format=%aN)" |
| 106 | + ) | tee -a "$GITHUB_ENV" |
| 107 | +
|
| 108 | + - name: Commit the changes |
| 109 | + if: env.CREATE_COMMIT == 'yes' |
| 110 | + env: |
| 111 | + GIT_AUTHOR_NAME: "GitHub Action on behalf of ${{ env.AUTHOR_NAME }}" |
| 112 | + GIT_COMMITTER_NAME: "GitHub Action on behalf of ${{ env.AUTHOR_NAME }}" |
| 113 | + GIT_AUTHOR_EMAIL: "${{ env.AUTHOR_EMAIL }}" |
| 114 | + GIT_COMMITTER_EMAIL: "${{ env.AUTHOR_EMAIL }}" |
| 115 | + NEW_COMMIT_MSG: "chore: processed ${{ env.COMMIT_MSG }}" |
| 116 | + working-directory: generated-todos |
| 117 | + run: | |
| 118 | + for FILE in pdd.xml todos-in-code.tsv; do |
| 119 | + mv -f ../$FILE . |
| 120 | + git add $FILE |
| 121 | + done |
| 122 | + git commit --all -m "$NEW_COMMIT_MSG" |
| 123 | + git push |
| 124 | +
|
| 125 | + - name: Cleanup |
| 126 | + if: always() |
| 127 | + run: | |
| 128 | + [ ! -f pdd.xml ] || rm -fv pdd.xml |
| 129 | + [ ! -f todos-in-code.tsv ] || rm -fv todos-in-code.tsv |
| 130 | + [ ! -d generated-todos ] || git worktree remove generated-todos |
0 commit comments