83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from argparse import ArgumentParser, Namespace
|
|
from git_flow import COMMIT_TYPES, GitFlowError
|
|
from git_flow.command.base import Command
|
|
from git_flow.git import Git
|
|
|
|
|
|
class ReleaseCommand(Command):
|
|
def name(self) -> str:
|
|
return "release"
|
|
|
|
def description(self) -> str:
|
|
return """Realiza el merge de la rama, o crea el PR para hacerlo si tiene un remoto configurado"""
|
|
|
|
def setup_parser(self, parser: ArgumentParser) -> ArgumentParser:
|
|
parser.add_argument(
|
|
"--group",
|
|
nargs="?",
|
|
help="Agrupar release",
|
|
)
|
|
|
|
return parser
|
|
|
|
def run(self, args: Namespace = Namespace()):
|
|
self.ensure_initialized()
|
|
|
|
branch = self.ensure_right_branch()
|
|
envs = self.flowconfig["flow.branches"].split(",")
|
|
(env, _) = self.get_branch_env_and_type(branch)
|
|
|
|
if env == envs[-1]:
|
|
raise GitFlowError(
|
|
"No se puede hacer release de una rama en el ultimo entorno."
|
|
)
|
|
|
|
has_remote = "flow.remote" in self.flowconfig
|
|
|
|
self.ensure_clean_worktree(has_remote)
|
|
|
|
next_env = envs[envs.index(env) + 1]
|
|
next_branch = (
|
|
branch.replace(f"/{env}/", f"/{next_env}/")
|
|
if branch.startswith("release/")
|
|
else f"release/{next_env}/{branch}"
|
|
)
|
|
|
|
group = args.group
|
|
|
|
if not group and self.confirm("¿Desea agrupar este release con otra rama?"):
|
|
group = self.choice("Grupo release: ", Git.get_branches("release/" + next_env + "/"))
|
|
else:
|
|
group = next_env
|
|
|
|
if has_remote:
|
|
Git("switch", group).exec(print="Cambiando a la rama objetivo")
|
|
Git("pull").exec(print="Sincronizando cambios la rama objetivo")
|
|
Git("switch", "-").exec(print="Volviendo a la rama original")
|
|
|
|
base = Git.get_first_fork_point(branch, env)
|
|
commits = int(Git("rev-list", base + "..", count=True).firstline())
|
|
|
|
if commits > 1 and self.confirm(
|
|
"Desea reemplazar los {commits} commits de la rama por uno solo?"
|
|
):
|
|
Git("switch", next_branch, base, create=True).exec(
|
|
print="Creando rama release en base"
|
|
)
|
|
Git("merge", branch, squash=True).exec(
|
|
print="Squasheando commits en uno solo"
|
|
)
|
|
|
|
commit_type = self.choice("Tipo de commit", COMMIT_TYPES[:-1])
|
|
commit_message = self.prompt(
|
|
"Mensaje (máximo recomendado: 100 caracteres)", persistent=True
|
|
)
|
|
message = commit_type + ": " + commit_message
|
|
Git("commit", m=message).exec(print="Creando commit único")
|
|
else:
|
|
Git("switch", next_branch, create=True).exec(print="Creando rama release")
|
|
|
|
Git("rebase", base, next_branch, onto=group).exec(
|
|
print="Moviendo cambios hacia el siguiente ambiente"
|
|
)
|