96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
from argparse import Namespace
|
|
from git_flow import GitFlowError
|
|
from git_flow.changelog import Changelog
|
|
from git_flow.command.base import Command
|
|
from git_flow.git import Git
|
|
|
|
|
|
class MergeCommand(Command):
|
|
def name(self) -> str:
|
|
return "merge"
|
|
|
|
def description(self) -> str:
|
|
return """Realiza el merge de la rama, o crea el PR para hacerlo si tiene un remoto configurado"""
|
|
|
|
def run(self, args: Namespace = Namespace()):
|
|
self.ensure_initialized()
|
|
|
|
branch = self.ensure_right_branch()
|
|
(target, _) = self.get_branch_env_and_type(branch)
|
|
|
|
if "flow.remote" in self.flowconfig:
|
|
remote = self.flowconfig["flow.remote"]
|
|
token = self.ensure_repository_token()
|
|
self.run_remote(remote, token, branch, target)
|
|
else:
|
|
self.run_local(branch, target)
|
|
|
|
def run_remote(self, remote: str, token: str, branch: str, target: str):
|
|
self.ensure_clean_worktree(True)
|
|
|
|
Git("switch", target).exec(print="Cambiando a rama destino")
|
|
Git("pull").exec(print="Obteniendo ultimos cambios del remoto")
|
|
Git("switch", "-").exec(print="Volviendo a rama a mergear")
|
|
|
|
self.check_merge_conflicts(target)
|
|
self.show_commits_to_merge(target)
|
|
|
|
if self.confirm(f"¿Crear PR de '{branch}' a '{target}'?"):
|
|
Git("push", remote, branch, set_upstream=True).exec(
|
|
print="Subiendo rama al remoto para crear PR"
|
|
)
|
|
|
|
self.create_pull_request(token, branch, target)
|
|
|
|
def run_local(self, branch: str, target: str):
|
|
self.ensure_clean_worktree(False)
|
|
self.check_merge_conflicts(target)
|
|
self.show_commits_to_merge(target)
|
|
|
|
if self.confirm(f"¿Mergear rama '{branch}' a '{target}'?"):
|
|
Git("switch", target).exec(print="Cambiando a rama destino")
|
|
Git("merge", branch, ff=False).exec(print="Mergeando")
|
|
|
|
def show_commits_to_merge(self, target):
|
|
commits = Git("log", target + "..", format="%s").lines()
|
|
|
|
if not commits:
|
|
raise GitFlowError("No hay cambios a mergear.")
|
|
|
|
print("Cambios a mergear:")
|
|
|
|
for commit in commits:
|
|
print("- " + commit)
|
|
|
|
def check_merge_conflicts(self, target: str):
|
|
merge_conflicts = Git("merge", target, ff=False, commit=False).code(
|
|
print="Realizando merge de prueba para verificar conflictos"
|
|
)
|
|
|
|
Git("merge", abort=True).exec(print="Abortando merge de prueba", check=False)
|
|
|
|
if merge_conflicts:
|
|
self.error(f"La rama actual tiene conflictos con {target}")
|
|
self.info(
|
|
f"""Se recomienda mergear {target} a la rama actual, resolver los conflictos localmente, y ejecutar nuevamente este comando"""
|
|
)
|
|
raise GitFlowError("Ejecución abortada.")
|
|
else:
|
|
self.success("No se detectaron merge conflicts.")
|
|
|
|
def create_pull_request(self, token: str, branch: str, target: str):
|
|
changelog = Changelog()
|
|
title = branch.replace("/", ": ").replace("-", " ")
|
|
|
|
if title.startswith("release: "):
|
|
title = title[title.index(' ', title.index(' ') + 1) + 1:]
|
|
|
|
message = self.get_remote_api(token).create_pull_request(
|
|
branch,
|
|
target,
|
|
branch.replace("/", ": ").replace("-", " "),
|
|
changelog.generate_content(target, branch),
|
|
)
|
|
|
|
self.success(message)
|