Skip to content

Commit 43db815

Browse files
committed
Switched login to username and password
- made the switch - the following commands do the prompt: publish, ready, hotfix - specs are basic for now, will add more - after initial prompt, oauth token is stored on the machine, so user is not prompted again ToDo - Mask the password input - Find a nicer way to wrap only specified commands into the credential prompt
1 parent fc4d73c commit 43db815

3 files changed

Lines changed: 66 additions & 23 deletions

File tree

‎bin/code‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ program_desc 'The Coderly Toolbelt'
2424

2525
version Code::VERSION
2626

27-
desc 'Authorize with GitHub'
28-
command :authorize, :login do |c|
29-
c.flag 'username', desc: 'GitHub username'
30-
c.flag 'password', desc: 'GitHub password'
31-
c.action do |global_options, options, args|
32-
username = options[:username]
33-
password = options[:password]
34-
35-
Code::GitHubAPI.new.authorize(username: username, password: password)
36-
end
37-
end
38-
3927
desc 'Describe some switch here'
4028
switch [:s,:switch]
4129

@@ -52,6 +40,9 @@ desc 'Upload your feature branch to GitHub'
5240
command :publish, :pr do |c|
5341
c.switch [:master], desc: 'Make the pull request based off the master branch'
5442
c.action do |global_options, options, args|
43+
44+
$gitHubAPI.ensure_authorized
45+
5546
message = args.join(' ')
5647

5748
base_branch = nil
@@ -76,13 +67,15 @@ end
7667
desc "Marks the current branch's pr as awaiting review"
7768
command :ready do |c|
7869
c.action do |global_options, options, args|
70+
$gitHubAPI.ensure_authorized
7971
Code::Branch.current.mark_prs_as_awaiting_review
8072
end
8173
end
8274

8375
desc "Quickly create a pull request from your changes on development."
8476
command :hotfix, :fix do |c|
8577
c.action do |global_options, options, args|
78+
$gitHubAPI.ensure_authorized
8679
hotfix_name = args.join('-')
8780
$git.hotfix hotfix_name
8881
end
@@ -146,6 +139,7 @@ end
146139

147140
pre do |global, command, options, args|
148141
$git = Code::Git.new
142+
$gitHubAPI = Code::GitHubAPI.new
149143
true
150144
end
151145

‎lib/code/github_api.rb‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
require "octokit"
22
require "code/repository"
3+
require "securerandom"
34

45
module Code
56

67
class GitHubAPI
78

8-
NoAuthTokenError = Class.new(StandardError)
9-
10-
AUTHORIZATION_NOTE = "Code gem authorization token"
9+
def authorization_note
10+
"CoderlyCode-" + SecureRandom.uuid
11+
end
1112

1213
def initialize(repository: Repository.current)
1314
@repository = repository
1415
end
1516

17+
def ensure_authorized
18+
if not authorized?
19+
username = prompt 'GitHub username'
20+
password = prompt 'GitHub password'
21+
22+
authorize(username: username, password: password)
23+
end
24+
end
25+
1626
def authorize(username:, password:)
1727
client = self.octokit_client_instance_from_basic_auth(username: username, password: password)
1828

19-
authorization = client.authorizations.detect { |auth| auth[:note] == AUTHORIZATION_NOTE }
20-
21-
if authorization
22-
token = authorization[:token]
23-
else
24-
token = client.create_authorization(scopes: ["repo"], note: AUTHORIZATION_NOTE)[:token]
25-
end
29+
token = client.create_authorization(scopes: ["repo"], note: authorization_note)[:token]
2630

2731
puts "Successfully authorized with token #{token}"
2832
self.authorization_token = token
@@ -45,6 +49,10 @@ def label_pr(pull_request, label)
4549
add_label_to_pr(pull_request.number, label)
4650
end
4751

52+
def authorized?
53+
!authorization_token.empty?
54+
end
55+
4856
private
4957

5058
def current_organization
@@ -65,12 +73,17 @@ def add_label_to_pr(number, label)
6573

6674
def authorization_token
6775
token = System.result('git config --get oauth.token')
68-
raise NoAuthTokenError, "Couldn't retrieve an authorization token. Make sure you authorize through 'code authorize --username {username} --password {password}' first" if System.command_failed?
6976
token
7077
end
7178

7279
def authorization_token=(token)
7380
System.result("git config --global oauth.token #{token}")
7481
end
82+
83+
def prompt(prompt_text)
84+
print prompt_text + ':'
85+
input = gets
86+
input.strip
87+
end
7588
end
7689
end

‎spec/code/github_api_spec.rb‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'code/github_api'
2+
3+
module Code
4+
describe GitHubAPI do
5+
let(:api) { GitHubAPI.new }
6+
7+
describe '#ensure_authorized' do
8+
9+
context 'when system is authorized' do
10+
before do
11+
allow(api).to receive(:authorized?).and_return(true)
12+
end
13+
14+
it 'should not call #authorize' do
15+
expect(api).not_to receive(:authorize)
16+
expect(api).not_to receive(:prompt)
17+
18+
api.ensure_authorized
19+
end
20+
end
21+
22+
context 'when system is not authorized' do
23+
before do
24+
allow(api).to receive(:authorized?).and_return(false)
25+
end
26+
27+
it 'should call #authorize' do
28+
expect(api).to receive(:authorize)
29+
expect(api).to receive(:prompt).twice
30+
31+
api.ensure_authorized
32+
end
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)