2013-02-15 14:35:03 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :web_hook do
|
2016-03-10 14:48:29 -05:00
|
|
|
desc "GitLab | Adds a webhook to the projects"
|
2013-02-15 14:35:03 -05:00
|
|
|
task :add => :environment do
|
|
|
|
web_hook_url = ENV['URL']
|
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
|
|
|
projects = find_projects(namespace_path)
|
|
|
|
|
2016-03-10 14:48:29 -05:00
|
|
|
puts "Adding webhook '#{web_hook_url}' to:"
|
2013-02-15 14:35:03 -05:00
|
|
|
projects.find_each(batch_size: 1000) do |project|
|
|
|
|
print "- #{project.name} ... "
|
|
|
|
web_hook = project.hooks.new(url: web_hook_url)
|
|
|
|
if web_hook.save
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "added".color(:green)
|
2013-02-15 14:35:03 -05:00
|
|
|
else
|
2016-06-01 18:37:15 -04:00
|
|
|
print "failed".color(:red)
|
2013-02-15 14:35:03 -05:00
|
|
|
puts " [#{web_hook.errors.full_messages.to_sentence}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-10 14:48:29 -05:00
|
|
|
desc "GitLab | Remove a webhook from the projects"
|
2013-02-15 14:35:03 -05:00
|
|
|
task :rm => :environment do
|
|
|
|
web_hook_url = ENV['URL']
|
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
|
|
|
projects = find_projects(namespace_path)
|
2016-08-04 00:35:17 -04:00
|
|
|
project_ids = projects.pluck(:id)
|
2013-02-15 14:35:03 -05:00
|
|
|
|
2016-03-10 14:48:29 -05:00
|
|
|
puts "Removing webhooks with the url '#{web_hook_url}' ... "
|
2016-08-04 00:35:17 -04:00
|
|
|
count = WebHook.where(url: web_hook_url, project_id: project_ids, type: 'ProjectHook').delete_all
|
2016-03-10 14:48:29 -05:00
|
|
|
puts "#{count} webhooks were removed."
|
2013-02-15 14:35:03 -05:00
|
|
|
end
|
|
|
|
|
2016-03-10 14:48:29 -05:00
|
|
|
desc "GitLab | List webhooks"
|
2013-02-15 14:35:03 -05:00
|
|
|
task :list => :environment do
|
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
|
|
|
projects = find_projects(namespace_path)
|
|
|
|
web_hooks = projects.all.map(&:hooks).flatten
|
|
|
|
web_hooks.each do |hook|
|
|
|
|
puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}"
|
|
|
|
end
|
|
|
|
|
2016-03-10 14:48:29 -05:00
|
|
|
puts "\n#{web_hooks.size} webhooks found."
|
2013-02-15 14:35:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_projects(namespace_path)
|
|
|
|
if namespace_path.blank?
|
|
|
|
Project
|
|
|
|
elsif namespace_path == '/'
|
2015-06-09 07:28:11 -04:00
|
|
|
Project.in_namespace(nil)
|
2013-02-15 14:35:03 -05:00
|
|
|
else
|
|
|
|
namespace = Namespace.where(path: namespace_path).first
|
|
|
|
if namespace
|
2015-06-09 07:28:11 -04:00
|
|
|
Project.in_namespace(namespace.id)
|
2013-02-15 14:35:03 -05:00
|
|
|
else
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Namespace not found: #{namespace_path}".color(:red)
|
2013-02-15 14:35:03 -05:00
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|