-
Notifications
You must be signed in to change notification settings - Fork 2
/
super-confluence.coffee
77 lines (63 loc) · 2.45 KB
/
super-confluence.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
# Description
# A hubot script for searching and browsing Confluence. Very super.
#
# Configuration:
# HUBOT_CONFLUENCE_USERNAME - Confluence username.
# HUBOT_CONFLUENCE_PASSWORD - Confluence password.
# HUBOT_CONFLUENCE_HOST - Hostname of the Confluence instance.
# HUBOT_CONFLUENCE_CONTEXT (optional) - Often '/wiki', defaults to ''
# HUBOT_SUPER_CONFLUENCE_AUTORESPONSE_REGEX (optional) -
# Sets up a `hear` callback on the first capture group of the regex.
# For example, if set to "how do i (.*)" and someone asks 'how can i
# deploy' then the Confluence will be queried with `deploy`.
#
# Commands:
# hubot wiki <query> - Perform a full text search with <query>
#
# Author:
# Lucas Chi
confluence = require('atlassian-confluence')
confluence.username = process.env.HUBOT_CONFLUENCE_USERNAME
confluence.password = process.env.HUBOT_CONFLUENCE_PASSWORD
confluence.host = process.env.HUBOT_CONFLUENCE_HOST
confluence.context = process.env.HUBOT_CONFLUENCE_CONTEXT or ''
CONFLUENCE_BASE_URL = "https://#{confluence.host}#{confluence.context}"
module.exports = (robot) ->
robot.respond /wiki\s+(.*)$/i, (response) ->
textSearch response
autorespond_regex = process.env.HUBOT_SUPER_CONFLUENCE_AUTORESPONSE_REGEX
if autorespond_regex
robot.hear new RegExp(autorespond_regex, 'i'), (response) ->
autoRespond response
textSearch = (response) ->
query = response.match[1]
opts =
limit: 5
confluence.advancedSearch "type=page and text~\"#{query}\"", opts, (err, res) ->
if err
response.send err
else
numResults = res.results.length
if numResults is 0
response.send "No matches found for '#{query}'"
else
response.send "#{numResults} matches found (limit: #{opts.limit})"
sendMatchesFound res.results, opts.limit, (s) -> response.send s
autoRespond = (response) ->
query = response.match[1]
opts =
limit: 3
confluence.advancedSearch "type=page and text~\"#{query}\"", opts, (err, res) ->
if query.length = 0 or err
# say nothing
else
numResults = res.results.length
if numResults > 0
response.reply "Searching our wiki for \"#{query}\" reveals..."
sendMatchesFound res.results, opts.limit, (s) -> response.reply s
sendMatchesFound = (results, limit, response_function) ->
pages = []
for page in results
pageLink = "#{CONFLUENCE_BASE_URL}#{page._links.webui}"
pages.push "#{page.title} - #{pageLink}"
response_function pages.join '\n'