diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index df8f540..4e0231d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -101,6 +101,8 @@ jobs: run: bash actions/release/test_update_changelog.sh - name: Test check_test_files.py run: bash actions/test-sdist/test_check_test_files.sh + - name: Test parse_diff.sh + run: bash actions/pre-commit-autoupdate/test_parse_diff.sh test_pre_commit_autoupdate: if: github.event_name != 'schedule' || github.repository == 'Calysto/maintainer_tools' diff --git a/actions/pre-commit-autoupdate/action.yml b/actions/pre-commit-autoupdate/action.yml index d156f14..1b59c4b 100644 --- a/actions/pre-commit-autoupdate/action.yml +++ b/actions/pre-commit-autoupdate/action.yml @@ -79,19 +79,7 @@ runs: FULL_BRANCH="${BRANCH}-${SUFFIX}" # Parse updated packages from diff - UPDATES="" - CURRENT_REPO="" - OLD_REV="" - while IFS= read -r line; do - if [[ "$line" == " "* ]] && [[ "$line" =~ repo:[[:space:]]+(.*) ]]; then - CURRENT_REPO=$(basename "${BASH_REMATCH[1]}") - elif [[ "$line" =~ ^-[[:space:]]+rev:[[:space:]]+(.*) ]]; then - OLD_REV="${BASH_REMATCH[1]}" - elif [[ "$line" =~ ^\+[[:space:]]+rev:[[:space:]]+(.*) ]]; then - NEW_REV="${BASH_REMATCH[1]}" - UPDATES="${UPDATES}- ${CURRENT_REPO}: \`${OLD_REV}\` → \`${NEW_REV}\`"$'\n' - fi - done < <(git diff .pre-commit-config.yaml) + UPDATES=$(git diff .pre-commit-config.yaml | bash "${{ github.action_path }}/parse_diff.sh" -) BODY="" if [ -n "$UPDATES" ]; then diff --git a/actions/pre-commit-autoupdate/parse_diff.sh b/actions/pre-commit-autoupdate/parse_diff.sh new file mode 100644 index 0000000..52e445e --- /dev/null +++ b/actions/pre-commit-autoupdate/parse_diff.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Parse a git diff of .pre-commit-config.yaml and emit a markdown list of updates. +# Usage: parse_diff.sh +# Outputs lines of the form: "- : `` → ``" +set -euo pipefail + +DIFF_FILE="${1:--}" + +CURRENT_REPO="" +OLD_REV="" +while IFS= read -r line; do + if [[ "$line" == " "* ]] && [[ "$line" =~ repo:[[:space:]]+(.*) ]]; then + CURRENT_REPO=$(basename "${BASH_REMATCH[1]}") + elif [[ "$line" =~ ^-[[:space:]]+rev:[[:space:]]+(.*) ]]; then + OLD_REV="${BASH_REMATCH[1]//\'/}" + OLD_REV="${OLD_REV//\"/}" + elif [[ "$line" =~ ^\+[[:space:]]+rev:[[:space:]]+(.*) ]]; then + NEW_REV="${BASH_REMATCH[1]//\'/}" + NEW_REV="${NEW_REV//\"/}" + echo "- ${CURRENT_REPO}: \`${OLD_REV}\` → \`${NEW_REV}\`" + fi +done < <(cat "$DIFF_FILE") diff --git a/actions/pre-commit-autoupdate/test_parse_diff.sh b/actions/pre-commit-autoupdate/test_parse_diff.sh new file mode 100644 index 0000000..e8bbc2b --- /dev/null +++ b/actions/pre-commit-autoupdate/test_parse_diff.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT="$(cd "$(dirname "$0")" && pwd)/parse_diff.sh" +FAIL=0 + +check() { + local desc="$1" + local expected="$2" + local actual="$3" + if [ "$actual" = "$expected" ]; then + echo "OK: $desc" + else + echo "FAIL: $desc" + echo " expected: $expected" + echo " actual: $actual" + FAIL=1 + fi +} + +# Simulate a git diff with three repos using no quotes, single quotes, and double quotes +DIFF=$(cat <<'EOF' + - repo: https://github.com/pre-commit/pre-commit-hooks +- rev: v4.4.0 ++ rev: v4.5.0 + - repo: https://github.com/psf/black +- rev: '23.1.0' ++ rev: '23.3.0' + - repo: https://github.com/PyCQA/flake8 +- rev: "6.0.0" ++ rev: "6.1.0" +EOF +) + +ACTUAL=$(echo "$DIFF" | bash "$SCRIPT" -) + +check "no-quotes version" \ + "- pre-commit-hooks: \`v4.4.0\` → \`v4.5.0\`" \ + "$(echo "$ACTUAL" | grep pre-commit-hooks)" + +check "single-quoted version" \ + "- black: \`23.1.0\` → \`23.3.0\`" \ + "$(echo "$ACTUAL" | grep black)" + +check "double-quoted version" \ + "- flake8: \`6.0.0\` → \`6.1.0\`" \ + "$(echo "$ACTUAL" | grep flake8)" + +exit $FAIL