feat: initial project setup
This commit is contained in:
66
agents/shared/github_client.py
Normal file
66
agents/shared/github_client.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
from github import Github
|
||||
|
||||
|
||||
def get_github_client():
|
||||
token = os.environ["GITHUB_TOKEN"]
|
||||
repo_name = os.environ["GITHUB_REPO"]
|
||||
g = Github(token)
|
||||
return g.get_repo(repo_name)
|
||||
|
||||
|
||||
def create_content_pr(
|
||||
file_path: str,
|
||||
content: str,
|
||||
title: str,
|
||||
description: str,
|
||||
agent_name: str,
|
||||
branch_prefix: str,
|
||||
) -> str:
|
||||
"""
|
||||
Crea una rama, hace commit de un archivo y abre un PR.
|
||||
Devuelve la URL del PR.
|
||||
NUNCA hace commit directo a main.
|
||||
"""
|
||||
repo = get_github_client()
|
||||
main_branch = repo.get_branch("main")
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
slug = title.lower().replace(" ", "-")[:40]
|
||||
branch_name = f"{branch_prefix}/{agent_name.lower()}-{timestamp}-{slug}"
|
||||
|
||||
# Crear rama desde main
|
||||
repo.create_git_ref(
|
||||
ref=f"refs/heads/{branch_name}",
|
||||
sha=main_branch.commit.sha,
|
||||
)
|
||||
|
||||
# Crear archivo en la nueva rama
|
||||
repo.create_file(
|
||||
path=file_path,
|
||||
message=f"content({branch_prefix}): {title}",
|
||||
content=content.encode("utf-8"),
|
||||
branch=branch_name,
|
||||
)
|
||||
|
||||
# Abrir Pull Request
|
||||
pr = repo.create_pull(
|
||||
title=f"[{agent_name}] {title}",
|
||||
body=(
|
||||
f"**Creado por el agente {agent_name}**\n\n"
|
||||
f"{description}\n\n"
|
||||
f"---\n*Este PR requiere revisión humana antes de mergear.*"
|
||||
),
|
||||
head=branch_name,
|
||||
base="main",
|
||||
)
|
||||
|
||||
# Añadir etiquetas (las crea si no existen)
|
||||
for label_name in ["agent-created", "needs-review"]:
|
||||
try:
|
||||
pr.add_to_labels(label_name)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return pr.html_url
|
||||
Reference in New Issue
Block a user