
極狐GitLab 目前暫時未提供通過“一鍵式”來完成 Gitee 到極狐GitLab 的遷移,但是可以利用 Gitee 的 API(導出方)、極狐GitLab 的 API(導入方)以及 git 命令來快速完成項目從 Gitee 到極狐GitLab 的遷移。
整體的遷移可以分為三步:倉庫的遷移、Issue 與 PR 的遷移以及最后用戶的遷移。
Repo 遷移
前期準備
- Gitee 賬號、Demo Repo(本例為 https://gitee.com/majinghe/dev-ops)
- 極狐GitLab 賬號(本文以正在內測、即將上線的極狐GitLab SaaS 為例,地址為 https://jihulab.com
Repo 遷移
在極狐GitLab 創建新項目時,選擇 import-project
:
在出現的界面中選擇 Repo by URL
:
接著需要輸入需要導出的 Gitee Repo 的地址以及 Gitee 賬號對應的用戶名和密碼:
最后點擊 Create project
即可開始 Gitee 項目的遷移。隨后可以在極狐GitLab 實例界面看到導入的項目:
由于 Gitee 會對 Repo 的路徑加分隔符 '-'(比如 DevOps 變成 dev-ops,DevSecOps 變成 dev-sec-ops,AISecOps 變成 aisec-ops 等等),最終會導致遷移過來的 Project 名稱里面會包含空格,這一點需要注意,在點擊
Create project
按鈕之前最好檢查一下 Project 名稱。
此外,在遷移的時候可以選擇將 Gitee 的項目遷移到極狐GitLab 的用戶名下還是某個 Group 下面。
下面先選擇將 Gitee Repo 遷移到極狐GitLab 的用戶下面(不在某一個特定的 Group/Subgroup)下面,最后在演示 Gitee Organization 遷移的時候會選擇遷移到特定的 Group/Subgroup。
Issue 遷移
上述方法只遷移了代碼庫到極狐GitLab 實例,但是并沒有把 Issue 及 Pull Request 遷移過來。下面演示一下如何把 Issue 遷移過來。Demo Issue 如下圖所示:
Issue 的遷移分兩步走:
從 Gitee 導出 Issue
使用如下的 API 可以直接導出對應項目的 Issue:
$ curl -X GET --header 'Content-Type: application/json;charset=UTF-8' \
'https://gitee.com/api/v5/repos/your-gitee-username/your-gitee-repo/issues?access_token=gitee-personal-access-token&state=open'
只要把 your-gitee-username/your-gitee-rep
以及 gitee-personal-access-token
進行真實值替換即可。返回值如下:
[
{
"id": 7900642,
"url": "https://gitee.com/api/v5/repos/majinghe/dev-ops/issues/I4PC6A",
"repository_url": "https://gitee.com/api/v5/repos/majinghe/dev-ops",
"labels_url": "https://gitee.com/api/v5/repos/majinghe/dev-ops/issues/I4PC6A/labels",
"comments_url": "https://gitee.com/api/v5/repos/majinghe/dev-ops/issues/I4PC6A/comments",
"html_url": "https://gitee.com/majinghe/dev-ops/issues/I4PC6A",
"parent_url": null,
"number": "I4PC6A",
"parent_id": 0,
"depth": 0,
"state": "open",
"title": "Test issue",
"body": "This is a test issue.",
......太長,保留演示所需要的內容......
}
]
在返回值中可以得到 title
、 body
等內容。這些值是 issue 的中重要內容,需要導入極狐GitLab。
由于上述命令并不能列取出 issue
中的 comment
,所以需要再用如下命令獲取該 issue
對應的 comment
:
$ curl -X GET --header 'Content-Type: application/json;charset=UTF-8' \
'https://gitee.com/api/v5/repos/majinghe/dev-ops/issues/issue-id/comments?access_token=gitee-personal-access-token'
返回值如下:
[
{
"id": 8159705,
"body": "Add some new comment!",
......太長,保留演示所需要的內容......
"created_at": "2022-01-04T22:08:43+08:00",
"updated_at": "2022-01-04T22:08:43+08:00"
}
]
body
即為 issue
對應的 comment
內容。
把 Issue 導入極狐GitLab
使用如下命令,將 Gitee Repo issue 的 title
以及 body
導入遷移過來的極狐GitLab Project:
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" \
"https://jihulab.com/api/v4/projects/project-id/issues?title=Title-Content&description=Issue-body"
參數說明:
- PRIVATE-TOKEN:極狐GitLab Personal Access Token;
- project-id:導入過來的極狐GitLab Project;
- title:issue title 內容;
- description:issue body 內容;
極狐GitLab Personal Access Token 的獲取方法參考如下視頻:
極狐GitLab Project ID 可以直接在極狐GitLab Project 獲?。?/p>
可以看到 Project ID 為 13689
。
使用上述信息,將以上面的 issue 用如下命令導入:
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" \
"https://jihulab.com/api/v4/projects/13689/issues?title=Test%20issue&description=This%20is%20a%20test%20issue."
返回值如下:
{
"id": 64292,
"iid": 1,
"project_id": 13689,
"title": "Test issue",
"description": "This is a test issue.",
"state": "opened",
"created_at": "2022-01-05T16:39:49.742+08:00",
"updated_at": "2022-01-05T16:39:49.742+08:00",
......太長,保留演示所需要的內容......
}
在極狐GitLab Project 上面可以看到 issue 已經導入成功:
再用如下命令導入 issue comment 即可:
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" \
"https://jihulab.com/api/v4/projects/13689/issues/1/notes?body=Add%20some%20new%20comment\!"
返回結果如下:
{
"id": 391898,
"type": null,
"body": "Add some new comment!",
"attachment": null,
"author": {
"id": 204,
"username": "majinghe",
"name": "JingHe Ma",
"state": "active",
"avatar_url": "https://jihulab.com/uploads/-/system/user/avatar/204/avatar.png",
"web_url": "https://jihulab.com/majinghe"
},
"created_at": "2022-01-05T16:49:48.092+08:00",
"updated_at": "2022-01-05T16:49:48.092+08:00",
......太長,保留演示所需要的內容......
}
繼續查看 issue 界面:
至此 issue 也從 Gitee 遷移到了極狐GitLab。接下來進行 PR 的遷移。
Pull Request 遷移
PR 的本質是兩個分支的文件差異對比。遷移方法有多種,下面將一一演示。
方法一:在極狐GitLab 界面上直接操作
當將倉庫從 Gitee 遷移到極狐GitLab 的時候,所有的 branch 也被遷移過來了,在極狐GitLab 界面上可以查看到:
可以看到 Gitee 上用于創建 PR 的四個分支(demo,demo-1,demo-2,demo-3,分別對應四個 PR)都存在,接下來只需要在極狐GitLab 界面上創建相應的 PR 即可:
重復相同的操作即可完成 PR 的全部創建,結果如下:
可以看到已經完成了 PR 的順利遷移。
方法二:git 命令來完成 Pull Request 遷移
git 命令的遷移本質是對于 git 的操作,需要對于 git 原理、代碼托管有比較深的認識。核心原理就是找到 Gitee PR 所對應的 commit 信息,然后用 cherry-pick 將 commit 信息提交到極狐GitLab 的新建分支上,然后創建 PR 即可。下面進行演示。
首先 clone Gitee Repo 到本地:
$ git clone git@gitee.com:majinghe/dev-ops.git
進入代碼目錄,查看遠端分支:
$ git remote -v
origin git@gitee.com:majinghe/dev-ops.git (fetch)
origin git@gitee.com:majinghe/dev-ops.git (push)
接下來將最開始遷移過來的庫添加為上述庫的遠端倉庫:
$ git remote add jh git@jihulab.com:majinghe/jh-devops.git
接著查看上述倉庫的遠端分支并拉取分支信息
$ git remote -v
jh git@jihulab.com:majinghe/jh-devops.git (fetch)
jh git@jihulab.com:majinghe/jh-devops.git (push)
origin git@gitee.com:majinghe/dev-ops.git (fetch)
origin git@gitee.com:majinghe/dev-ops.git (push)
$ git fetch jh main
From jihulab.com:majinghe/jh-devops
* branch main -> FETCH_HEAD
* [new branch] main -> jh/main
可以看到極狐GitLab 的 Project 已經為 Gitee Repo 的遠端倉庫了。接著先獲取 Gitee PR 所對應的 source branch 上的 commit 信息即可:
$ git ck demo
$ git log
commit 024b3fac1751c03cbaa9ac54f24edd2aa3ac7957 (HEAD -> demo, origin/demo)
Author: majinghe <devops008@sina.com>
Date: Tue Jan 4 09:49:12 2022 +0000
update demo.sh.
commit d70e3443357b6d497c489da78efe97610d242806 (origin/master, origin/HEAD, jh/main, master)
Author: majinghe <devops008@sina.com>
Date: Tue Jan 4 09:47:36 2022 +0000
add demo.sh file.
commit df7aa835192c9846f69c0bef1f90e2173fdc2bb4
Author: majinghe <devops008@sina.com>
Date: Tue Jan 4 09:45:22 2022 +0000
其中第一個 commit 即為創建 PR 的代碼變更,記住 commit id:024b3fac1751c03cbaa9ac54f24edd2aa3ac7957
。然后將代碼切回到極狐GitLab 遠端倉庫分支:
$ git ck -b jh-demo jh/main
上述命令基于極狐GitLab 遠端倉庫(jh)的 main 分支,新建了一個 jh-demo 分支,接下來使用 cherry-pick
命令將 Gitee PR 分支上的 commit 提交到極狐GitLab 的 jh-demo 分支上:
$ git cherry-pick 024b3fac1751c03cbaa9ac54f24edd2aa3ac7957
[jh-demo 66d67ca] update demo.sh.
Author: majinghe <devops008@sina.com>
Date: Tue Jan 4 09:49:12 2022 +0000
1 file changed, 1 insertion(+), 1 deletion(-)
然后將代碼推送到極狐GitLab 遠端倉庫即可:
$ git push jh jh-demo
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 328 bytes | 328.00 KiB/s, done.
Total 3 (delta 1), reused 1 (delta 0), pack-reused 0
remote:
remote: To create a merge request for jh-demo, visit:
remote: https://jihulab.com/majinghe/jh-devops/-/merge_requests/new?merge_request%5Bsource_branch%5D=jh-demo
remote:
To jihulab.com:majinghe/jh-devops.git
* [new branch] jh-demo -> jh-demo
最后可以在極狐GitLab 界面上看到提交的代碼:
點擊創建 Merge Request 即可:
最終完成 Gitee Pull Request 到極狐GitLab Merge Request 的遷移:
方法三:用 API 來遷移 PR
可以用如下 API 查看到 Gitee 上 PR 的變更內容:
$ curl -X GET --header 'Content-Type: application/json;charset=UTF-8' \
'https://gitee.com/api/v5/repos/your-gitee-username/your-gitee-repo/pulls/1/files?access_token=gitee-personal-access-token'
返回結果如下:
[
{
"sha": "024b3fac1751c03cbaa9ac54f24edd2aa3ac7957",
"filename": "demo.sh",
"status": null,
"additions": "1",
"deletions": "1",
"blob_url": "https://gitee.com/majinghe/dev-ops/blob/024b3fac1751c03cbaa9ac54f24edd2aa3ac7957/demo.sh",
"raw_url": "https://gitee.com/majinghe/dev-ops/raw/024b3fac1751c03cbaa9ac54f24edd2aa3ac7957/demo.sh",
"patch": {
"diff": "@@ -1,3 +1,3 @@\n #!/bin/bash\r\n \r\n-echo \"Hello World!!!\"\n\\ No newline at end of file\n+echo \"Hello World!!!,this is xiaomage\"\n\\ No newline at end of file\n",
"new_path": "demo.sh",
"old_path": "demo.sh",
"a_mode": "100644",
"b_mode": "100644",
"new_file": false,
"renamed_file": false,
"deleted_file": false,
"too_large": false
}
}
]
可以看到該 PR 變更的文件為 demo.sh
,變更內容為:
"diff": "@@ -1,3 +1,3 @@\n #!/bin/bash\r\n \r\n-echo \"Hello World!!!\"\n\\ No newline at end of file\n+echo \"Hello World!!!,this is xiaomage\"\n\\ No newline at end of file\n",
也即把 "Hello World!!!"
變更為了 "Hello World!!!,this is xiaomage"
。
接著用極狐GitLab 的如下 API 創建一個 commit
:
PAYLOAD=$(cat << 'JSON'
{
"branch": "demo",
"commit_message": "some commit message",
"actions": [
{
"action": "update",
"file_path": "demo.sh",
"content": "echo \"Hello World!!!,this is xiaomage\""
}
]
}
JSON
)
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" --header "Content-Type: application/json" \
--data "$PAYLOAD" "https://jihulab.com/api/v4/projects/13689/repository/commits"
上述命令在 demo
分支上添加一個 commit
,內容為更新 demo.sh
文件,內容變更為 "echo "Hello World!!!,this is xiaomage"
。上述命令的返回結果為:
{
"id": "2086ce1d8bf2774bd2da8f79962b72fda974f0ba",
"short_id": "2086ce1d",
"created_at": "2022-01-05T14:07:27.000+00:00",
"parent_ids": [
"19978f8a29b19a368509d3563533cccb108bca88"
],
"title": "some commit message",
"message": "some commit message",
"author_name": "JingHe Ma",
"author_email": "jhma@jihulab.com",
"authored_date": "2022-01-05T14:07:27.000+00:00",
"committer_name": "JingHe Ma",
"committer_email": "jhma@jihulab.com",
"committed_date": "2022-01-05T14:07:27.000+00:00",
"trailers": {},
"web_url": "https://jihulab.com/majinghe/jh-devops/-/commit/2086ce1d8bf2774bd2da8f79962b72fda974f0ba",
"stats": {
"additions": 0,
"deletions": 0,
"total": 0
},
"status": null,
"project_id": 13689,
"last_pipeline": null
}
可以看到 commit 信息為:https://jihulab.com/majinghe/jh-devops/-/commit/2086ce1d8bf2774bd2da8f79962b72fda974f0ba??梢钥吹降膬热萑缦拢?/p>
接著用如下命令創建 Merge Request 即可:
$ curl --request POST --header "PRIVATE-TOKEN: jh-personal-access-token" "https://jihulab.com/api/v4/projects/13689/merge_requests?source_branch=demo&target_branch=main&id=13689&title=migration%20demo%20pr" | jq .
返回結果如下:
{
"id": 32125,
"iid": 3,
"project_id": 13689,
"title": "migration demo pr",
"description": null,
"state": "opened",
"created_at": "2022-01-05T22:11:29.058+08:00",
"updated_at": "2022-01-05T22:11:29.058+08:00",
"merged_by": null,
"merged_at": null,
"closed_by": null,
"closed_at": null,
"target_branch": "main",
"source_branch": "demo",
"user_notes_count": 0,
"upvotes": 0,
"downvotes": 0,
"author": {
"id": 204,
"username": "majinghe",
"name": "JingHe Ma",
"state": "active",
"avatar_url": "https://jihulab.com/uploads/-/system/user/avatar/204/avatar.png",
"web_url": "https://jihulab.com/majinghe"
},
"assignees": [],
"assignee": null,
"reviewers": [],
"source_project_id": 13689,
"target_project_id": 13689,
"labels": [],
"draft": false,
"work_in_progress": false,
"milestone": null,
"merge_when_pipeline_succeeds": false,
"merge_status": "checking",
"sha": "2086ce1d8bf2774bd2da8f79962b72fda974f0ba",
"merge_commit_sha": null,
"squash_commit_sha": null,
"discussion_locked": null,
"should_remove_source_branch": null,
"force_remove_source_branch": null,
"reference": "!3",
"references": {
"short": "!3",
"relative": "!3",
"full": "majinghe/jh-devops!3"
},
"web_url": "https://jihulab.com/majinghe/jh-devops/-/merge_requests/3",
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
},
"squash": false,
"task_completion_status": {
"count": 0,
"completed_count": 0
},
"has_conflicts": false,
"blocking_discussions_resolved": true,
"approvals_before_merge": null,
"subscribed": true,
"changes_count": "1",
"latest_build_started_at": null,
"latest_build_finished_at": null,
"first_deployed_to_production_at": null,
"pipeline": null,
"head_pipeline": null,
"diff_refs": {
"base_sha": "d70e3443357b6d497c489da78efe97610d242806",
"head_sha": "2086ce1d8bf2774bd2da8f79962b72fda974f0ba",
"start_sha": "d70e3443357b6d497c489da78efe97610d242806"
},
"merge_error": null,
"user": {
"can_merge": true
}
}
此時在極狐GitLab Merge Request 界面上可以看到創建的 MR:
用戶的遷移
由于 Gitee 也有 Organization 的概念,Organization 下面可以添加用戶。下面以 https://gitee.com/xiaomage-devops/dev-ops.git 為例演示整個遷移過程。
可以用如下命令 list 出某個 Gitee Organization 下的 user 信息為:
$ curl -X GET --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/orgs/organization-name/members?access_token=gitee-personal-access-token'
輸入 Organization 名稱以及 Gitee personal access token,即可列取出該 Organization 下面的 user。返回值如下:
[
{
"id": 146619,
"login": "hiveer",
"name": "hiveer",
......太長,保留遷移所需信息......
"type": "User",
"member_role": "member"
},
{
"id": 9684289,
"login": "majinghe",
"name": "majinghe",
......太長,保留遷移所需信息......
"type": "User",
"member_role": "admin"
},
{
"id": 10292540,
"login": "tiaotiaott",
"name": "tiaotiao",
......太長,保留遷移所需信息......
"type": "User",
"member_role": "member"
}
]
可以看到有三個 user 信息。將上述 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 的 Group 中(極狐GitLab 用 Group/Subgroup 來對 Project 進行多層級安全管理)。此 Group/Subgroup 用來對應 Gitee 中要遷移過來的 Organization??梢栽跇O狐GitLab 上創建一個 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": "xiaomage-devops", "path": "xiaomage-devops", "description": "Create Group using API" }'
返回值如下:
{
"id": 5149,
"web_url": "https://jihulab.com/groups/xiaomage-devops",
"name": "xiaomage-devops",
"path": "xiaomage-devops",
......太長,保留演示所需要的內容......
}
名為 xiaomage-devops 的 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 界面上,看到新增了一個用戶:
接下來就可以把 Gitee Organization 下面的項目按照最開始的方法遷移過來,在導入的時候可以選擇要導入的目的 Group/Subgroup:
選擇完畢,點擊 Create Project
即可完成項目導入,結果如下:
接著就可以用上面的方法導入其他的 Repo 以及對應的 Issue 與 PR 了。趕快開啟你的極狐GitLab 之旅吧。