Skip to content

Commit

Permalink
Add workflow to check sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Nov 23, 2024
1 parent bcfc62e commit 832e166
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/check_sorting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

file="awesome-generator/awesome.toml"
exit_code=0

IFS=";"

# shellcheck disable=SC2207
# This just feels like the easiest to understand. I don't know how to split the
# file on multiple newlines any better way.
# Ref: https://stackoverflow.com/a/62608718/2274551
sections=($(awk -v RS= -v ORS=";" '{print}' "$file"))

for section in "${sections[@]}"; do
header=$(echo "$section" | head -n 1)
rows=$(echo "$section" | tail -n +2)
sorted=$(echo "$rows" | sort)

if [ "$rows" != "$sorted" ]; then
invalid_sections+=("$header")
exit_code=1
fi
done

if [ $exit_code -ne 0 ]; then
cat <<-EOF
Thanks for adding new resources to this project!
To help with consistency and easie maintenance, e.g. spot duplicates the items in each section is sorted alphanumerically
Your change doesn't conform to this so please ensure the section(s) you've edited are sorted!
These sections are currently not sorted:
EOF

for s in "${invalid_sections[@]}"; do
echo " - \\\`$s\\\`"
done
fi

exit $exit_code
57 changes: 57 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Validate `awesome.toml`

on:
workflow_dispatch:
push:
branches:
- main
pull_request:

jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Validate sections
id: validate
run: |
set +e # Allow the script to fail but capture its output
{
echo "output<<EOF"
bash .github/workflows/check_sorting.sh
exit_code="$?"
echo EOF
} >> "$GITHUB_OUTPUT"
echo "exit_code=$exit_code" >> "$GITHUB_OUTPUT"
exit "$exit_code"
- name: Post PR Comment
if: always()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
COMMENT_BODY="${{ steps.validate.outputs.output }}"
EXIT_CODE="${{ steps.validate.outputs.exit_code }}"
EXISTING_COMMENT=$(gh pr view "$PR_NUMBER" \
--json comments \
--jq '.comments[] | select(.author.login=="github-actions")')
if [ "$EXIT_CODE" -ne 0 ]; then
if [ -n "$EXISTING_COMMENT" ]; then
gh pr comment "$PR_NUMBER" --body "$COMMENT_BODY" --edit-last
else
gh pr comment "$PR_NUMBER" --body "$COMMENT_BODY"
fi
else
if [ -n "$EXISTING_COMMENT" ]; then
# Currently the gh cli and the API fail to delete comments so we
# just update any existing one for now.
gh pr comment "$PR_NUMBER" --body "Thanks for fixing the CI issues!" --edit-last
fi
fi

0 comments on commit 832e166

Please sign in to comment.