Skip to content

Commit 597aabf

Browse files
committed
ci: extract todos from code with pdd and save them into pdd.xml and todos-in-code.tsv
Part of #1610
1 parent 777c9ca commit 597aabf

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Algorithm:
4+
# 1) sort by ticket and id
5+
# 2) prepend a header (that will be used by GitHub for preview)
6+
# 3) transform a list of objects to many object: [{}, {}] => {}, {}
7+
# 4) transform each object to a list: {}, {} => [], []
8+
# 4a) surround .body with quotes and escape double quotes inside by doubling them (this is what @csv filter does).
9+
# Required to be able to view at GitHub. See also:
10+
# https://docs.github.com/en/repositories/working-with-files/using-files/working-with-non-code-files#rendering-csv-and-tsv-data
11+
# 5) transform lists to TSV rows
12+
13+
exec jq --raw-output '. | sort_by([ .ticket | tonumber ], .id) | [ { "id":"Id", "ticket":"Ticket", "body":"Title", "file":"File", "lines":"Lines" } ] + . | .[] | [ .id, .ticket, ([ .body ] | @csv), .file, .lines ] | @tsv'
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# Treat unset variables and parameters as an error when performing parameter expansion
4+
set -o nounset
5+
6+
# Exit immediately if command returns a non-zero status
7+
set -o errexit
8+
9+
# Return value of a pipeline is the value of the last command to exit with a non-zero status
10+
set -o pipefail
11+
12+
if [ -z "${1:-}" ]; then
13+
echo >&2 "Usage: $(dirname "$0") /path/to/pdd.xml"
14+
exit 1
15+
fi
16+
17+
cat "$1" \
18+
| sed \
19+
-e '/<?xml/d' \
20+
-e '/<estimate>/d' \
21+
-e '/<role>/d' \
22+
-e '/<time>/d' \
23+
-e '/<author>/d' \
24+
-e '/<email>/d' \
25+
-e 's|"|\\"|g' \
26+
-e 's|&quot;|"|g' \
27+
-e "s|&apos;|'|g" \
28+
-e 's|&amp;|&|g' \
29+
-e 's|<puzzles [^>]\+>|[|' \
30+
| paste -s -d' ' - \
31+
| sed \
32+
-E \
33+
-e 's|</(id\|ticket\|lines\|body\|file)>[ ]+</puzzle>[ ]+</puzzles>|" } ]|' \
34+
-e 's|</(id\|ticket\|lines\|body\|file)>[ ]+</puzzle>[ ]+<puzzle>|" }, {|g' \
35+
-e 's|<(id\|ticket\|lines\|body\|file)>|"\1": "|g' \
36+
-e 's|</(id\|ticket\|lines\|body\|file)>|",|g' \
37+
-e 's|<puzzle>|{|' \
38+
-e 's|</puzzles>|]|' \
39+
-e 's|&lt;|<|g' \
40+
-e 's|&gt;|>|g' \
41+
#

0 commit comments

Comments
 (0)