lita-enhance 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE +19 -0
  6. data/README.md +95 -0
  7. data/Rakefile +6 -0
  8. data/lib/lita-enhance.rb +7 -0
  9. data/lib/lita/handlers/enhance.rb +209 -0
  10. data/lib/lita/handlers/enhance/chef_indexer.rb +133 -0
  11. data/lib/lita/handlers/enhance/enhancer.rb +58 -0
  12. data/lib/lita/handlers/enhance/enhancers/hostname_enhancer.rb +72 -0
  13. data/lib/lita/handlers/enhance/enhancers/instance_id_enhancer.rb +40 -0
  14. data/lib/lita/handlers/enhance/enhancers/ip_enhancer.rb +40 -0
  15. data/lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb +42 -0
  16. data/lib/lita/handlers/enhance/node.rb +57 -0
  17. data/lib/lita/handlers/enhance/node_index.rb +41 -0
  18. data/lib/lita/handlers/enhance/session.rb +77 -0
  19. data/lita-enhance.gemspec +23 -0
  20. data/locales/en.yml +16 -0
  21. data/spec/data/box01.json +214 -0
  22. data/spec/data/box02.json +163 -0
  23. data/spec/data/box03.json +123 -0
  24. data/spec/data/stg-web01.json +89 -0
  25. data/spec/data/web01.json +89 -0
  26. data/spec/lita/handlers/enhance/chef_indexer_spec.rb +18 -0
  27. data/spec/lita/handlers/enhance/enhancer_example.rb +16 -0
  28. data/spec/lita/handlers/enhance/enhancers/hostname_enhancer_spec.rb +58 -0
  29. data/spec/lita/handlers/enhance/enhancers/instance_id_enhancer_spec.rb +31 -0
  30. data/spec/lita/handlers/enhance/enhancers/ip_enhancer_spec.rb +48 -0
  31. data/spec/lita/handlers/enhance/enhancers/mac_address_enhancer_spec.rb +32 -0
  32. data/spec/lita/handlers/enhance/node_index_spec.rb +33 -0
  33. data/spec/lita/handlers/enhance/node_spec.rb +51 -0
  34. data/spec/lita/handlers/enhance/session_spec.rb +48 -0
  35. data/spec/lita/handlers/enhance_spec.rb +136 -0
  36. data/spec/spec_helper.rb +64 -0
  37. metadata +168 -0
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lita::Handlers::Enhance::Session do
4
+ include_context 'indexed'
5
+
6
+ let(:session) { Lita::Handlers::Enhance::Session.new(redis, 'foo', 30) }
7
+
8
+ it 'should return nil for last_level and last_message if no message has been enhanced' do
9
+ expect(session.last_message).to be_nil
10
+ expect(session.last_level).to be_nil
11
+ end
12
+
13
+ it 'should return the last message and level after enhancing a message' do
14
+ session.enhance!('hello world', 2)
15
+ expect(session.last_message).to eq('hello world')
16
+ expect(session.last_level).to eq(2)
17
+ end
18
+
19
+ it 'should edit the message to enhance it' do
20
+ message = 'hello world box01'
21
+ enhanced_message = session.enhance!(message, 1)
22
+ expect(enhanced_message).to eq('hello world *box01*')
23
+ end
24
+
25
+ it 'should enhance at the supplied level' do
26
+ message = 'hello world box01'
27
+ enhanced_message = session.enhance!(message, 2)
28
+ expect(enhanced_message).to eq('hello world *box01 (us-west-2b)*')
29
+ end
30
+
31
+ it 'should should take care to not double enhance text', focus: true do
32
+ message = 'hello world 10.254.74.122'
33
+ enhanced_message = session.enhance!(message, 2)
34
+ expect(enhanced_message).to eq('hello world *stg-web01 (us-west-2b)*')
35
+ end
36
+
37
+ it 'should be able to enhance multiple items' do
38
+ message = 'box01 box02'
39
+ enhanced_message = session.enhance!(message, 2)
40
+ expect(enhanced_message).to eq('*box01 (us-west-2b)* *box02 (us-west-1c)*')
41
+ end
42
+
43
+ it 'should be able to correctly apply substitutions that result in shorter text' do
44
+ message = 'before 22:00:0A:FE:4A:79 F2:3C:91:56:A2:00 after'
45
+ enhanced_message = session.enhance!(message, 1)
46
+ expect(enhanced_message).to eq('before *box01* *box03* after')
47
+ end
48
+ end
@@ -0,0 +1,136 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Enhance, lita_handler: true do
4
+ include_context 'indexed'
5
+
6
+ # Make sure that we are indexing into the same Redis namespace that the handler uses.
7
+ let(:redis) { subject.redis }
8
+
9
+ let(:alice) { Lita::User.create("2", name: "Alice") }
10
+
11
+ it { routes_command('refresh enhance').to(:refresh) }
12
+ it { routes_command('enhance stats').to(:stats) }
13
+
14
+ it { routes_command('enhance 127.0.0.1').to(:enhance) }
15
+ it { routes_command("enhance lvl:1 blah\nblah").to(:enhance) }
16
+ it { routes_command("enhance").to(:enhance) }
17
+ it { routes_command("enhance lvl:2").to(:enhance) }
18
+
19
+ it 'should show stats about itself' do
20
+ send_command('enhance stats')
21
+ expect(replies.last).to include('Last refreshed')
22
+ end
23
+
24
+ it 'should be possible to force a refresh of the index' do
25
+ expect(subject).to receive(:lock_and_refresh_index)
26
+
27
+ send_command('refresh enhance')
28
+
29
+ # Give the timer a chance to run
30
+ sleep(0.5)
31
+
32
+ expect(replies).to include('Will refresh enhance index...')
33
+ expect(replies).to include('Refreshed enhance index')
34
+ end
35
+
36
+ it 'should require a message to enhance' do
37
+ send_command('enhance')
38
+ expect(replies).to include('I need a string to enhance')
39
+ end
40
+
41
+ it 'should enhance IP addresses' do
42
+ send_command('enhance 54.214.188.37')
43
+ expect(replies).to include('*box01*')
44
+ end
45
+
46
+ it 'should return text that is the same format with enhancements applied' do
47
+ send_command('enhance Finished hinted handoff of 8826 rows to endpoint /10.254.74.121')
48
+ expect(replies).to include('Finished hinted handoff of 8826 rows to endpoint /*box01*')
49
+ end
50
+
51
+ it 'should allow increasing the level of enhancement' do
52
+ send_command('enhance lvl:2 54.214.188.37')
53
+ expect(replies).to include('*box01 (us-west-2b)*')
54
+ end
55
+
56
+ it 'should return an error when the enhancement level is too high' do
57
+ send_command('enhance lvl:9 54.214.188.37')
58
+ expect(replies).to include('Cannot enhance above level 5')
59
+ end
60
+
61
+ it 'should allow implicitly enhancing the last enhanced message at a higher level' do
62
+ # Messages are enhanced at level 1 by default
63
+ send_command('enhance 54.214.188.37')
64
+ expect(replies).to include('*box01*')
65
+
66
+ # Now we're re-enhancing at level 2
67
+ send_command('enhance')
68
+ expect(replies).to include('*box01 (us-west-2b)*')
69
+ end
70
+
71
+ it 'should allow implicitly enhancing the last message at an explicit level' do
72
+ # If we explicitly enhance at level 3, the next level would be 4
73
+ send_command('enhance lvl:3 54.214.188.37')
74
+ expect(replies).to include('*box01 (us-west-2b, _default)*')
75
+
76
+ # A user can choose an explicit level....
77
+ send_command('enhance lvl:1')
78
+ expect(replies).to include('*box01*')
79
+
80
+ # ... which then is retained for the next implicit enhancement
81
+ send_command('enhance')
82
+ expect(replies).to include('*box01 (us-west-2b)*')
83
+ end
84
+
85
+ it 'implicit messages are remembered per-source' do
86
+ send_command('enhance test user 54.214.188.37')
87
+ expect(replies).to include('test user *box01*')
88
+
89
+ send_command('enhance alice 54.214.188.37', as: alice)
90
+ expect(replies).to include('alice *box01*')
91
+
92
+ send_command('enhance lvl:3')
93
+ expect(replies).to include('test user *box01 (us-west-2b, _default)*')
94
+
95
+ send_command('enhance', as: alice)
96
+ expect(replies).to include('alice *box01 (us-west-2b)*')
97
+ end
98
+
99
+ it 'should call out when nothing could be enhanced' do
100
+ send_command('enhance bubbles')
101
+ expect(replies).to include('I could not find anything to enhance')
102
+ end
103
+
104
+ describe 'under HipChat' do
105
+ before do
106
+ robot.config.robot.adapter = :hipchat
107
+ end
108
+
109
+ it 'should mark success using an emoji' do
110
+ expect(subject).to receive(:lock_and_refresh_index)
111
+
112
+ send_command('refresh enhance')
113
+
114
+ # Give the timer a chance to run
115
+ sleep(0.5)
116
+
117
+ expect(replies).to include('Will refresh enhance index...')
118
+ expect(replies).to include('(successful) Refreshed enhance index')
119
+ end
120
+
121
+ it 'mark failure using an emoji' do
122
+ send_command('enhance')
123
+ expect(replies).to include('(failed) I need a string to enhance')
124
+ end
125
+
126
+ it 'should use /quote to render mono text' do
127
+ send_command('enhance 54.214.188.37')
128
+ expect(replies).to include('/quote *box01*')
129
+ end
130
+
131
+ it 'should use emoji to call out when nothing was found to enhance' do
132
+ send_command('enhance bubbles')
133
+ expect(replies).to include('(nothingtodohere) I could not find anything to enhance')
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,64 @@
1
+ require "lita-enhance"
2
+ require "lita/rspec"
3
+
4
+ require 'pry'
5
+
6
+ RSpec.shared_context 'mocks' do
7
+ include_context 'redis'
8
+
9
+ def spec_data(file_name)
10
+ File.read(File.expand_path("../data/#{file_name}", __FILE__))
11
+ end
12
+
13
+ let(:west2_chef_node) do
14
+ chef_nodes.detect {|n| n.name == 'box01' }
15
+ end
16
+
17
+ let(:west1_chef_node) do
18
+ chef_nodes.detect {|n| n.name == 'box02' }
19
+ end
20
+
21
+ let(:linode_chef_node) do
22
+ chef_nodes.detect {|n| n.name == 'box03' }
23
+ end
24
+
25
+ let(:chef_nodes) do
26
+ Dir['spec/data/*.json'].map do |node_json|
27
+ Chef::Node.json_create(JSON.parse(IO.read(node_json)))
28
+ end
29
+ end
30
+
31
+ let(:nodes) do
32
+ chef_indexer = Lita::Handlers::Enhance::ChefIndexer.new(redis, {})
33
+ chef_nodes.map do |chef_node|
34
+ node = chef_indexer.node_from_chef_node(chef_node)
35
+ node.store!(redis)
36
+ node
37
+ end
38
+ end
39
+
40
+ let(:nodes_and_chef_nodes) do
41
+ nodes.zip(chef_nodes)
42
+ end
43
+ end
44
+
45
+ RSpec.shared_context 'redis' do
46
+ let(:redis) { Redis.new }
47
+
48
+ before do
49
+ redis.flushdb
50
+ end
51
+ end
52
+
53
+ RSpec.shared_context 'indexed' do
54
+ include_context 'mocks'
55
+ include_context 'redis'
56
+
57
+ let(:chef_indexer) { Lita::Handlers::Enhance::ChefIndexer.new(redis, {}) }
58
+
59
+ before do
60
+ chef_nodes.each do |chef_node|
61
+ chef_indexer.index_chef_node(chef_node)
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-enhance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Doug Barth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-10 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: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chef
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0.beta2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0.beta2
83
+ description: A Lita handler that enhances text by replacing opaque identifiers with
84
+ Chef node names
85
+ email:
86
+ - doug@pagerduty.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - lib/lita-enhance.rb
98
+ - lib/lita/handlers/enhance.rb
99
+ - lib/lita/handlers/enhance/chef_indexer.rb
100
+ - lib/lita/handlers/enhance/enhancer.rb
101
+ - lib/lita/handlers/enhance/enhancers/hostname_enhancer.rb
102
+ - lib/lita/handlers/enhance/enhancers/instance_id_enhancer.rb
103
+ - lib/lita/handlers/enhance/enhancers/ip_enhancer.rb
104
+ - lib/lita/handlers/enhance/enhancers/mac_address_enhancer.rb
105
+ - lib/lita/handlers/enhance/node.rb
106
+ - lib/lita/handlers/enhance/node_index.rb
107
+ - lib/lita/handlers/enhance/session.rb
108
+ - lita-enhance.gemspec
109
+ - locales/en.yml
110
+ - spec/data/box01.json
111
+ - spec/data/box02.json
112
+ - spec/data/box03.json
113
+ - spec/data/stg-web01.json
114
+ - spec/data/web01.json
115
+ - spec/lita/handlers/enhance/chef_indexer_spec.rb
116
+ - spec/lita/handlers/enhance/enhancer_example.rb
117
+ - spec/lita/handlers/enhance/enhancers/hostname_enhancer_spec.rb
118
+ - spec/lita/handlers/enhance/enhancers/instance_id_enhancer_spec.rb
119
+ - spec/lita/handlers/enhance/enhancers/ip_enhancer_spec.rb
120
+ - spec/lita/handlers/enhance/enhancers/mac_address_enhancer_spec.rb
121
+ - spec/lita/handlers/enhance/node_index_spec.rb
122
+ - spec/lita/handlers/enhance/node_spec.rb
123
+ - spec/lita/handlers/enhance/session_spec.rb
124
+ - spec/lita/handlers/enhance_spec.rb
125
+ - spec/spec_helper.rb
126
+ homepage: https://github.com/PagerDuty/lita-enhance
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ lita_plugin_type: handler
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.5
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: A Lita handler that enhances text by replacing opaque identifiers with Chef
151
+ node names
152
+ test_files:
153
+ - spec/data/box01.json
154
+ - spec/data/box02.json
155
+ - spec/data/box03.json
156
+ - spec/data/stg-web01.json
157
+ - spec/data/web01.json
158
+ - spec/lita/handlers/enhance/chef_indexer_spec.rb
159
+ - spec/lita/handlers/enhance/enhancer_example.rb
160
+ - spec/lita/handlers/enhance/enhancers/hostname_enhancer_spec.rb
161
+ - spec/lita/handlers/enhance/enhancers/instance_id_enhancer_spec.rb
162
+ - spec/lita/handlers/enhance/enhancers/ip_enhancer_spec.rb
163
+ - spec/lita/handlers/enhance/enhancers/mac_address_enhancer_spec.rb
164
+ - spec/lita/handlers/enhance/node_index_spec.rb
165
+ - spec/lita/handlers/enhance/node_spec.rb
166
+ - spec/lita/handlers/enhance/session_spec.rb
167
+ - spec/lita/handlers/enhance_spec.rb
168
+ - spec/spec_helper.rb