Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

GitHubのOrganization Plan導入のためGitLab.comから引っ越すための移行スクリプトを作成しました

f:id:tbpg:20160524230026p:plain

今までPrivateで扱いたい主要プロジェクトはGitHub,
ツール類など雑多なプロジェクトはGitLab.comで扱っていました。
GitHubの新プランが公開され、Private リポジトリ無制限のプランが登場したため
プランを変更して全てのリポジトリGitHubで一括管理することにしました。
手動で移行するのは面倒なので移行スクリプトを作成します。

f:id:tbpg:20160524230455p:plain

Introducing unlimited private repositories

移行対象

  • リポジトリ(全てPrivate Repository)
  • プロジェクトの説明文(description)

今回はIssueの移行はしません

前準備

  • gitlab gem をインストール済み
  • dotenv gem をインストール済み
  • hub をインストール済み
  • GitLab, GitHub のトークンを取得済み

必要な処理

スクリプト

スクリプトRubyで作成しました

環境変数

.envファイルに環境変数を設定します

GitLab, GitHubから必要な値を取得して設定してください

GITLAB_API_ENDPOINT=https://gitlab.com/api/v3
GITLAB_API_PRIVATE_TOKEN=your token
GITHUB_TOKEN=your token
ORGANIZATION=your organization

コード

require "gitlab"
require "dotenv"
require "fileutils"

Dotenv.load
Gitlab.configure do |config|
  config.endpoint       = ENV['GITLAB_API_ENDPOINT']
  config.private_token  = ENV['GITLAB_API_PRIVATE_TOKEN']
end

organization = ENV['ORGANIZATION']
projects = Gitlab.projects(per_page: 100).map{|e|e.to_hash}
repos = projects.map{|e|
  {
    name: e["name"],
    description: e["description"],
    ssh_url_to_repo: e["ssh_url_to_repo"]
  }
}

`rm -rf ./test;mkdir ./test`

all = repos.size
puts "start move repositories from GitLab to GitHub. target repository count = #{all}"
repos.each.with_index(1) do |repo, i|
  `cd ./test;git clone --mirror #{repo[:ssh_url_to_repo]}`
  `cd ./test/#{repo[:name]}.git && hub create -p -d \"#{repo[:description]}\" #{organization}/#{repo[:name]}`
  `cd ./test/#{repo[:name]}.git && git push --mirror git@github.com:#{organization}/#{repo[:name]}.git`
  puts "\t#{i}/#{all} Success push #{repo[:name]} from GitLab to GitHub"
end
puts "complete move repositories from GitLab to GitHub."

実行

$ ruby gitlab_to_github.rb
start move repositories from GitLab to GitHub. target repository count = 22
: clone の log
  1/22 Success push repo-name1 from GitLab to GitHub
: clone の log
  2/22 Success push repo-name2 from GitLab to GitHub
: 中略
  22/22 Success push repo-name22 from GitLab to GitHub
complete move repositories from GitLab to GitHub.