gitlab-org--gitlab-foss/lib/tasks/gitlab/web_hook.rake

85 lines
2.2 KiB
Ruby
Raw Normal View History

namespace :gitlab do
namespace :web_hook do
2016-03-10 14:48:29 -05:00
desc "GitLab | Adds a webhook to the projects"
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:"
projects.find_each(batch_size: 1000) do |project|
print "- #{project.name} ... "
web_hook = project.hooks.new(url: web_hook_url)
if web_hook.save
puts "added".color(:green)
else
print "failed".color(:red)
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"
task rm: :environment do
web_hook_url = ENV['URL']
namespace_path = ENV['NAMESPACE']
2018-12-06 13:30:20 -05:00
web_hooks = find_web_hooks(namespace_path)
2016-03-10 14:48:29 -05:00
puts "Removing webhooks with the url '#{web_hook_url}' ... "
2018-12-06 13:30:20 -05:00
# FIXME: Hook URLs are now encrypted, so there is no way to efficiently
# find them all in SQL. For now, check them in Ruby. If this is too slow,
# we could consider storing a hash of the URL alongside the encrypted
# value to speed up searches
count = 0
web_hooks.find_each do |hook|
next unless hook.url == web_hook_url
hook.destroy!
count += 1
end
2016-03-10 14:48:29 -05:00
puts "#{count} webhooks were removed."
end
2016-03-10 14:48:29 -05:00
desc "GitLab | List webhooks"
task list: :environment do
namespace_path = ENV['NAMESPACE']
2018-12-06 13:30:20 -05:00
web_hooks = find_web_hooks(namespace_path)
web_hooks.find_each do |hook|
puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}"
end
2018-12-06 13:30:20 -05:00
puts "\n#{web_hooks.count} webhooks found."
end
end
def find_projects(namespace_path)
if namespace_path.blank?
Project
else
2018-12-06 13:30:20 -05:00
namespace = Namespace.find_by_full_path(namespace_path)
unless namespace
puts "Namespace not found: #{namespace_path}".color(:red)
exit 2
end
2018-12-06 13:30:20 -05:00
Project.in_namespace(namespace.id)
end
end
def find_web_hooks(namespace_path)
if namespace_path.blank?
ProjectHook
else
project_ids = find_projects(namespace_path).select(:id)
ProjectHook.where(project_id: project_ids)
end
end
end