-->

How can I change the mirroring settings in Gitlab

2019-05-18 14:51发布

问题:

I want to change the Gitlab mirroring settings for each of my repositories. Is it possible to do this over the Gitlab API?

The options the Gitlab WebUi allows, are the following:

How can I change them via the API?

Remark: This is not the same question as "How can I tell Gitlab to mirror my Github repositories over the API?" as there my question was how to start the mirroring, here I want to know how I can change the mirroring settings.

回答1:

On https://docs.gitlab.com/ee/api/projects.html#edit-project there are several parameters to configure the pull mirroring:

> mirror
> mirror_user_id
> mirror_trigger_builds
> only_mirror_protected_branches
> mirror_overwrites_diverged_branches

Note: If your HTTP repository is not publicly accessible, add authentication information to the URL: https://username:password@gitlab.company.com/group/project.git where password is a public access key with the api scope enabled

Notice that Push mirroring is not yet implemented: https://gitlab.com/gitlab-org/gitlab-ee/issues/7599



回答2:

I have put my script here if anyone searches for it: https://github.com/SeppPenner/GitlabAutoPullMirroring (mirrored here: https://gitlab.com/SeppPenner/GitlabAutoPullMirroring)



回答3:

I could never find an API endpoint for the mirroring URL. You can kinda, sorta hack this through the database but I couldn't figure out how to obscure the remote login token I was using (for BitBucket) from the database. When you go through the web interface it must encrypt it or put it someplace other than import_url. Anyway, here are things I changed to establish mirroring for some new projects. You do this from the gitlab-psql console.

Again, just want to repeat that the URL, including password, will be exposed in the DB and you probably don't want that long-term. In my case I was doing an initial import of over 100 repos from BitBucket where we intended the mirroring to be shut off in a couple weeks.

You'll need the GitLab project ID.

UPDATE projects SET import_type = 'bare_repository' WHERE id=123;
UPDATE projects SET import_url = 'https://[user]:[API token]@bitbucket.org/path/to/repo.git' WHERE id=123;
UPDATE projects SET mirror_user_id = '21' WHERE id=123;
UPDATE projects SET mirror = 't' WHERE id=123;
UPDATE projects SET only_mirror_protected_branches = 'f' WHERE id=123;
UPDATE projects SET mirror_overwrites_diverged_branches = 'f' WHERE id=123;
UPDATE project_mirror_data SET next_execution_timestamp = '2018-07-01 00:42:47.701103' WHERE project_id=123;

curl --header "PRIVATE-TOKEN:[your_gitlab_private_token]" --request POST https://gitlab.yourdomain.com/api/v4/projects/123/mirror/pull

Notes:

  • The mirror_user_id is probably yourself.
  • The next_execution_timestamp seemingly can't be blank, put any date in there.
  • The curl call triggers a pull. You could do this through the web interface, too.