Commit 1df225bb384ad53ca081bdda85a805105a3eff7c
Exists in
master
and in
4 other branches
Merge pull request #3006 from jweslley/master
add rake tasks for web hooks management
Showing
2 changed files
with
96 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +### Add a web hook for **ALL** projects: | ||
| 2 | + | ||
| 3 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:add URL="http://example.com/hook" | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +### Add a web hook for projects in a given **NAMESPACE**: | ||
| 7 | + | ||
| 8 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:add URL="http://example.com/hook" NAMESPACE=acme | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +### Remove a web hook from **ALL** projects using: | ||
| 12 | + | ||
| 13 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:rm URL="http://example.com/hook" | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +### Remove a web hook from projects in a given **NAMESPACE**: | ||
| 17 | + | ||
| 18 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:rm URL="http://example.com/hook" NAMESPACE=acme | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +### List **ALL** web hooks: | ||
| 22 | + | ||
| 23 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:list | ||
| 24 | + | ||
| 25 | + | ||
| 26 | +### List the web hooks from projects in a given **NAMESPACE**: | ||
| 27 | + | ||
| 28 | + RAILS_ENV=production bundle exec rake gitlab:web_hook:list NAMESPACE=/ | ||
| 29 | + | ||
| 30 | +> Note: `/` is the global namespace. | ||
| 31 | + |
| @@ -0,0 +1,65 @@ | @@ -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 |