While looking into how to import a Git repository through the GitLab API, into GitLab, my search results appear to be polluted by methods to mirror a GitHub repository into GitLab using ssh. I'm trying to do the CLI/Bash equivalent of going to: http://127.0.0.1/projects/new#import_project on a self-hosted GitLab server, and entering:
http://www.somegit.com/somegituser/somegitrepository.git, as visualised below:
Except, using the GitLab API (and a personal access token). So I looked at the GitLab documentation and ran:
curl --request POST --header "PRIVATE-TOKEN: $personal_access_token" "http://127.0.0.1/api/v4/projects/1/export" \
--data "upload[http_method]=PUT" \
--data-urlencode "upload[url]=http://www.somegit.com/someuser/somegithubrepository.git"
Which returns:
{"message":"202 Accepted"}(base)
However, the repository does not appear in the GitLab server. Hence, I was wondering: How can I add an arbitrary public git repository to a self-hosted GitLab server using the import method and the GitLab API (without using ssh for GitLab)?
There are two ways to handle this:
what you are looking for is the Import API (https://docs.gitlab.com/ee/api/import.html#import-repository-from-github) at least for GitHub and Bitbucket Servers there are own requests like:
curl --request POST \
--url "https://gitlab.example.com/api/v4/import/github" \
--header "content-type: application/json" \
--header "PRIVATE-TOKEN: <your_access_token>" \
--data '{
"personal_access_token": "aBc123abC12aBc123abC12abC123+_A/c123",
"repo_id": "12345",
"target_namespace": "group/subgroup",
"new_name": "NEW-NAME",
"github_hostname": "https://github.example.com"
}'
if the remote is not GitHub or Bitbucket server, there is just one way i can think of:
curl --request POST \
--url "https://gitlab.example.com/api/v4/import/github" \
--header "content-type: application/json" \
--header "PRIVATE-TOKEN: <your_access_token>" \
--data '{ "path":"<path>", "name": "<name>" }'
curl --request POST --data "url=https://username:[email protected]/gitlab/example.git" \
--header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<ID>/remote_mirrors"
Be aware that the remote pull mirror api does not support SSH Authentication, so if you need to provide authentication, you need to use https.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With