极狐 GitLab

导入 API

Tier: 基础版,专业版,旗舰版

Offering: JihuLab.com,私有化部署

此功能的可用性由功能标志控制。

使用此 API 从外部来源导入代码仓库。

将项目导入到个人命名空间时,不支持用户贡献映射。 当您导入到个人命名空间时,所有贡献都会分配给个人命名空间所有者,且无法重新分配。

从 GitHub 导入代码仓库#

将代码仓库从 GitHub 导入到极狐GitLab。

先决条件:

  • GitHub 导入器的先决条件。
  • target_namespace中设置的命名空间必须存在。
  • 该命名空间可以是您的用户命名空间,也可以是您拥有维护者或所有者角色的现有群组。
plaintext
POST /import/github
属性类型是否必需描述
personal_access_tokenstring是GitHub 个人访问令牌。
repo_idinteger是GitHub 代码仓库 ID。
target_namespacestring是要将代码仓库导入到的命名空间。支持 /namespace/subgroup 等子群组。不得为空。
github_hostnamestring否自定义 GitHub Enterprise 主机名。对于 GitHub.com 请勿设置。从极狐GitLab 16.5 到极狐GitLab 17.1,您必须包含路径 /api/v3。
new_namestring否新项目的名称。同时用作新路径,因此不得以特殊字符开头或结尾,也不得包含连续的特殊字符。
optional_stagesobject否要导入的其他条目。
pagination_limitinteger否每次向 GitHub 发送 API 请求时检索的条目数。默认值为每页 100 个条目。对于从大型代码仓库导入的项目,较小的数值可以降低 GitHub API 端点返回 500 或 502 错误的风险。但是,较小的页面大小会增加迁移时间。
timeout_strategystring否处理导入超时的策略。有效值为 optimistic(继续导入的下一阶段)或 pessimistic(立即失败)。默认为 pessimistic。
shell
1curl --request POST \ 2 --url "https://gitlab.example.com/api/v4/import/github" \ 3 --header "content-type: application/json" \ 4 --header "Authorization: Bearer <your_access_token>" \ 5 --data '{ 6 "personal_access_token": "aBc123abC12aBc123abC12abC123+_A/c123", 7 "repo_id": "12345", 8 "target_namespace": "group/subgroup", 9 "new_name": "NEW-NAME", 10 "github_hostname": "https://github.example.com", 11 "optional_stages": { 12 "attachments_import": true, 13 "collaborators_import": true 14 } 15}'

optional_stages 可使用以下键:

  • attachments_import,用于导入 Markdown 附件。
  • collaborators_import,用于导入非外部协作者的直接代码仓库协作者。
  • single_endpoint_notes_import,用于另一种更全面的评论导入方法,该方法在极狐GitLab 19.3 及更早版本中为可选项。在极狐GitLab 19.4 及更高版本中,对于新配置的导入,此方法为必选项。

有关更多信息,请参阅选择要导入的其他条目。

示例响应:

json
1{ 2 "id": 27, 3 "name": "my-repo", 4 "full_path": "/root/my-repo", 5 "full_name": "Administrator / my-repo", 6 "refs_url": "/root/my-repo/refs", 7 "import_source": "my-github/repo", 8 "import_status": "scheduled", 9 "human_import_status_name": "scheduled", 10 "provider_link": "/my-github/repo", 11 "relation_type": null, 12 "import_warning": null 13}

使用群组访问令牌通过 API 导入公开项目#

当您使用群组访问令牌通过 API 将项目从 GitHub 导入到极狐GitLab 时:

  • 极狐GitLab 项目会继承原项目的可见性设置。因此,如果原项目是公开的,则该项目也可公开访问。
  • 如果 path 或 target_namespace 不存在,则项目导入失败。

取消 GitHub 项目导入#

取消正在进行的 GitHub 项目导入。

plaintext
POST /import/github/cancel
属性类型是否必需描述
project_idinteger是极狐GitLab 项目 ID。
shell
1curl --request POST \ 2 --url "https://gitlab.example.com/api/v4/import/github/cancel" \ 3 --header "content-type: application/json" \ 4 --header "PRIVATE-TOKEN: <your_access_token>" \ 5 --data '{ 6 "project_id": 12345 7}'

示例响应:

json
1{ 2 "id": 160, 3 "name": "my-repo", 4 "full_path": "/root/my-repo", 5 "full_name": "Administrator / my-repo", 6 "import_source": "source/source-repo", 7 "import_status": "canceled", 8 "human_import_status_name": "canceled", 9 "provider_link": "/source/source-repo" 10}

返回以下状态码:

  • 200 OK:项目导入正在取消。
  • 400 Bad Request:项目导入无法取消。
  • 404 Not Found:与 project_id 关联的项目不存在。

将 GitHub gist 导入到极狐GitLab 代码片段#

将个人 GitHub gist 导入到极狐GitLab 代码片段。 您可以导入最多包含 10 个文件的 gist。包含超过 10 个文件的 GitHub gist 会被跳过。您应手动迁移这些 GitHub gist。

如果任何 gist 无法导入,系统会发送一封电子邮件,其中列出未导入的 gist。

plaintext
POST /import/github/gists
属性类型是否必需描述
personal_access_tokenstring是GitHub 个人访问令牌。
shell
1curl --request POST \ 2 --url "https://gitlab.example.com/api/v4/import/github/gists" \ 3 --header "content-type: application/json" \ 4 --header "PRIVATE-TOKEN: <your_gitlab_access_token>" \ 5 --data '{ 6 "personal_access_token": "<your_github_personal_access_token>" 7}'

返回以下状态码:

  • 202 Accepted:gist 导入正在启动。
  • 401 Unauthorized:用户的 GitHub 个人访问令牌无效。
  • 422 Unprocessable Entity:gist 导入已在进行中。
  • 429 Too Many Requests:用户已超出 GitHub 的速率限制。

从 Bitbucket Server 导入代码仓库#

将代码仓库从 Bitbucket Server 导入到极狐GitLab。

Bitbucket 项目键仅用于在 Bitbucket 中查找代码仓库。 如果要将代码仓库导入到极狐GitLab 群组,您必须指定 target_namespace。如果未指定 target_namespace,项目将导入到您的个人用户命名空间。

先决条件:

plaintext
POST /import/bitbucket_server
属性类型是否必需描述
bitbucket_server_projectstring是Bitbucket 项目键。只能包含字母、数字、连字符、下划线、句点或空白字符。个人项目键以 ~ 开头。
bitbucket_server_repostring是Bitbucket 代码仓库名称。只能包含字母、数字、连字符、下划线、句点或空白字符。
bitbucket_server_urlstring是Bitbucket Server URL。
bitbucket_server_usernamestring是Bitbucket Server 用户名。
personal_access_tokenstring是Bitbucket Server 个人访问令牌或密码。
new_namestring否新项目的名称。同时用作新路径,因此不得以特殊字符开头或结尾,也不得包含连续的特殊字符。
target_namespacestring否要将代码仓库导入到的命名空间。支持 /namespace/subgroup 等子群组。
timeout_strategystring否处理导入超时的策略。有效值为 optimistic(继续导入的下一阶段)或 pessimistic(立即失败)。默认为 pessimistic。
shell
1curl --request POST \ 2 --url "https://gitlab.example.com/api/v4/import/bitbucket_server" \ 3 --header "content-type: application/json" \ 4 --header "PRIVATE-TOKEN: <your_access_token>" \ 5 --data '{ 6 "bitbucket_server_url": "http://bitbucket.example.com", 7 "bitbucket_server_username": "root", 8 "personal_access_token": "Nzk4MDcxODY4MDAyOiP8y410zF3tGAyLnHRv/E0+3xYs", 9 "bitbucket_server_project": "NEW", 10 "bitbucket_server_repo": "my-repo", 11 "new_name": "NEW-NAME" 12}'

从 Bitbucket Cloud 导入代码仓库#

将代码仓库从 Bitbucket Cloud 导入到极狐GitLab。

先决条件:

plaintext
POST /import/bitbucket
属性类型是否必需描述
bitbucket_api_tokenstring是Bitbucket Cloud API 令牌。
bitbucket_emailstring是Bitbucket Cloud 电子邮件。
repo_pathstring是代码仓库的路径。
target_namespacestring是要将代码仓库导入到的命名空间。支持 /namespace/subgroup 等子群组。
new_namestring否新项目的名称。同时用作新路径,因此不得以特殊字符开头或结尾,也不得包含连续的特殊字符。
shell
1curl --request POST \ 2 --url "https://gitlab.example.com/api/v4/import/bitbucket" \ 3 --header "content-type: application/json" \ 4 --header "PRIVATE-TOKEN: <your_access_token>" \ 5 --data '{ 6 "bitbucket_email": "email@example.com", 7 "bitbucket_api_token": "your_bitbucket_api_token", 8 "repo_path": "username/my_project", 9 "target_namespace": "my_group/my_subgroup", 10 "new_name": "new_project_name" 11}'

Bitbucket Cloud API 令牌范围#

如果您使用 Bitbucket Cloud API 令牌进行身份验证,该令牌必须具有以下范围:

  • read:repository:bitbucket
  • read:pullrequest:bitbucket
  • read:issue:bitbucket
  • read:wiki:bitbucket