-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (116 loc) · 6.1 KB
/
pull-request-24.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
name: Diff code list 24
on:
pull_request:
paths:
- 'CLR/Rec24/current/code-list.csv'
jobs:
diff:
runs-on: ubuntu-latest
steps:
- name: Check out this repo
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Checkout PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh pr checkout ${{ github.event.pull_request.number }}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install Python dependencies
run: |-
pip install csv-diff
- name: Fetch latest data
run: |-
cp "CLR/Rec24/current/code-list.csv" code-list-new.csv
curl -o code-list-old.csv "https://raw.githubusercontent.com/uncefact/vocab-codes/main/CLR/Rec24/current/code-list.csv"
# Remove heading line and use it to start a new file
## todo: check if need sorting, simple sort isn't working because CI is the first column
#head -n 1 code-list-new.csv > code-list-new-sorted.csv
## Sort all but the first line and append to that file
#tail -n +2 "code-list-new.csv" | sort >> code-list-new-sorted.csv
# Generate commit message using csv-diff
csv-diff code-list-old.csv code-list-new.csv --key=Code --singular=record --plural=records > message.txt
csv-diff code-list-old.csv code-list-new.csv --key=Code --singular=record --plural=records --json > message.json
cat message.txt
# todo: add check for message.txt content
# mv code-list-new-sorted.csv "CRL/Rec24/current/code-list.csv"
- run: jq -c '.added | select (.!=null) | .[] | select (.CI != "+")' < message.json >> errors-added.txt
- run: |
[ -s errors-added.txt ] && \
echo 'A new code list value was added, but CI was not set to `+`:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-added.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-added.txt
- run: jq -c '.removed | select (.!=null) | .[]' < message.json >> errors-removed.txt
- run: |
[ -s errors-removed.txt ] && \
echo 'No lines should be removed, set CI to `X` instead:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-removed.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-removed.txt
- run: jq -c '.changed | select (.!=null) | .[] | select (.changes.CI != ["","="] and .changes.CI != ["","X"] and .changes.CI != ["","|"] and .changes.CI != ["","#"])' < message.json >> errors-CI.txt
- run: |
[ -s errors-CI.txt ] && \
echo 'Unexpected value set for CI:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-CI.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-CI.txt
- run: jq -c '.changed | select (.!=null) | .[] | select (.changes.CI == ["","="]) | if (.changes != {CI:["","="]}) then . else null end | select (.!=null)' < message.json >> errors-reinstated.txt
- run: |
[ -s errors-reinstated.txt ] && \
echo 'Only CI should be changed for the record marked as reinstated:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-reinstated.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-reinstated.txt
- run: jq -c '.changed | select (.!=null) | .[] | select (.changes.CI == ["","X"]) | if (.changes != {CI:["","X"]}) then . else null end | select (.!=null)' < message.json >> errors-deleted.txt
- run: |
[ -s errors-deleted.txt ] && \
echo 'Only CI should be changed for the record marked as deleted:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-deleted.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-deleted.txt
- run: jq -c '.changed | select (.!=null) | .[] | select (.changes.CI == ["","|"] and (.changes.Description == null or .changes.Name != null))' < message.json >> errors-changed-description.txt
- run: |
[ -s errors-changed-description.txt ] && \
echo 'The description needs to be changed for the record with CI set to `|`:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-changed-description.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-changed-description.txt
- run: jq -c '.changed | select (.!=null) | .[] | select (.changes.CI == ["","#"] and (.changes.Description != null or .changes.Name == null))' < message.json >> errors-changed-description.txt
- run: |
[ -s errors-changed-name.txt ] && \
echo 'The name needs to be changed for the record with CI set to `#`:' >> errors.txt && \
echo '```' >> errors.txt && cat errors-changed-name.txt >> errors.txt && echo '```' >> errors.txt || rm -f errors-changed-name.txt
- name: Set variables
if: ${{ hashFiles('errors.txt') != '' }}
run: |
{
echo 'ERRORS<<EOF'
cat errors.txt
echo EOF
} >> "$GITHUB_ENV"
- name: Check errors.txt file
if: ${{ hashFiles('errors.txt') != '' }}
uses: mb2dev/[email protected]
with:
message: |
${{ env.ERRORS }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Fail check if there are errors
if: ${{ hashFiles('errors.txt') != '' }}
run: echo "Validation failed" && exit 1
- name: Set variables
if: ${{ hashFiles('message.txt') != '' }}
run: |
{
echo 'MESSAGE<<EOF'
cat message.txt
echo EOF
} >> "$GITHUB_ENV"
- name: Comment a pull_request
if: ${{ hashFiles('message.txt') != '' }}
uses: mb2dev/[email protected]
with:
message: |
message:
${{ env.MESSAGE }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Commit and push if it changed
run: |-
git config user.name "Automated"
git config user.email "[email protected]"
git add "CLR/Rec24/current/code-list.csv"
timestamp=$(date -u)
git commit -F message.txt || exit 0
git push