2014-09-02 18:07:02 +05:30
|
|
|
namespace :gitlab do
|
|
|
|
namespace :web_hook do
|
2020-03-13 15:44:24 +05:30
|
|
|
desc "GitLab | Webhook | Adds a webhook to the projects"
|
2017-08-17 22:00:37 +05:30
|
|
|
task add: :environment do
|
2014-09-02 18:07:02 +05:30
|
|
|
web_hook_url = ENV['URL']
|
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
|
|
|
projects = find_projects(namespace_path)
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
puts "Adding webhook '#{web_hook_url}' to:"
|
2014-09-02 18:07:02 +05:30
|
|
|
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-16 23:09:34 +05:30
|
|
|
puts "added".color(:green)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-06-16 23:09:34 +05:30
|
|
|
print "failed".color(:red)
|
2014-09-02 18:07:02 +05:30
|
|
|
puts " [#{web_hook.errors.full_messages.to_sentence}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
desc "GitLab | Webhook | Remove a webhook from the projects"
|
2017-08-17 22:00:37 +05:30
|
|
|
task rm: :environment do
|
2014-09-02 18:07:02 +05:30
|
|
|
web_hook_url = ENV['URL']
|
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
web_hooks = find_web_hooks(namespace_path)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
puts "Removing webhooks with the url '#{web_hook_url}' ... "
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
result = WebHooks::DestroyService.new(nil).sync_destroy(hook)
|
|
|
|
|
|
|
|
raise "Unable to destroy Web hook" unless result[:status] == :success
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
puts "#{count} webhooks were removed."
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
desc "GitLab | Webhook | List webhooks"
|
2017-08-17 22:00:37 +05:30
|
|
|
task list: :environment do
|
2014-09-02 18:07:02 +05:30
|
|
|
namespace_path = ENV['NAMESPACE']
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
web_hooks = find_web_hooks(namespace_path)
|
|
|
|
web_hooks.find_each do |hook|
|
2014-09-02 18:07:02 +05:30
|
|
|
puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}"
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
puts "\n#{web_hooks.count} webhooks found."
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_projects(namespace_path)
|
|
|
|
if namespace_path.blank?
|
|
|
|
Project
|
|
|
|
else
|
2019-02-15 15:39:39 +05:30
|
|
|
namespace = Namespace.find_by_full_path(namespace_path)
|
|
|
|
|
|
|
|
unless namespace
|
2016-06-16 23:09:34 +05:30
|
|
|
puts "Namespace not found: #{namespace_path}".color(:red)
|
2014-09-02 18:07:02 +05:30
|
|
|
exit 2
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
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)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|