UNPKG

4.73 kBtext/coffeescriptView Raw
1_ = require "underscore"
2
3Config = require "../config"
4Utils = require "../utils"
5User = require "./user"
6
7class Webhook
8 constructor: (@robot) ->
9 @robot.router.post "/hubot/jira-events", (req, res) =>
10 return unless req.body?
11 event = req.body
12 if event.changelog?
13 @onChangelog event
14 else if event.comment?
15 @onComment event
16 else if event.webhookEvent is "jira:issue_created"
17 @onCreate event
18
19 res.send 'OK'
20
21 onChangelog: (event) ->
22 return unless event.changelog.items?.length > 0
23 for item in event.changelog.items
24 switch item.field
25 when "status"
26 @onStatusChange event, item
27 when "description"
28 @onDescriptionChange event, item
29 when "assignee"
30 @onAssigneeChange event, item
31
32 onComment: (event) ->
33 if Config.mention.regex.test event.comment.body
34 @onAtHandleMention event, event.comment.body.match(Config.mention.regex)[1], event.comment.body
35 if Config.jira.mentionRegex.test event.comment.body
36 @onJiraMention event, event.comment.body.match(Config.jira.mentionRegex)[1], event.comment.body
37 Create = require "./create"
38 Create.fromKey(event.issue.key)
39 .then (ticket) =>
40 if author = Utils.cache.get "#{ticket.key}:Comment"
41 User.withEmail(author)
42 .then (user) ->
43 event.comment.author = user
44 ticket
45 else
46 ticket
47 .then (ticket) =>
48 @robot.emit "JiraWebhookTicketComment", ticket, event.comment
49
50 onCreate: (event) ->
51 @onDescriptionChange event,
52 toString: event.issue.fields.description or ""
53 fromString: ""
54
55 if event.issue.fields.assignee
56 @onAssigneeChange event,
57 field: "assignee"
58 fieldtype: "jira"
59 to: event.issue.fields.assignee.name
60
61 onJiraMention: (event, username, context) ->
62 chatUser = null
63 User.withUsername(username)
64 .then (jiraUser) ->
65 chatUser = Utils.lookupChatUserWithJira jiraUser
66 Promise.reject() unless chatUser
67 Create = require "./create"
68 Create.fromKey(event.issue.key)
69 .then (ticket) =>
70 @robot.emit "JiraWebhookTicketMention", ticket, chatUser, event, context
71
72 onAtHandleMention: (event, handle, context) ->
73 chatUser = Utils.lookupChatUser handle
74 return unless chatUser
75 Create = require "./create"
76 Create.fromKey(event.issue.key)
77 .then (ticket) =>
78 @robot.emit "JiraWebhookTicketMention", ticket, chatUser, event, context
79
80 onDescriptionChange: (event, item) ->
81 if Config.mention.regex.test item.toString
82 previousMentions = item.fromString.match Config.mention.regexGlobal
83 latestMentions = item.toString.match Config.mention.regexGlobal
84 newMentions = _(latestMentions).difference previousMentions
85 for mention in newMentions
86 handle = mention.match(Config.mention.regex)[1]
87 @onAtHandleMention event, handle, item.toString
88
89 if Config.jira.mentionRegex.test item.toString
90 previousMentions = item.fromString.match Config.jira.mentionRegexGlobal
91 latestMentions = item.toString.match Config.jira.mentionRegexGlobal
92 newMentions = _(latestMentions).difference previousMentions
93 for mention in newMentions
94 username = mention.match(Config.jira.mentionRegex)[1]
95 @onJiraMention event, username, item.toString
96
97 onAssigneeChange: (event, item) ->
98 return unless item.to
99
100 chatUser = null
101 User.withUsername(item.to)
102 .then (jiraUser) ->
103 chatUser = Utils.lookupChatUserWithJira jiraUser
104 Promise.reject() unless chatUser
105 Create = require "./create"
106 Create.fromKey(event.issue.key)
107 .then (ticket) =>
108 if author = Utils.cache.get "#{ticket.key}:Assigned"
109 User.withEmail(author)
110 .then (user) ->
111 event.user = user
112 ticket
113 else
114 ticket
115 .then (ticket) =>
116 @robot.emit "JiraWebhookTicketAssigned", ticket, chatUser, event
117
118 onStatusChange: (event, item) ->
119 states = [
120 keywords: "done completed resolved fixed merged"
121 name: "JiraWebhookTicketDone"
122 ,
123 keywords: "progress"
124 name: "JiraWebhookTicketInProgress"
125 ,
126 keywords: "review reviewed"
127 name: "JiraWebhookTicketInReview"
128 ]
129 status = Utils.fuzzyFind item.toString.toLowerCase(), states, ['keywords']
130 return @robot.logger.debug "#{event.issue.key}: Ignoring transition to '#{item.toString}'" unless status
131
132 Create = require "./create"
133 Create.fromKey(event.issue.key)
134 .then (ticket) =>
135 @robot.logger.debug "#{event.issue.key}: Emitting #{status.name} because of the transition to '#{item.toString}'"
136 @robot.emit status.name, ticket, event
137
138module.exports = Webhook