Commit fc064ade authored by Laurent Peuch's avatar Laurent Peuch
Browse files

feat: add new command rebase-all-my-mrs

parent 4d3e36096496
......@@ -296,7 +296,7 @@ class Repository:
).decode()
else:
print(f">> {command}")
subprocess.check_call(f"cd {self.path} && {command}", shell=True)
return subprocess.check_call(f"cd {self.path} && {command}", shell=True)
def read_file(self, file_name):
return open(self.path / file_name, "r").read()
......@@ -573,3 +573,85 @@ class Command:
result.append(line)
repo.write_file(".gitlab-ci.yml", "\n".join(result) + "\n")
class NoMRCommand(Command):
def workflow(
self,
apply=False,
query="",
interactif=False,
merge_when_pipeline_succeeds=False,
number=None,
additional_checks=None,
project=None,
target="cubes",
branch_name=None,
commit_message=None,
other_args=None,
):
if self.base_query:
if query:
query += " . " + self.base_query
else:
query = self.base_query
if project:
cubes = [get_project(project)]
else:
targets = {
"cubes": get_all_cubes,
"clients": get_all_clients,
"core": get_all_core_projects,
}
cubes = targets[target](query)
number = int(number) if number is not None else None
cube_number_x = 0
for cube in cubes:
if number is not None and cube_number_x >= number:
return
try:
root_files = {
x["name"]
for x in cube.repository_tree(all=True, ref="branch/default")
}
except gitlab.exceptions.GitlabGetError:
print(f"ERROR: {cube.name} is EMPTY, skip it")
continue
branches = [x.name for x in cube.branches.list(all=True)]
if self.pre_check(root_files) == "continue":
continue
if additional_checks:
for check in additional_checks:
if check(cube=cube) == "continue":
continue
print()
message = f"{cube.name} {cube.web_url}"
print(message)
print("=" * len(message))
logger.info(f'cloning {cube.attributes["web_url"]}')
# hg clone
with Repository(url=cube.attributes["web_url"]) as repo:
modifications = self.modify_code(
cube, repo, root_files, branches, other_args, apply=apply
)
while True:
repo.run_command(
'hg up -C -r "last(public() and branch(default))"',
capture_output=True,
)
try:
# {"branch_name": ..., "commit_message": ...}
next(modifications)
except StopIteration:
break
cube_number_x += 1
......@@ -20,6 +20,7 @@ from cube_doctor.transforms.update_cube_licence_dates import UpdateCubeLicenceDa
from cube_doctor.transforms.add_yamllint import AddYamlLint
from cube_doctor.transforms.run_script import RunScript
from cube_doctor.transforms.regenerate_gitlab_ci import RegenerateGitlabCI
from cube_doctor.transforms.rebase_all import RebaseAllMyMRs
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("cube_doctor")
......@@ -96,6 +97,7 @@ commands = {
"auto-upgrade-dependencies": AutoUpgradeDependencies().workflow,
"regenerate-gitlab-ci": RegenerateGitlabCI().workflow,
"run-script": RunScript().workflow,
"rebase-all-my-mrs": RebaseAllMyMRs().workflow,
# "add-new-version": AddNewVersion().workflow,
}
......
import logging
import subprocess
from cube_doctor import NoMRCommand
logger = logging.getLogger("cube_doctor")
class RebaseAllMyMRs(NoMRCommand):
TARGETS = ("cubes", "clients", "core")
def modify_code(self, cube, repo, root_files, branches, other_args, apply=False):
all_my_topics = []
for mr in cube.mergerequests.list(all=True, as_list=False, state="opened"):
if mr.author["username"] == "cube-doctor" and mr.state == 'opened':
topic = mr.source_branch.split("/", 2)[-1]
logger.info(f"select topic '{topic}' for rebasing")
all_my_topics.append(topic)
for topic in all_my_topics:
try:
logger.info(f"see if I can rebase topic '{topic}'")
repo.run_command(f"hg rebase -d 'max(branch('default') and public())' -s 'min(topic('{topic}'))' --dry-run")
except subprocess.CalledProcessError as e:
if e.returncode == 255:
logger.info(f"no rebase needed on topic '{topic}', skip")
continue
else:
logger.warning(f"merge conflict when trying to rebase topic '{topic}'! skip")
continue
logger.warning(f"rebasing topic '{topic}'...")
repo.run_command(f"hg rebase -d 'max(branch('default') and public())' -s 'min(topic('{topic}'))'")
if apply:
repo.run_command(f"hg push -r {topic}")
else:
print(f"would have push rebased topic '{topic}' on {cube.name}")
yield {}
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