Repository navigation
Expand file tree
/
Copy pathgitlab.coffee
More file actions
111 lines (100 loc) · 4.42 KB
/
Copy pathgitlab.coffee
File metadata and controls
111 lines (100 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Description:
# Post gitlab related events using gitlab hooks
#
# Dependencies:
# "url" : ""
# "querystring" : ""
#
# Configuration:
# GITLAB_CHANNEL
# GITLAB_DEBUG
#
# Put http://<HUBOT_URL>:<PORT>/gitlab/system as your system hook
# Put http://<HUBOT_URL>:<PORT>/gitlab/web as your web hook (per repository)
# You can also append "?targets=%23room1,%23room2" to the URL to control the
# message destination. Using the "target" parameter to override the
# GITLAB_CHANNEL configuration value.
#
# Commands:
# None
#
# URLS:
# /gitlab/system
# /gitlab/web
#
# Author:
# omribahumi
url = require 'url'
querystring = require 'querystring'
module.exports = (robot) ->
gitlabChannel = process.env.GITLAB_CHANNEL or "#gitlab"
debug = process.env.GITLAB_DEBUG?
if robot.adapter.constructor.name is 'IrcBot'
bold = (text) ->
"\x02" + text + "\x02"
underline = (text) ->
"\x1f" + text + "\x1f"
else
bold = (text) ->
text
underline = (text) ->
text
trim_commit_url = (url) ->
url.replace(/(\/[0-9a-f]{9})[0-9a-f]+$/, '$1')
handler = (type, req, res) ->
query = querystring.parse(url.parse(req.url).query)
hook = req.body
if debug
console.log('query', query)
console.log('hook', hook)
user = {}
user.room = if query.targets then query.targets else gitlabChannel
user.type = query.type if query.type
switch type
when "system"
switch hook.event_name
when "project_create"
robot.send user, "Yay! New gitlab project #{bold(hook.name)} created by #{bold(hook.owner_name)} (#{bold(hook.owner_email)})"
when "project_destroy"
robot.send user, "Oh no! #{bold(hook.owner_name)} (#{bold(hook.owner_email)}) deleted the #{bold(hook.name)} project"
when "user_add_to_team"
robot.send user, "#{bold(hook.project_access)} access granted to #{bold(hook.user_name)} (#{bold(hook.user_email)}) on #{bold(hook.project_name)} project"
when "user_remove_from_team"
robot.send user, "#{bold(hook.project_access)} access revoked from #{bold(hook.user_name)} (#{bold(hook.user_email)}) on #{bold(hook.project_name)} project"
when "user_create"
robot.send user, "Please welcome #{bold(hook.name)} (#{bold(hook.email)}) to Gitlab!"
when "user_destroy"
robot.send user, "We will be missing #{bold(hook.name)} (#{bold(hook.email)}) on Gitlab"
when "web"
message = ""
# is it code being pushed?
if hook.ref
branch = hook.ref.split("/")[2..].join("/")
# if the ref before the commit is 00000, this is a new branch
if /^0+$/.test(hook.before)
message = "#{bold(hook.user_name)} pushed a new branch (#{bold(branch)}) to #{bold(hook.repository.name)} #{underline(encodeURI(hook.repository.homepage))}"
else
message = "#{bold(hook.user_name)} pushed to #{branch} at #{hook.repository.name} #{underline(encodeURI(hook.repository.homepage + '/compare/' + hook.before.substr(0,9) + '...' + hook.after.substr(0,9)))}"
message += "\n" + hook.commits.map((commit) -> commit.message.split("\n")[0]).join("\n")
robot.send user, message
# not code? must be a something good!
else
switch hook.object_kind
when "issue"
robot.send user, "Issue #{bold(hook.object_attributes.iid)}: #{hook.object_attributes.title} (#{hook.object_attributes.state}) at #{hook.object_attributes.url}"
when "merge_request"
link = "http://code.siftware.com/#{hook.object_attributes.target.namespace}/#{hook.object_attributes.target.name}/merge_requests/#{hook.object_attributes.iid}"
if hook.object_attributes.created_at == hook.object_attributes.updated_at
info = "New MR created \#"
else
info = "MR Updated \#"
robot.send user, "#{info}#{bold(hook.object_attributes.iid)} \"#{hook.object_attributes.title}\" (#{link})"
when "note"
if hook.object_attributes.noteable_type == "MergeRequest"
robot.send user, "New comment on MR \##{hook.merge_request.id} \"#{hook.merge_request.title}\" by #{hook.user.username} (#{link})"
robot.router.post "/gitlab/system", (req, res) ->
handler "system", req, res
res.end ""
robot.router.post "/gitlab/web", (req, res) ->
handler "web", req, res
res.end ""