lita-stackstorm 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 355c7b51c436309cd271eda9838f50e3ee509eae
4
+ data.tar.gz: 07650ddee68c8a1e03c313d1888845a7959b36be
5
+ SHA512:
6
+ metadata.gz: 6acb3369080dac3bcdd133e7ea32b37af81066f554da856f21ff5530e6d234cfa3a4311e7125c0a41f56e58add0d309775cecbf78fc7406de69279c13be8584f
7
+ data.tar.gz: 3b3725f736f71bbd1c12cfa43cf5a8bd4f012d1772049dc0bb76ed7d5cf7ff76614c907c45704aff20a6569e42d9bee1fbb75a08c4e58c7332940c95e8b9b640
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # lita-stackstorm
2
+
3
+ **lita-stackstorm** is an handler for [Lita](https://www.lita.io) that allows your bot to interact with your stackstorm installation via st2api.
4
+
5
+ ## Installation
6
+
7
+ Add lita-stackstorm to your Lita instance's Gemfile:
8
+
9
+ ``` ruby
10
+ gem "lita-stackstorm"
11
+ ```
12
+
13
+ ## Configuration
14
+
15
+ ### Required
16
+
17
+ * `url` (String) – The location of the running st2api service.
18
+ * `username` (String) – The username used to authenticate with st2api.
19
+ * `password` (String) – The password used to authenticate with st2api.
20
+
21
+ ### Optional
22
+
23
+ * `auth_port` (Integer) – Port used for Authentication. Defaults to `9101`.
24
+ * `execution_port` (Integer) – Port for executions. Defaults to `9100`.
25
+
26
+ ### Example
27
+
28
+ ``` ruby
29
+ Lita.configure do |config|
30
+ config.handlers.stackstorm.url = "https://st2.example.com"
31
+ config.handlers.stackstorm.username = ENV["ST2_USERNAME"]
32
+ config.handlers.stackstorm.password = ENV["ST2_PASSWORD"]
33
+ end
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ TODO: Describe the plugin's features and how to use them.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/stackstorm"
8
+
9
+ Lita::Handlers::Stackstorm.template_root File.expand_path(
10
+ File.join("..", "..", "templates"),
11
+ __FILE__
12
+ )
@@ -0,0 +1,132 @@
1
+ require 'json'
2
+
3
+ module Lita
4
+ module Handlers
5
+ class Stackstorm < Handler
6
+ # insert handler code here
7
+
8
+ config :url, required: true
9
+ config :username, required: true
10
+ config :password, required: true
11
+ config :auth_port, required: false, default: 9100
12
+ config :execution_port, required: false, default: 9101
13
+
14
+ class << self
15
+ attr_accessor :token, :expires
16
+ end
17
+
18
+ def self.config(config)
19
+ self.token = nil
20
+ self.expires = nil
21
+ end
22
+
23
+ route /^st2 login$/, :login, command: false, help: { "st2 login" => "login with st2-api" }
24
+ route /^st2 list$/, :list, command: false, help: { "st2 list" => "list available st2 chatops commands" }
25
+
26
+ route /^!(.*)$/, :call_alias, command: false, help: {}
27
+
28
+ def authenticate
29
+ resp = http.post("#{config.url}:#{config.auth_port}/v1/tokens") do |req|
30
+ req.body = {}
31
+ req.headers['Authorization'] = http.set_authorization_header(:basic_auth, config.username, config.password)
32
+ end
33
+ self.class.token = JSON.parse(resp.body)['token']
34
+ self.class.expires = JSON.parse(resp.body)['expiry']
35
+ resp
36
+ end
37
+
38
+ def call_alias(msg)
39
+ if expired
40
+ authenticate
41
+ end
42
+ command_array = msg.matches.flatten.first.split
43
+ candidates = redis.scan_each(:match => "#{command_array[0..1].join(' ')}*")
44
+ p = candidates.take_while {|i| i.split.length == command_array.length}
45
+ l = candidates.take_while {|i| i.split.length > command_array.length}
46
+ if p.length == 1
47
+ payload = {
48
+ name: command_array[0..1].join('_'),
49
+ format: "#{command_array[0..1].join(' ')} {{pack}}",
50
+ command: msg,
51
+ user: msg.user,
52
+ source_channel: 'chatops',
53
+ notification_channel: 'lita'
54
+ }
55
+ s = make_post_request(":#{config.execution_port}/v1/aliasexecution", payload)
56
+ msg.reply "#{config.url}:#{config.execution_port}/#/history/#{s.body.to_s[1..-2]}/general"
57
+ elsif l.length > 0
58
+ response_text = "possible matches:"
59
+ l.each do |match|
60
+ response_text+= "\n\t#{match}"
61
+ end
62
+ msg.reply response_text
63
+ else
64
+ msg.reply "Failed! No Aliases Found..."
65
+ end
66
+ end
67
+
68
+ def list(msg)
69
+ if expired
70
+ authenticate
71
+ end
72
+ s = make_request(":#{config.execution_port}/v1/actionalias", "")
73
+ if JSON.parse(s.body).empty?
74
+ msg.reply "No Action Aliases Registered"
75
+ else
76
+ j = JSON.parse(s.body)
77
+ a = ""
78
+ j.take_while{|i| i['enabled'] }.each do |command|
79
+ command['formats'].each do |format|
80
+ redis.set(format, command['action_ref'])
81
+ a+= "#{format} -> #{command['action_ref']}\n"
82
+ end
83
+ end
84
+ msg.reply a
85
+ end
86
+ end
87
+
88
+ def login(msg)
89
+ http_resp = authenticate
90
+ if ![200, 201, 280].index(http_resp.status).nil?
91
+ msg.reply "login successful\ntoken: #{self.class.token}"
92
+ elsif http_resp.status == 500
93
+ msg.reply "#{http_resp.status}: login failed!!"
94
+ else
95
+ msg.reply "#{http_resp.status}: login failed!!"
96
+ end
97
+ end
98
+
99
+ def expired
100
+ self.class.token.nil? || Time.now >= Time.parse(self.class.expires)
101
+ end
102
+
103
+ def make_request(path, body)
104
+ resp = http.get("#{config.url}#{path}") do |req|
105
+ req.body = {}
106
+ req.headers = headers
107
+ req.body = body.to_json
108
+ end
109
+ resp
110
+ end
111
+
112
+ def make_post_request(path, body)
113
+ resp = http.post("#{config.url}#{path}") do |req|
114
+ req.body = {}
115
+ req.headers = headers
116
+ req.body = body.to_json
117
+ end
118
+ resp
119
+ end
120
+
121
+
122
+ def headers
123
+ headers = {}
124
+ headers['Content-Type'] = 'application/json'
125
+ headers['X-Auth-Token'] = "#{self.class.token}"
126
+ headers
127
+ end
128
+
129
+ Lita.register_handler(self)
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-stackstorm"
3
+ spec.version = "0.4.0"
4
+ spec.authors = ["Jurnell Cockhren"]
5
+ spec.email = ["jurnell@sophicware.com"]
6
+ spec.description = "Stackstorm handler for lita 4+"
7
+ spec.summary = spec.description
8
+ spec.homepage = "https://github.com/sophicware/lita-stackstorm"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", ">= 4.4"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "pry-byebug"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rack-test"
23
+ spec.add_development_dependency "rspec", ">= 3.0.0"
24
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ stackstorm:
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Stackstorm, lita_handler: true do
4
+ end
@@ -0,0 +1,6 @@
1
+ require "lita-stackstorm"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-stackstorm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Jurnell Cockhren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ description: Stackstorm handler for lita 4+
98
+ email:
99
+ - jurnell@sophicware.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - README.md
107
+ - Rakefile
108
+ - lib/lita-stackstorm.rb
109
+ - lib/lita/handlers/stackstorm.rb
110
+ - lita-stackstorm.gemspec
111
+ - locales/en.yml
112
+ - spec/lita/handlers/stackstorm_spec.rb
113
+ - spec/spec_helper.rb
114
+ - templates/.gitkeep
115
+ homepage: https://github.com/sophicware/lita-stackstorm
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ lita_plugin_type: handler
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.5
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Stackstorm handler for lita 4+
140
+ test_files:
141
+ - spec/lita/handlers/stackstorm_spec.rb
142
+ - spec/spec_helper.rb