-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_client.py
More file actions
70 lines (59 loc) · 2.58 KB
/
Copy pathgithub_client.py
File metadata and controls
70 lines (59 loc) · 2.58 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
import os
import re
import time
from github import Auth
from github.Auth import Token
from github.MainClass import Github
from dotenv import load_dotenv
load_dotenv()
GITHUB_TOKEN=os.getenv('GITHUB_TOKEN')
if GITHUB_TOKEN is None:
raise RuntimeError("GITHUB_TOKEN environment variable is not set")
auth = Auth.Token(GITHUB_TOKEN)
gh = Github(auth=auth)
def fetch_file(repo_full_name: str, file_path: str, branch: str = "main") -> tuple[str, str]:
repo = gh.get_repo(repo_full_name)
contents = repo.get_contents(file_path, ref=branch)
file_content = contents.decoded_content.decode("utf-8") # type: ignore[reportCallIssue]
if file_content is None:
raise ValueError()
print(file_content[:20])
return file_content, contents.sha # type: ignore[reportCallIssue]
def create_branch(repo_full_name: str, base_branch: str = "main") -> str:
repo = gh.get_repo(repo_full_name)
base_ref = repo.get_git_ref(f"heads/{base_branch}")
new_branch_name = f"agent/{time.time()}"
repo.create_git_ref(ref=f"refs/heads/{new_branch_name}", sha=base_ref.object.sha)
return new_branch_name
def parse_coder_output(output_code: str) -> dict[str, str]:
pattern = r"```([^\n`]+)\n(.*?)```"
matches = re.findall(pattern, output_code, re.DOTALL)
return {path.strip(): content.strip() for path, content in matches}
def update_files(repo_full_name: str, file_updates: dict[str, str], file_shas: dict[str, str], branch: str, commit_message: str):
repo = gh.get_repo(repo_full_name)
for path, content in file_updates.items():
if path not in file_shas:
raise ValueError(f"No known sha for {path}.")
repo.update_file(
path=path,
message=commit_message,
content=content,
sha=file_shas[path],
branch=branch,
)
def open_pr(repo_full_name: str, branch: str, base_branch: str, title: str, body: str):
repo = gh.get_repo(repo_full_name)
pr = repo.create_pull(
title=title,
body=body,
head=branch,
base=base_branch
)
return pr.html_url
def create_pr_from_output(repo_full_name: str, output_code: str, file_shas: dict[str, str], plan: str, prompt: str) -> str:
file_updates = parse_coder_output(output_code)
if not file_updates:
raise ValueError("Coder output contained no parseable file blocks")
branch = create_branch(repo_full_name)
update_files(repo_full_name, file_updates, file_shas, branch, commit_message=f"Agent: {prompt[:40]}")
return open_pr(repo_full_name, branch, "main", title=f"Agent: {prompt[:40]}", body=plan)