diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 756bd07..785d40c 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -1,12 +1,12 @@ on: pull_request jobs: - check_size: + check_files: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Check file size - run: .github/workflows/scripts/check-size.sh + - name: Check files for compliance + run: .github/workflows/scripts/check-files.sh diff --git a/.github/workflows/scripts/check-files.sh b/.github/workflows/scripts/check-files.sh new file mode 100755 index 0000000..2b19052 --- /dev/null +++ b/.github/workflows/scripts/check-files.sh @@ -0,0 +1,125 @@ +#!/bin/sh +set -eu + +SIZE_LIMIT=150000 + +check_size() { + size="$(stat --printf="%s" "$1")" + if [ "$size" -gt "$SIZE_LIMIT" ]; then + echo "File $1 is bigger than specified $SIZE_LIMIT limit" + exit 1 + fi +} + +check_webp_name() { + shouldname="$(echo "$1" | + iconv --to-code=utf-8 | + sed "s/^\(data\/pix\/\)\?/data\/pix\//" | + tr '[:upper:]' '[:lower:]' | + tr '_ ' '-')" + if [ "$shouldname" != "$1" ]; then + echo "$1 should be named $shouldname." + exit 1 + fi +} + +check_recipe_name() { + shouldname="$(echo "$1" | + iconv --to-code=utf-8 | + sed "s/^\(src\/\)\?/src\//" | + tr '[:upper:]' '[:lower:]' | + tr '_ ' '-')" + if [ "$shouldname" != "$1" ]; then + echo "$1 should be named $shouldname." + exit 1 + fi +} + +check_recipe_content() { + awk ' + BEGIN { + HAS_TITLE = 0; + HAS_TAGS = 0; + NUM_TAGS = 0; + HAS_INGREDIENTS = 0; + HAS_DIRECTIONS = 0; + } + + # First line should be the title + NR == 1 && /^# / { + HAS_TITLE = 1; + next; + } + + /^## Ingredients/ { + HAS_INGREDIENTS = 1; + next; + } + + /^## Directions/ { + HAS_DIRECTIONS = 1; + next; + } + + END { + # Last line should be the tags list + if ($1 == ";tags:") { + HAS_TAGS = 1; + NUM_TAGS = NF - 1; + } + + FAIL = 0; + + if (!HAS_TITLE) { + print "Recipe does not have a properly formatted title on the first line." + FAIL = 1; + } + + if (!HAS_TAGS) { + print "Recipe does not have a properly formatted tags on the last line." + FAIL = 1; + } else if (NUM_TAGS < 2) { + print "Recipe only has " NUM_TAGS " tags. Add some more." + FAIL = 1; + } else if (NUM_TAGS > 5) { + print "Recipe has " NUM_TAGS " tags which is too many. Remove some tags." + FAIL = 1; + } + + if (!HAS_INGREDIENTS) { + print "Recipe does not have an ingredients list." + FAIL = 1; + } + + if (!HAS_DIRECTIONS) { + print "Recipe does not have a directions section." + FAIL = 1; + } + + if (FAIL) { + exit 1; + } + } + ' "$1" + + if [ $? -ne 0 ]; then + exit 1; + fi +} + +git diff --name-only "$(git merge-base origin/master HEAD)" | while IFS= read -r file; do + case "$file" in + *.webp) + echo "Checking size of $file" + check_size "$file" + check_webp_name "$file" + ;; + .github/*.md) + exit 0; + ;; + *.md) + check_recipe_name "$file" + check_recipe_content "$file" + ;; + esac +done diff --git a/.github/workflows/scripts/check-size.sh b/.github/workflows/scripts/check-size.sh deleted file mode 100755 index e6efd06..0000000 --- a/.github/workflows/scripts/check-size.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh -set -eu - -SIZE_LIMIT=150000 - -check_size() { - local size=$(stat --printf="%s" $1) - if [ "$size" -gt "$SIZE_LIMIT" ]; then - echo "File $1 is bigger than specified $SIZE_LIMIT limit" - exit 1 - fi -} - -check_md_name() { - shouldname="$(echo "$1" | - iconv --to-code=utf-8 | - sed "s/^\(src\/\)\?/src\//" | - tr '[:upper:]' '[:lower:]' | - tr ' ' '-')" - if [ "$shouldname" != "$1" ]; then - echo "$1 should be named $shouldname." - exit 1 - fi -} - -git diff --name-only `git merge-base origin/master HEAD` | while IFS= read -r file; do - case "$file" in - *.webp) - echo "Checking size of $file" - check_size $file - ;; - *.md) check_md_name "$file" ;; - esac -done