BigW Consortium Gitlab

bulk_add_permission.rake 2.04 KB
Newer Older
1 2
namespace :gitlab do
  namespace :import do
3
    desc "GitLab | Add all users to all projects (admin users are added as masters)"
4 5 6
    task all_users_to_all_projects: :environment  do |t, args|
      user_ids = User.where(admin: false).pluck(:id)
      admin_ids = User.where(admin: true).pluck(:id)
7
      project_ids = Project.pluck(:id)
8

9 10
      puts "Importing #{user_ids.size} users into #{project_ids.size} projects"
      ProjectMember.add_users_to_projects(project_ids, user_ids, ProjectMember::DEVELOPER)
Lennart Rosam committed
11

12 13
      puts "Importing #{admin_ids.size} admins into #{project_ids.size} projects"
      ProjectMember.add_users_to_projects(project_ids, admin_ids, ProjectMember::MASTER)
14 15
    end

16
    desc "GitLab | Add a specific user to all projects (as a developer)"
17
    task :user_to_projects, [:email] => :environment  do |t, args|
skv committed
18
      user = User.find_by(email: args.email)
19
      project_ids = Project.pluck(:id)
Lennart Rosam committed
20
      puts "Importing #{user.email} users into #{project_ids.size} projects"
21
      ProjectMember.add_users_to_projects(project_ids, Array.wrap(user.id), ProjectMember::DEVELOPER)
22
    end
23

24
    desc "GitLab | Add all users to all groups (admin users are added as owners)"
25 26 27 28 29 30 31 32
    task all_users_to_all_groups: :environment  do |t, args|
      user_ids = User.where(admin: false).pluck(:id)
      admin_ids = User.where(admin: true).pluck(:id)
      groups = Group.all

      puts "Importing #{user_ids.size} users into #{groups.size} groups"
      puts "Importing #{admin_ids.size} admins into #{groups.size} groups"
      groups.each do |group|
33 34
        group.add_users(user_ids, GroupMember::DEVELOPER)
        group.add_users(admin_ids, GroupMember::OWNER)
35 36 37
      end
    end

38
    desc "GitLab | Add a specific user to all groups (as a developer)"
39 40 41 42 43
    task :user_to_groups, [:email] => :environment  do |t, args|
      user = User.find_by_email args.email
      groups = Group.all
      puts "Importing #{user.email} users into #{groups.size} groups"
      groups.each do |group|
44
        group.add_users(Array.wrap(user.id), GroupMember::DEVELOPER)
45 46
      end
    end
47
  end
48
end