Commit 5e387900 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

feat(cli option): add -u/--update-mr

parent 94ac63cbc891
......@@ -341,6 +341,7 @@ class Command:
target="cubes",
branch_name=None,
commit_message=None,
update_mr=False,
other_args=None,
):
forced_branch_name = branch_name
......@@ -392,7 +393,11 @@ class Command:
target_branch = None
# we already sent the MR
if target_branch is not None and target_branch in branches:
if (
not update_mr
and target_branch is not None
and target_branch in branches
):
logger.info(f"{cube.name} already has a topic {target_branch}, skip")
continue
......@@ -433,34 +438,111 @@ class Command:
else:
commit_message = metadata["commit_message"]
branches = [x.name for x in cube.branches.list(all=True)]
if branch_name in branches:
logger.info(f"branch '{branch_name}' already exists, skip")
continue
# if nothing has changed, continue
if not repo.run_command("hg status", capture_output=True):
logger.info("nothing has changed, skip")
continue
needs_amend = False
branches = [x.name for x in cube.branches.list(all=True)]
if branch_name in branches:
topic = branch_name.split("/", -1)[-1]
if not update_mr:
logger.info(f"branch '{branch_name}' already exists, skip")
continue
logger.info(
f"branch '{branch_name}' already exists but --update-mr is used, will "
"update it"
)
try:
logger.info(f"see if I need to rebase topic '{topic}'")
repo.run_command("hg shelve")
repo.run_command(
f"hg rebase -d 'max(branch(default) and public())' "
f"-s 'min(topic({topic}))' --dry-run"
)
except subprocess.CalledProcessError as e:
if e.returncode == 255:
logger.info(f"no rebase needed on topic '{topic}'")
else:
logger.warning(
f"merge conflict when trying to rebase topic '{topic}', let's "
"prune it"
)
repo.run_command(f"hg update {topic}")
repo.run_command("hg prune -r .")
if apply:
repo.run_command("hg push -r .")
repo.run_command(
'hg up -C -r "last(public() and branch(default))"',
)
else:
logger.warning(f"rebasing topic '{topic}'...")
repo.run_command(
f"hg rebase -d 'max(branch(default) and public())' "
f"-s 'min(topic({topic}))'"
)
finally:
repo.run_command("hg unshelve")
repo.run_command(f"hg diff -r {branch_name.split('/')[-1]}")
if not repo.run_command(
f"hg diff -r {branch_name.split('/')[-1]}",
capture_output=True,
).strip():
logger.info(
"actually no need to update because the existing branch won't be "
"changed"
)
continue
needs_amend = True
repo.run_command("hg shelve")
repo.run_command(f"hg update {branch_name.split('/')[-1]}")
try:
repo.run_command("hg uncommit -a")
except subprocess.CalledProcessError as e:
# 255 means nothing to do
if e.returncode != 255:
raise
else:
repo.run_command("hg revert -a")
repo.run_command("hg unshelve")
print()
message = f"{cube.name} {cube.web_url}"
print(message)
print("=" * len(message))
repo.run_command(f"hg topic '{branch_name.split('/')[-1]}'")
if not needs_amend:
repo.run_command(f"hg topic '{branch_name.split('/')[-1]}'")
repo.run_command("hg status")
repo.run_command("hg diff")
repo.run_command(f"hg commit -m '{commit_message}'")
if not needs_amend:
repo.run_command(f"hg commit -m '{commit_message}'")
else:
repo.run_command("hg amend")
cube_number_x += 1
if apply or (
interactif
and input(f"Apply on {cube.name}? [Y/n] ").lower()[:1]
and input(
f"{'Apply to' if not update_mr else 'Update on'} {cube.name}? [Y/n] "
).lower()[:1]
in ("y", "")
):
repo.run_command("hg push -r tip")
if update_mr and cube.mergerequests.list(
state="opened", source_branch=branch_name
):
logger.info(
f"mr for branch '{branch_name}' already exist, don't recreate it"
)
continue
print("Creating MR...")
mr = cube.mergerequests.create(
{
......@@ -476,7 +558,7 @@ class Command:
"\n\nKind regards,",
}
)
print(f"MR created at {mr.attributes['web_url']} \\o/")
print(f"\n=> MR created at {mr.attributes['web_url']} \\o/\n")
if merge_when_pipeline_succeeds:
# here "mr" is a ProjectMergeRequest and not the real MR somehow?
......@@ -587,6 +669,7 @@ class NoMRCommand(Command):
target="cubes",
branch_name=None,
commit_message=None,
update_mr=False,
other_args=None,
):
if self.base_query:
......@@ -639,7 +722,12 @@ class NoMRCommand(Command):
# hg clone
with Repository(url=cube.attributes["web_url"]) as repo:
modifications = self.modify_code(
cube, repo, root_files, branches, other_args, apply=apply
cube,
repo,
root_files,
branches=branches,
other_args=other_args,
apply=apply,
)
while True:
repo.run_command(
......
......@@ -111,6 +111,13 @@ def main():
parser.add_argument("-n", "--number", type=int)
parser.add_argument("-q", "--query", default="")
parser.add_argument("-p", "--project", default=None)
parser.add_argument(
"-u",
"--update-mr",
action="store_true",
default=False,
help="update existing MR if it exists",
)
parser.add_argument(
"-t", "--target", choices=["clients", "cubes", "core"], default="cubes"
)
......@@ -142,6 +149,7 @@ def main():
target=args.target,
branch_name=args.branch_name,
commit_message=args.commit_message,
update_mr=args.update_mr,
other_args=args,
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment