GitLab | GitLab API で既存プロジェクトを別グループの新プロジェクトに移動する
概要
GitLab API で既存プロジェクトを別グループの新プロジェクトに移動します。
仕様
移動前プロジェクト: before
移動後プロジェクト: after
サンプルコード
move_project_group.rb
require 'gitlab'
require 'pp'
Gitlab.configure do |config|
config.endpoint = 'http://some_path/api/v3'
config.private_token = 'some token'
end
old_group = 'before'
new_group = 'after'
base_dir = 'some_working_directory_path' # git clone するための作業ディレクトリ。どこでもいい
old_git_url = 'git@some_path:before/%s.git'
new_git_url = 'git@some_path:after/%s.git'
target_repositoies = %w{project1 project2 project3}
Dir.chdir(base_dir)
target_repositoies.each do |repository|
clone_code = format("git clone #{old_git_url}", repository)
# 旧プロジェクトをクローン
system(clone_code)
Dir.chdir("./#{repository}")
# リポジトリの remote の設定を削除
system("git remote rm origin")
old_project = Gitlab.projects(per_page: 100).find { |e|e.name == repository }
description = old_project.description
# 新プロジェクトの作成
Gitlab.create_project(repository, description: description, namespace_id: Gitlab.groups.find { |g|g.name == new_group }.id)
# リポジトリの remote の設定を新プロジェクトにする
remote_add_code = format("git remote add origin #{new_git_url}", repository)
system(remote_add_code)
# 新プロジェクトに旧プロジェクトの内容をpush
system('git push origin master')
# 旧プロジェクトを削除
Gitlab.delete_project(old_project.id)
Dir.chdir('../')
end