|
1 | | -from collections import defaultdict |
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +from datetime import datetime, timedelta, UTC |
| 6 | +from typing import Any |
2 | 7 | import requests |
3 | 8 |
|
4 | | -page = 1 |
| 9 | +API_URL = "https://api.github.com/graphql" |
5 | 10 |
|
6 | | -data = defaultdict(list) |
| 11 | +QUERY = """ |
| 12 | +query ($query: String!, $after: String) { |
| 13 | + search(type: ISSUE, query: $query, first: 100, after: $after) { |
| 14 | + pageInfo { hasNextPage endCursor } |
| 15 | + nodes { |
| 16 | + ... on PullRequest { |
| 17 | + title |
| 18 | + url |
| 19 | + author { login } |
| 20 | + mergedAt |
| 21 | + labels(first: 100) { |
| 22 | + nodes { name } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +""" |
7 | 29 |
|
8 | | -for page in range(1, 7): |
9 | | - r = requests.get( |
10 | | - "https://api.github.com/repos/DataDog/system-tests/pulls", |
11 | | - params={"state": "closed", "per_page": "100", "page": str(page)}, |
12 | | - timeout=10, |
| 30 | + |
| 31 | +def last_completed_month_range(month: int | None = None) -> tuple[str, str, int, int]: |
| 32 | + today = datetime.now(UTC).date() |
| 33 | + |
| 34 | + if month is not None: |
| 35 | + # Validate month |
| 36 | + if not 1 <= month <= 12: # noqa: PLR2004 |
| 37 | + raise ValueError("Month must be between 1 and 12") |
| 38 | + |
| 39 | + # Use the specified month for the current year |
| 40 | + target_date = today.replace(month=month, day=1) |
| 41 | + |
| 42 | + # If the specified month is in the future, use the previous year |
| 43 | + if target_date > today: |
| 44 | + target_date = target_date.replace(year=today.year - 1) |
| 45 | + |
| 46 | + # Get the last day of the specified month |
| 47 | + if month == 12: # noqa: PLR2004 |
| 48 | + next_month = target_date.replace(year=target_date.year + 1, month=1) |
| 49 | + else: |
| 50 | + next_month = target_date.replace(month=month + 1) |
| 51 | + |
| 52 | + last_day = next_month - timedelta(days=1) |
| 53 | + return target_date.isoformat(), last_day.isoformat(), target_date.year, target_date.month |
| 54 | + # Original logic for last completed month |
| 55 | + first_of_current = today.replace(day=1) |
| 56 | + last_of_prev = first_of_current - timedelta(days=1) |
| 57 | + first_of_prev = last_of_prev.replace(day=1) |
| 58 | + return first_of_prev.isoformat(), last_of_prev.isoformat(), first_of_prev.year, first_of_prev.month |
| 59 | + |
| 60 | + |
| 61 | +def gh_request(token: str, query: str, variables: dict[str, Any]) -> dict[str, Any]: |
| 62 | + r = requests.post( |
| 63 | + API_URL, |
| 64 | + headers={ |
| 65 | + "Authorization": f"Bearer {token}", |
| 66 | + "Accept": "application/vnd.github+json", |
| 67 | + }, |
| 68 | + json={"query": query, "variables": variables}, |
| 69 | + timeout=30, |
| 70 | + ) |
| 71 | + r.raise_for_status() |
| 72 | + j = r.json() |
| 73 | + if "errors" in j: |
| 74 | + raise RuntimeError(j["errors"]) |
| 75 | + return j["data"] |
| 76 | + |
| 77 | + |
| 78 | +def print_pr_data(month: int | None = None) -> None: |
| 79 | + token = os.environ.get("GITHUB_TOKEN") |
| 80 | + if not token: |
| 81 | + print("Please set GITHUB_TOKEN in your environment.") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + start_date, end_date, year, month = last_completed_month_range(month) |
| 85 | + q = f"repo:DataDog/system-tests is:pr merged:{start_date}..{end_date}" |
| 86 | + target_label = "build-python-base-images" |
| 87 | + |
| 88 | + pr_prints = [] |
| 89 | + n_prs = 0 |
| 90 | + |
| 91 | + after = None |
| 92 | + while True: |
| 93 | + data = gh_request(token, QUERY, {"query": q, "after": after}) |
| 94 | + |
| 95 | + for pr in data["search"]["nodes"]: |
| 96 | + n_prs += 1 |
| 97 | + labels = [labels["name"] for labels in pr["labels"]["nodes"]] |
| 98 | + # if True: |
| 99 | + if target_label in labels: |
| 100 | + merged_at = pr["mergedAt"][:10] |
| 101 | + title = pr["title"] |
| 102 | + url = pr["url"] |
| 103 | + author = pr["author"]["login"] |
| 104 | + |
| 105 | + pr_prints.append(f"* {merged_at} [{title}]({url}) by @{author}") |
| 106 | + |
| 107 | + if not data["search"]["pageInfo"]["hasNextPage"]: |
| 108 | + break |
| 109 | + after = data["search"]["pageInfo"]["endCursor"] |
| 110 | + |
| 111 | + print(f"### {year}-{month:02d} ({n_prs} PR merged)\n") |
| 112 | + for line in pr_prints: |
| 113 | + print(line) |
| 114 | + |
| 115 | + |
| 116 | +def main() -> None: |
| 117 | + parser = argparse.ArgumentParser( |
| 118 | + description="Generate changelog for system-tests repository", |
| 119 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 120 | + epilog=""" |
| 121 | +Examples: |
| 122 | + python get-change-log.py # Use last completed month |
| 123 | + python get-change-log.py --month 12 # Use December of current year |
| 124 | + python get-change-log.py --month 1 # Use January of current year |
| 125 | + """, |
| 126 | + ) |
| 127 | + |
| 128 | + parser.add_argument( |
| 129 | + "--month", |
| 130 | + type=int, |
| 131 | + choices=range(1, 13), |
| 132 | + help="Month to generate changelog for (1-12). If not specified, uses the last completed month.", |
13 | 133 | ) |
14 | 134 |
|
15 | | - for pr in r.json(): |
16 | | - if pr["merged_at"]: |
17 | | - data[pr["merged_at"][:7]].append(pr) |
| 135 | + args = parser.parse_args() |
| 136 | + print_pr_data(args.month) |
18 | 137 |
|
19 | | -for month in sorted(data, reverse=True): |
20 | | - prs = data[month] |
21 | 138 |
|
22 | | - print(f"\n\n### {month} ({len(prs)} PR merged)\n") |
23 | | - for pr in prs: |
24 | | - pr["merged_at"] = pr["merged_at"][:10] |
25 | | - pr["author"] = pr["user"]["login"] |
26 | | - print("* {merged_at} [{title}]({html_url}) by @{author}".format(**pr)) |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments