70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
from typing import Optional
|
|
|
|
import typer
|
|
|
|
import git_flow.command.base as base
|
|
from git_flow import BRANCH_TYPES
|
|
from git_flow.git import Git
|
|
|
|
app = typer.Typer()
|
|
|
|
|
|
@app.command()
|
|
def new():
|
|
"""Crea una nueva rama siguiendo Conventional Branches"""
|
|
|
|
base.ensure_initialized()
|
|
branch = base.ensure_right_branch()
|
|
|
|
if branch not in base.environments:
|
|
base.io_warning("La rama actual no corresponde a un entorno configurado.")
|
|
|
|
base.io_info(
|
|
"Se creará una nueva rama de trabajo sobre la rama actual.\n"
|
|
"Debe seleccionar el tipo de cambio a realizar.",
|
|
title="Tipo de cambio",
|
|
)
|
|
|
|
branch_type = base.io_choice("Tipos de cambio", BRANCH_TYPES)
|
|
ticket = _get_optional_ticket()
|
|
new_branch = _get_unique_branch_name(branch_type, ticket)
|
|
|
|
if "flow.remote" in base.flowconfig and Git.get_tracking_branch(branch):
|
|
Git("pull").exec(print="Actualizando rama actual")
|
|
|
|
if base.io_confirm(f"¿Crear rama '{new_branch}' sobre la rama actual?"):
|
|
Git("switch", new_branch, create=True).exec(print="Creando nueva rama")
|
|
|
|
|
|
def _get_optional_ticket() -> Optional[str]:
|
|
return base.io_prompt(
|
|
"Ticket que respalda el cambio (en formato ABC-123)",
|
|
validator=lambda x: len(x.strip()) == 0 or base.is_valid_ticket(x),
|
|
)
|
|
|
|
|
|
def _get_unique_branch_name(branch_type: str, ticket: Optional[str]) -> str:
|
|
base.io_info(
|
|
f"Ingrese las palabras clave que describan el cambio de tipo '{branch_type}'.\n"
|
|
"Las mismas se utilizaran para generar el nombre de la rama.",
|
|
title="Nombre de rama",
|
|
)
|
|
branch = None
|
|
branches = Git.get_branches()
|
|
|
|
while branch is None:
|
|
keywords = base.io_prompt("Palabras clave", persistent=True)
|
|
|
|
keywords = filter(lambda k: len(k) > 0, keywords.split(" "))
|
|
branch = (
|
|
branch_type + "/" + (ticket + "-" if ticket else "") + "-".join(keywords)
|
|
)
|
|
|
|
if branch in branches:
|
|
base.io_error(
|
|
f"La rama '{branch}' ya existe, cambie las palabras claves para generar otra"
|
|
)
|
|
branch = None
|
|
|
|
return branch
|