79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import requests
|
||
import json
|
||
import os
|
||
|
||
# CONFIGURACIÓN
|
||
# Cuando tengas el token, ponlo aquí o en variable de entorno
|
||
GITEA_URL = "http://git.gk-saas.komkida.duckdns.org" # Interno: "http://localhost:3000" si corre en la misma Pi
|
||
GITEA_TOKEN = "20e7a102677c5cd7e2e0a241e9ea1cf6364db643"
|
||
|
||
def create_org(org_name):
|
||
"""Crea una organización para un cliente (ej. 'cliente-123')"""
|
||
headers = {
|
||
'Authorization': f'token {GITEA_TOKEN}',
|
||
'Content-Type': 'application/json'
|
||
}
|
||
data = {
|
||
"username": org_name,
|
||
"visibility": "private",
|
||
"description": f"Organización para {org_name}",
|
||
"website": ""
|
||
}
|
||
|
||
response = requests.post(f"{GITEA_URL}/api/v1/orgs", headers=headers, json=data)
|
||
|
||
if response.status_code == 201:
|
||
print(f"✅ Organización '{org_name}' creada.")
|
||
return True
|
||
elif response.status_code == 422:
|
||
print(f"ℹ️ La organización '{org_name}' ya existe.")
|
||
return True
|
||
else:
|
||
print(f"❌ Error al crear organización: {response.text}")
|
||
return False
|
||
|
||
def create_repo(org_name, repo_name, description="Sitio generado por GKACHELE SaaS"):
|
||
"""Crea un repositorio dentro de una organización"""
|
||
headers = {
|
||
'Authorization': f'token {GITEA_TOKEN}',
|
||
'Content-Type': 'application/json'
|
||
}
|
||
data = {
|
||
"name": repo_name,
|
||
"description": description,
|
||
"private": True,
|
||
"auto_init": True, # Crea README y branch main automáticamente
|
||
"gitignores": "Python",
|
||
"license": "MIT",
|
||
"default_branch": "main"
|
||
}
|
||
|
||
# Endpoint para crear repo en organización
|
||
url = f"{GITEA_URL}/api/v1/orgs/{org_name}/repos"
|
||
|
||
response = requests.post(url, headers=headers, json=data)
|
||
|
||
if response.status_code == 201:
|
||
repo_data = response.json()
|
||
print(f"✅ Repositorio '{org_name}/{repo_name}' creado exitosamente.")
|
||
print(f"🔗 Clone URL: {repo_data['clone_url']}")
|
||
return repo_data
|
||
elif response.status_code == 409:
|
||
print(f"ℹ️ El repositorio '{repo_name}' ya existe en '{org_name}'.")
|
||
# Intentar obtener el repo existente
|
||
# response = requests.get(f"{GITEA_URL}/api/v1/repos/{org_name}/{repo_name}", headers=headers)
|
||
return None
|
||
else:
|
||
print(f"❌ Error al crear repositorio: {response.text}")
|
||
return None
|
||
|
||
if __name__ == "__main__":
|
||
print("--- 🧪 Test de Conexión con Gitea ---")
|
||
if GITEA_TOKEN == "TU_TOKEN_DE_GITEA_AQUI":
|
||
print("⚠️ Primero genera un token en Gitea (Settings -> Applications -> Generate Token)")
|
||
else:
|
||
# Prueba
|
||
CLIENTE_TEST = "cliente-prueba-1"
|
||
create_org(CLIENTE_TEST)
|
||
create_repo(CLIENTE_TEST, "sitio-web-1")
|