in devai-api/app/github_utils.py [0:0]
def create_github_pr(branch: str, files: dict[str, str]):
"""Creates a GitHub pull request with the specified branch and file updates.
Args:
branch (str): The name of the branch to create the pull request from.
files (dict[str, str]): A dictionary where keys are filepaths and
values are the new file content.
Returns:
The response from the GitHub API. Returns None if an error occurs.
"""
github = GitHubAPIWrapper(
github_app_id=os.getenv("GITHUB_APP_ID"),
github_app_private_key=os.getenv("GITHUB_APP_PRIVATE_KEY"),
github_repository=f"{os.getenv('GITHUB_ACCOUNT')}/{os.getenv('GITHUB_REPO_NAME')}",
)
try:
resp = github.create_branch(branch)
print(resp)
except Exception as e:
print(f"Error creating branch: {e}")
return
existing_files = {}
existing_source_code = ""
new_source_code = ""
for filepath, content in files.items():
try:
old_file_contents = github.read_file(filepath)
existing_files[filepath] = old_file_contents
resp = github.update_file(
file_update_request.format(filepath, old_file_contents, content)
)
print(resp)
existing_source_code += f"\nFile: {filepath}\nContent:\n{old_file_contents}"
new_source_code += f"\nFile: {filepath}\nContent:\n{content}"
except Exception as e:
print(f"Error updating file {filepath}: {e}")
return
try:
pr_summary = generate_pr_summary(existing_source_code, new_source_code)
resp = github.create_pull_request(pr_summary)
print(resp)
github_account = os.environ["GITHUB_ACCOUNT"]
repo_name = os.environ["GITHUB_REPO_NAME"]
pr_link = f"https://github.com/{github_account}/{repo_name}/pulls"
return pr_link
except Exception as e:
print(f"Error creating pull request: {e}")
return