Home > Mobile >  Import Git repository into GitLab using API?
Import Git repository into GitLab using API?

Time:10-18

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: enter image description here 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)?

CodePudding user response:

There are two ways to handle this:

  1. the remote is GitHub or Bitbucket
  2. any other remote

GitHub or Bitbucket

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"
}'

any other git

if the remote is not GitHub or Bitbucket server, there is just one way i can think of:

  1. create the project via API: https://docs.gitlab.com/ee/api/projects.html#create-project
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>" }'
  1. use the ID response of that request to create a pull mirror like: https://docs.gitlab.com/ee/api/remote_mirrors.html#create-a-pull-mirror
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.

  • Related