|
| 1 | +name: Credit Reward Automation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + call-webhook: |
| 9 | + if: > |
| 10 | + github.event.pull_request.merged == true && |
| 11 | + contains(github.event.pull_request.labels.*.name, 'contribution') |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Debug context |
| 15 | + run: | |
| 16 | + echo "merged=${{ github.event.pull_request.merged }}" |
| 17 | + echo "labels=${{ join(github.event.pull_request.labels.*.name, ',') }}" |
| 18 | + echo "base=${{ github.event.pull_request.base.ref }}" |
| 19 | + echo "head=${{ github.event.pull_request.head.ref }}" |
| 20 | + echo "merge_commit_sha=${{ github.event.pull_request.merge_commit_sha }}" |
| 21 | +
|
| 22 | + - name: Checkout repo (full history) |
| 23 | + uses: actions/checkout@v5 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Determine first commit of PR branch |
| 28 | + id: first_commit |
| 29 | + shell: bash |
| 30 | + run: | |
| 31 | + set -euo pipefail |
| 32 | + pr="${{ github.event.pull_request.number }}" |
| 33 | + base_sha="${{ github.event.pull_request.base.sha }}" |
| 34 | +
|
| 35 | + # 1) Ensure both sides exist locally |
| 36 | + git fetch --no-tags origin "refs/pull/${pr}/head:pr-${pr}" |
| 37 | + git fetch --no-tags origin "${base_sha}" |
| 38 | +
|
| 39 | + # 2) Commits introduced by the PR, taking base commit as a reference |
| 40 | + first_sha="$(git rev-list --reverse --no-merges "${base_sha}..pr-${pr}" | head -n1 || true)" |
| 41 | +
|
| 42 | + # Fallback for whenever the range is empty (rarely happens) |
| 43 | + if [ -z "${first_sha}" ]; then |
| 44 | + mb="$(git merge-base "${base_sha}" "pr-${pr}")" |
| 45 | + first_sha="$(git rev-list --reverse "${mb}..pr-${pr}" | head -n1)" |
| 46 | + fi |
| 47 | +
|
| 48 | + # 3) Email address of first commit author (fallback on committer) |
| 49 | + author_email="$(git show -s --format='%ae' "${first_sha}")" |
| 50 | + committer_email="$(git show -s --format='%ce' "${first_sha}")" |
| 51 | + email="${author_email:-$committer_email}" |
| 52 | +
|
| 53 | + echo "sha=${first_sha}" >> "$GITHUB_OUTPUT" |
| 54 | + echo "email=${email}" >> "$GITHUB_OUTPUT" |
| 55 | +
|
| 56 | + - name: Compute PR diff stats (files/lines) |
| 57 | + id: pr_stats |
| 58 | + shell: bash |
| 59 | + run: | |
| 60 | + set -euo pipefail |
| 61 | + pr="${{ github.event.pull_request.number }}" |
| 62 | + base_sha="${{ github.event.pull_request.base.sha }}" |
| 63 | +
|
| 64 | + # Make sure refs exist (already done earlier, but harmless if repeated) |
| 65 | + git fetch --no-tags origin "refs/pull/${pr}/head:pr-${pr}" || true |
| 66 | + git fetch --no-tags origin "${base_sha}" || true |
| 67 | +
|
| 68 | + # Per-file additions/deletions; binary files show '-' → treat as 0 |
| 69 | + stats="$(git diff --numstat "${base_sha}..pr-${pr}" || true)" |
| 70 | +
|
| 71 | + files_changed=$(printf "%s\n" "$stats" | awk 'NF{c++} END{print c+0}') |
| 72 | + lines_added=$(printf "%s\n" "$stats" | awk '{a+=($1 ~ /^[0-9]+$/ ? $1 : 0)} END{print a+0}') |
| 73 | + lines_deleted=$(printf "%s\n" "$stats" | awk '{d+=($2 ~ /^[0-9]+$/ ? $2 : 0)} END{print d+0}') |
| 74 | + lines_changed=$((lines_added + lines_deleted)) |
| 75 | +
|
| 76 | + # Count files added and files deleted (A/D); renames (R) are excluded |
| 77 | + files_added=$(git diff --diff-filter=A --name-only "${base_sha}..pr-${pr}" | sed '/^$/d' | wc -l | tr -d ' ') |
| 78 | + files_deleted=$(git diff --diff-filter=D --name-only "${base_sha}..pr-${pr}" | sed '/^$/d' | wc -l | tr -d ' ') |
| 79 | +
|
| 80 | + { |
| 81 | + echo "lines_added=${lines_added}" |
| 82 | + echo "lines_deleted=${lines_deleted}" |
| 83 | + echo "lines_changed=${lines_changed}" |
| 84 | + echo "files_changed=${files_changed}" |
| 85 | + echo "files_added=${files_added}" |
| 86 | + echo "files_deleted=${files_deleted}" |
| 87 | + } >> "$GITHUB_OUTPUT" |
| 88 | +
|
| 89 | + - name: Call webhook |
| 90 | + run: | |
| 91 | + curl -X POST "https://hooks.zapier.com/hooks/catch/1615118/uh44k4y/" \ |
| 92 | + -H "Content-Type: application/json" \ |
| 93 | + -d '{ |
| 94 | + "pr_number": "${{ github.event.pull_request.number }}", |
| 95 | + "title": "${{ github.event.pull_request.title }}", |
| 96 | + "base_branch": "${{ github.event.pull_request.base.ref }}", |
| 97 | + "contributor_login": "${{ github.event.pull_request.user.login }}", |
| 98 | + "contributor_email": "${{ steps.first_commit.outputs.email }}", |
| 99 | + "files_changed": "${{ steps.pr_stats.outputs.files_changed }}", |
| 100 | + "files_added": "${{ steps.pr_stats.outputs.files_added }}", |
| 101 | + "files_deleted": "${{ steps.pr_stats.outputs.files_deleted }}", |
| 102 | + "lines_added": "${{ steps.pr_stats.outputs.lines_added }}", |
| 103 | + "lines_deleted": "${{ steps.pr_stats.outputs.lines_deleted }}", |
| 104 | + "lines_changed": "${{ steps.pr_stats.outputs.lines_changed }}" |
| 105 | + }' |
0 commit comments