update-readme-contributors #71
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: update-readme-contributors | |
| on: | |
| # Run weekly on Sunday at 00:00 UTC | |
| schedule: | |
| - cron: '0 0 * * 0' | |
| # Run when changes are merged to main | |
| push: | |
| branches: | |
| - main | |
| paths-ignore: | |
| - 'ReadMe.md' # Skip when only the ReadMe.md changes to avoid infinite loops | |
| - '.github/workflows/update-readme-contributors.yml' # Skip when this workflow file changes | |
| # Allows manual triggering from the Actions tab | |
| workflow_dispatch: | |
| jobs: | |
| update-contributors: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout the repo | |
| uses: actions/checkout@v4 | |
| with: | |
| # Fetch with token that has write access to the repo | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Update ReadMe with Contributors | |
| id: update-readme | |
| shell: pwsh | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| $ErrorActionPreference = 'Stop' | |
| try { | |
| Write-Host 'Resolve owner/repo and API base' | |
| $owner, $repo = $env:GITHUB_REPOSITORY -split '/' | |
| $apiBase = if ($env:GITHUB_API_URL) { $env:GITHUB_API_URL } else { 'https://api.github.com' } | |
| Write-Host 'Prepare headers' | |
| $headers = @{ | |
| Authorization = "Bearer $env:GITHUB_TOKEN" | |
| 'User-Agent' = $env:GITHUB_REPOSITORY | |
| 'X-GitHub-Api-Version' = '2022-11-28' | |
| Accept = 'application/vnd.github+json' | |
| } | |
| Write-Host 'Fetch contributors with simple pagination' | |
| $contributors = @() | |
| $page = 1 | |
| $perPage = 100 | |
| do { | |
| $url = "$apiBase/repos/$owner/$repo/contributors?per_page=$perPage&page=$page" | |
| Write-Host "Fetching contributors page $page..." | |
| $resp = Invoke-RestMethod -Headers $headers -Method GET -Uri $url | |
| if ($null -eq $resp) { $resp = @() } | |
| $contributors += $resp | |
| $count = @($resp).Count | |
| $page++ | |
| } while ($count -eq $perPage) | |
| Write-Host ("Found {0} contributors (including bots)." -f $contributors.Count) | |
| if ($contributors.Count -eq 0) { | |
| Write-Host 'No contributors found. Skipping update.' | |
| 'ReadMeUpdated=false' | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| exit 0 | |
| } | |
| Write-Host 'Filter out bots' | |
| $filtered = $contributors | Where-Object { | |
| $_.login -ne 'github-actions[bot]' -and | |
| $_.login -ne 'dependabot[bot]' -and | |
| $_.login -ne 'Copilot' | |
| } | |
| Write-Host 'Reading ReadMe' | |
| $readmePath = Join-Path $env:GITHUB_WORKSPACE 'ReadMe.md' | |
| $readmeContent = Get-Content -Path $readmePath -Raw | |
| $originalReadMeContent = $readmeContent | |
| Write-Host 'Building contributors section' | |
| $contributorsSection = @() | |
| $contributorsSection += '## 👥 Contributors' | |
| $contributorsSection += '' | |
| $contributorsSection += 'Thanks to these wonderful people who have contributed to tiPS:' | |
| $contributorsSection += '' | |
| $contributorsSection += '<p align="center">' | |
| foreach ($c in $filtered) { | |
| $contributorsSection += ' <a href="' + $($c.html_url) + '" title="' + $($c.login) + '"><img src="' + $($c.avatar_url) + '" width="50" height="50" style="border-radius:50%;margin:5px;" alt="' + $($c.login) + '"></a>' | |
| } | |
| $contributorsSection += '</p>' | |
| $contributorsSection += '' | |
| $contributorsSectionText = ($contributorsSection -join "`n") + "`n" | |
| Write-Host 'Update or insert section' | |
| if ($readmeContent -match '## 👥 Contributors') { | |
| $pattern = '## 👥 Contributors[\s\S]*?(?=## 🛣️ Roadmap)' | |
| $readmeContent = [regex]::Replace($readmeContent, $pattern, $contributorsSectionText) | |
| } else { | |
| if ($readmeContent -match '## 🛣️ Roadmap') { | |
| $readmeContent = [regex]::Replace($readmeContent, '(?ms)(?=^## 🛣️ Roadmap)', $contributorsSectionText) | |
| } else { | |
| $readmeContent = $readmeContent + "`n`n" + $contributorsSectionText | |
| } | |
| } | |
| if ($readmeContent -eq $originalReadMeContent) { | |
| Write-Host 'Contributors section is already up to date. No changes needed.' | |
| 'ReadMeUpdated=false' | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| exit 0 | |
| } | |
| Write-Host 'Write updated ReadMe' | |
| Set-Content -Path $readmePath -Value $readmeContent -Encoding utf8 | |
| Write-Host 'ReadMe updated with contributors.' | |
| Write-Host 'Set output for next step' | |
| 'ReadMeUpdated=true' | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| } catch { | |
| Write-Error ("Failed to update ReadMe: {0}" -f $_.Exception.Message) | |
| 'ReadMeUpdated=false' | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append | |
| exit 1 | |
| } | |
| - name: Create Pull Request if ReadMe was updated | |
| if: steps.update-readme.outputs.ReadMeUpdated == 'true' | |
| uses: peter-evans/create-pull-request@v5 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "docs: update contributors in ReadMe.md [skip ci]" | |
| title: "docs: Update contributors in ReadMe.md" | |
| body: | | |
| This PR updates the ReadMe.md file with the latest contributors to the project. | |
| *This is an automated PR created by the update-readme-contributors workflow.* | |
| branch: auto-update-readme-contributors | |
| branch-suffix: timestamp | |
| delete-branch: true | |
| base: main |