is an open source collaboration platform that you can host on your own servers. It is similar to GitHub and has a nice , which we will explore and integrate with Hubot.
To create a web hook in GitLab, simply go to projet’s settings and select “Web Hooks” in the menu. Then add web hook URL and tick the checkbox of the payload you want to be delivered.
Warning: GitLab has a bug that , therefore you may see some webhook event duplications.
The following script will allow you to subscribe to repository push events using GitLab’s Web Hook. To make it work, add a web hook with http://<hubot.server>:<hubot.port>/gitlab/pushes/pubsub/gitlab.pushes
url to any GitLab repo.
scripts/gitlab-pubsub-pushes.coffee
# Description:
# hubot-pubsub based GitLab push notifier
#
# Dependencies:
# "hubot-pubsub": "1.0.0"
#
# Commands:
# None
#
# URLS:
# POST /gitlab/pushes/pubsub/<pubsub-event>
#
# Authors:
# spajus
module.exports =
(robot) ->
robot
.
router
.
post
"/gitlab/pushes/pubsub/:event"
,
(req, res) ->
res
.
end
(
''
)
event =
req
.
params
.
event
try
payload =
req
.
body
prefix =
">>> "
if
payload
.
commits
.
length
>
0
merge_commit =
false
author =
payload
.
commits
[
0
].
author
.
name
for
commit
in
payload
.
commits
if
commit
.
author
.
name
!=
author
merge_commit =
true
break
if
merge_commit
message =
"
#{
prefix
}
merged
#{
payload
.
commits
.
length
}
"
+
"commits on
#{
payload
.
repository
.
name
}
:"
+
payload
.
ref
.
replace
(
'refs/heads/'
,
''
)
robot
.
emit
'pubsub:publish'
,
event
,
message
if
payload
.
commits
.
length
<
10
for
commit
in
payload
.
commits
robot
.
emit
'pubsub:publish'
,
event
,
" *
#{
commit
.
author
.
name
}
:
#{
commit
.
message
}
(
#{
com
\
mit
.
url
}
)"
else
message =
"
#{
prefix
}#{
payload
.
commits
[
0
].
author
.
name
}
"
+
"pushed
#{
payload
.
commits
.
length
}
commits to "
+
"
#{
payload
.
repository
.
name
}
:
#{
payload
.
ref
.
replace
(
'refs/h\
eads/'
,
''
)
}
"
robot
.
emit
'pubsub:publish'
,
event
,
message
for
commit
in
payload
.
commits
robot
.
emit
'pubsub:publish'
,
event
,
" *
#{
commit
.
message
}
(
#{
com
\
mit
.
url
}
)"
catch
error
console
.
log
"gitlab-pubsub-pushes error:
#{
error
}
. Payload:
#{
req
.
body
}
"
Script source available at
Example output:
Tomas V. hubot subscribe gitlab.pushes Hubot Subscribed 585164 to gitlab.pushes events Hubot gitlab.pushes: >>> Tomas Varaneckas pushed 1 commits to Andromeda:m\ aster gitlab.pushes: * Improve planetary defense system (http://gitlab.bo\ tserv.org/spajus/andromeda/commit/1b8f2e53...)
To receive Issue notifications, begin by creating a web hook on your target repository. Set webhook url to http://<hubot.server>:<hubot.port>/gitlab/pushes/issues/gitlab.issues
and check “Issues events”.
The script:
scripts/gitlab-pubsub-issues.coffee
# Description:
# hubot-pubsub based GitLab issue notifier
#
# Dependencies:
# "hubot-pubsub": "1.0.0"
#
# Commands:
# None
#
# Configuration:
# GITLAB_ROOT_URL
# GITLAB_API_TOKEN
#
# URLS:
# POST /gitlab/issues/pubsub/<pubsub-event>
#
# Authors:
# spajus
module.exports =
(robot) ->
api_root =
"
#{
process
.
env
.
GITLAB_ROOT_URL
}
/api/v3"
robot
.
router
.
post
"/gitlab/issues/pubsub/:event"
,
(req, res) ->
res
.
end
(
''
)
event =
req
.
params
.
event
try
payload =
req
.
body
attribs =
payload
.
object_attributes
project_url =
"
#{
api_root
}
/projects/
#{
attribs
.
project_id
}
"
robot
.
http
(
project_url
)
.
header
(
'PRIVATE-TOKEN'
,
process
.
env
.
GITLAB_API_TOKEN
)
.
get
()
(err, res, body) ->
body =
JSON
.
parse
(
body
)
issue_url =
"
#{
body
.
web_url
}
/issues/
#{
attribs
.
id
}
"
issue_message =
"
#{
payload
.
object_kind
}
#{
attribs
.
state
}
:
#{
attribs
\
.
title
}
"
message =
"
#{
issue_message
}
(
#{
issue_url
}
)"
robot
.
emit
'pubsub:publish'
,
event
,
message
catch
error
console
.
log
"gitlab-pubsub-issues error:
#{
error
}
. Payload:
#{
req
.
body
}
"
Script source available at
You will have to configure it first. Find your “Private token” in “Account settings” page, then add export GITLAB_ROOT_URL="http://gitlab.your.org"
and export GITLAB_API_TOKEN=<your-private-token>
to hubot.conf
, restart Hubot, subscribe to an appropriate event and give it a try:
Example output:
Tomas V. hubot subscribe gitlab.issues Hubot Subscribed 585164 to gitlab.issues events Hubot gitlab.issues: issue opened: Squadron one out of fuel (http://gitla\ b.botserv.org/spajus/andromeda/issues/3 gitlab.issues: issue closed: Squadron one out of fuel (http://gitla\ b.botserv.org/spajus/andromeda/issues/3)
Merge Requests in GitLab is like Pull Requests in GitHub. And you can get notifications about them in your chat as well. The script is nearly identical to the one that handles Issues, but with a few slight differences. If GitLab API was a little more consistent, one script would do.
Merge Requests script’s webhook is set up just like in two scripts above, just check “Merge Request events”, and probably choose a different pubsub event name, like gitlab.merges
.
You should also add GITLAB_ROOT_URL
and GITLAB_API_TOKEN
to your hubot.conf
, as described in “Issues” section above. Now, the nearly identical script:
scripts/gitlab-pubsub-merges.coffee
# Description:
# hubot-pubsub based GitLab merge notifier
#
# Dependencies:
# "hubot-pubsub": "1.0.0"
#
# Commands:
# None
#
# Configuration:
# GITLAB_ROOT_URL
# GITLAB_API_TOKEN
#
# URLS:
# POST /gitlab/merges/pubsub/<pubsub-event>
#
# Authors:
# spajus
module.exports =
(robot) ->
api_root =
"
#{
process
.
env
.
GITLAB_ROOT_URL
}
/api/v3"
robot
.
router
.
post
"/gitlab/merges/pubsub/:event"
,
(req, res) ->
res
.
end
(
''
)
event =
req
.
params
.
event
try
payload =
req
.
body
attribs =
payload
.
object_attributes
project_url =
"
#{
api_root
}
/projects/
#{
attribs
.
target_project_id
}
"
robot
.
http
(
project_url
)
.
header
(
'PRIVATE-TOKEN'
,
process
.
env
.
GITLAB_API_TOKEN
)
.
get
()
(err, res, body) ->
body =
JSON
.
parse
(
body
)
issue_url =
"
#{
body
.
web_url
}
/merge_requests/
#{
attribs
.
id
}
"
issue_message =
"
#{
payload
.
object_kind
}
#{
attribs
.
state
}
:
#{
attribs
\
.
title
}
"
message =
"
#{
issue_message
}
(
#{
issue_url
}
)"
robot
.
emit
'pubsub:publish'
,
event
,
message
catch
error
console
.
log
"gitlab-pubsub-merges error:
#{
error
}
. Payload:
#{
req
.
body
}
"
Script source available at
How it looks in action:
Tomas V. hubot subscribe gitlab.merges Hubot Subscribed 585164 to gitlab.merges events Hubot gitlab.merges: merge_request opened: Add even more phasers (http://\ gitlab.botserv.org/spajus/andromeda/merge_requests/3)
These scripts should give you a decent starting point for deeper integration with . Try adding author’s name to Merge Request notification. Hint, you will have to query /users/#{author_id}
to get that data.