|
| 1 | +name: Update Contributors |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 0 * * *' # Run daily at midnight UTC |
| 6 | + workflow_dispatch: # Allow manual trigger |
| 7 | + |
| 8 | +jobs: |
| 9 | + contributors: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + token: ${{ secrets.CONTRIBUTORS_TOKEN }} |
| 15 | + |
| 16 | + - name: Update contributors |
| 17 | + env: |
| 18 | + GH_TOKEN: ${{ secrets.CONTRIBUTORS_TOKEN }} |
| 19 | + run: | |
| 20 | + # Fetch code contributors (exclude bots) |
| 21 | + code_contributors=$(gh api repos/${{ github.repository }}/contributors --paginate --jq '.[] | select(.type != "Bot") | select(.login | test("\\[bot\\]$") | not) | .login') |
| 22 | +
|
| 23 | + # Fetch closed issues and check if they were closed by a PR |
| 24 | + issue_authors="" |
| 25 | + closed_issues=$(gh api repos/${{ github.repository }}/issues --paginate -q '.[] | select(.state == "closed") | select(.pull_request == null) | {number, login: .user.login}') |
| 26 | +
|
| 27 | + for row in $(echo "$closed_issues" | jq -c '.'); do |
| 28 | + issue_num=$(echo "$row" | jq -r '.number') |
| 29 | + login=$(echo "$row" | jq -r '.login') |
| 30 | +
|
| 31 | + # Check timeline for closed event with commit (meaning closed by PR) |
| 32 | + closed_by_pr=$(gh api repos/${{ github.repository }}/issues/$issue_num/timeline --paginate -q '.[] | select(.event == "closed") | select(.commit_id != null) | .commit_id' | head -1) |
| 33 | +
|
| 34 | + if [[ -n "$closed_by_pr" ]]; then |
| 35 | + issue_authors="$issue_authors$login"$'\n' |
| 36 | + fi |
| 37 | + done |
| 38 | + issue_authors=$(echo "$issue_authors" | sort -u) |
| 39 | +
|
| 40 | + # Combine and deduplicate |
| 41 | + all_contributors=$(echo -e "$code_contributors\n$issue_authors" | sort -u | grep -v '^$') |
| 42 | +
|
| 43 | + # Build markdown for each contributor |
| 44 | + contributor_md="" |
| 45 | + for login in $all_contributors; do |
| 46 | + # Skip bots |
| 47 | + if [[ "$login" == *"[bot]" ]]; then |
| 48 | + continue |
| 49 | + fi |
| 50 | +
|
| 51 | + # Get user info |
| 52 | + user_info=$(gh api users/$login --jq '{avatar_url, html_url}') |
| 53 | + avatar=$(echo "$user_info" | jq -r '.avatar_url') |
| 54 | + url=$(echo "$user_info" | jq -r '.html_url') |
| 55 | +
|
| 56 | + contributor_md="$contributor_md[]($url) " |
| 57 | + done |
| 58 | +
|
| 59 | + # Build the contributors section |
| 60 | + contrib_section="<!-- readme: contributors -start --> |
| 61 | + $contributor_md |
| 62 | + <!-- readme: contributors -end -->" |
| 63 | +
|
| 64 | + # Update README between the markers |
| 65 | + awk -v contrib="$contrib_section" ' |
| 66 | + /<!-- readme: contributors -start -->/{found=1; print contrib; next} |
| 67 | + /<!-- readme: contributors -end -->/{found=0; next} |
| 68 | + !found{print} |
| 69 | + ' README.md > README.tmp && mv README.tmp README.md |
| 70 | +
|
| 71 | + - name: Commit and push |
| 72 | + run: | |
| 73 | + git config user.name "github-actions[bot]" |
| 74 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 75 | + git add README.md |
| 76 | + git diff --staged --quiet || (git commit -m "docs: update contributors [skip ci]" && git push) |
0 commit comments