debian-mirror-gitlab/lib/tasks/gitlab/web_hook.rake

66 lines
1.9 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
namespace :gitlab do
namespace :web_hook do
2015-09-11 14:41:01 +05:30
desc "GitLab | Adds a web hook to the projects"
2014-09-02 18:07:02 +05:30
task :add => :environment do
web_hook_url = ENV['URL']
namespace_path = ENV['NAMESPACE']
projects = find_projects(namespace_path)
puts "Adding web hook '#{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".green
else
print "failed".red
puts " [#{web_hook.errors.full_messages.to_sentence}]"
end
end
end
2015-09-11 14:41:01 +05:30
desc "GitLab | Remove a web hook from the projects"
2014-09-02 18:07:02 +05:30
task :rm => :environment do
web_hook_url = ENV['URL']
namespace_path = ENV['NAMESPACE']
projects = find_projects(namespace_path)
projects_ids = projects.pluck(:id)
puts "Removing web hooks with the url '#{web_hook_url}' ... "
count = WebHook.where(url: web_hook_url, project_id: projects_ids, type: 'ProjectHook').delete_all
puts "#{count} web hooks were removed."
end
2015-09-11 14:41:01 +05:30
desc "GitLab | List web hooks"
2014-09-02 18:07:02 +05:30
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
puts "\n#{web_hooks.size} web hooks found."
end
end
def find_projects(namespace_path)
if namespace_path.blank?
Project
elsif namespace_path == '/'
2015-09-11 14:41:01 +05:30
Project.in_namespace(nil)
2014-09-02 18:07:02 +05:30
else
namespace = Namespace.where(path: namespace_path).first
if namespace
2015-09-11 14:41:01 +05:30
Project.in_namespace(namespace.id)
2014-09-02 18:07:02 +05:30
else
puts "Namespace not found: #{namespace_path}".red
exit 2
end
end
end
end