Skip to content

Commit

Permalink
fix: dry run failure when project transfer is configured along with o…
Browse files Browse the repository at this point in the history
…ther additional configs (#726)

The `project: transfer_from` config fails when run with `--noop`, if there are other configurations for the project (i.e. `project_settings`). This is due to the `project_settings` processor getting a 404 when trying to fetch the settings from GitLab API(since the project doesn't get moved in dry-run).

The dry-run/noop logic is updated to account for situation when project transfer configuration is used. Additional relevant test cases have been included as well.

Fixes #725
  • Loading branch information
long-wan-ep committed Apr 12, 2024
1 parent ec6ba78 commit 71070bf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
10 changes: 8 additions & 2 deletions gitlabform/__init__.py
Expand Up @@ -47,7 +47,13 @@


class GitLabForm:
def __init__(self, include_archived_projects=True, target=None, config_string=None):
def __init__(
self,
include_archived_projects=True,
target=None,
config_string=None,
noop=False,
):
if target and config_string:
# this mode is basically only for testing

Expand All @@ -58,7 +64,7 @@ def __init__(self, include_archived_projects=True, target=None, config_string=No
self.strict = True
self.start_from = 1
self.start_from_group = 1
self.noop = False
self.noop = noop
self.output_file = None
self.skip_version_check = True
self.include_archived_projects = include_archived_projects
Expand Down
10 changes: 10 additions & 0 deletions gitlabform/processors/abstract_processor.py
Expand Up @@ -51,6 +51,16 @@ def process(
verbose(
f"Processing section '{self.configuration_name}' in dry-run mode."
)
try:
project_transfer_source = configuration["project"]["transfer_from"]
verbose(
f"""Project {project_or_project_and_group} is configured to be transferred,
diffing config from transfer source project {project_transfer_source}."""
)
project_or_project_and_group = project_transfer_source
except KeyError:
pass

self._print_diff(
project_or_project_and_group,
configuration.get(self.configuration_name),
Expand Down
3 changes: 2 additions & 1 deletion tests/acceptance/__init__.py
Expand Up @@ -247,7 +247,7 @@ def randomize_case(input: str) -> str:
return "".join(random.choice((str.upper, str.lower))(char) for char in input)


def run_gitlabform(config, target, include_archived_projects=True):
def run_gitlabform(config, target, include_archived_projects=True, noop=False):
# f-strings with """ used as configs have the disadvantage of having indentation in them - let's remove it here
config = textwrap.dedent(config)

Expand All @@ -264,5 +264,6 @@ def run_gitlabform(config, target, include_archived_projects=True):
include_archived_projects=include_archived_projects,
config_string=config,
target=target,
noop=noop,
)
gf.run()
17 changes: 17 additions & 0 deletions tests/acceptance/standard/test_running.py
Expand Up @@ -22,6 +22,23 @@ def test__ALL(self, gl, project, other_project):
other_project = gl.projects.get(other_project.id)
assert other_project.request_access_enabled is True

# noinspection PyPep8Naming
def test__ALL_dry_run(self, gl, project, other_project):
config = f"""
projects_and_groups:
'*':
project_settings:
request_access_enabled: false
"""

run_gitlabform(config, "ALL", noop=True)

project = gl.projects.get(project.id)
assert project.request_access_enabled is True

other_project = gl.projects.get(other_project.id)
assert other_project.request_access_enabled is True

# noinspection PyPep8Naming
def test__ALL_DEFINED(self, gl, project, other_project):
config = f"""
Expand Down
24 changes: 24 additions & 0 deletions tests/acceptance/standard/test_transfer_project.py
Expand Up @@ -9,6 +9,30 @@


class TestTransferProject:
def test__transfer_between_two_root_groups_dry_run(
self, project_for_function, group, other_group
):
project_new_path_with_namespace = (
f"{other_group.path}/{project_for_function.name}"
)
projects_in_destination_before_transfer = other_group.projects.list()

config = f"""
projects_and_groups:
{project_new_path_with_namespace}:
project:
transfer_from: {project_for_function.path_with_namespace}
project_settings:
description: test
"""

run_gitlabform(config, project_new_path_with_namespace, noop=True)
projects_in_destination_after_transfer = other_group.projects.list()

assert len(projects_in_destination_after_transfer) == len(
projects_in_destination_before_transfer
)

def test__transfer_between_two_root_groups(
self, project_for_function, group, other_group
):
Expand Down

0 comments on commit 71070bf

Please sign in to comment.