
把項目從 GitHub 遷移到極狐GitLab 有多種方式,可以直接在極狐GitLab 創建項目的頁面選擇 import from GitHub,通過“一鍵式”來完成遷移,也可以使用 API 的方式通過“分步式”完成遷移。下面將分別進行展示。
通過
import
(下面的“一鍵式”遷移) 方式來遷移的項目只適合非企業版 GitHub。企業版 GitHub 的項目遷移建議用“分步式”遷移,也就是通過 API 的方式來進行。
一鍵式遷移
從 GitHub 到極狐GitLab 最簡單的遷移方式就是在創建項目時,選擇 import project
選項:
接著選擇 GitHub 即可:
隨后需要輸入 GitHub 的 Personal Access Token 來 list 出 GitHub 賬號下面的所有 Repo,選擇想要 import 的項目即可完成項目遷移。這是最簡單的遷移方式。
分步式遷移
分步式遷移是利用 GitHub 與極狐GitLab 自身的 API 來完成數據的導出(GitHub 為導出方)、導入(極狐GitLab 為導入方)??梢苑忠韵聝煞N情況分別進行導入。
個人賬號下的倉庫遷移
個人賬號下的倉庫指不在任何一個 organization
下面的項目,這種情況的遷移很簡單,只需要確認需要從 GitHub 導出的 Repo,找到 Repo ID,然后調用極狐GitLab 的 import
API 即可。下面進行演示。
前提條件
- 確認需要遷移的項目,本文演示的倉庫地址為:https://github.com/majinghe/tekton-demo
- 獲取可用的極狐GitLab 與 GitHub Personal Access Token
極狐GitLab 創建 Personal Access Token 的方法如下:
GitHub 創建 Personal Access Token 的方法和極狐GitLab 類似:右上角用戶信息 –> Settings –> Developer settings –> Personal access tokens。
獲取 GitHub Repo ID
獲取 Demo Repo:https://github.com/majinghe/tekton-demo 的 Repo ID:
$ curl -ks https://api.github.com/repos/majinghe/tekton-demo
{
"id": 374861350,
"node_id": "MDEwOlJlcG9zaXRvcnkzNzQ4NjEzNTA=",
"name": "tekton-demo",
"full_name": "majinghe/tekton-demo",
"private": false,
"owner": {
"login": "majinghe",
"id": 42570491,
"node_id": "MDQ6VXNlcjQyNTcwNDkx",
"avatar_url": "https://avatars.githubusercontent.com/u/42570491?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/majinghe",
"html_url": "https://github.com/majinghe",
"followers_url": "https://api.github.com/users/majinghe/followers",
"following_url": "https://api.github.com/users/majinghe/following{/other_user}",
"gists_url": "https://api.github.com/users/majinghe/gists{/gist_id}",
"starred_url": "https://api.github.com/users/majinghe/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/majinghe/subscriptions",
"organizations_url": "https://api.github.com/users/majinghe/orgs",
"repos_url": "https://api.github.com/users/majinghe/repos",
"events_url": "https://api.github.com/users/majinghe/events{/privacy}",
"received_events_url": "https://api.github.com/users/majinghe/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/majinghe/tekton-demo",
"description": "Tekton demo",
"fork": false,
"url": "https://api.github.com/repos/majinghe/tekton-demo",
......太長,只保留部分信息......
"visibility": "public",
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "main",
"temp_clone_token": null,
"network_count": 0,
"subscribers_count": 1
}
返回值的第一行 ` "id": 374861350,` 就是該 Repo 的 Repo ID。接下來調用極狐GitLab import API 導入即可。
導入極狐GitLab
調用極狐GitLab 的 import API,將上述 GitHub Repo 導入極狐GitLab,API 為:
$ curl --request POST \
--url "https://gitlab.example.com/api/v4/import/github" \
--header "content-type: application/json" \
--header "PRIVATE-TOKEN: your-jh-gitlab-personal-access-token" \
--data '{
"personal_access_token": "your-github-personal-access-token",
"repo_id": "12345",
"target_namespace": "group/subgroup",
"new_name": "NEW-NAME",
"github_hostname": "https://github.example.com"
}'
參數說明:
- –url:極狐GitLab 實例地址;
- PRIVATE-TOKEN:極狐GitLab Personal Access Token,參考前提環節進行獲??;
- personal_access_token:GitHub Personal Access Token,參考前提環節進行獲??;
- repo_id:上一步獲取的 GitHub Repo ID;
- target_namespace:極狐GitLab 中,導入項目所在的 Group/SubGroup;
- new_name:導入項目在極狐GitLab 中的項目名稱;
- github_hostname:GitHub 實例地址,如果是 github.com,則可不寫;
將上述獲取的 Personal Access Token 以及 Repo ID 分別替換,然后執行如下命令進行導入:
$ curl --request POST \
--url "https://jihulab.com/api/v4/import/github" \
--header "content-type: application/json" \
--header "PRIVATE-TOKEN: jh-gitlab-personal-token" \
--data '{
"personal_access_token": "github-personal-access-token",
"repo_id": "374861350",
"target_namespace": "xiaomage",
"new_name": "jh-tekton-demo"
}'
可以看到返回值:
{
"id":13535,
"name":"jh-tekton-demo",
"full_path":"/xiaomage/jh-tekton-demo",
"full_name":"xiaomage / jh-tekton-demo"
}
可以看到極狐GitLab 中,項目的 ID 為 13535
,name 為 jh-tekton-demo
,所在的 Group/Subgroup 為 xiaomage
??梢栽跇O狐GitLab 實例界面上進行查看:
可以看到 issue
,PR
(極狐GitLab 中自動轉換為 MR)一并進行了導入。接下來就可以在極狐GitLab 中進行協作開發了。
Organization 下面的倉庫遷移
極狐GitLab 沒有 organization 這樣的概念,但是有 Group/Subgroup 來對項目(Project)進行編排管理。GitHub organization 與極狐GitLab Group/Subgroup 的轉換示意圖如下:
以 https://github.com/opensource-f2f/episode 為例,說明 organization 下面的倉庫遷移。episode
是位于 organization opensource-f2f 下的一個 Repo。
獲取 Organization 信息
可以用 API 來 list 某個賬號下面的所有 organization,也可以 list 出某個具體 organization 的詳細信息。兩種方式都可以獲取到 organization ID。比如用如何命令 list 出所有的 Organizations:
$ curl -i -H "Authorization: token your-github-personal-access-token" \
https://api.github.com/user/orgs
可以得到如下返回結果:
[
{
"login": "k8smeetup",
"id": 30145889,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjMwMTQ1ODg5",
"url": "https://api.github.com/orgs/k8smeetup",
"repos_url": "https://api.github.com/orgs/k8smeetup/repos",
"events_url": "https://api.github.com/orgs/k8smeetup/events",
"hooks_url": "https://api.github.com/orgs/k8smeetup/hooks",
"issues_url": "https://api.github.com/orgs/k8smeetup/issues",
"members_url": "https://api.github.com/orgs/k8smeetup/members{/member}",
"public_members_url": "https://api.github.com/orgs/k8smeetup/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/30145889?v=4",
"description": ""
},
..... 省略其他 organization 信息 ......
{
"login": "opensource-f2f",
"id": 91397969,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxMzk3OTY5",
"url": "https://api.github.com/orgs/opensource-f2f",
"repos_url": "https://api.github.com/orgs/opensource-f2f/repos",
"events_url": "https://api.github.com/orgs/opensource-f2f/events",
"hooks_url": "https://api.github.com/orgs/opensource-f2f/hooks",
"issues_url": "https://api.github.com/orgs/opensource-f2f/issues",
"members_url": "https://api.github.com/orgs/opensource-f2f/members{/member}",
"public_members_url": "https://api.github.com/orgs/opensource-f2f/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/91397969?v=4",
"description": ""
}
]
可以在返回值中看到 organization opensource-f2f
的 ID 為 91397969。接下來 list 此 organization 下的 repo。
獲取 Organization 下的 Repo 信息
用如下命令 list 出 organization opensource-f2f 下的 Repo 信息
$ curl -i -H "Authorization: token your-github-personal-access-token" \
https://api.github.com/orgs/opensource-f2f/repos
返回值為:
[
{
"id": 410418183,
"node_id": "R_kgDOGHZ8Bw",
"name": "artwork",
"full_name": "opensource-f2f/artwork",
"private": false,
"owner": {
"login": "opensource-f2f",
"id": 91397969,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxMzk3OTY5",
"avatar_url": "https://avatars.githubusercontent.com/u/91397969?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/opensource-f2f",
"html_url": "https://github.com/opensource-f2f",
"followers_url": "https://api.github.com/users/opensource-f2f/followers",
"following_url": "https://api.github.com/users/opensource-f2f/following{/other_user}",
"gists_url": "https://api.github.com/users/opensource-f2f/gists{/gist_id}",
"starred_url": "https://api.github.com/users/opensource-f2f/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/opensource-f2f/subscriptions",
"organizations_url": "https://api.github.com/users/opensource-f2f/orgs",
"repos_url": "https://api.github.com/users/opensource-f2f/repos",
"events_url": "https://api.github.com/users/opensource-f2f/events{/privacy}",
"received_events_url": "https://api.github.com/users/opensource-f2f/received_events",
"type": "Organization",
"site_admin": false
}
......太長了,忽略其他信息......
},
{
"id": 410540405,
"node_id": "R_kgDOGHhZdQ",
"name": "episode",
"full_name": "opensource-f2f/episode",
"private": false,
"owner": {
"login": "opensource-f2f",
"id": 91397969,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxMzk3OTY5",
"avatar_url": "https://avatars.githubusercontent.com/u/91397969?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/opensource-f2f",
"html_url": "https://github.com/opensource-f2f",
"followers_url": "https://api.github.com/users/opensource-f2f/followers",
"following_url": "https://api.github.com/users/opensource-f2f/following{/other_user}",
"gists_url": "https://api.github.com/users/opensource-f2f/gists{/gist_id}",
"starred_url": "https://api.github.com/users/opensource-f2f/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/opensource-f2f/subscriptions",
"organizations_url": "https://api.github.com/users/opensource-f2f/orgs",
"repos_url": "https://api.github.com/users/opensource-f2f/repos",
"events_url": "https://api.github.com/users/opensource-f2f/events{/privacy}",
"received_events_url": "https://api.github.com/users/opensource-f2f/received_events",
"type": "Organization",
"site_admin": false
}
......太長了,忽略其他信息......
},
{
"id": 410542067,
"node_id": "R_kgDOGHhf8w",
"name": ".github",
"full_name": "opensource-f2f/.github",
"private": false,
"owner": {
"login": "opensource-f2f",
"id": 91397969,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxMzk3OTY5",
"avatar_url": "https://avatars.githubusercontent.com/u/91397969?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/opensource-f2f",
"html_url": "https://github.com/opensource-f2f",
"followers_url": "https://api.github.com/users/opensource-f2f/followers",
"following_url": "https://api.github.com/users/opensource-f2f/following{/other_user}",
"gists_url": "https://api.github.com/users/opensource-f2f/gists{/gist_id}",
"starred_url": "https://api.github.com/users/opensource-f2f/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/opensource-f2f/subscriptions",
"organizations_url": "https://api.github.com/users/opensource-f2f/orgs",
"repos_url": "https://api.github.com/users/opensource-f2f/repos",
"events_url": "https://api.github.com/users/opensource-f2f/events{/privacy}",
"received_events_url": "https://api.github.com/users/opensource-f2f/received_events",
"type": "Organization",
"site_admin": false
},
"license": {
"key": "apache-2.0",
"name": "Apache License 2.0",
"spdx_id": "Apache-2.0",
"url": "https://api.github.com/licenses/apache-2.0",
"node_id": "MDc6TGljZW5zZTI="
},
"allow_forking": true,
"is_template": false,
"topics": [
],
"visibility": "public",
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master",
"permissions": {
"admin": true,
"maintain": true,
"push": true,
"triage": true,
"pull": true
}
}
]
從返回值可以看到,organization opensource-f2f 下面有三個 Repo,ID 分別為 410418183(artwork Repo),410540405(episode Repo),410542067(.github Repo)。這些 ID 在后面導入的時候要用到。
獲取 Organization 下的 team 信息
用如下命令 list 出 organization opensource-f2f 下面的 teams
$ curl -i -H "Authorization: token your-github-personal-access-token" \
https://api.github.com/orgs/opensource-f2f/teams
返回值為:
[
{
"name": "admin",
"id": 5157920,
"node_id": "T_kwDOBXKfUc4ATrQg",
"slug": "admin",
"description": "",
"privacy": "closed",
"url": "https://api.github.com/organizations/91397969/team/5157920",
"html_url": "https://github.com/orgs/opensource-f2f/teams/admin",
"members_url": "https://api.github.com/organizations/91397969/team/5157920/members{/member}",
"repositories_url": "https://api.github.com/organizations/91397969/team/5157920/repos",
"permission": "pull",
"parent": null
},
{
"name": "guest",
"id": 5158284,
"node_id": "T_kwDOBXKfUc4ATrWM",
"slug": "guest",
"description": "The guests of the open-source talk",
"privacy": "closed",
"url": "https://api.github.com/organizations/91397969/team/5158284",
"html_url": "https://github.com/orgs/opensource-f2f/teams/guest",
"members_url": "https://api.github.com/organizations/91397969/team/5158284/members{/member}",
"repositories_url": "https://api.github.com/organizations/91397969/team/5158284/repos",
"permission": "pull",
"parent": null
}
]
可以看到有兩個 team:admin 和 guest。ID 分別為 5157920 和 5158284。
獲取 Organization team 下面的 user 信息
用如下命令 list organization pensource-f2f 下面 admin team 下的 user:
$ curl -i -H "Authorization: token your-github-personal-access-token" \
https://api.github.com/orgs/opensource-f2f/teams/admin/members
返回值如下:
[
{
"login": "LinuxSuRen",
"id": 1450685,
"node_id": "MDQ6VXNlcjE0NTA2ODU=",
"avatar_url": "https://avatars.githubusercontent.com/u/1450685?u=9ebd4373578a82540f97f204317df40a852265af&v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/LinuxSuRen",
"html_url": "https://github.com/LinuxSuRen",
"followers_url": "https://api.github.com/users/LinuxSuRen/followers",
"following_url": "https://api.github.com/users/LinuxSuRen/following{/other_user}",
"gists_url": "https://api.github.com/users/LinuxSuRen/gists{/gist_id}",
"starred_url": "https://api.github.com/users/LinuxSuRen/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/LinuxSuRen/subscriptions",
"organizations_url": "https://api.github.com/users/LinuxSuRen/orgs",
"repos_url": "https://api.github.com/users/LinuxSuRen/repos",
"events_url": "https://api.github.com/users/LinuxSuRen/events{/privacy}",
"received_events_url": "https://api.github.com/users/LinuxSuRen/received_events",
"type": "User",
"site_admin": false
},
{
"login": "wey-gu",
"id": 1651790,
"node_id": "MDQ6VXNlcjE2NTE3OTA=",
"avatar_url": "https://avatars.githubusercontent.com/u/1651790?u=5a5ea37c495f7787f35172f0f86569daf5a5a65e&v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wey-gu",
"html_url": "https://github.com/wey-gu",
"followers_url": "https://api.github.com/users/wey-gu/followers",
"following_url": "https://api.github.com/users/wey-gu/following{/other_user}",
"gists_url": "https://api.github.com/users/wey-gu/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wey-gu/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wey-gu/subscriptions",
"organizations_url": "https://api.github.com/users/wey-gu/orgs",
"repos_url": "https://api.github.com/users/wey-gu/repos",
"events_url": "https://api.github.com/users/wey-gu/events{/privacy}",
"received_events_url": "https://api.github.com/users/wey-gu/received_events",
"type": "User",
"site_admin": false
},
{
"login": "majinghe",
"id": 42570491,
"node_id": "MDQ6VXNlcjQyNTcwNDkx",
"avatar_url": "https://avatars.githubusercontent.com/u/42570491?u=6d018892fca0c602704d6b46b6bd7ca4966c7009&v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/majinghe",
"html_url": "https://github.com/majinghe",
"followers_url": "https://api.github.com/users/majinghe/followers",
"following_url": "https://api.github.com/users/majinghe/following{/other_user}",
"gists_url": "https://api.github.com/users/majinghe/gists{/gist_id}",
"starred_url": "https://api.github.com/users/majinghe/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/majinghe/subscriptions",
"organizations_url": "https://api.github.com/users/majinghe/orgs",
"repos_url": "https://api.github.com/users/majinghe/repos",
"events_url": "https://api.github.com/users/majinghe/events{/privacy}",
"received_events_url": "https://api.github.com/users/majinghe/received_events",
"type": "User",
"site_admin": false
}
]
可以看到該 team 下面有三個 user 并顯示了詳細信息。
將 user 導入極狐GitLab
將上述 list 出的 user 信息作信息匹配(如果需要,比如為了方便識別用戶,確定對應用戶的 email 等),然后可以將用戶加入到極狐GitLab 中??梢哉?Admin 人員在極狐GitLab 界面上添加 user,也可以利用下面的 API 直接調用 API 添加。不管是哪種方法,都需要確定有 Admin 權限或者找 Admin 人員完成。
$ curl --request POST --header "PRIVATE-TOKEN: jh-gitlab-personal-acess-token" \
"https://your-jh-gitlab-url/api/v4/users?email=devops008@sina.com&password=user-pasword&username=username&name=name"
返回值為:
{
"id": 4,
"username": "xiaomage",
"name": "xiaomage",
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/784477b59c09d7731bd613f1c9a5555a?s=80&d=identicon",
"web_url": "https://your-jh-gitlab-url/xiaomage",
"created_at": "2021-12-30T07:44:24.241Z",
...... 太長,刪除其他信息......
}
可以看到 user 添加成功,ID 為 4
,username 和 name 都是 xiaomage
??梢栽跇O狐GitLab 界面上看到:
可以用如上方法將 user 都導入極狐GitLab。
將項目導入極狐GitLab
由于極狐GitLab 里面是用 Group 來對項目做管理,先創建一個 Group:
$ curl --request POST \
--url "https://jihulab.com/api/v4/groups" \
--header "PRIVATE-TOKEN: jh-personal-access-token" \
--header "Content-Type: application/json" \
--data '{"name": "opensource-f2f", "path": "opensource-f2f", "description": "Create Group using API" }'
返回值如下:
{
"id":4948,
"web_url":"https://jihulab.com/groups/opensource-f2f",
"name":"opensource-f2f",
"path":"opensource-f2f",
"description":"Create Group using API",
......太長,省略其他信息......
}
名為 opensource-f2f 的 Group 已經創建起來了,在極狐GitLab 實例界面上可以看到:
接下來,將 user 添加到 Group 中,可以在極狐GitLab 界面上,直接添加:
也可以用如下的 API 進行添加:
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" \
--data "user_id=user-id&access_level=30" "https://jihulab.com/api/v4/groups/your-group-id/members"
將需要添加的 user 的 user-id 以及極狐GitLab token、目的 Group 替換完畢,執行命令即可??稍跇O狐GitLab 界面上,看到新增了一個用戶:
最后導入 GitHub Repo,比如 ID 為 410540405 的 episode:
$ curl --request POST \
--url "https://jihulab.com/api/v4/import/github" \
--header "content-type: application/json" \
--header "PRIVATE-TOKEN: your-jh-personal-access-token" \
--data '{
"personal_access_token": "your-github-personal-access-token",
"repo_id": "410540405",
"target_namespace": "opensource-f2f",
"new_name": "jh-episode"
}'
返回值如下:
{
"id":13544,
"name":"jh-episode",
"full_path":"/opensource-f2f/jh-episode",
"full_name":"opensource-f2f / jh-episode"
}
在極狐GitLab 界面查看導入的項目:
接下來可以根據需要,給此項目添加 user,方法和給 Group 添加 user 的類似??梢詫⑵渌鸊itHub 如法炮制般的全部遷移到極狐GitLab 下面,開啟極狐GitLab 之旅。