Update Contributor Highlights #14
This file contains 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 Contributor Highlights | |
on: | |
schedule: | |
- cron: "0 0 1 * *" # Every Month' | |
workflow_dispatch: | |
jobs: | |
update-highlights: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # All history | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.x" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install PyGithub | |
- name: Calculate highlights | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
python <<EOF | |
import os | |
from github import Github | |
from collections import defaultdict | |
import logging | |
EXCLUDED_USERS = ["statefb", "wadabee", "Yukinobu-Mine"] # Core maintainers | |
EXCLUDED_USERS.append("dependabot[bot]") # Dependabot | |
logging.basicConfig(level=logging.INFO) | |
def calculate_score(stats): | |
# Calculate score based on lines changed and number of PRs | |
WEIGHT_LINES_CHANGED = 0.7 | |
WEIGHT_PRS = 1000 | |
return stats["lines_changed"] * WEIGHT_LINES_CHANGED + stats["prs"] * WEIGHT_PRS | |
g = Github(os.environ['GITHUB_TOKEN']) | |
repo = g.get_repo('${{ github.repository }}') | |
contributors = defaultdict(lambda: {'lines_changed': 0, 'prs': 0}) | |
logging.info("Fetching commits...") | |
commits = repo.get_commits() | |
commit_count = 0 | |
for commit in commits: | |
if commit.author: | |
stats = commit.stats | |
contributors[commit.author.login]["lines_changed"] += ( | |
stats.additions + stats.deletions | |
) | |
commit_count += 1 | |
if commit_count % 100 == 0: | |
logging.info(f"Processed {commit_count} commits...") | |
logging.info("Fetching pull requests...") | |
prs = repo.get_pulls(state="closed", sort="created", direction="desc") | |
pr_count = 0 | |
for pr in prs: | |
if pr.merged and pr.user: | |
contributors[pr.user.login]["prs"] += 1 | |
pr_count += 1 | |
if pr_count % 10 == 0: | |
logging.info(f"Processed {pr_count} pull requests...") | |
logging.info(f"Total pull requests processed: {pr_count}") | |
logging.info(f"Total contributors: {len(contributors)}") | |
logging.info("Calculating contributor scores...") | |
highlighted_contributors = sorted( | |
contributors.items(), key=lambda x: calculate_score(x[1]), reverse=True | |
) | |
logging.info("Top 20 contributors:") | |
for username, stats in highlighted_contributors[:20]: | |
score = calculate_score(stats) | |
logging.info(f"{username}: {stats}, Score: {score}") | |
PLATINUM_THRESHOLD = 40000 | |
platinum_contributors = [ | |
(username, calculate_score(stats)) | |
for username, stats in highlighted_contributors | |
if calculate_score(stats) >= PLATINUM_THRESHOLD and username not in EXCLUDED_USERS | |
] | |
logging.info(f"Platinum contributors: {platinum_contributors}") | |
section_title = f"## 🏆 Significant Contributors\n\n" | |
platinum_md = section_title + "\n".join( | |
[ | |
f"- [{username}](https://github.com/{username})" | |
for username, score in platinum_contributors | |
] | |
) | |
readme_path = "README.md" | |
with open(readme_path, "r") as file: | |
readme_content = file.read() | |
start = readme_content.find(section_title) | |
end = readme_content.find("## Contributors") | |
# Insert the platinum contributors section between "## Contacts" and "## Contributors" | |
new_readme_content = ( | |
readme_content[:start] | |
+ platinum_md | |
+ "\n\n" | |
+ readme_content[end:] | |
) | |
with open(readme_path, "w") as file: | |
file.write(new_readme_content) | |
EOF | |
- name: Set git identity | |
run: |- | |
git config user.name "github-actions" | |
git config user.email "[email protected]" | |
- name: Create Pull Request | |
id: create-pr | |
uses: peter-evans/create-pull-request@b1ddad2c994a25fbc81a28b3ec0e368bb2021c50 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: |- | |
update(readme): update contributor highlights | |
[Workflow Run]: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
------ | |
*Automatically created by projen via the "upgrade-contributor-highlights" workflow* | |
branch: github-actions/update-contributor-highlights | |
title: "update(readme): update contributor highlights" | |
body: |- | |
update(readme): update contributor highlights | |
[Workflow Run]: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
------ | |
*Automatically created by projen via the "upgrade-contributor-highlights" workflow* | |
author: github-actions <[email protected]> | |
committer: github-actions <[email protected]> | |
signoff: true | |
base: v2 |