This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathjira-issues.coffee
98 lines (84 loc) · 3.95 KB
/
jira-issues.coffee
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
# Description:
# Looks up jira issues when they're mentioned in chat
#
# Will ignore users set in HUBOT_JIRA_ISSUES_IGNORE_USERS (by default, JIRA and GitHub).
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_JIRA_URL (format: "https://jira-domain.com:9090")
# HUBOT_JIRA_IGNORECASE (optional; default is "true")
# HUBOT_JIRA_USERNAME (optional)
# HUBOT_JIRA_PASSWORD (optional)
# HUBOT_JIRA_ISSUES_IGNORE_USERS (optional, format: "user1|user2", default is "jira|github")
#
# Commands:
#
# Author:
# stuartf
module.exports = (robot) ->
cache = []
# In case someone upgrades form the previous version, we'll default to the
# previous behavior.
jiraUrl = process.env.HUBOT_JIRA_URL || "https://#{process.env.HUBOT_JIRA_DOMAIN}"
jiraUsername = process.env.HUBOT_JIRA_USERNAME
jiraPassword = process.env.HUBOT_JIRA_PASSWORD
if jiraUsername != undefined && jiraUsername.length > 0
auth = "#{jiraUsername}:#{jiraPassword}"
jiraIgnoreUsers = process.env.HUBOT_JIRA_ISSUES_IGNORE_USERS
if jiraIgnoreUsers == undefined
jiraIgnoreUsers = "jira|github"
robot.http(jiraUrl + "/rest/api/2/project")
.auth(auth)
.get() (err, res, body) ->
json = JSON.parse(body)
jiraPrefixes = ( entry.key for entry in json )
reducedPrefixes = jiraPrefixes.reduce (x,y) -> x + "-|" + y
jiraPattern = "/\\b(" + reducedPrefixes + "-)(\\d+)\\b/g"
ic = process.env.HUBOT_JIRA_IGNORECASE
if ic == undefined || ic == "true"
jiraPattern += "i"
robot.hear eval(jiraPattern), (msg) ->
return if msg.message.user.name.match(new RegExp(jiraIgnoreUsers, "gi"))
for i in msg.match
issue = i.toUpperCase()
now = new Date().getTime()
if cache.length > 0
cache.shift() until cache.length is 0 or cache[0].expires >= now
msg.send item.message for item in cache when item.issue is issue
if cache.length == 0 or (item for item in cache when item.issue is issue).length == 0
robot.http(jiraUrl + "/rest/api/2/issue/" + issue)
.auth(auth)
.get() (err, res, body) ->
try
json = JSON.parse(body)
key = json.key
message = "[" + key + "] " + json.fields.summary
message += '\nStatus: '+json.fields.status.name
if (json.fields.assignee == null)
message += ', unassigned'
else if ('value' of json.fields.assignee or 'displayName' of json.fields.assignee)
if (json.fields.assignee.name == "assignee" and json.fields.assignee.value.displayName)
message += ', assigned to ' + json.fields.assignee.value.displayName
else if (json.fields.assignee and json.fields.assignee.displayName)
message += ', assigned to ' + json.fields.assignee.displayName
else
message += ', unassigned'
message += ", rep. by "+json.fields.reporter.displayName
if json.fields.fixVersions and json.fields.fixVersions.length > 0
message += ', fixVersion: '+json.fields.fixVersions[0].name
else
message += ', fixVersion: NONE'
if json.fields.priority and json.fields.priority.name
message += ', priority: ' + json.fields.priority.name
urlRegex = new RegExp(jiraUrl + "[^\\s]*" + key)
if not msg.message.text.match(urlRegex)
message += "\n" + jiraUrl + "/browse/" + key
msg.send message
cache.push({issue: issue, expires: now + 120000, message: message})
catch error
try
msg.send "[*ERROR*] " + json.errorMessages[0]
catch reallyError
msg.send "[*ERROR*] " + reallyError