require 'trustie/gitlab/sync'

namespace :gitlab do
  namespace :sync do
    desc "sync users to gitlab"
    task :users => :environment do
      # User.where(username: 'root').find_each do |user|
      s = Trustie::Gitlab::Sync.new
      User.find_each do |user|
        s.sync_user(user)
      end
    end

    task :members => :environment do
      projects = Project.where("gpid is not null")
      s = Trustie::Gitlab::Sync.new
      g = Gitlab.client
      projects.each do |project|
        begin
          if project.members.count != g.team_members(project.gpid).count
            s.only_members(project)
          end
        rescue => e
          puts e
        end
      end
    end

    desc "update user password"
    task :password => :environment do
      s = Trustie::Gitlab::Sync.new
      s.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb')
    end

    desc "sync projects to gitlab"
    task :projects => :environment do
      s = Trustie::Gitlab::Sync.new
      Project.where(id: ENV["PROJECT_ID"]).find_each do |project|
        s.sync_project(project, path: ENV["REP_NAME"], import_url: project.repository.url)
      end
    end

    desc "remove all projects" 
    task :remove_all_projects => :environment do
      g = Gitlab.client
      100.times do
      g.projects(scope: 'all').each do |p|
        puts p.id
        begin
        g.delete_project(p.id)
        rescue => e
          puts e 
        end
      end
      end
    end

  end
end
