-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_tools.py
More file actions
35 lines (27 loc) · 944 Bytes
/
branch_tools.py
File metadata and controls
35 lines (27 loc) · 944 Bytes
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
import requests
from config import GITHUB_TOKEN, OWNER, REPO
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json"
}
def create_branch(branch_name):
# Get latest commit SHA from main
url = f"https://api.github.com/repos/{OWNER}/{REPO}/git/ref/heads/main"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to fetch main branch")
return None
sha = response.json()["object"]["sha"]
# Create new branch
data = {
"ref": f"refs/heads/{branch_name}",
"sha": sha
}
url = f"https://api.github.com/repos/{OWNER}/{REPO}/git/refs"
response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
print("Branch created:", branch_name)
return branch_name
else:
print("Branch creation failed:", response.json())
return None