Commit automático inicial

This commit is contained in:
2025-09-11 03:42:33 +02:00
parent 8179601209
commit 5c21f206c0
2 changed files with 100 additions and 5 deletions
+29 -3
View File
@@ -32,7 +32,33 @@
},
{
"id": "KsAh",
"code_hash": "6b9a0985d72cf6686753e11cc4da58c7",
"code_hash": "9b30df28058d9b85e3f8b440bea8bd9e",
"outputs": [
{
"type": "data",
"data": {
"text/plain": ""
}
}
],
"console": []
},
{
"id": "nSXV",
"code_hash": "1b76b63c5cef32f4010a58e6bcd112f3",
"outputs": [
{
"type": "data",
"data": {
"text/html": "<span class=\"markdown prose dark:prose-invert\"><span class=\"paragraph\">Aqui borramos si generamos sin querer, cuidado!</span></span>"
}
}
],
"console": []
},
{
"id": "HxZB",
"code_hash": "67b2c321a1a80633c3328a8f16239616",
"outputs": [
{
"type": "data",
@@ -45,12 +71,12 @@
{
"type": "stream",
"name": "stdout",
"text": "ok: controlando_git_desde_python_20250911 http://10.8.0.6:3123/egutierrez/controlando_git_desde_python_20250911\n"
"text": "Resultado borrado: ok: controlando_git_desde_python borrado\n"
}
]
},
{
"id": "HxZB",
"id": "ZYaS",
"code_hash": null,
"outputs": [],
"console": []
+71 -2
View File
@@ -95,6 +95,7 @@ def _():
except Exception:
return "fail"
def run_cmd(cmd):
"""Ejecuta un comando en shell y muestra salida."""
@@ -125,13 +126,22 @@ def _():
if not os.path.exists(".git"):
run_cmd("git init")
run_cmd("git branch -M main")
# Detectar rama actual (master, main, etc.)
result = subprocess.run(
"git rev-parse --abbrev-ref HEAD",
shell=True, text=True, capture_output=True
)
branch = result.stdout.strip()
if not branch: # Si aún no hay rama, crear main
branch = "main"
run_cmd(f"git checkout -b {branch}")
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")
run_cmd(f"git push -u origin {branch}")
# --- Flujo principal ---
@@ -145,6 +155,65 @@ def _():
if resultado.startswith("ok:"):
commit_and_push(resultado)
return HOST, TOKEN, USERNAME, requests
@app.cell
def _(mo):
mo.md(r"""Aqui borramos si generamos sin querer, cuidado!""")
return
@app.cell
def _(HOST, TOKEN, USERNAME, requests):
NOMBRE_REPO = "controlando_git_desde_python"
def borrar_repo(host, username, token, repo_name):
"""Borra un repositorio en GitHub, GitLab o Gitea según el host.
Devuelve 'ok: nombre_repo borrado' o 'fail'.
"""
headers = {"Content-Type": "application/json"}
try:
# --- GitHub ---
if "github" in host:
headers["Authorization"] = f"token {token}"
api_url = "https://api.github.com"
r = requests.delete(f"{api_url}/repos/{username}/{repo_name}", headers=headers)
if r.status_code == 204:
return f"ok: {repo_name} borrado"
return "fail"
# --- GitLab ---
elif "gitlab" in host:
headers["PRIVATE-TOKEN"] = token
api_url = f"{host}/api/v4"
# Ojo: GitLab usa ID o namespace/proyecto
r = requests.delete(f"{api_url}/projects/{username}%2F{repo_name}", headers=headers)
if r.status_code == 202:
return f"ok: {repo_name} borrado"
return "fail"
# --- Gitea ---
elif "gitea" in host or "http" in host:
headers["Authorization"] = f"token {token}"
api_url = f"{host}/api/v1"
r = requests.delete(f"{api_url}/repos/{username}/{repo_name}", headers=headers)
if r.status_code == 204:
return f"ok: {repo_name} borrado"
return "fail"
else:
return "fail"
except Exception as e:
print("Error:", str(e))
return "fail"
# Ejemplo de uso:
resultado_borrado = borrar_repo(HOST, USERNAME, TOKEN, NOMBRE_REPO)
print("Resultado borrado:", resultado_borrado)
return