Commit automático inicial
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import marimo
|
||||
|
||||
__generated_with = "0.15.2"
|
||||
app = marimo.App(width="medium")
|
||||
|
||||
|
||||
@app.cell
|
||||
def _():
|
||||
import marimo as mo
|
||||
return (mo,)
|
||||
|
||||
|
||||
@app.cell
|
||||
def _(mo):
|
||||
mo.md(r"""# Github: Crear repo y subirlo""")
|
||||
return
|
||||
|
||||
|
||||
@app.cell
|
||||
def _():
|
||||
import os
|
||||
import requests
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
# Variables configurables
|
||||
HOST = "http://10.8.0.6:3123/" # Cambia entre GitHub, GitLab o tu servidor Gitea
|
||||
USERNAME = "egutierrez"
|
||||
TOKEN = "7dedbd8257bd85ac023d18fb15f3c26d6fb3c3b5" # Token personal / PAT
|
||||
|
||||
# Nombre de la carpeta padre
|
||||
carpeta_actual = os.path.basename(os.getcwd())
|
||||
repo_name = carpeta_actual
|
||||
|
||||
|
||||
def crear_repo(host, username, token, repo_name, private=True, description=""):
|
||||
"""Crea un repositorio en GitHub, GitLab o Gitea según el host.
|
||||
Devuelve 'ok: nombre_repo url' o 'fail'.
|
||||
"""
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
try:
|
||||
# --- GitHub ---
|
||||
if "github" in host:
|
||||
headers["Authorization"] = f"token {token}"
|
||||
api_url = "https://api.github.com"
|
||||
resp = requests.get(f"{api_url}/user/repos", headers=headers)
|
||||
repos_existentes = [r["name"] for r in resp.json()]
|
||||
if repo_name in repos_existentes:
|
||||
repo_name += "_" + datetime.now().strftime("%Y%m%d")
|
||||
payload = {"name": repo_name, "private": private, "description": description}
|
||||
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
||||
if r.status_code in (200, 201):
|
||||
url = r.json().get("html_url", "")
|
||||
return f"ok: {repo_name} {url}"
|
||||
return "fail"
|
||||
|
||||
# --- GitLab ---
|
||||
elif "gitlab" in host:
|
||||
headers["PRIVATE-TOKEN"] = token
|
||||
api_url = f"{host}/api/v4"
|
||||
resp = requests.get(f"{api_url}/projects?owned=true", headers=headers)
|
||||
repos_existentes = [r["name"] for r in resp.json()]
|
||||
if repo_name in repos_existentes:
|
||||
repo_name += "_" + datetime.now().strftime("%Y%m%d")
|
||||
payload = {
|
||||
"name": repo_name,
|
||||
"visibility": "private" if private else "public",
|
||||
"description": description,
|
||||
}
|
||||
r = requests.post(f"{api_url}/projects", headers=headers, data=payload)
|
||||
if r.status_code in (200, 201):
|
||||
url = r.json().get("web_url", "")
|
||||
return f"ok: {repo_name} {url}"
|
||||
return "fail"
|
||||
|
||||
# --- Gitea ---
|
||||
elif "gitea" in host or "http" in host:
|
||||
headers["Authorization"] = f"token {token}"
|
||||
api_url = f"{host}/api/v1"
|
||||
resp = requests.get(f"{api_url}/user/repos", headers=headers)
|
||||
repos_existentes = [r["name"] for r in resp.json()]
|
||||
if repo_name in repos_existentes:
|
||||
repo_name += "_" + datetime.now().strftime("%Y%m%d")
|
||||
payload = {"name": repo_name, "private": private, "description": description}
|
||||
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
||||
if r.status_code in (200, 201):
|
||||
url = r.json().get("html_url", "")
|
||||
return f"ok: {repo_name} {url}"
|
||||
return "fail"
|
||||
|
||||
else:
|
||||
return "fail"
|
||||
|
||||
except Exception:
|
||||
return "fail"
|
||||
|
||||
|
||||
def run_cmd(cmd):
|
||||
"""Ejecuta un comando en shell y muestra salida."""
|
||||
result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
|
||||
if result.returncode != 0:
|
||||
print("Error:", result.stderr.strip())
|
||||
else:
|
||||
print(result.stdout.strip())
|
||||
return result.returncode == 0
|
||||
|
||||
|
||||
def commit_and_push(resultado_creacion, commit_message="Commit automático inicial"):
|
||||
"""
|
||||
Recibe el resultado de crear_repo, por ejemplo:
|
||||
'ok: nombre_repo https://gitlab.com/usuario/nombre_repo'
|
||||
Hace init, commit y push a ese remoto.
|
||||
"""
|
||||
if not resultado_creacion.startswith("ok:"):
|
||||
print("No hay repo remoto válido.")
|
||||
return
|
||||
|
||||
partes = resultado_creacion.split()
|
||||
if len(partes) < 3:
|
||||
print("Formato inesperado:", resultado_creacion)
|
||||
return
|
||||
repo_name = partes[1]
|
||||
remote_url = partes[2]
|
||||
|
||||
if not os.path.exists(".git"):
|
||||
run_cmd("git init")
|
||||
run_cmd("git branch -M main")
|
||||
|
||||
run_cmd("git remote remove origin || true")
|
||||
run_cmd(f"git remote add origin {remote_url}")
|
||||
run_cmd("git add .")
|
||||
run_cmd(f'git commit -m "{commit_message}" || echo "Nada que commitear"')
|
||||
run_cmd("git push -u origin main")
|
||||
|
||||
|
||||
# --- Flujo principal ---
|
||||
resultado = crear_repo(
|
||||
HOST, USERNAME, TOKEN, repo_name,
|
||||
private=True,
|
||||
description=f"Repositorio generado automáticamente para {carpeta_actual}"
|
||||
)
|
||||
|
||||
print("Resultado creación:", resultado)
|
||||
|
||||
if resultado.startswith("ok:"):
|
||||
commit_and_push(resultado)
|
||||
|
||||
return
|
||||
|
||||
|
||||
@app.cell
|
||||
def _():
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
Reference in New Issue
Block a user