right_hook 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,7 @@ module RightHook
25
25
  # Create a new GitHub authorization with the given note.
26
26
  # If one already exists with that note, it will not create a duplicate.
27
27
  def create_authorization(note)
28
- _client.create_authorization(scopes: %w(repo), note: note, idempotent: true).token
28
+ _client.create_authorization(scopes: %w(repo), note: note).token
29
29
  end
30
30
 
31
31
  # Returns an array of all of the authorizations for the authenticated account.
@@ -33,6 +33,16 @@ module RightHook
33
33
  _client.list_authorizations
34
34
  end
35
35
 
36
+ # If there is already an authorization by this note, use it; otherwise create it
37
+ def find_or_create_authorization_by_note(note)
38
+ found_auth = list_authorizations.find {|auth| auth.note == note}
39
+ if found_auth
40
+ found_auth.token
41
+ else
42
+ create_authorization(note)
43
+ end
44
+ end
45
+
36
46
  private
37
47
  attr_reader :_client
38
48
  # Enforce use of build methods
@@ -1,3 +1,3 @@
1
1
  module RightHook
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -21,7 +21,7 @@ describe RightHook::Authenticator do
21
21
 
22
22
  describe '#create_authorization' do
23
23
  it 'delegates to create_authorization' do
24
- Octokit::Client.any_instance.should_receive(:create_authorization).with(scopes: %w(repo), note: 'test note', idempotent: true).and_return(OpenStruct.new(token: 'my_token'))
24
+ Octokit::Client.any_instance.should_receive(:create_authorization).with(scopes: %w(repo), note: 'test note').and_return(OpenStruct.new(token: 'my_token'))
25
25
 
26
26
  expect(described_class.build('octocat', 'pass').create_authorization('test note')).to eq('my_token')
27
27
  end
@@ -34,4 +34,24 @@ describe RightHook::Authenticator do
34
34
  expect(described_class.build('octocat', 'pass').list_authorizations).to eq(:the_authorizations)
35
35
  end
36
36
  end
37
+
38
+ describe '#find_or_create_authorization_by_note' do
39
+ let(:auth) { OpenStruct.new(token: 'a token', note: 'the note')}
40
+ context 'when #list_authorizations has a note that is an exact match' do
41
+ before { Octokit::Client.any_instance.should_receive(:list_authorizations).and_return([auth]) }
42
+ it 'returns that authorization' do
43
+ authenticator = described_class.build('octocat', 'pass')
44
+ expect(authenticator.find_or_create_authorization_by_note('the note')).to eq('a token')
45
+ end
46
+ end
47
+
48
+ context 'when #list_authorizations does not have a note that matches' do
49
+ before { Octokit::Client.any_instance.should_receive(:list_authorizations).and_return([]) }
50
+ before { Octokit::Client.any_instance.should_receive(:create_authorization).and_return(auth) }
51
+ it 'creates a new authorization' do
52
+ authenticator = described_class.build('octocat', 'pass')
53
+ expect(authenticator.find_or_create_authorization_by_note('the note')).to eq('a token')
54
+ end
55
+ end
56
+ end
37
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: