update-ops-gitlab-net-projects.rb 1.13 KB
Newer Older
John Jarvis's avatar
John Jarvis committed
1 2 3 4 5
## You must have the following set in your environment to use this script

#  OPS_PRIVATE_TOKEN
#  CI_PRIVATE_KEY

6
raise "Environment not set, aborting!" unless ENV['OPS_PRIVATE_TOKEN'] && ENV['CI_PRIVATE_KEY']
John Jarvis's avatar
John Jarvis committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
u = User.find_by(username: 'ops-gitlab-net')
File.open("/tmp/projects.list").each_line do |full_path|
  full_path = full_path.chomp
  group_path = File.dirname(full_path)
  path = File.basename(full_path)
  unless Group.find_by_full_path(group_path)
    puts "Warning, group #{group_path} does not exist! - skipping"
    next
  end

  p = Project.find_by_full_path(full_path)
  unless p  
    p = ::Projects::CreateService.new(u, path: path, visibility_level: Gitlab::VisibilityLevel::INTERNAL, description: "Created by ops automation", namespace_id: Group.find_by_full_path(group_path).id).execute
  end

  p.mirror = true
  p.only_mirror_protected_branches = false
  p.mirror_user = u
  p.mirror_trigger_builds = true
  p.import_url = "https://ops-gitlab-net:#{ENV['OPS_PRIVATE_TOKEN']}@gitlab.com/#{full_path}"
  p.variables.build(key: 'CI_PRIVATE_KEY', value: ENV['CI_PRIVATE_KEY']) unless p.variables.find_by(key: 'CI_PRIVATE_KEY')
  p.save
end