Commit 836b561a12a6954c5bbc87bc8fbc0f51636f71c5

Authored by Jonhnny Weslley
1 parent f9dd547a

add rake tasks for web hooks management

Showing 1 changed file with 65 additions and 0 deletions   Show diff stats
lib/tasks/gitlab/web_hook.rake 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +namespace :gitlab do
  2 + namespace :web_hook do
  3 + desc "GITLAB | Adds a web hook to the projects"
  4 + task :add => :environment do
  5 + web_hook_url = ENV['URL']
  6 + namespace_path = ENV['NAMESPACE']
  7 +
  8 + projects = find_projects(namespace_path)
  9 +
  10 + puts "Adding web hook '#{web_hook_url}' to:"
  11 + projects.find_each(batch_size: 1000) do |project|
  12 + print "- #{project.name} ... "
  13 + web_hook = project.hooks.new(url: web_hook_url)
  14 + if web_hook.save
  15 + puts "added".green
  16 + else
  17 + print "failed".red
  18 + puts " [#{web_hook.errors.full_messages.to_sentence}]"
  19 + end
  20 + end
  21 + end
  22 +
  23 + desc "GITLAB | Remove a web hook from the projects"
  24 + task :rm => :environment do
  25 + web_hook_url = ENV['URL']
  26 + namespace_path = ENV['NAMESPACE']
  27 +
  28 + projects = find_projects(namespace_path)
  29 + projects_ids = projects.pluck(:id)
  30 +
  31 + puts "Removing web hooks with the url '#{web_hook_url}' ... "
  32 + count = WebHook.where(url: web_hook_url, project_id: projects_ids, type: 'ProjectHook').delete_all
  33 + puts "#{count} web hooks were removed."
  34 + end
  35 +
  36 + desc "GITLAB | List web hooks"
  37 + task :list => :environment do
  38 + namespace_path = ENV['NAMESPACE']
  39 +
  40 + projects = find_projects(namespace_path)
  41 + web_hooks = projects.all.map(&:hooks).flatten
  42 + web_hooks.each do |hook|
  43 + puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}"
  44 + end
  45 +
  46 + puts "\n#{web_hooks.size} web hooks found."
  47 + end
  48 + end
  49 +
  50 + def find_projects(namespace_path)
  51 + if namespace_path.blank?
  52 + Project
  53 + elsif namespace_path == '/'
  54 + Project.where(namespace_id: nil)
  55 + else
  56 + namespace = Namespace.where(path: namespace_path).first
  57 + if namespace
  58 + Project.where(namespace_id: namespace.id)
  59 + else
  60 + puts "Namespace not found: #{namespace_path}".red
  61 + exit 2
  62 + end
  63 + end
  64 + end
  65 +end
... ...