Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
73294804
Commit
73294804
authored
Dec 06, 2018
by
Nick Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix gitlab:web_hook tasks
parent
2e3cefa6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
131 additions
and
17 deletions
+131
-17
changelogs/unreleased/54975-fix-web-hooks-rake-task.yml
changelogs/unreleased/54975-fix-web-hooks-rake-task.yml
+5
-0
doc/raketasks/web_hooks.md
doc/raketasks/web_hooks.md
+2
-4
lib/tasks/gitlab/web_hook.rake
lib/tasks/gitlab/web_hook.rake
+32
-13
spec/tasks/gitlab/web_hook_rake_spec.rb
spec/tasks/gitlab/web_hook_rake_spec.rb
+92
-0
No files found.
changelogs/unreleased/54975-fix-web-hooks-rake-task.yml
0 → 100644
View file @
73294804
---
title
:
Fix gitlab:web_hook tasks
merge_request
:
23635
author
:
type
:
fixed
doc/raketasks/web_hooks.md
View file @
73294804
...
@@ -38,8 +38,6 @@
...
@@ -38,8 +38,6 @@
## List the webhooks from projects in a given **NAMESPACE**:
## List the webhooks from projects in a given **NAMESPACE**:
# omnibus-gitlab
# omnibus-gitlab
sudo gitlab-rake gitlab:web_hook:list NAMESPACE=
/
sudo gitlab-rake gitlab:web_hook:list NAMESPACE=
acme
# source installations
# source installations
bundle exec rake gitlab:web_hook:list NAMESPACE=/ RAILS_ENV=production
bundle exec rake gitlab:web_hook:list NAMESPACE=acme RAILS_ENV=production
> Note: `/` is the global namespace.
lib/tasks/gitlab/web_hook.rake
View file @
73294804
...
@@ -25,11 +25,22 @@ namespace :gitlab do
...
@@ -25,11 +25,22 @@ namespace :gitlab do
web_hook_url
=
ENV
[
'URL'
]
web_hook_url
=
ENV
[
'URL'
]
namespace_path
=
ENV
[
'NAMESPACE'
]
namespace_path
=
ENV
[
'NAMESPACE'
]
projects
=
find_projects
(
namespace_path
)
web_hooks
=
find_web_hooks
(
namespace_path
)
project_ids
=
projects
.
pluck
(
:id
)
puts
"Removing webhooks with the url '
#{
web_hook_url
}
' ... "
puts
"Removing webhooks with the url '
#{
web_hook_url
}
' ... "
count
=
WebHook
.
where
(
url:
web_hook_url
,
project_id:
project_ids
,
type:
'ProjectHook'
).
delete_all
# 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
puts
"
#{
count
}
webhooks were removed."
puts
"
#{
count
}
webhooks were removed."
end
end
...
@@ -37,29 +48,37 @@ namespace :gitlab do
...
@@ -37,29 +48,37 @@ namespace :gitlab do
task
list: :environment
do
task
list: :environment
do
namespace_path
=
ENV
[
'NAMESPACE'
]
namespace_path
=
ENV
[
'NAMESPACE'
]
projects
=
find_projects
(
namespace_path
)
web_hooks
=
find_web_hooks
(
namespace_path
)
web_hooks
=
projects
.
all
.
map
(
&
:hooks
).
flatten
web_hooks
.
find_each
do
|
hook
|
web_hooks
.
each
do
|
hook
|
puts
"
#{
hook
.
project
.
name
.
truncate
(
20
).
ljust
(
20
)
}
->
#{
hook
.
url
}
"
puts
"
#{
hook
.
project
.
name
.
truncate
(
20
).
ljust
(
20
)
}
->
#{
hook
.
url
}
"
end
end
puts
"
\n
#{
web_hooks
.
size
}
webhooks found."
puts
"
\n
#{
web_hooks
.
count
}
webhooks found."
end
end
end
end
def
find_projects
(
namespace_path
)
def
find_projects
(
namespace_path
)
if
namespace_path
.
blank?
if
namespace_path
.
blank?
Project
Project
elsif
namespace_path
==
'/'
Project
.
in_namespace
(
nil
)
else
else
namespace
=
Namespace
.
where
(
path:
namespace_path
).
first
namespace
=
Namespace
.
find_by_full_path
(
namespace_path
)
if
namespace
Project
.
in_namespace
(
namespace
.
id
)
unless
namespace
else
puts
"Namespace not found:
#{
namespace_path
}
"
.
color
(
:red
)
puts
"Namespace not found:
#{
namespace_path
}
"
.
color
(
:red
)
exit
2
exit
2
end
end
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
end
end
end
spec/tasks/gitlab/web_hook_rake_spec.rb
0 → 100644
View file @
73294804
require
'rake_helper'
describe
'gitlab:web_hook namespace rake tasks'
do
set
(
:group
)
{
create
(
:group
)
}
set
(
:project1
)
{
create
(
:project
,
namespace:
group
)
}
set
(
:project2
)
{
create
(
:project
,
namespace:
group
)
}
set
(
:other_group_project
)
{
create
(
:project
)
}
let
(
:url
)
{
'http://example.com'
}
let
(
:hook_urls
)
{
(
project1
.
hooks
+
project2
.
hooks
).
map
(
&
:url
)
}
let
(
:other_group_hook_urls
)
{
other_group_project
.
hooks
.
map
(
&
:url
)
}
before
do
Rake
.
application
.
rake_require
'tasks/gitlab/web_hook'
end
describe
'gitlab:web_hook:add'
do
it
'adds a web hook to all projects'
do
stub_env
(
'URL'
=>
url
)
run_rake_task
(
'gitlab:web_hook:add'
)
expect
(
hook_urls
).
to
contain_exactly
(
url
,
url
)
expect
(
other_group_hook_urls
).
to
contain_exactly
(
url
)
end
it
'adds a web hook to projects in the specified namespace'
do
stub_env
(
'URL'
=>
url
,
'NAMESPACE'
=>
group
.
full_path
)
run_rake_task
(
'gitlab:web_hook:add'
)
expect
(
hook_urls
).
to
contain_exactly
(
url
,
url
)
expect
(
other_group_hook_urls
).
to
be_empty
end
it
'raises an error if an unknown namespace is specified'
do
stub_env
(
'URL'
=>
url
,
'NAMESPACE'
=>
group
.
full_path
)
group
.
destroy
expect
{
run_rake_task
(
'gitlab:web_hook:add'
)
}.
to
raise_error
(
SystemExit
)
end
end
describe
'gitlab:web_hook:rm'
do
let!
(
:hook1
)
{
create
(
:project_hook
,
project:
project1
,
url:
url
)
}
let!
(
:hook2
)
{
create
(
:project_hook
,
project:
project2
,
url:
url
)
}
let!
(
:other_group_hook
)
{
create
(
:project_hook
,
project:
other_group_project
,
url:
url
)
}
let!
(
:other_url_hook
)
{
create
(
:project_hook
,
url:
other_url
,
project:
project1
)
}
let
(
:other_url
)
{
'http://other.example.com'
}
it
'removes a web hook from all projects by URL'
do
stub_env
(
'URL'
=>
url
)
run_rake_task
(
'gitlab:web_hook:rm'
)
expect
(
hook_urls
).
to
contain_exactly
(
other_url
)
expect
(
other_group_hook_urls
).
to
be_empty
end
it
'removes a web hook from projects in the specified namespace by URL'
do
stub_env
(
'NAMESPACE'
=>
group
.
full_path
,
'URL'
=>
url
)
run_rake_task
(
'gitlab:web_hook:rm'
)
expect
(
hook_urls
).
to
contain_exactly
(
other_url
)
expect
(
other_group_hook_urls
).
to
contain_exactly
(
url
)
end
it
'raises an error if an unknown namespace is specified'
do
stub_env
(
'URL'
=>
url
,
'NAMESPACE'
=>
group
.
full_path
)
group
.
destroy
expect
{
run_rake_task
(
'gitlab:web_hook:rm'
)
}.
to
raise_error
(
SystemExit
)
end
end
describe
'gitlab:web_hook:list'
do
let!
(
:hook1
)
{
create
(
:project_hook
,
project:
project1
)
}
let!
(
:hook2
)
{
create
(
:project_hook
,
project:
project2
)
}
let!
(
:other_group_hook
)
{
create
(
:project_hook
,
project:
other_group_project
)
}
it
'lists all web hooks'
do
expect
{
run_rake_task
(
'gitlab:web_hook:list'
)
}.
to
output
(
/3 webhooks found/
).
to_stdout
end
it
'lists web hooks in a particular namespace'
do
stub_env
(
'NAMESPACE'
,
group
.
full_path
)
expect
{
run_rake_task
(
'gitlab:web_hook:list'
)
}.
to
output
(
/2 webhooks found/
).
to_stdout
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment