|
| 1 | +name: contributor automation |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: # runs daily at midnight |
| 5 | + - cron: '0 0 * * *' |
| 6 | + pull_request: |
| 7 | + types: [closed] |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | + # allows manual trigger |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + update-readme: |
| 17 | + if: github.event_name != 'pull_request' || github.event.pull_request.merged == true |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Fetch Merged PR Authors (Pagination) |
| 24 | + env: |
| 25 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | + PAGE=1 |
| 29 | + echo "[]" > final_list.json |
| 30 | + while true; do |
| 31 | + # Using pulls endpoint to capture only merged contributors |
| 32 | + RESPONSE=$(gh api "/repos/steam-bell-92/python-mini-project/pulls?state=closed&per_page=100&page=${PAGE}") |
| 33 | + COUNT=$(echo "$RESPONSE" | jq '. | length') |
| 34 | + if [ "$COUNT" -eq 0 ]; then break; fi |
| 35 | + jq -s '.[0] + .[1]' final_list.json <(echo "$RESPONSE") > tmp.json && mv tmp.json final_list.json |
| 36 | + PAGE=$((PAGE + 1)) |
| 37 | + done |
| 38 | + # Filters for actual merged PRs and unique users |
| 39 | + jq '[.[] |
| 40 | + | select(.merged_at != null and .user != null and .user.type != "Bot") |
| 41 | + | .user.login] |
| 42 | + | unique |
| 43 | + | sort' final_list.json > contributors.json |
| 44 | + - name: Build Gallery HTML |
| 45 | + run: | |
| 46 | + set -euo pipefail |
| 47 | + GALLERY_HTML="" |
| 48 | + for USERNAME in $(jq -r '.[]' contributors.json); do |
| 49 | + GALLERY_HTML="${GALLERY_HTML}<a href=\"https://github.com/${USERNAME}\"><img src=\"https://github.com/${USERNAME}.png\" width=\"50px\" loading=\"lazy\" title=\"${USERNAME}\" style=\"border-radius:50%;margin:5px;\" alt=\"${USERNAME}\" /></a>" |
| 50 | + done |
| 51 | + echo "$GALLERY_HTML" > gallery_fragment.txt |
| 52 | +
|
| 53 | + - name: Update README.md |
| 54 | + run: | |
| 55 | + set -euo pipefail |
| 56 | + # Safety check for markers |
| 57 | + start_count=$(grep -c '<!-- CONTRIBUTORS_START -->' README.md || true) |
| 58 | + end_count=$(grep -c '<!-- CONTRIBUTORS_END -->' README.md || true) |
| 59 | + if [ "$start_count" -ne 1 ] || [ "$end_count" -ne 1 ]; then |
| 60 | + echo "Error: README.md must contain exactly one pair of markers." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + start_line=$(grep -n '<!-- CONTRIBUTORS_START -->' README.md | cut -d: -f1) |
| 65 | + end_line=$(grep -n '<!-- CONTRIBUTORS_END -->' README.md | cut -d: -f1) |
| 66 | + if [ "$start_line" -ge "$end_line" ]; then |
| 67 | + echo "Error: CONTRIBUTORS_START must appear before CONTRIBUTORS_END." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + |
| 71 | + sed -i '/<!-- CONTRIBUTORS_START -->/,/<!-- CONTRIBUTORS_END -->/ { |
| 72 | + /<!-- CONTRIBUTORS_START -->/! { /<!-- CONTRIBUTORS_END -->/! d; } |
| 73 | + }' README.md |
| 74 | + sed -i '/<!-- CONTRIBUTORS_START -->/r gallery_fragment.txt' README.md |
| 75 | + |
| 76 | + - name: Commit and Push |
| 77 | + run: | |
| 78 | + git config --global user.name "github-actions[bot]" |
| 79 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 80 | + git add README.md |
| 81 | + if git diff --staged --quiet; then |
| 82 | + echo "No changes to README." |
| 83 | + else |
| 84 | + git commit -m "docs: update contributor gallery" |
| 85 | + git push |
| 86 | + fi |
0 commit comments