|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com> |
| 3 | + |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# This script retrieves the list of changed PHP files for a pull request |
| 7 | +# using the GitHub API and sets two outputs: |
| 8 | +# - any_changed: "true" if at least one PHP file changed, "false" otherwise |
| 9 | +# - all_changed_files: space-separated list of changed PHP file paths |
| 10 | +# |
| 11 | +# Required environment variables: |
| 12 | +# GITHUB_TOKEN - GitHub token with repo access |
| 13 | +# GITHUB_REPOSITORY - "owner/repo" |
| 14 | +# GITHUB_EVENT_PATH - Path to the event JSON payload |
| 15 | + |
| 16 | +# Verify required environment variables are set |
| 17 | +if [[ -z "${GITHUB_TOKEN:-}" ]]; then |
| 18 | + echo "GITHUB_TOKEN is not set" >&2 |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | +if [[ -z "${GITHUB_REPOSITORY:-}" ]]; then |
| 22 | + echo "GITHUB_REPOSITORY is not set" >&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | +if [[ -z "${GITHUB_EVENT_PATH:-}" ]]; then |
| 26 | + echo "GITHUB_EVENT_PATH is not set" >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Extract the pull request number from the event payload |
| 31 | +pr_number=$(jq --raw-output '.pull_request.number' "$GITHUB_EVENT_PATH") |
| 32 | +if [[ "$pr_number" == "null" ]]; then |
| 33 | + echo "Not a pull request event" |
| 34 | + exit 0 |
| 35 | +fi |
| 36 | + |
| 37 | +# Split repository into owner and repo name |
| 38 | +# Split repository into owner and repo name using Bash parameter expansion |
| 39 | +owner="${GITHUB_REPOSITORY%%/*}" # Extract text before the first '/' |
| 40 | +repo="${GITHUB_REPOSITORY##*/}" # Extract text after the last '/' |
| 41 | + |
| 42 | +page=1 |
| 43 | +per_page=100 |
| 44 | +changed_php_files=() |
| 45 | + |
| 46 | +# Loop through all pages to gather changed files |
| 47 | +while true; do |
| 48 | + response=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 49 | + "https://api.github.com/repos/${owner}/${repo}/pulls/${pr_number}/files?per_page=${per_page}&page=${page}") |
| 50 | + |
| 51 | + # Filter for files ending with .php and add them to the list |
| 52 | + mapfile -t files < <(echo "$response" | jq -r '.[] | select(.filename | test("\\.php$")) | .filename') |
| 53 | + changed_php_files+=("${files[@]}") |
| 54 | + |
| 55 | + # Check if we have reached the last page (less than per_page results) |
| 56 | + count=$(echo "$response" | jq 'length') |
| 57 | + if (( count < per_page )); then |
| 58 | + break |
| 59 | + fi |
| 60 | + ((page++)) |
| 61 | +done |
| 62 | + |
| 63 | + |
| 64 | +# Build a space-separated string of changed PHP files |
| 65 | +# This does not cope with files that have spaces. |
| 66 | +# But such files do not exist in the project (at least not for the |
| 67 | +# files we are filtering). |
| 68 | +all_changed_files=$(IFS=" " ; echo "${changed_php_files[*]}") |
| 69 | + |
| 70 | + |
| 71 | +# Determine changed files flag |
| 72 | +if [ -z "$all_changed_files" ]; then |
| 73 | + any_changed="false" |
| 74 | +else |
| 75 | + any_changed="true" |
| 76 | +fi |
| 77 | + |
| 78 | +# Set outputs for GitHub Actions if GITHUB_OUTPUT is available |
| 79 | +if [ -n "${GITHUB_OUTPUT:-}" ]; then |
| 80 | + echo "any_changed=${any_changed}" >> "$GITHUB_OUTPUT" |
| 81 | + echo "all_changed_files=${all_changed_files}" >> "$GITHUB_OUTPUT" |
| 82 | +else |
| 83 | + # Otherwise, print the outputs |
| 84 | + echo "any_changed=${any_changed}" |
| 85 | + echo "all_changed_files=${all_changed_files}" |
| 86 | +fi |
0 commit comments