|
| 1 | +""" |
| 2 | +This script fetches open issues from the microsoft/vscode-python repository, |
| 3 | +calculates the thumbs-up per day for each issue, and generates a markdown |
| 4 | +summary of the issues sorted by highest thumbs-up per day. Issues with zero |
| 5 | +thumbs-up are excluded from the summary. |
| 6 | +""" |
| 7 | + |
| 8 | +import requests |
| 9 | +import os |
| 10 | +from datetime import datetime, timezone |
| 11 | + |
| 12 | + |
| 13 | +GITHUB_API_URL = "https://api.github.com" |
| 14 | +REPO = "microsoft/vscode-python" |
| 15 | +TOKEN = os.getenv("GITHUB_TOKEN") |
| 16 | + |
| 17 | + |
| 18 | +def fetch_issues(): |
| 19 | + """ |
| 20 | + Fetches all open issues from the specified GitHub repository. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + list: A list of dictionaries representing the issues. |
| 24 | + """ |
| 25 | + headers = {"Authorization": f"token {TOKEN}"} |
| 26 | + issues = [] |
| 27 | + page = 1 |
| 28 | + while True: |
| 29 | + query = ( |
| 30 | + f"{GITHUB_API_URL}/repos/{REPO}/issues?state=open&per_page=25&page={page}" |
| 31 | + ) |
| 32 | + response = requests.get(query, headers=headers) |
| 33 | + if response.status_code == 403: |
| 34 | + raise Exception( |
| 35 | + "Access forbidden: Check your GitHub token and permissions." |
| 36 | + ) |
| 37 | + response.raise_for_status() |
| 38 | + page_issues = response.json() |
| 39 | + if not page_issues: |
| 40 | + break |
| 41 | + issues.extend(page_issues) |
| 42 | + page += 1 |
| 43 | + return issues |
| 44 | + |
| 45 | + |
| 46 | +def calculate_thumbs_up_per_day(issue): |
| 47 | + """ |
| 48 | + Calculates the thumbs-up per day for a given issue. |
| 49 | +
|
| 50 | + Args: |
| 51 | + issue (dict): A dictionary representing the issue. |
| 52 | +
|
| 53 | + Returns: |
| 54 | + float: The thumbs-up per day for the issue. |
| 55 | + """ |
| 56 | + created_at = datetime.strptime(issue["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace( |
| 57 | + tzinfo=timezone.utc |
| 58 | + ) |
| 59 | + now = datetime.now(timezone.utc) |
| 60 | + days_open = (now - created_at).days or 1 |
| 61 | + thumbs_up = issue["reactions"].get("+1", 0) |
| 62 | + return thumbs_up / days_open |
| 63 | + |
| 64 | + |
| 65 | +def generate_markdown_summary(issues): |
| 66 | + """ |
| 67 | + Generates a markdown summary of the issues. |
| 68 | +
|
| 69 | + Args: |
| 70 | + issues (list): A list of dictionaries representing the issues. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + str: A markdown-formatted string summarizing the issues. |
| 74 | + """ |
| 75 | + summary = "| URL | Title | 👍 | Days Open | 👍/day |\n| --- | ----- | --- | --------- | ------ |\n" |
| 76 | + issues_with_thumbs_up = [] |
| 77 | + for issue in issues: |
| 78 | + created_at = datetime.strptime( |
| 79 | + issue["created_at"], "%Y-%m-%dT%H:%M:%SZ" |
| 80 | + ).replace(tzinfo=timezone.utc) |
| 81 | + now = datetime.now(timezone.utc) |
| 82 | + days_open = (now - created_at).days or 1 |
| 83 | + thumbs_up = issue["reactions"].get("+1", 0) |
| 84 | + if thumbs_up > 0: |
| 85 | + thumbs_up_per_day = thumbs_up / days_open |
| 86 | + issues_with_thumbs_up.append( |
| 87 | + (issue, thumbs_up, days_open, thumbs_up_per_day) |
| 88 | + ) |
| 89 | + |
| 90 | + # Sort issues by thumbs_up_per_day in descending order |
| 91 | + issues_with_thumbs_up.sort(key=lambda x: x[3], reverse=True) |
| 92 | + |
| 93 | + for issue, thumbs_up, days_open, thumbs_up_per_day in issues_with_thumbs_up: |
| 94 | + summary += f"| {issue['html_url']} | {issue['title']} | {thumbs_up} | {days_open} | {thumbs_up_per_day:.2f} |\n" |
| 95 | + |
| 96 | + return summary |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + """ |
| 101 | + Main function to fetch issues, generate the markdown summary, and write it to a file. |
| 102 | + """ |
| 103 | + issues = fetch_issues() |
| 104 | + summary = generate_markdown_summary(issues) |
| 105 | + with open("endorsement_velocity_summary.md", "w") as f: |
| 106 | + f.write(summary) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments