【问题标题】:Import Git repository into GitLab using API?使用 API 将 Git 存储库导入 GitLab?
【发布时间】:2021-12-04 15:19:36
【问题描述】:

在研究如何通过 GitLab API 将 Git 存储库导入 GitLab 时,我的搜索结果似乎被使用 ssh 将 GitHub 存储库镜像到 GitLab 的方法污染了。我正在尝试在自托管 GitLab 服务器上执行相当于转到:http://127.0.0.1/projects/new#import_project 的 CLI/Bash,然后输入: http://www.somegit.com/somegituser/somegitrepository.git,如下图所示: 除了使用 GitLab API(和个人访问令牌)。于是我看了the GitLab documentation就跑了:

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"

返回:

{"message":"202 Accepted"}(base)

但是,存储库不会出现在 GitLab 服务器中。因此,我想知道:如何使用导入方法和 GitLab API 将任意公共 git 存储库添加到自托管 GitLab 服务器(不为 GitLab 使用 ssh)?

【问题讨论】:

  • 是否有意在 curl 中使用 export api 而不是 import api?
  • 不,这是无意的,在撰写本文时我并不知道这两个选项的存在。

标签: git gitlab mirroring


【解决方案1】:

有两种处理方法:

  1. 远程是 GitHub 或 Bitbucket
  2. 任何其他遥控器

GitHub 或 Bitbucket

您正在寻找的是Import API (https://docs.gitlab.com/ee/api/import.html#import-repository-from-github),至少对于 GitHub 和 Bitbucket 服务器有自己的请求,例如:

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

任何其他 git

如果远程不是 GitHub 或 Bitbucket 服务器,我只能想到一种方法:

  1. 通过 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. 使用该请求的 ID 响应创建一个拉镜像,例如:https://docs.gitlab.com/ee/api/remote_mirrors.html#create-a-pull-mirror
curl --request POST --data "url=https://username:token@example.com/gitlab/example.git" \
     --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<ID>/remote_mirrors"

注意远程拉镜像api不支持SSH认证,所以如果需要提供认证,需要使用https。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2019-08-10
    • 2017-03-04
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    相关资源
    最近更新 更多