forked from PolicyEngine/policyengine-us
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
151 lines (119 loc) · 4.14 KB
/
test.py
File metadata and controls
151 lines (119 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""
Script to fetch all PRs to master branch for policyengine/policyengine-us
and pull the latest master branch.
"""
import subprocess
import sys
import json
from urllib.request import urlopen, Request
from urllib.error import HTTPError
def get_all_prs_to_master(
owner="policyengine", repo="policyengine-us", branch="master"
):
"""Fetch all PRs (open and closed) targeting the master branch."""
prs = []
page = 1
per_page = 100
print(f"Fetching PRs for {owner}/{repo} targeting {branch} branch...")
i = 0
while i < 2:
i += 1
# GitHub API endpoint for PRs
url = f"https://api.github.com/repos/{owner}/{repo}/pulls"
params = f"?state=all&base={branch}&per_page={per_page}&page={page}"
try:
# Create request with headers
req = Request(url + params)
req.add_header("Accept", "application/vnd.github.v3+json")
# Add User-Agent header to avoid rate limiting
req.add_header("User-Agent", "PR-Fetcher-Script")
# If you have a GitHub token, uncomment and add it here:
# req.add_header('Authorization', 'token YOUR_GITHUB_TOKEN')
response = urlopen(req)
data = json.loads(response.read().decode())
if not data:
break
prs.extend(data)
print(f" Fetched page {page} ({len(data)} PRs)")
page += 1
except HTTPError as e:
print(f"Error fetching PRs: {e}")
print(
"Note: GitHub API has rate limits. Consider using a personal access token."
)
break
return prs
def pull_latest_master():
"""Pull the latest master branch from origin."""
print("\nPulling latest master branch...")
try:
# Ensure we're on master branch
subprocess.run(
["git", "checkout", "master"],
check=True,
capture_output=True,
text=True,
)
# Pull latest changes
result = subprocess.run(
["git", "pull", "origin", "master"],
check=True,
capture_output=True,
text=True,
)
print("Successfully pulled latest master:")
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error pulling master: {e}")
print(f"Error output: {e.stderr}")
return False
return True
def display_pr_summary(prs):
"""Display a summary of the PRs."""
print(f"\nTotal PRs to master: {len(prs)}")
open_prs = [pr for pr in prs if pr["state"] == "open"]
closed_prs = [pr for pr in prs if pr["state"] == "closed"]
merged_prs = [pr for pr in closed_prs if pr.get("merged_at")]
print(f" Open PRs: {len(open_prs)}")
print(f" Closed PRs: {len(closed_prs)}")
print(f" Merged PRs: {len(merged_prs)}")
# Show recent PRs
print("\nMost recent 10 PRs:")
for pr in prs[:10]:
state = pr["state"]
if state == "closed" and pr.get("merged_at"):
state = "merged"
print(f" #{pr['number']}: {pr['title']} [{state}]")
print(f" Author: {pr['user']['login']}")
print(f" Created: {pr['created_at']}")
print()
def save_pr_data(prs, filename="policyengine_prs.json"):
"""Save PR data to a JSON file."""
with open(filename, "w") as f:
json.dump(prs, f, indent=2)
print(f"\nPR data saved to {filename}")
def main():
# Check if we're in a git repository
try:
subprocess.run(["git", "status"], check=True, capture_output=True)
except subprocess.CalledProcessError:
print("Error: Not in a git repository!")
print(
"Please run this script from within the policyengine-us repository."
)
sys.exit(1)
# Fetch all PRs
prs = get_all_prs_to_master()
if prs:
# Display summary
display_pr_summary(prs)
# Save PR data
save_pr_data(prs)
# Pull latest master
print("\n" + "=" * 50)
pull_latest_master()
else:
print("No PRs found or unable to fetch PRs.")
if __name__ == "__main__":
main()