60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
from argparse import Namespace
|
|
from git_flow import BRANCH_TYPES
|
|
from git_flow.command.base import Command
|
|
from git_flow.git import Git
|
|
|
|
|
|
class NewCommand(Command):
|
|
def name(self) -> str:
|
|
return "new"
|
|
|
|
def description(self) -> str:
|
|
return """Crea una nueva rama siguiendo conventional branches"""
|
|
|
|
def run(self, args: Namespace = Namespace()):
|
|
self.ensure_initialized()
|
|
branch = self.ensure_right_branch()
|
|
envs = self.flowconfig["flow.branches"].split(",")
|
|
|
|
if branch not in envs:
|
|
self.warning("La rama actual no corresponde a un entorno configurado.")
|
|
|
|
self.info("Se creará una nueva rama de trabajo sobre la rama actual.")
|
|
self.info("Debe seleccionar el tipo de cambio a realizar.")
|
|
|
|
branch_type = self.choice("Tipos de cambio", BRANCH_TYPES)
|
|
new_branch = self._get_unique_branch_name(branch_type)
|
|
|
|
if "flow.remote" in self.flowconfig and Git.get_tracking_branch(branch):
|
|
Git("pull").exec(print="Actualizando rama actual")
|
|
|
|
if self.confirm(f"¿Crear rama '{new_branch}' sobre la rama actual?"):
|
|
Git("switch", new_branch, create=True).exec(print="Creando nueva rama")
|
|
|
|
def _get_unique_branch_name(self, branch_type: str) -> str:
|
|
self.info(
|
|
f"Ingrese las palabras clave que describan el cambio de tipo '{branch_type}'."
|
|
)
|
|
branch = None
|
|
branches = Git.get_branches()
|
|
|
|
while branch is None:
|
|
keywords = self.prompt(
|
|
"Palabras clave del cambio (por ejemplo: 'create worker form')",
|
|
persistent=True,
|
|
)
|
|
|
|
ticket = self.prompt("[Opcional] Ingrese el ID del ticket (por ejemplo ABC-123 o #123)")
|
|
ticket = ticket[1:] if ticket.startswith("#") else ticket
|
|
keywords = ticket + ' ' + keywords
|
|
keywords = filter(lambda k: len(k) > 0, keywords.split(" "))
|
|
branch = branch_type + "/" + "-".join(keywords)
|
|
|
|
if branch in branches:
|
|
self.error(
|
|
f"La rama '{branch}' ya existe, cambie las palabras claves para generar otra"
|
|
)
|
|
branch = None
|
|
|
|
return branch
|