====== 使用 API 方式將 GitHub Repo 匯入 GitLab ====== 想要將 GitHub 特定的 Repo 批次匯入本地端的 GitLab, 所以了解一下 GitHub 與 GitLab 現有的 API, 應該可以達成這個需求 ===== 在 GitLab 建立 Group ===== * 需要先在 * GitLab 申請一個有建立 Group 權限的 access token Exp. QMi2xxxxxxxxxxxx-oaQ * 要建立 Group name Exp. iii-org-app * 使用 GitLab API - New group POST /groups * 參考網址 - https://docs.gitlab.com/12.10/ee/api/groups.html * curl 呼叫語法: curl -H "Content-Type: application/json" -H "PRIVATE-TOKEN: QMi2xxxxxxxxxxxx-oaQ" -X POST -d '{"name": "iii-org-app","path": "iii-org-app"}' https://gitlab.iiidevops.org/api/v4/groups/ * 如果成功會回覆類似以下的訊息{ "id":17, "web_url":"http://gitlab.iiidevops.org/groups/iii-org-app", "name":"iii-org-app", "path":"iii-org-app", "description":"", "visibility":"private", : "full_name":"iii-org-app", "full_path":"iii-org-app", "created_at":"2021-02-18T12:51:00.821Z", "parent_id":null, "projects":[], "shared_projects":[] } ===== 找出 GitHub 預計匯入的 Repo_ID ===== * Exp. 要將 GitHub 上 iii-org-app 的所有 Repo 都匯入 GitLab, 先列出 iii-org-app 的所有 Repo 清單 * 使用 GitHub API - List organization repositories GET /orgs/{org}/repos * 參考網址 - https://docs.github.com/en/rest/reference/repos * curl 呼叫語法: curl -H "Accept: application/vnd.github.inertia-preview+json" https://api.github.com/orgs/iii-org-app/repos * 預計每個 Repo 需要的資料項目 "id": 336984846, "name": "django-postgresql-todo", "description": "Python+django+postgresql 的 TODO 範例", "created_at": "2021-02-08T06:46:41Z", "updated_at": "2021-02-08T06:47:14Z", "pushed_at": "2021-02-08T06:50:46Z" ===== 匯入 GitLab ===== * 需要先在 * GitLab 申請一個有匯入 repo 權限的 access token Exp. QMi2xxxxxxxxxxxx-oaQ * GitHub 申請一個可讀取 repo 的 access token Exp. de8b68cxxxxxxxxxx3a9 * 取得 GitHub 上的 Repo * id Exp. 336984846 * name Exp. django-postgresql-todo * 匯入 GitLab 的 Group Exp. iii-org-app * 使用 GitLab API - Import repository from GitHub POST /import/github * 參考網址 - https://docs.gitlab.com/12.10/ee/api/import.html * curl 呼叫語法: curl --request POST --header "PRIVATE-TOKEN: QMi2xxxxxxxxxxxx-oaQ" \ --data "personal_access_token=de8b68cxxxxxxxxxx3a9&repo_id=336984846&new_name=django-postgresql-todo&target_namespace=iii-org-app" \ https://gitlab.iiidevops.org/api/v4/import/github * 如果成功會回覆類似以下的訊息 {"id":13,"name":"django-postgresql-todo","full_path":"/iii-org-app/django-postgresql-todo","full_name":"iii-org-app / django-postgresql-todo"} {{tag>k8s gitlab github}}