|
| 1 | +name: 'Check Changed Files' |
| 2 | +description: | |
| 3 | + Check if all changed files in a PR match provided regex patterns. |
| 4 | +
|
| 5 | + This action compares changed files in a pull request against one or more regex patterns |
| 6 | + and determines if all changed files match at least one of the provided patterns. |
| 7 | +
|
| 8 | + Inputs: |
| 9 | + - patterns: List of regex patterns (multiline string) to match against changed file paths |
| 10 | +
|
| 11 | + Outputs: |
| 12 | + - only_changed: Boolean indicating if all changed files matched the patterns |
| 13 | + - no_changes: Boolean indicating if there were no changed files in the PR |
| 14 | + - changed_files: JSON array of all changed files in the PR |
| 15 | + - matched_files: JSON array of files that matched at least one pattern |
| 16 | + - unmatched_files: JSON array of files that didn't match any pattern |
| 17 | +inputs: |
| 18 | + patterns: |
| 19 | + description: 'List of regex patterns to match against changed files' |
| 20 | + required: true |
| 21 | + |
| 22 | +outputs: |
| 23 | + only_changed: |
| 24 | + description: 'True if all changed files match the provided patterns, false otherwise' |
| 25 | + value: ${{ steps.check_files.outputs.only_changed }} |
| 26 | + no_changes: |
| 27 | + description: 'True if there were no changed files in the PR, false otherwise' |
| 28 | + value: ${{ steps.check_files.outputs.no_changes }} |
| 29 | + changed_files: |
| 30 | + description: 'List of changed files' |
| 31 | + value: ${{ steps.check_files.outputs.changed_files }} |
| 32 | + matched_files: |
| 33 | + description: 'List of changed files that matched the patterns' |
| 34 | + value: ${{ steps.check_files.outputs.matched_files }} |
| 35 | + unmatched_files: |
| 36 | + description: 'List of changed files that did not match any pattern' |
| 37 | + value: ${{ steps.check_files.outputs.unmatched_files }} |
| 38 | + |
| 39 | +runs: |
| 40 | + using: "composite" |
| 41 | + steps: |
| 42 | + - name: Check changed files against patterns |
| 43 | + id: check_files |
| 44 | + shell: bash |
| 45 | + run: | |
| 46 | + set -ex |
| 47 | +
|
| 48 | + # Only support pull request events |
| 49 | + if [ "${{ github.event_name }}" != "pull_request" ]; then |
| 50 | + echo "Error: This action only supports pull_request events, got: ${{ github.event_name }}" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +
|
| 54 | + # Check if jq is available |
| 55 | + if ! command -v jq >/dev/null 2>&1; then |
| 56 | + echo "Error: jq is required but not installed" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + # Read patterns from input (multiline string) |
| 61 | + PATTERNS_INPUT="${{ inputs.patterns }}" |
| 62 | +
|
| 63 | + # Validate patterns input |
| 64 | + if [ -z "$PATTERNS_INPUT" ]; then |
| 65 | + echo "Error: patterns input is required" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + # Get list of changed files for pull requests |
| 70 | + BASE_REF="${{ github.event.pull_request.base.sha }}" |
| 71 | + HEAD_REF="${{ github.event.pull_request.head.sha }}" |
| 72 | + if ! CHANGED_FILES=$(git diff --name-only "$BASE_REF".."$HEAD_REF" 2>/dev/null); then |
| 73 | + echo "Error: Failed to get changed files. Base ref: $BASE_REF HEAD ref: $HEAD_REF" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + echo "Changed files:" |
| 78 | + echo "$CHANGED_FILES" |
| 79 | +
|
| 80 | + # Convert patterns to array and filter out empty lines |
| 81 | + PATTERNS=() |
| 82 | + while IFS= read -r pattern; do |
| 83 | + # Remove leading/trailing whitespace |
| 84 | + pattern=$(echo "$pattern" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') |
| 85 | + # Skip empty patterns |
| 86 | + if [ -n "$pattern" ]; then |
| 87 | + PATTERNS+=("$pattern") |
| 88 | + fi |
| 89 | + done <<< "$PATTERNS_INPUT" |
| 90 | +
|
| 91 | + # Check if we have any valid patterns |
| 92 | + if [ ${#PATTERNS[@]} -eq 0 ]; then |
| 93 | + echo "Error: No valid patterns provided" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + # Initialize arrays |
| 98 | + MATCHED_FILES=() |
| 99 | + UNMATCHED_FILES=() |
| 100 | +
|
| 101 | + # Handle the case where there are no changed files |
| 102 | + if [ -z "$CHANGED_FILES" ]; then |
| 103 | + echo "No files changed in this PR" |
| 104 | + ONLY_CHANGED="true" # No files changed - treat as success |
| 105 | + NO_CHANGES="true" |
| 106 | + else |
| 107 | + # Check each changed file against patterns |
| 108 | + while IFS= read -r file; do |
| 109 | + if [ -z "$file" ]; then |
| 110 | + continue |
| 111 | + fi |
| 112 | +
|
| 113 | + MATCHED=false |
| 114 | + for pattern in "${PATTERNS[@]}"; do |
| 115 | + # Use safe regex matching with error handling |
| 116 | + if [[ "$file" =~ $pattern ]] 2>/dev/null; then |
| 117 | + MATCHED=true |
| 118 | + break |
| 119 | + fi |
| 120 | + done |
| 121 | +
|
| 122 | + if [ "$MATCHED" = true ]; then |
| 123 | + MATCHED_FILES+=("$file") |
| 124 | + else |
| 125 | + UNMATCHED_FILES+=("$file") |
| 126 | + fi |
| 127 | + done <<< "$CHANGED_FILES" |
| 128 | +
|
| 129 | + # Determine if only matched files changed |
| 130 | + if [ ${#UNMATCHED_FILES[@]} -eq 0 ]; then |
| 131 | + ONLY_CHANGED="true" # All changed files matched |
| 132 | + else |
| 133 | + ONLY_CHANGED="false" # Some files didn't match |
| 134 | + fi |
| 135 | + NO_CHANGES="false" |
| 136 | + fi |
| 137 | +
|
| 138 | + # Convert arrays to JSON for output (handle empty arrays safely) |
| 139 | + if [ -z "$CHANGED_FILES" ]; then |
| 140 | + CHANGED_FILES_JSON="[]" |
| 141 | + else |
| 142 | + CHANGED_FILES_JSON=$(printf '%s\n' "$CHANGED_FILES" | jq -R . | jq -s .) |
| 143 | + fi |
| 144 | +
|
| 145 | + if [ ${#MATCHED_FILES[@]} -eq 0 ]; then |
| 146 | + MATCHED_FILES_JSON="[]" |
| 147 | + else |
| 148 | + MATCHED_FILES_JSON=$(printf '%s\n' "${MATCHED_FILES[@]}" | jq -R . | jq -s .) |
| 149 | + fi |
| 150 | +
|
| 151 | + if [ ${#UNMATCHED_FILES[@]} -eq 0 ]; then |
| 152 | + UNMATCHED_FILES_JSON="[]" |
| 153 | + else |
| 154 | + UNMATCHED_FILES_JSON=$(printf '%s\n' "${UNMATCHED_FILES[@]}" | jq -R . | jq -s .) |
| 155 | + fi |
| 156 | +
|
| 157 | + # Set outputs with proper escaping |
| 158 | + { |
| 159 | + echo "only_changed=$ONLY_CHANGED" |
| 160 | + echo "no_changes=$NO_CHANGES" |
| 161 | + echo "changed_files<<EOF" |
| 162 | + echo "$CHANGED_FILES_JSON" |
| 163 | + echo "EOF" |
| 164 | + echo "matched_files<<EOF" |
| 165 | + echo "$MATCHED_FILES_JSON" |
| 166 | + echo "EOF" |
| 167 | + echo "unmatched_files<<EOF" |
| 168 | + echo "$UNMATCHED_FILES_JSON" |
| 169 | + echo "EOF" |
| 170 | + } >> $GITHUB_OUTPUT |
| 171 | +
|
| 172 | + echo "Only changed matching files: $ONLY_CHANGED" |
| 173 | + echo "Matched files: ${MATCHED_FILES[*]}" |
| 174 | + echo "Unmatched files: ${UNMATCHED_FILES[*]}" |
0 commit comments