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
0
Merge Requests
0
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
Tatuya Kamada
gitlab-ce
Commits
0cd5edf3
Commit
0cd5edf3
authored
Apr 27, 2016
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backported minimal safewebhook implementation to GitLab CE
parent
40c38644
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
58 additions
and
25 deletions
+58
-25
app/models/hooks/project_hook.rb
app/models/hooks/project_hook.rb
+1
-0
app/models/hooks/service_hook.rb
app/models/hooks/service_hook.rb
+1
-0
app/models/hooks/system_hook.rb
app/models/hooks/system_hook.rb
+1
-0
app/models/hooks/web_hook.rb
app/models/hooks/web_hook.rb
+15
-9
db/migrate/20160413115152_add_token_to_web_hooks.rb
db/migrate/20160413115152_add_token_to_web_hooks.rb
+5
-0
db/schema.rb
db/schema.rb
+1
-0
spec/factories/project_hooks.rb
spec/factories/project_hooks.rb
+4
-0
spec/models/hooks/web_hook_spec.rb
spec/models/hooks/web_hook_spec.rb
+30
-16
No files found.
app/models/hooks/project_hook.rb
View file @
0cd5edf3
...
...
@@ -16,6 +16,7 @@
# note_events :boolean default(FALSE), not null
# enable_ssl_verification :boolean default(TRUE)
# build_events :boolean default(FALSE), not null
# token :string
#
class
ProjectHook
<
WebHook
...
...
app/models/hooks/service_hook.rb
View file @
0cd5edf3
...
...
@@ -16,6 +16,7 @@
# note_events :boolean default(FALSE), not null
# enable_ssl_verification :boolean default(TRUE)
# build_events :boolean default(FALSE), not null
# token :string
#
class
ServiceHook
<
WebHook
...
...
app/models/hooks/system_hook.rb
View file @
0cd5edf3
...
...
@@ -16,6 +16,7 @@
# note_events :boolean default(FALSE), not null
# enable_ssl_verification :boolean default(TRUE)
# build_events :boolean default(FALSE), not null
# token :string
#
class
SystemHook
<
WebHook
...
...
app/models/hooks/web_hook.rb
View file @
0cd5edf3
...
...
@@ -16,6 +16,7 @@
# note_events :boolean default(FALSE), not null
# enable_ssl_verification :boolean default(TRUE)
# build_events :boolean default(FALSE), not null
# token :string
#
class
WebHook
<
ActiveRecord
::
Base
...
...
@@ -43,23 +44,17 @@ class WebHook < ActiveRecord::Base
if
parsed_url
.
userinfo
.
blank?
response
=
WebHook
.
post
(
url
,
body:
data
.
to_json
,
headers:
{
"Content-Type"
=>
"application/json"
,
"X-Gitlab-Event"
=>
hook_name
.
singularize
.
titleize
},
headers:
build_headers
(
hook_name
),
verify:
enable_ssl_verification
)
else
post_url
=
url
.
gsub
(
"
#{
parsed_url
.
userinfo
}
@"
,
""
)
post_url
=
url
.
gsub
(
"
#{
parsed_url
.
userinfo
}
@"
,
''
)
auth
=
{
username:
CGI
.
unescape
(
parsed_url
.
user
),
password:
CGI
.
unescape
(
parsed_url
.
password
),
}
response
=
WebHook
.
post
(
post_url
,
body:
data
.
to_json
,
headers:
{
"Content-Type"
=>
"application/json"
,
"X-Gitlab-Event"
=>
hook_name
.
singularize
.
titleize
},
headers:
build_headers
(
hook_name
),
verify:
enable_ssl_verification
,
basic_auth:
auth
)
end
...
...
@@ -73,4 +68,15 @@ class WebHook < ActiveRecord::Base
def
async_execute
(
data
,
hook_name
)
Sidekiq
::
Client
.
enqueue
(
ProjectWebHookWorker
,
id
,
data
,
hook_name
)
end
private
def
build_headers
(
hook_name
)
headers
=
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
hook_name
.
singularize
.
titleize
}
headers
[
'X-Gitlab-Token'
]
=
token
if
token
.
present?
headers
end
end
db/migrate/20160413115152_add_token_to_web_hooks.rb
0 → 100644
View file @
0cd5edf3
class
AddTokenToWebHooks
<
ActiveRecord
::
Migration
def
change
add_column
:web_hooks
,
:token
,
:string
end
end
db/schema.rb
View file @
0cd5edf3
...
...
@@ -1025,6 +1025,7 @@ ActiveRecord::Schema.define(version: 20160421130527) do
t
.
boolean
"enable_ssl_verification"
,
default:
true
t
.
boolean
"build_events"
,
default:
false
,
null:
false
t
.
boolean
"wiki_page_events"
,
default:
false
,
null:
false
t
.
string
"token"
end
add_index
"web_hooks"
,
[
"created_at"
,
"id"
],
name:
"index_web_hooks_on_created_at_and_id"
,
using: :btree
...
...
spec/factories/project_hooks.rb
View file @
0cd5edf3
FactoryGirl
.
define
do
factory
:project_hook
do
url
{
FFaker
::
Internet
.
uri
(
'http'
)
}
trait
:token
do
token
{
SecureRandom
.
hex
(
10
)
}
end
end
end
spec/models/hooks/web_hook_spec.rb
View file @
0cd5edf3
...
...
@@ -43,51 +43,65 @@ describe WebHook, models: true do
end
describe
"execute"
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project_hook
)
{
create
(
:project_hook
)
}
before
(
:each
)
do
@project_hook
=
create
(
:project_hook
)
@project
=
create
(
:project
)
@project
.
hooks
<<
[
@project_hook
]
project
.
hooks
<<
[
project_hook
]
@data
=
{
before:
'oldrev'
,
after:
'newrev'
,
ref:
'ref'
}
WebMock
.
stub_request
(
:post
,
@project_hook
.
url
)
WebMock
.
stub_request
(
:post
,
project_hook
.
url
)
end
context
'when token is defined'
do
let
(
:project_hook
)
{
create
(
:project_hook
,
:token
)
}
it
'POSTs to the webhook URL'
do
project_hook
.
execute
(
@data
,
'push_hooks'
)
expect
(
WebMock
).
to
have_requested
(
:post
,
project_hook
.
url
).
with
(
headers:
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
'Push Hook'
,
'X-Gitlab-Token'
=>
project_hook
.
token
}
).
once
end
end
it
"POSTs to the webhook URL"
do
@
project_hook
.
execute
(
@data
,
'push_hooks'
)
expect
(
WebMock
).
to
have_requested
(
:post
,
@
project_hook
.
url
).
with
(
headers:
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
'Push Hook'
}
project_hook
.
execute
(
@data
,
'push_hooks'
)
expect
(
WebMock
).
to
have_requested
(
:post
,
project_hook
.
url
).
with
(
headers:
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
'Push Hook'
}
).
once
end
it
"POSTs the data as JSON"
do
@
project_hook
.
execute
(
@data
,
'push_hooks'
)
expect
(
WebMock
).
to
have_requested
(
:post
,
@
project_hook
.
url
).
with
(
headers:
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
'Push Hook'
}
project_hook
.
execute
(
@data
,
'push_hooks'
)
expect
(
WebMock
).
to
have_requested
(
:post
,
project_hook
.
url
).
with
(
headers:
{
'Content-Type'
=>
'application/json'
,
'X-Gitlab-Event'
=>
'Push Hook'
}
).
once
end
it
"catches exceptions"
do
expect
(
WebHook
).
to
receive
(
:post
).
and_raise
(
"Some HTTP Post error"
)
expect
{
@
project_hook
.
execute
(
@data
,
'push_hooks'
)
}.
to
raise_error
(
RuntimeError
)
expect
{
project_hook
.
execute
(
@data
,
'push_hooks'
)
}.
to
raise_error
(
RuntimeError
)
end
it
"handles SSL exceptions"
do
expect
(
WebHook
).
to
receive
(
:post
).
and_raise
(
OpenSSL
::
SSL
::
SSLError
.
new
(
'SSL error'
))
expect
(
@
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
false
,
'SSL error'
])
expect
(
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
false
,
'SSL error'
])
end
it
"handles 200 status code"
do
WebMock
.
stub_request
(
:post
,
@
project_hook
.
url
).
to_return
(
status:
200
,
body:
"Success"
)
WebMock
.
stub_request
(
:post
,
project_hook
.
url
).
to_return
(
status:
200
,
body:
"Success"
)
expect
(
@
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
true
,
'Success'
])
expect
(
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
true
,
'Success'
])
end
it
"handles 2xx status codes"
do
WebMock
.
stub_request
(
:post
,
@
project_hook
.
url
).
to_return
(
status:
201
,
body:
"Success"
)
WebMock
.
stub_request
(
:post
,
project_hook
.
url
).
to_return
(
status:
201
,
body:
"Success"
)
expect
(
@
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
true
,
'Success'
])
expect
(
project_hook
.
execute
(
@data
,
'push_hooks'
)).
to
eq
([
true
,
'Success'
])
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