-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmerge_all_prs.py
More file actions
79 lines (60 loc) · 2.16 KB
/
merge_all_prs.py
File metadata and controls
79 lines (60 loc) · 2.16 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
import os
import subprocess
import requests
from dotenv import load_dotenv
load_dotenv()
# GitHub credentials
username = "aronchick"
# GitHub personal access token from the GH_TOKEN_SECRET environment variable
token = os.environ.get("GH_TOKEN_SECRET")
if token is None:
raise ValueError("GH_TOKEN_SECRET environment variable not set")
# Repository information
repo_owner = "mlspec"
repo_name = "mlspec-lib"
# PR number to merge all open PRs into
pr_to_merge_into = "dependabot/pip/jupyter-lsp-2.2.2"
# PR number to exclude from merging
exclude_pr_number = "49"
# Function to merge a branch into a PR locally
def merge_branch_into_pr(branch_name, pr_number):
# Checkout the branch locally
subprocess.run(["git", "checkout", branch_name])
# Merge the branch into the specified PR locally
subprocess.run(["git", "fetch", "origin", branch_name])
# Merge the branch into the specified PR locally with a commit message and no confirmation
subprocess.run(
[
"git",
"merge",
f"origin/{branch_name}",
"--no-ff",
"-m",
f"Merging {branch_name} into {pr_to_merge_into}",
]
)
# Commit the changes
subprocess.run(
["git", "commit", "-m", f"Merging {branch_name} into {pr_to_merge_into}"]
)
# Push the changes to the remote branch
subprocess.run(["git", "push", "origin", branch_name])
# Delete the branch locally and remotely
subprocess.run(["git", "branch", "-d", branch_name])
# Delete the branch remotely
subprocess.run(["git", "push", "origin", "--delete", branch_name])
# Get list of open PRs
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls"
params = {"state": "open"}
headers = {"Authorization": f"token {token}"}
response = requests.get(url, headers=headers, params=params)
open_prs = response.json()
# Exclude the most recent PR
if open_prs:
exclude_pr_number = open_prs[0]["number"]
# Merge each branch into the specified PR locally
for pr in open_prs:
pr_number = pr["number"]
if pr_number != exclude_pr_number:
branch_name = pr["head"]["ref"]
merge_branch_into_pr(branch_name, pr_number)