Update Contributors #5
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 Contributors | |
| on: | |
| schedule: | |
| - cron: '0 0 * * 0' # Runs weekly on Sunday at midnight UTC | |
| workflow_dispatch: # Allows manual trigger | |
| jobs: | |
| update-contributors: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history to access commit details | |
| - name: Log checkout completion | |
| run: echo "Checked out repository at $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| - name: Get Contributors, Lines Added, and Commits | |
| id: contributors | |
| run: | | |
| echo "Starting contributor data collection at $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| # Initialize output file for contributor stats | |
| echo "{}" > contributors.json | |
| commit_count=0 | |
| # Switch to dev branch | |
| git checkout dev | |
| # Get all commits in dev branch | |
| commits=$(git log --pretty=format:%H) | |
| echo "Total commits to process: $(echo "$commits" | wc -l)" | |
| for commit in $commits; do | |
| ((commit_count++)) | |
| echo "Processing commit $commit ($commit_count)" | |
| author_email=$(git show -s --format=%ae $commit) | |
| login=$(echo "$author_email" | sed 's/@.*//') | |
| if [ -z "$login" ]; then | |
| echo "Skipping commit $commit with no author email" | |
| continue | |
| fi | |
| stats=$(git show --shortstat --format= $commit | grep -E '[0-9]+ file(s)? changed' | awk '{add=$4; del=$6} END {print (add ? add : 0) - (del ? del : 0)}') | |
| net_lines=${stats:-0} | |
| echo "Commit $commit by $login: $net_lines net lines" | |
| # Update JSON with contributor stats | |
| jq --arg login "$login" --arg net_lines "$net_lines" \ | |
| '.[$login] |= (. // {netLines: 0, commits: 0, login: $login} | .netLines += ($net_lines | tonumber) | .commits += 1)' \ | |
| contributors.json > tmp.json && mv tmp.json contributors.json | |
| done | |
| echo "Total commits processed: $commit_count" | |
| # Format table | |
| table=$(jq -r 'to_entries | sort_by(.value.netLines) | reverse | .[] | select(.value.netLines > 0 or .value.commits > 0) | "| [\(.key)](https://github.com/\(.key)) | \(.value.netLines) | \(.value.commits) |"' contributors.json) | |
| if [ -z "$table" ]; then | |
| table="No contributors with commits or net lines added." | |
| else | |
| table="| Contributor | Net Lines Added | Commits |\n|-------------|-----------------|---------|\n$table" | |
| fi | |
| echo "Total contributors: $(jq 'length' contributors.json)" | |
| echo "Generated table:" | |
| echo "$table" | |
| echo "$table" > contributors.txt | |
| shell: bash | |
| - name: Update README | |
| run: | | |
| echo "Starting README update at $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| if [ ! -s contributors.txt ] || [ "$(cat contributors.txt)" = "No contributors with commits or net lines added." ]; then | |
| echo "No contributors with commits or net lines added. Skipping README update." | |
| exit 0 | |
| fi | |
| if grep -q "## Contributors" README.md; then | |
| echo "Found existing ## Contributors section. Replacing content." | |
| sed -i '/## Contributors/,/##/c\## Contributors\n\n'"$(cat contributors.txt)"'\n\n##' README.md | |
| else | |
| echo "No ## Contributors section found. Appending to README." | |
| echo -e "\n## Contributors\n\n$(cat contributors.txt)\n" >> README.md | |
| fi | |
| echo "Updated README content. Checking for changes." | |
| git config user.name "GitHub Action" | |
| git config user.email "[email protected]" | |
| git add README.md | |
| if git diff-index --quiet HEAD; then | |
| echo "No changes to commit. Exiting." | |
| exit 0 | |
| fi | |
| echo "Committing changes to README at $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| git commit -m "Update contributors table with net lines and commits" | |
| git push | |
| echo "Pushed updated README to dev branch." | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.CUSTOM_GITHUB_TOKEN }} |