Skip to content

[MR](Rec24): add new trade and transport codes 380 and 381 #43

[MR](Rec24): add new trade and transport codes 380 and 381

[MR](Rec24): add new trade and transport codes 380 and 381 #43

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