Commit automático inicial
This commit is contained in:
@@ -32,7 +32,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "KsAh",
|
"id": "KsAh",
|
||||||
"code_hash": "9b30df28058d9b85e3f8b440bea8bd9e",
|
"code_hash": "a8b08f30b385ad82f8918f577e5deb8f",
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"type": "data",
|
"type": "data",
|
||||||
@@ -41,28 +41,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"console": [
|
"console": []
|
||||||
{
|
|
||||||
"type": "stream",
|
|
||||||
"name": "stdout",
|
|
||||||
"text": "Resultado creaci\u00f3n: ok: controlando_git_desde_python http://10.8.0.6:3123/egutierrez/controlando_git_desde_python\n"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "stream",
|
|
||||||
"name": "stdout",
|
|
||||||
"text": "\n\n"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "stream",
|
|
||||||
"name": "stdout",
|
|
||||||
"text": "\n[master 253ac3b] Commit autom\u00e1tico inicial\n 1 file changed, 22 insertions(+), 1 deletion(-)\n"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "stream",
|
|
||||||
"name": "stdout",
|
|
||||||
"text": "Branch 'master' set up to track remote branch 'master' from 'origin'.\n"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "nSXV",
|
"id": "nSXV",
|
||||||
|
|||||||
+12
-7
@@ -35,7 +35,7 @@ def _():
|
|||||||
|
|
||||||
def crear_repo(host, username, token, repo_name, private=False, description=""):
|
def crear_repo(host, username, token, repo_name, private=False, description=""):
|
||||||
"""Crea un repositorio en GitHub, GitLab o Gitea según el host.
|
"""Crea un repositorio en GitHub, GitLab o Gitea según el host.
|
||||||
Devuelve 'ok: nombre_repo url' o 'fail'.
|
Devuelve 'ok: nombre_repo url_push' o 'fail'.
|
||||||
"""
|
"""
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
|
|
||||||
@@ -51,7 +51,8 @@ def _():
|
|||||||
payload = {"name": repo_name, "private": private, "description": description}
|
payload = {"name": repo_name, "private": private, "description": description}
|
||||||
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
||||||
if r.status_code in (200, 201):
|
if r.status_code in (200, 201):
|
||||||
url = r.json().get("html_url", "")
|
# URL con token embebido para push HTTPS
|
||||||
|
url = f"https://{username}:{token}@github.com/{username}/{repo_name}.git"
|
||||||
return f"ok: {repo_name} {url}"
|
return f"ok: {repo_name} {url}"
|
||||||
return "fail"
|
return "fail"
|
||||||
|
|
||||||
@@ -70,7 +71,8 @@ def _():
|
|||||||
}
|
}
|
||||||
r = requests.post(f"{api_url}/projects", headers=headers, data=payload)
|
r = requests.post(f"{api_url}/projects", headers=headers, data=payload)
|
||||||
if r.status_code in (200, 201):
|
if r.status_code in (200, 201):
|
||||||
url = r.json().get("web_url", "")
|
# URL con token para push
|
||||||
|
url = f"https://{username}:{token}@gitlab.com/{username}/{repo_name}.git"
|
||||||
return f"ok: {repo_name} {url}"
|
return f"ok: {repo_name} {url}"
|
||||||
return "fail"
|
return "fail"
|
||||||
|
|
||||||
@@ -85,7 +87,10 @@ def _():
|
|||||||
payload = {"name": repo_name, "private": private, "description": description}
|
payload = {"name": repo_name, "private": private, "description": description}
|
||||||
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
r = requests.post(f"{api_url}/user/repos", headers=headers, json=payload)
|
||||||
if r.status_code in (200, 201):
|
if r.status_code in (200, 201):
|
||||||
url = r.json().get("html_url", "")
|
# URL de push con token embebido
|
||||||
|
url = f"{host}/{username}/{repo_name}.git"
|
||||||
|
if url.startswith("http"):
|
||||||
|
url = url.replace("://", f"://{username}:{token}@")
|
||||||
return f"ok: {repo_name} {url}"
|
return f"ok: {repo_name} {url}"
|
||||||
return "fail"
|
return "fail"
|
||||||
|
|
||||||
@@ -96,7 +101,6 @@ def _():
|
|||||||
return "fail"
|
return "fail"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def run_cmd(cmd):
|
def run_cmd(cmd):
|
||||||
"""Ejecuta un comando en shell y muestra salida."""
|
"""Ejecuta un comando en shell y muestra salida."""
|
||||||
result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
|
result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
|
||||||
@@ -110,7 +114,7 @@ def _():
|
|||||||
def commit_and_push(resultado_creacion, commit_message="Commit automático inicial"):
|
def commit_and_push(resultado_creacion, commit_message="Commit automático inicial"):
|
||||||
"""
|
"""
|
||||||
Recibe el resultado de crear_repo, por ejemplo:
|
Recibe el resultado de crear_repo, por ejemplo:
|
||||||
'ok: nombre_repo https://gitlab.com/usuario/nombre_repo'
|
'ok: nombre_repo https://usuario:token@host/usuario/nombre_repo.git'
|
||||||
Hace init, commit y push a ese remoto.
|
Hace init, commit y push a ese remoto.
|
||||||
"""
|
"""
|
||||||
if not resultado_creacion.startswith("ok:"):
|
if not resultado_creacion.startswith("ok:"):
|
||||||
@@ -127,7 +131,7 @@ def _():
|
|||||||
if not os.path.exists(".git"):
|
if not os.path.exists(".git"):
|
||||||
run_cmd("git init")
|
run_cmd("git init")
|
||||||
|
|
||||||
# Detectar rama actual (master, main, etc.)
|
# Detectar rama actual
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
"git rev-parse --abbrev-ref HEAD",
|
"git rev-parse --abbrev-ref HEAD",
|
||||||
shell=True, text=True, capture_output=True
|
shell=True, text=True, capture_output=True
|
||||||
@@ -155,6 +159,7 @@ def _():
|
|||||||
|
|
||||||
if resultado.startswith("ok:"):
|
if resultado.startswith("ok:"):
|
||||||
commit_and_push(resultado)
|
commit_and_push(resultado)
|
||||||
|
|
||||||
return HOST, TOKEN, USERNAME, requests
|
return HOST, TOKEN, USERNAME, requests
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user