lita-locker 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51d68722a9a837146295120ccd13ec48d9135bbe
4
- data.tar.gz: a821ec80a688f3b6ff13cae131d6fddb3c2233a5
3
+ metadata.gz: f96bd48a3cc828d528cce0186e9f06b17eb00447
4
+ data.tar.gz: 1f7497cce3183e0e76c330a75515da10871b13d5
5
5
  SHA512:
6
- metadata.gz: 793ac5553e20c774572bc4f558c9badc5086b03689c5bcb8f5f48e2e5acc0d9633940a3f0bb1e3d509450fbfb47a93682477f20166e970b383e3c177b0acbef1
7
- data.tar.gz: f7c7b6596d3b5d1a5b7eadedcdd9c0fad27cc929cfd4a650da06890cfb43227cf01b4d259542cc050466e95583c8955b0e79de14e790d6bce647db69a5ff427e
6
+ metadata.gz: a24977fe218e05da74fc7c153fb3f01034a57286f38c2440bb1c9be16952d306e4fb4dcb910d066c12bca99c39d02740b5984524db841e94244d8c305708eb53
7
+ data.tar.gz: f815885ce041b6fafd846bc052a458c920fc3765093de89ec8d26c31e144793e637ae8e43ab620af979bc7d313bf639dec81720faece20822f72c88940f7b2b6
@@ -41,6 +41,13 @@ module Lita
41
41
  help: { t('help.steal.syntax') => t('help.steal.desc') }
42
42
  )
43
43
 
44
+ route(
45
+ /^locker\sgive\s#{LABEL_REGEX}\sto\s#{USER_REGEX}#{COMMENT_REGEX}$/,
46
+ :give,
47
+ command: true,
48
+ help: { t('help.give.syntax') => t('help.give.desc') }
49
+ )
50
+
44
51
  route(
45
52
  /^locker\sobserve\s#{LABEL_REGEX}#{COMMENT_REGEX}$/,
46
53
  :observe,
@@ -118,8 +125,33 @@ module Lita
118
125
  response.reply(attempt_steal(name, response.user))
119
126
  end
120
127
 
128
+ def give(response)
129
+ name = response.match_data['label'].rstrip
130
+
131
+ return response.reply(failed(t('subject.does_not_exist', name: name))) unless Label.exists?(name)
132
+ l = Label.new(name)
133
+ owner_mention = l.owner.mention_name ? "(@#{l.owner.mention_name})" : ''
134
+ return response.reply(t('give.not_owner',
135
+ label: name,
136
+ owner: l.owner.name,
137
+ mention: owner_mention)) unless l.owner == response.user
138
+ recipient = Lita::User.fuzzy_find(response.match_data['username'].rstrip)
139
+ return response.reply(t('user.unknown')) unless recipient
140
+
141
+ response.reply(attempt_give(name, response.user, recipient))
142
+ end
143
+
121
144
  private
122
145
 
146
+ def attempt_give(name, giver, recipient)
147
+ label = Label.new(name)
148
+ return t('give.self') if recipient == giver
149
+ old_owner = label.owner
150
+ label.give!(recipient.id)
151
+ mention = recipient.mention_name ? "(@#{recipient.mention_name})" : ''
152
+ success(t('give.given', label: name, giver: old_owner.name, recipient: recipient.name, mention: mention))
153
+ end
154
+
123
155
  def attempt_steal(name, user)
124
156
  label = Label.new(name)
125
157
  return t('steal.self') if label.owner == user
@@ -58,11 +58,7 @@ module Lita
58
58
  return response.reply(t('subject.does_not_exist', name: name)) unless Label.exists?(name)
59
59
  l = Label.new(name)
60
60
  l.wait_queue.delete(response.user.id)
61
- queued = l.wait_queue.to_a
62
- l.wait_queue.clear
63
- queued.chunk { |x| x }.map(&:first).each do |user|
64
- l.wait_queue << user
65
- end
61
+ l.dedupe!
66
62
  response.reply(t('label.removed_from_queue', name: name))
67
63
  end
68
64
 
@@ -103,9 +103,25 @@ module Locker
103
103
  def steal!(owner_id)
104
104
  log("Stolen from #{owner.id} to #{owner_id}")
105
105
  wait_queue.unshift(owner_id)
106
+ self.dedupe!
106
107
  self.unlock!
107
108
  end
108
109
 
110
+ def give!(recipient_id)
111
+ log("Given from #{owner.id} to #{recipient_id}")
112
+ wait_queue.unshift(recipient_id)
113
+ self.dedupe!
114
+ self.unlock!
115
+ end
116
+
117
+ def dedupe!
118
+ queued = wait_queue.to_a
119
+ wait_queue.clear
120
+ queued.chunk { |x| x }.map(&:first).each do |user|
121
+ wait_queue << user
122
+ end
123
+ end
124
+
109
125
  def add_observer!(observer_id)
110
126
  observer_ids.add(observer_id)
111
127
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-locker'
3
- spec.version = '1.0.3'
3
+ spec.version = '1.0.4'
4
4
  spec.authors = ['Eric Sigler']
5
5
  spec.email = ['me@esigler.com']
6
6
  spec.description = '"lock" and "unlock" arbitrary subjects'
@@ -6,6 +6,10 @@ en:
6
6
  stolen: "%{label} stolen from %{old_owner} %{mention}"
7
7
  already_unlocked: "%{label} was already unlocked"
8
8
  self: Why are you stealing the lock from yourself?
9
+ give:
10
+ not_owner: "The lock on %{label} can only be given by its current owner: %{owner} %{mention}"
11
+ given: "%{giver} gave %{label} to %{recipient} %{mention}"
12
+ self: Why are you giving the lock to yourself?
9
13
  observe:
10
14
  now_observing: "Now observing %{name}"
11
15
  already_observing: "You are already observing %{name}"
@@ -33,6 +37,9 @@ en:
33
37
  steal:
34
38
  syntax: steal <subject>
35
39
  desc: Force removal of a reservation. Can have # comments afterwards.
40
+ give:
41
+ syntax: give <subject> to <username>
42
+ desc: Transfer ownership of a lock to another user. Can have # comments afterwards.
36
43
  status:
37
44
  syntax: locker status <label or resource>
38
45
  desc: Show the current state of <label or resource>
@@ -80,7 +80,7 @@ describe Lita::Handlers::LockerMisc, lita_handler: true do
80
80
  send_command('lock foo', as: bob)
81
81
  send_command('locker dequeue foo', as: doris)
82
82
  send_command('locker status foo')
83
- expect(replies.last).to eq('foo is locked by Alice (taken 1 second ago). Next up: Bob')
83
+ expect(replies.last).to match(/^foo is locked by Alice \(taken \d seconds? ago\)\. Next up: Bob$/)
84
84
  end
85
85
  end
86
86
 
@@ -93,13 +93,13 @@ describe Lita::Handlers::LockerMisc, lita_handler: true do
93
93
  expect(replies.last).to eq('foo is unlocked')
94
94
  send_command('lock foo')
95
95
  send_command('locker status foo')
96
- expect(replies.last).to eq('foo is locked by Test User (taken 1 second ago)')
96
+ expect(replies.last).to match(/^foo is locked by Test User \(taken \d seconds? ago\)$/)
97
97
  send_command('lock foo', as: alice)
98
98
  send_command('locker status foo')
99
- expect(replies.last).to eq('foo is locked by Test User (taken 1 second ago). Next up: Alice')
99
+ expect(replies.last).to match(/^foo is locked by Test User \(taken \d seconds? ago\)\. Next up: Alice$/)
100
100
  send_command('lock foo', as: bob)
101
101
  send_command('locker status foo')
102
- expect(replies.last).to eq('foo is locked by Test User (taken 1 second ago). Next up: Alice, Bob')
102
+ expect(replies.last).to match(/^foo is locked by Test User \(taken \d seconds? ago\)\. Next up: Alice, Bob$/)
103
103
  end
104
104
 
105
105
  it 'shows the status of a resource' do
@@ -30,6 +30,8 @@ describe Lita::Handlers::Locker, lita_handler: true do
30
30
  is_expected.to route_command("steal #{l}").to(:steal)
31
31
  is_expected.to route_command("steal #{l} ").to(:steal)
32
32
  is_expected.to route_command("steal #{l} #this is a comment").to(:steal)
33
+ is_expected.to route_command("locker give #{l} to alice").to(:give)
34
+ is_expected.to route_command("locker give #{l} to alice #this is a comment").to(:give)
33
35
  is_expected.to route_command("locker observe #{l}").to(:observe)
34
36
  is_expected.to route_command("locker observe #{l} #this is a comment").to(:observe)
35
37
  is_expected.to route_command("locker unobserve #{l}").to(:unobserve)
@@ -37,15 +39,15 @@ describe Lita::Handlers::Locker, lita_handler: true do
37
39
  end
38
40
  end
39
41
 
40
- let(:alice) do
42
+ let!(:alice) do
41
43
  Lita::User.create('9001@hipchat', name: 'Alice', mention_name: 'alice')
42
44
  end
43
45
 
44
- let(:bob) do
46
+ let!(:bob) do
45
47
  Lita::User.create('9002@hipchat', name: 'Bob', mention_name: 'bob')
46
48
  end
47
49
 
48
- let(:charlie) do
50
+ let!(:charlie) do
49
51
  Lita::User.create('9003@hipchat', name: 'Charlie', mention_name: 'charlie')
50
52
  end
51
53
 
@@ -85,7 +87,7 @@ describe Lita::Handlers::Locker, lita_handler: true do
85
87
  send_command('lock bazbat', as: bob)
86
88
  send_command('lock bazbat', as: bob)
87
89
  send_command('locker status bazbat')
88
- expect(replies.last).to eq('bazbat is locked by Alice (taken 1 second ago). Next up: Bob')
90
+ expect(replies.last).to match(/^bazbat is locked by Alice \(taken \d seconds? ago\)\. Next up: Bob$/)
89
91
  end
90
92
 
91
93
  it 'shows a warning when a label has no resources' do
@@ -125,8 +127,9 @@ describe Lita::Handlers::Locker, lita_handler: true do
125
127
  send_command('locker label add foobar to bazbat')
126
128
  send_command('lock bazbat', as: alice)
127
129
  send_command('lock bazbat', as: bob)
128
- expect(replies.last).to eq('bazbat is locked by Alice (@alice) (taken 1 second ago), you have been ' \
129
- 'added to the queue (currently: Bob), type \'locker dequeue bazbat\' to be removed')
130
+ # rubocop:disable Metrics/LineLength
131
+ expect(replies.last).to match(/^bazbat is locked by Alice \(@alice\) \(taken \d seconds? ago\), you have been added to the queue \(currently: Bob\), type 'locker dequeue bazbat' to be removed$/)
132
+ # rubocop:enable Metrics/LineLength
130
133
  end
131
134
 
132
135
  it 'shows an error when a label does not exist' do
@@ -208,7 +211,7 @@ describe Lita::Handlers::Locker, lita_handler: true do
208
211
  send_command('locker label add foobar to bazbat')
209
212
  send_command('lock bazbat', as: alice)
210
213
  send_command('unlock bazbat', as: bob)
211
- expect(replies.last).to eq('bazbat is locked by Alice (@alice) (taken 1 second ago)')
214
+ expect(replies.last).to match(/^bazbat is locked by Alice \(@alice\) \(taken \d seconds? ago\)$/)
212
215
  end
213
216
 
214
217
  it 'shows a warning when a label is already unlocked' do
@@ -244,7 +247,7 @@ describe Lita::Handlers::Locker, lita_handler: true do
244
247
  send_command('lock bazbat', as: bob)
245
248
  send_command('steal bazbat', as: charlie)
246
249
  send_command('locker status bazbat')
247
- expect(replies.last).to eq('bazbat is locked by Charlie (taken 1 second ago). Next up: Bob')
250
+ expect(replies.last).to match(/^bazbat is locked by Charlie \(taken \d seconds? ago\)\. Next up: Bob$/)
248
251
  end
249
252
 
250
253
  it 'shows a warning when the label is already unlocked' do
@@ -270,6 +273,61 @@ describe Lita::Handlers::Locker, lita_handler: true do
270
273
  end
271
274
  end
272
275
 
276
+ describe '#give' do
277
+ it 'transfers a lock from the owner to a recipient' do
278
+ send_command('locker resource create foobar')
279
+ send_command('locker label create bazbat')
280
+ send_command('locker label add foobar to bazbat')
281
+ send_command('lock bazbat', as: alice)
282
+ send_command('locker give bazbat to @bob # with a comment', as: alice)
283
+ expect(replies.last).to eq('Alice gave bazbat to Bob (@bob)')
284
+ end
285
+
286
+ it 'preserves the state of the queue when there is one' do
287
+ send_command('locker resource create foobar')
288
+ send_command('locker label create bazbat')
289
+ send_command('locker label add foobar to bazbat')
290
+ send_command('lock bazbat', as: alice)
291
+ send_command('lock bazbat', as: bob)
292
+ send_command('lock bazbat', as: charlie)
293
+ send_command('locker give bazbat to @charlie # with a comment', as: alice)
294
+ send_command('locker status bazbat')
295
+ expect(replies.last).to match(/^bazbat is locked by Charlie \(taken \d seconds? ago\)\. Next up: Bob, Charlie$/)
296
+ end
297
+
298
+ it 'shows a warning when the owner attempts to give the label to herself' do
299
+ send_command('locker resource create foobar')
300
+ send_command('locker label create bazbat')
301
+ send_command('locker label add foobar to bazbat')
302
+ send_command('lock bazbat', as: alice)
303
+ send_command('locker give bazbat to @alice # with a comment', as: alice)
304
+ expect(replies.last).to eq('Why are you giving the lock to yourself?')
305
+ end
306
+
307
+ it 'shows an error when the attempted giver is not the owner' do
308
+ send_command('locker resource create foobar')
309
+ send_command('locker label create bazbat')
310
+ send_command('locker label add foobar to bazbat')
311
+ send_command('lock bazbat', as: alice)
312
+ send_command('locker give bazbat to @charlie # with a comment', as: bob)
313
+ expect(replies.last).to eq('The lock on bazbat can only be given by its current owner: Alice (@alice)')
314
+ end
315
+
316
+ it 'shows an error when the label does not exist' do
317
+ send_command('locker give foobar to @bob', as: alice)
318
+ expect(replies.last).to eq('Sorry, that does not exist')
319
+ end
320
+
321
+ it 'shows an error when the recipient does not exist' do
322
+ send_command('locker resource create foobar')
323
+ send_command('locker label create bazbat')
324
+ send_command('locker label add foobar to bazbat')
325
+ send_command('lock bazbat', as: alice)
326
+ send_command('locker give bazbat to @doris', as: alice)
327
+ expect(replies.last).to eq('Unknown user')
328
+ end
329
+ end
330
+
273
331
  describe '#observe' do
274
332
  it 'adds a user as observer of a label' do
275
333
  send_command('locker resource create foobar')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-locker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Sigler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-12 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita