Skip to content

Add method to move repository to namespace in API client

What does this MR do and why?

This MR adds a feature to ContainerRegistry::GitlabApiClient for changing the namespace of a container repository. The PATCH /gitlab/v1/repositories/<path>/ endpoint now accepts a namespace parameter in addition to name, allowing repositories to be moved or renamed. Only one parameter can be used at a time. We then use that endpoint to enable moving container repositories between namespaces in the ContainerRegistry::GitlabApiClient.

Documentation: PATCH /gitlab/v1/repositories/.

A new token with the required permissions is also introduced:

  • pull/push access to the origin repository path
  • pull access on all sub-repositories of the origin path
  • push access to the destination path

This feature will then be used in succeeding MRs in #475001 (closed) to transfer projects and groups.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

Prerequisites:

  1. Container registry running (best to checkout master as we want this commit to be included, make sure to run make once you have pulled from master).
  2. Access to the registry database
  3. auth is setup in the container registry config yml. It looks something like:
auth:
  token:
    realm: http://gdk.test:3000/jwt/auth
    service: container_registry
    issuer: gitlab-issuer
    rootcertbundle: /Users/adie/gitlab-development-kit/localhost.crt
    autoredirect: false

Steps

The API allows moving of container repositories between different groups in the same top-level namespace. It would be helpful to setup subgroups inside a group. For example, I have the project set at: group-may2024/subgroup1/project-may2024 and want to move it under group-may2024/sample-subgroup

  1. Before we do any changes, we can see how the registry database looks like. Feel free to change the path value to what you have locally.
psql -U postgres
\c registry_dev
select * from repositories where path = 'group-may2024/subgroup1/project-may2024';
registry_dev=# select * from repositories where id = 7;
 id | top_level_namespace_id | parent_id |          created_at           | updated_at |      name       |                  path                   | deleted_at |      last_published_at
----+------------------------+-----------+-------------------------------+------------+-----------------+-----------------------------------------+------------+------------------------------
  7 |                      3 |           | 2024-07-09 04:23:59.345886+02 |            | project-may2024 | group-may2024/subgroup1/project-may2024 |            | 2024-07-09 04:33:19.75158+02
  1. We can first try a dry_run of the operation. As mentioned previous, we have a container repository at the project group-may2024/subgroup1/project-may2024 and want it to move under group-may2024/sample-subgroup. Feel free to change the values based on what you have locally. Make sure that the path exists in the database (see step 1).
path = "group-may2024/subgroup1/project-may2024"
namespace = "group-may2024/sample-subgroup"

ContainerRegistry::GitlabApiClient.move_repository_to_namespace(
  path,
  namespace: namespace,
  dry_run: true
)

The above command should be valid and would return :accepted. If you get an error response, please go back to step one and check the database.

  1. Now, we can run the command again in step 2 with dry_run: false. When successful, it should return :ok.

  2. Now we can check back again in the database and see the difference. Replace x with the ID of the row that you got in step 1.

select * from repositories where id = x;
  1. The path should now be updated to:
group-may2024/sample-subgroup/project-may2024

Or what you provided in namespace and then appended /project-may2024 (or your project name).

IMPORTANT NOTE: The changes will not be reflected yet in the Gitlab UI. What we are doing here is to only do the registry updates, we are not yet doing the updates needed for Gitlab as well, which will be in another issue/MR. After the registry database updates, your setup will be in a inconsistent state since the move has been done in the registry but not yet in Gitlab. To fix this, just update the path of the repository back to the original path that it had in Step 1 before we changed anything.

update repositories set path = 'group-may2024/subgroup1/project-may2024' where id = 7;

Related to #475000 (closed)

Edited by Adie (she/her)

Merge request reports

Loading