-
Notifications
You must be signed in to change notification settings - Fork 33
185 lines (160 loc) · 7.57 KB
/
todos-extract-from-code.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: Create issues from todos
on:
push:
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpull_requestpull_request_targetbranchesbranches-ignore
branches:
- master
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-excluding-paths
paths-ignore:
- '**.json'
- '**.jpg'
- '**.png'
- '**.ico'
- '**.md'
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions
permissions:
contents: write # for "git push"
issues: write # connect-todos-to-issues.sh uses "gh issue create"
defaults:
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun
run:
# Enable fail-fast behavior using set -eo pipefail
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference
shell: bash
jobs:
extract-pdd-puzzles:
name: Extract todos from code
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on
runs-on: ubuntu-20.04
steps:
- name: Clone source code
uses: actions/checkout@v4.1.0 # https://github.com/actions/checkout
with:
# Whether to configure the token or SSH key with the local git config. Default: true
persist-credentials: true
- name: Install pdd
run: sudo gem install pdd:0.23.1 --no-document
- name: Checkout existing todos to another directory
run: |
git fetch --depth=1 origin generated-todos
git worktree add generated-todos generated-todos
- name: Extract todos
run: |
pdd \
--skip-gitignore \
--exclude '**/*.json' \
--exclude '**/*.jpg' \
--exclude '**/*.png' \
--exclude '**/*.ico' \
--exclude '**/*.md' \
--exclude '**/*.enc' \
--exclude 'src/test/wiremock/**/*' \
--exclude 'generated-todos/**' \
--verbose \
--file pdd.xml
- name: Remove the current date from pdd.xml
run: sed -i -E '/<time>[-0-9T:Z]+<\/time>/d;/<puzzles/s| date="[-0-9T:Z]+"||' pdd.xml
- name: Generate todos-in-code.tsv
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
# @todo #1610 Close an issue or post a comment when a puzzle got removed from code
# @todo #1610 Post a comment when issue got closed without removing a puzzle
- name: Check whether there are new todos
run: |
PUZZLES_FILES_MODIFIED=no
if [ ! -f generated-todos/pdd.xml ]; then
echo 'pdd.xml has just been created'
mv pdd.xml generated-todos/pdd.xml
PUZZLES_FILES_MODIFIED=yes
elif ! diff --brief generated-todos/pdd.xml pdd.xml >/dev/null; then
echo 'pdd.xml has been modified'
mv pdd.xml generated-todos/pdd.xml
PUZZLES_FILES_MODIFIED=yes
fi
if [ ! -f generated-todos/todos-in-code.tsv ]; then
echo 'todos-in-code.tsv has just been created'
mv todos-in-code.tsv generated-todos/todos-in-code.tsv
PUZZLES_FILES_MODIFIED=yes
elif ! diff --brief generated-todos/todos-in-code.tsv todos-in-code.tsv >/dev/null; then
echo 'todos-in-code.tsv has been modified'
mv todos-in-code.tsv generated-todos/todos-in-code.tsv
PUZZLES_FILES_MODIFIED=yes
fi
if [ "$PUZZLES_FILES_MODIFIED" = 'yes' ]; then
printf 'pdd.xml: %d puzzles\n' "$(grep -c '<puzzle>' generated-todos/pdd.xml)"
printf 'todos-in-code.tsv: %d todos\n' "$(sed 1d generated-todos/todos-in-code.tsv | wc -l)"
else
echo 'neither pdd.xml nor todos-in-code.tsv have been modified'
fi
# Make variable available for the next steps
echo "PUZZLES_FILES_MODIFIED=$PUZZLES_FILES_MODIFIED" | tee -a "$GITHUB_ENV"
# It's possible that for some puzzles we haven't created issues yet
# (because of network, rate limit or other kind of errors)
- name: Check whether there are puzzles without related issues
if: env.PUZZLES_FILES_MODIFIED == 'no'
working-directory: generated-todos
run: |
NEW_PUZZLES="$(comm -23 <(cut -d$'\t' -f1 todos-in-code.tsv | sort) <(cut -d$'\t' -f1 todos-on-github.tsv | sort))"
if [ -n "$NEW_PUZZLES" ]; then
echo "Found puzzles without related issues:"
echo "$NEW_PUZZLES" | while read PUZZLE_ID; do
TITLE="$(grep "$PUZZLE_ID" todos-in-code.tsv | cut -d$'\t' -f3 | sed 's|^"||;s|"$||;s|""|"|g')"
echo "- $PUZZLE_ID: $TITLE"
done
HAVE_PUZZLES_WITHOUT_ISSUES=yes
else
HAVE_PUZZLES_WITHOUT_ISSUES=no
fi
# Make variable available for the next steps
echo "HAVE_PUZZLES_WITHOUT_ISSUES=$HAVE_PUZZLES_WITHOUT_ISSUES" | tee -a "$GITHUB_ENV"
- name: Connect todos to the issues
if: env.PUZZLES_FILES_MODIFIED == 'yes' || env.HAVE_PUZZLES_WITHOUT_ISSUES == 'yes'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./src/main/scripts/ci/connect-todos-to-issues.sh generated-todos/todos-in-code.tsv
- name: Check whether todos-on-github.tsv has been modified
if: env.PUZZLES_FILES_MODIFIED == 'yes' || env.HAVE_PUZZLES_WITHOUT_ISSUES == 'yes'
working-directory: generated-todos
run: |
PUZZLES_MAPPING_MODIFIED=no
if ! git diff-index --quiet HEAD -- todos-on-github.tsv; then
PUZZLES_MAPPING_MODIFIED=yes
echo 'todos-on-github.tsv has been modified'
fi
# Make variable available for the next steps
echo "PUZZLES_MAPPING_MODIFIED=$PUZZLES_MAPPING_MODIFIED" | tee -a "$GITHUB_ENV"
- name: Initialize Git variables for committing the changes
if: env.PUZZLES_FILES_MODIFIED == 'yes' || env.PUZZLES_MAPPING_MODIFIED == 'yes'
run: |
AUTHOR_EMAIL="$(git show "$GITHUB_SHA" --no-patch --format=%aE)"
AUTHOR_NAME="$(git show "$GITHUB_SHA" --no-patch --format=%aN)"
# Make variables available for the next steps
(
echo "COMMIT_MSG=$(git show "$GITHUB_SHA" --no-patch --format=oneline --abbrev-commit)"
echo "GIT_AUTHOR_NAME=GitHub Action on behalf of $AUTHOR_NAME"
echo "GIT_COMMITTER_NAME=GitHub Action on behalf of $AUTHOR_NAME"
echo "GIT_AUTHOR_EMAIL=$AUTHOR_EMAIL"
echo "GIT_COMMITTER_EMAIL=$AUTHOR_EMAIL"
) | tee -a "$GITHUB_ENV"
- name: Commit the changes
if: env.PUZZLES_FILES_MODIFIED == 'yes'
env:
NEW_COMMIT_MSG: "chore: processed ${{ env.COMMIT_MSG }}"
working-directory: generated-todos
run: |
git commit pdd.xml todos-in-code.tsv -m "$NEW_COMMIT_MSG"
git push
- name: Commit updated mapping
if: env.PUZZLES_MAPPING_MODIFIED == 'yes'
env:
NEW_COMMIT_MSG: "chore: sync issues for ${{ env.COMMIT_MSG }}"
working-directory: generated-todos
run: |
git add todos-on-github.tsv
git commit todos-on-github.tsv -m "$NEW_COMMIT_MSG"
git push
- name: Cleanup
if: always()
run: |
[ ! -f pdd.xml ] || rm -fv pdd.xml
[ ! -f todos-in-code.tsv ] || rm -fv todos-in-code.tsv
[ ! -d generated-todos ] || git worktree remove generated-todos