From dad7ba33deed79c5a8fb4fded060dd62095764e4 Mon Sep 17 00:00:00 2001 From: Lyra Naeseth Date: Mon, 10 Sep 2018 13:18:06 -0700 Subject: [PATCH] Default to SSH clone URI when private_key is set Fixes jtarchie/github-pullrequest-resource#205. --- assets/lib/commands/in.rb | 4 +++- spec/commands/in_spec.rb | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/assets/lib/commands/in.rb b/assets/lib/commands/in.rb index 3308c0d..028a4b2 100755 --- a/assets/lib/commands/in.rb +++ b/assets/lib/commands/in.rb @@ -79,7 +79,9 @@ def pr end def uri - input.source.uri || "https://github.com/#{input.source.repo}" + return input.source.uri if input.source.uri + return "git@github.com:#{input.source.repo}.git" if input.source.private_key + return "https://github.com/#{input.source.repo}" end def ref diff --git a/spec/commands/in_spec.rb b/spec/commands/in_spec.rb index b40b084..5092441 100644 --- a/spec/commands/in_spec.rb +++ b/spec/commands/in_spec.rb @@ -16,11 +16,15 @@ def git_uri let(:dest_dir) { Dir.mktmpdir } - def get(payload) + def command(payload) payload['source']['skip_ssl_verification'] = true Input.instance(payload: payload) - command = Commands::In.new(destination: dest_dir) - command.output + + Commands::In.new(destination: dest_dir) + end + + def get(payload) + command(payload).output end def stub_json(uri, body) @@ -158,6 +162,20 @@ def dest_dir end end + context 'when an SSH key is specified' do + it 'defaults to an SSH clone URI' do + cmd = command('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'private_key' => '-----BEGIN', 'repo' => 'jtarchie/test' }) + expect(cmd.send(:uri)).to eq 'git@github.com:jtarchie/test.git' + end + end + + context 'when an SSH key is not specified' do + it 'defaults to an HTTPS clone URI' do + cmd = command('version' => { 'ref' => @ref, 'pr' => '1' }, 'source' => { 'repo' => 'jtarchie/test' }) + expect(cmd.send(:uri)).to eq 'https://github.com/jtarchie/test' + end + end + context 'when the git clone fails' do it 'provides a helpful erorr message' do stub_json('https://api.github.com:443/repos/jtarchie/test/pulls/1', html_url: 'http://example.com', number: 1, head: { ref: 'foo' }, base: { ref: 'master', user: { login: 'jtarchie' } }, user: { login: 'jtarchie-contributor' })