|
3 | 3 | import boto3 |
4 | 4 | import json |
5 | 5 | import os |
6 | | -from datetime import datetime, timedelta |
| 6 | +from datetime import datetime, timedelta, timezone |
7 | 7 |
|
8 | 8 | # GitHub API details |
9 | 9 | GITHUB_API_URL = "https://api.github.com" |
@@ -49,27 +49,39 @@ def fetch_data(url): |
49 | 49 | else: |
50 | 50 | print(f"Failed to fecth data: {response.status_code}") |
51 | 51 | return None |
52 | | - return len(items) |
| 52 | + return items |
53 | 53 |
|
54 | 54 | def fetch_num_open_issues(): |
55 | 55 | url_issues = f"{GITHUB_API_URL}/repos/aws-actions/{REPO_NAME}/issues?state=open" |
56 | | - return fetch_data(url_issues) |
| 56 | + data = fetch_data(url_issues) |
| 57 | + return len(data) |
57 | 58 |
|
58 | 59 | def fetch_num_open_prs(): |
59 | 60 | url_prs = f"{GITHUB_API_URL}/repos/aws-actions/{REPO_NAME}/pulls?state=open" |
60 | | - return fetch_data(url_prs) |
| 61 | + data = fetch_data(url_prs) |
| 62 | + return len(data) |
61 | 63 |
|
62 | 64 | def fetch_num_closed_issues(): |
63 | 65 | url_closed_issues = f"{GITHUB_API_URL}/repos/aws-actions/{REPO_NAME}/issues?state=closed" |
64 | | - return fetch_data(url_closed_issues) |
| 66 | + data = fetch_data(url_closed_issues) |
| 67 | + return len(data) |
65 | 68 |
|
66 | 69 | def fetch_num_closed_prs_yesterday(): |
67 | | - today = datetime.utcnow().date() |
68 | | - yesterday = today - timedelta(days=1) |
69 | | - start_of_yesterday = datetime.combine(yesterday, datetime.min.time()) |
70 | | - end_of_yesterday = datetime.combine(yesterday, datetime.max.time()) |
71 | | - url_closed_prs_yesterday = f"{GITHUB_API_URL}/repos/aws-actions/{REPO_NAME}/pulls?state=closed&since={start_of_yesterday.isoformat()}&until={end_of_yesterday.isoformat()}" |
72 | | - return fetch_data(url_closed_prs_yesterday) |
| 70 | + url_closed_prs_yesterday = f"{GITHUB_API_URL}/repos/aws-actions/{REPO_NAME}/pulls?state=closed&sort=updated&direction=desc" |
| 71 | + data = fetch_data(url_closed_prs_yesterday) |
| 72 | + now = datetime.now(timezone.utc) |
| 73 | + yesterday = now - timedelta(days=1) |
| 74 | + |
| 75 | + merged_count = 0 |
| 76 | + for pr in data: |
| 77 | + if pr['merged_at']: |
| 78 | + merged_at = datetime.strptime(pr['merged_at'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) |
| 79 | + |
| 80 | + if merged_at >= yesterday: |
| 81 | + merged_count += 1 |
| 82 | + else: |
| 83 | + break |
| 84 | + return merged_count |
73 | 85 |
|
74 | 86 | def upload_metrics_to_cloudwatch(num_issues, num_prs_open, num_prs_closed_yesterday): |
75 | 87 |
|
|
0 commit comments