-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_handler.py
More file actions
127 lines (104 loc) · 5.05 KB
/
github_handler.py
File metadata and controls
127 lines (104 loc) · 5.05 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
import os
import time
from github import Github, GithubException, BadCredentialsException, InputGitTreeElement
# Initialize the GitHub client using the token from environment variables
g = Github(os.getenv("GITHUB_TOKEN"))
github_user = g.get_user()
MIT_LICENSE = """
MIT License
Copyright (c) 2025 Your Name
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The foregoing copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
def create_or_get_repo(repo_name):
"""Creates a new public GitHub repo or gets it if it already exists for Round 2."""
try:
print(f"Creating new repository: {repo_name}")
repo = github_user.create_repo(repo_name, private=False)
time.sleep(2) # Brief pause to ensure repo is ready
return repo
except GithubException as e:
if e.status == 422: # Repository already exists
print(f"Repository {repo_name} already exists. Fetching it.")
return g.get_repo(f"{github_user.login}/{repo_name}")
else:
raise e
def get_file_content(repo, file_path):
"""Gets the content of a file from a repo, used for Round 2 revisions."""
try:
file_content = repo.get_contents(file_path, ref=repo.default_branch)
return file_content.decoded_content.decode('utf-8')
except GithubException:
return None # File not found
def update_repo_files(repo, files_to_commit, round_num):
"""Creates or updates files in the repo and returns the commit SHA."""
commit_message = f"feat: Round {round_num} project setup"
if round_num > 1:
commit_message = f"feat: Round {round_num} revision based on new brief"
# For Round 2+, update files using the Git Trees API
try:
# Check if repo is not empty to get the latest commit
repo.get_contents("/")
main_ref = repo.get_git_ref(f'heads/{repo.default_branch}')
latest_commit = repo.get_git_commit(main_ref.object.sha)
base_tree = repo.get_git_tree(latest_commit.sha)
# *** THIS IS THE CORRECTED PART ***
# Create a list of InputGitTreeElement objects
element_list = [
InputGitTreeElement(path, '100644', 'blob', content=content)
for path, content in files_to_commit.items()
]
# Create the new tree
tree = repo.create_git_tree(element_list, base_tree)
parent = latest_commit
commit = repo.create_git_commit(commit_message, tree, [parent])
main_ref.edit(commit.sha)
final_commit_sha = commit.sha
except GithubException: # Repo is empty (Round 1)
for path, content in files_to_commit.items():
repo.create_file(path, f"init: create {path}", content)
commit = repo.get_commits().get_page(0)[0]
final_commit_sha = commit.sha
print(f"Successfully committed changes. SHA: {final_commit_sha}")
return final_commit_sha
def enable_github_pages(repo):
"""Enables GitHub Pages for the repository and returns the URL."""
try:
source = {"source": {"branch": repo.default_branch, "path": "/"}}
headers = {'Accept': 'application/vnd.github.v3+json'}
repo._requester.requestJsonAndCheck("POST", repo.url + "/pages", input=source, headers=headers)
print("GitHub Pages site created. It may take a minute to deploy.")
except GithubException as e:
if e.status == 409:
print("GitHub Pages is already enabled.")
else:
print(f"An unexpected error occurred while enabling GitHub Pages: {e}")
raise e
pages_url = f"https://{github_user.login}.github.io/{repo.name}/"
return pages_url
def deploy_project(task_id, files, round_num):
"""Full workflow: create repo, push files, enable pages, and return details."""
repo = create_or_get_repo(task_id)
files['LICENSE'] = MIT_LICENSE.replace("Your Name", github_user.name or github_user.login)
commit_sha = update_repo_files(repo, files, round_num)
pages_url = enable_github_pages(repo)
print("Waiting 30 seconds for GitHub Pages to build...")
time.sleep(30)
return {
"repo_url": repo.html_url,
"commit_sha": commit_sha,
"pages_url": pages_url
}