-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add script to generate CONTRIBUTORS.md based on merged PRs
- Loading branch information
1 parent
01c2c28
commit 645f98f
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Contributors | ||
|
||
## Valued Contributor | ||
|
||
- ![yeisonvargasf Badge](https://img.shields.io/badge/yeisonvargasf-Valued%20Contributor-brightgreen) (46 merged PRs) | ||
|
||
- ![dylanpulver Badge](https://img.shields.io/badge/dylanpulver-Valued%20Contributor-brightgreen) (29 merged PRs) | ||
|
||
## First Contributor | ||
|
||
- ![cclauss Badge](https://img.shields.io/badge/cclauss-First%20Contributor-brightgreen) (2 merged PRs) | ||
|
||
- ![cb22 Badge](https://img.shields.io/badge/cb22-First%20Contributor-brightgreen) (2 merged PRs) | ||
|
||
- ![Zeckie Badge](https://img.shields.io/badge/Zeckie-First%20Contributor-brightgreen) (1 merged PRs) | ||
|
||
- ![mwermuth Badge](https://img.shields.io/badge/mwermuth-First%20Contributor-brightgreen) (1 merged PRs) | ||
|
||
- ![Jwomers Badge](https://img.shields.io/badge/Jwomers-First%20Contributor-brightgreen) (1 merged PRs) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import os | ||
import requests | ||
from collections import defaultdict | ||
|
||
# Repository details and GitHub token | ||
GITHUB_REPO = "pyupio/safety" | ||
GITHUB_TOKEN = os.getenv("YOUR_GITHUB_TOKEN") | ||
CONTRIBUTORS_FILE = "CONTRIBUTORS.md" | ||
|
||
# Tier thresholds | ||
TIERS = {"Valued Contributor": 10, "Frequent Contributor": 5, "First Contributor": 1} | ||
|
||
|
||
# API request to get merged PRs | ||
def get_merged_prs(): | ||
url = f"https://api.github.com/repos/{GITHUB_REPO}/pulls?state=closed&per_page=100" | ||
headers = {"Authorization": f"token {GITHUB_TOKEN}"} | ||
response = requests.get(url, headers=headers) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
# Count contributions for each user | ||
def count_contributions(prs): | ||
contributions = defaultdict(int) | ||
for pr in prs: | ||
if pr.get("merged_at"): | ||
user = pr["user"]["login"] | ||
contributions[user] += 1 | ||
return contributions | ||
|
||
|
||
# Categorize contributors by tier | ||
def categorize_contributors(contributions): | ||
tiers = {tier: [] for tier in TIERS} | ||
for user, count in contributions.items(): | ||
for tier, threshold in TIERS.items(): | ||
if count >= threshold: | ||
tiers[tier].append((user, count)) | ||
break | ||
return tiers | ||
|
||
|
||
# Generate Shieldify badge | ||
def generate_badge(user, tier): | ||
badge_url = f"https://img.shields.io/badge/{user.replace('-', '--')}-{tier.replace(' ', '%20')}-brightgreen" | ||
return f"![{user} Badge]({badge_url})" | ||
|
||
|
||
# Generate CONTRIBUTORS.md content | ||
def generate_contributors_md(tiers): | ||
lines = ["# Contributors\n"] | ||
for tier, contributors in tiers.items(): | ||
if contributors: | ||
lines.append(f"## {tier}\n") | ||
for user, count in sorted(contributors, key=lambda x: x[1], reverse=True): | ||
badge = generate_badge(user, tier) | ||
lines.append(f"- {badge} ({count} merged PRs)\n") | ||
return "\n".join(lines) | ||
|
||
|
||
# Write the CONTRIBUTORS.md file | ||
def write_contributors_file(content): | ||
with open(CONTRIBUTORS_FILE, "w") as file: | ||
file.write(content) | ||
|
||
|
||
def main(): | ||
prs = get_merged_prs() | ||
contributions = count_contributions(prs) | ||
tiers = categorize_contributors(contributions) | ||
content = generate_contributors_md(tiers) | ||
write_contributors_file(content) | ||
print(f"{CONTRIBUTORS_FILE} generated successfully.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |