|
| 1 | +name: Codacy Variation Fallback |
| 2 | + |
| 3 | +# This workflow injects a success check on 'Codacy Coverage Variation' when the check is not reported by Codacy, |
| 4 | +# so it can avoid bloking auto-merging on Pull Requests that has a check success requirement on it. |
| 5 | + |
| 6 | +# Examples where 'Codacy Coverage Variation' is not reported by Codacy: |
| 7 | +# - Modification on just a test class, and your test classes are ignored at 'codacy.yml'. |
| 8 | +# - Modification of 'README.md', which has no coverage. |
| 9 | +# - Modification of a GitHub Action workflow, which has no coverage. |
| 10 | + |
| 11 | +# In those cases, this workflow will bypass the 'Codacy Coverage Variation' check, allowing the PR to auto-merge. |
| 12 | + |
| 13 | +on: |
| 14 | + pull_request: |
| 15 | + branches: [ "master", "main" ] |
| 16 | + |
| 17 | +permissions: |
| 18 | + checks: write |
| 19 | + contents: read |
| 20 | + |
| 21 | +jobs: |
| 22 | + unblock_codacy_variation: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + |
| 26 | + # Checkout Code to obtain 'codacy.yml' configuration |
| 27 | + - name: Checkout Code |
| 28 | + uses: actions/checkout@v6 |
| 29 | + |
| 30 | + # Parse 'codacy.yml' exclusions |
| 31 | + - name: Parse codacy.yml exclusions |
| 32 | + id: parse-codacy-exclusions |
| 33 | + run: | |
| 34 | + |
| 35 | + if [ ! -f codacy.yml ]; then |
| 36 | + echo "No codacy.yml found, defaulting to basic ignores." |
| 37 | + echo "filters=**" >> $GITHUB_OUTPUT |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + # Extract excluded paths and add the 'codacy.yml' file itself |
| 42 | + EXCLUDES=$(yq '.exclude_paths[]' codacy.yml) |
| 43 | + STATIC_IGNORES=$'.github/**\ncodacy.yml' |
| 44 | + |
| 45 | + |
| 46 | + # Build a paths-filter pattern |
| 47 | + PATTERNS="**" |
| 48 | + |
| 49 | + # Prexies with '!' for every exclusion in the file. |
| 50 | + add_bang() { |
| 51 | + while IFS= read -r line; do |
| 52 | + if [[ -n "$line" ]]; then |
| 53 | + PATTERNS="$PATTERNS"$'\n'"!$line" |
| 54 | + fi |
| 55 | + done <<< "$1" |
| 56 | + } |
| 57 | +
|
| 58 | + add_bang "$EXCLUDES" |
| 59 | + add_bang "$STATIC_IGNORES" |
| 60 | +
|
| 61 | + # Save the result on a variable |
| 62 | + { |
| 63 | + echo 'filters<<EOF' |
| 64 | + echo "$PATTERNS" |
| 65 | + echo 'EOF' |
| 66 | + } >> $GITHUB_OUTPUT |
| 67 | +
|
| 68 | + # Display the result for debugging |
| 69 | + echo "Generated Filters:" |
| 70 | + echo "$PATTERNS" |
| 71 | + |
| 72 | + # Verify if there are changes on relevant files |
| 73 | + # relevant_files will be: |
| 74 | + # - 'true' when there is at least one file that will trigger the 'Codacy Coverage Variation' check |
| 75 | + # - will be false otherwise. |
| 76 | + - uses: dorny/paths-filter@v3 |
| 77 | + id: changes |
| 78 | + with: |
| 79 | + filters: | |
| 80 | + relevant_files: |
| 81 | + ${{ steps.config.outputs.filters }} |
| 82 | +
|
| 83 | + # Bypass the 'Codacy Coverage Variation' when there are no relevant files able to trigger it. |
| 84 | + - name: Create Success Check for Variation |
| 85 | + if: steps.changes.outputs.relevant_files == 'false' |
| 86 | + uses: actions/github-script@v7 |
| 87 | + with: |
| 88 | + script: | |
| 89 | + const checkName = 'Codacy Coverage Variation'; |
| 90 | + console.log(`Reporting manual success for: ${checkName}`); |
| 91 | + |
| 92 | + await github.rest.checks.create({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + name: checkName, |
| 96 | + head_sha: context.payload.pull_request.head.sha, |
| 97 | + status: 'completed', |
| 98 | + conclusion: 'success', |
| 99 | + output: { |
| 100 | + title: 'Skipped by Dynamic Exclusion', |
| 101 | + summary: 'All modified files workflows or they are excluded by codacy.yml. Reported bypassed success check.' |
| 102 | + } |
| 103 | + }); |
0 commit comments