-
Notifications
You must be signed in to change notification settings - Fork 0
/
swimlane.coffee
134 lines (122 loc) · 3.98 KB
/
swimlane.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Description
# Swimlane Record Management and Creation
#
# Dependencies:
# hubot-conversation
#
# Configuration:
#
# Commands:
# hubot swim record new - Starts dialog to create new record
# hubot swim get apps - Lists all current applications and their ID's
# hubot swim set <field> <value> - Sets <field> to <value>
#
# Notes:
# Swimlane is an automation and incident management
# platform. Use this script to aid in automation
# efforts and resolve incidents like a boss.
# - byt3smith
#
# Author:
# Bob Argenbright - @byt3smith
Conversation = require 'hubot-conversation'
SWIM_USER = process.env.SWIM_USER
SWIM_PASS = process.env.SWIM_PASS
SWIM_SERVER = process.env.SWIM_SERVER
COOKIE = null
authHeader = null
options =
rejectUnauthorized: false
# Check Authentication
# Purpose: Self-explanatory
# #####################
checkAuth = (msg, cb) ->
if SWIM_USER?
auth(msg, cb)
else
msg.send "Swimlane credentials not configured"
# Authentication Function
# Purpose: Verifies credentials and returns cookie
##########################
auth = (msg, cb) ->
creds = "userName=#{encodeURIComponent SWIM_USER}&password=#{encodeURIComponent SWIM_PASS}"
msg.http("https://#{SWIM_SERVER}/api/user/login", options)
.headers('Content-Type': 'application/x-www-form-urlencoded')
.post(creds) (err, res, body) ->
if res.statusCode is 200
rawCookie = res.headers['set-cookie'][0]
COOKIE = rawCookie.split('; ')[0].split('=')[1]
authHeader = {'Cookie': ".AspNet.ApplicationCookie=#{COOKIE}"}
if COOKIE?
cb()
else
msg.send "IDK what happened bro, something's broken :fire:"
else
msg.send "[:x:]: #{res.statusCode}. #{body} :facepalm:"
# Application Listing
######################
listApps = (msg, cb) ->
msg.send "Retrieving current application list..."
msg.http("https://#{SWIM_SERVER}/api/apps/light", options)
.headers(authHeader)
.get() (err, res, body) ->
msg.send res.statusCode
cb(JSON.parse body)
# User Listing
######################
listUsers = (msg, cb) ->
msg.send "Retrieving current users list..."
msg.http("https://#{SWIM_SERVER}/api/user", options)
.headers(authHeader)
.get() (err, res, body) ->
msg.send res.statusCode
cb(JSON.parse body)
# Bot Configuration
####################
module.exports = (robot) ->
switchboard = new Conversation(robot)
# Dialog to insert a new record
robot.respond /swim new record/i, (msg) ->
checkAuth msg, () ->
listApps msg, (data) ->
convo = switchboard.startDialog(msg)
count = 1
message = ''
apps = []
for item in data
appName = item.name.toLowerCase()
appId = item.id
message += "#{count}) #{appName}\n"
apps.push appId
count += 1
msg.reply 'Sure, which app would you like to create a record in?'
msg.send "#{message}"
convo.addChoice /([0-9]+)/i, (msg2) ->
app = Number(msg2.match[1])
msg2.send "You chose #{app}"
if apps.length < app < 0
msg2.send ":x: That is not a valid choice, you ninny!"
else
chosen = apps[app-1]
msg2.reply("On it boss! :+1: Creating record in #{chosen}")
# Retrieve list of apps and their ID's
robot.respond /swim get apps/i, (msg) ->
checkAuth msg, () ->
listApps msg, (data) ->
message = ''
for item in data
message += "#{item.name}: #{item.id}\n"
msg.send "#{message}"
# Retrieve list of users and their ID's
robot.respond /swim get users/i, (msg) ->
checkAuth msg, () ->
listUsers msg, (data) ->
message = ''
msg.send "Total Users: #{data['total']}"
for item in data['users']
msg.send "Name: #{item.displayName}\nUsername: #{item.userName}\nID: #{item.id}\n"
# Set field as value for record
robot.respond /swim set (.*)\s(.*)/i, (msg) ->
field = msg.match[1]
status = msg.match[2]
msg.send "Closing all records with field #{field} as #{status}"