summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/oxidized/hook/githubrepo.rb18
-rw-r--r--spec/githubrepo_spec.rb50
2 files changed, 50 insertions, 18 deletions
diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb
index 25b2cd0..22ee8ea 100644
--- a/lib/oxidized/hook/githubrepo.rb
+++ b/lib/oxidized/hook/githubrepo.rb
@@ -4,18 +4,17 @@ class GithubRepo < Oxidized::Hook
end
def run_hook(ctx)
- credentials = Rugged::Credentials::UserPassword.new(username: cfg.username, password: cfg.password)
repo = Rugged::Repository.new(Oxidized.config.output.git.repo)
log "Pushing local repository(#{repo.path})..."
remote = repo.remotes['origin'] || repo.remotes.create('origin', cfg.remote_repo)
log "to remote: #{remote.url}"
- fetch_and_merge_remote(repo, credentials)
+ fetch_and_merge_remote(repo)
remote.push([repo.head.name], credentials: credentials)
end
- def fetch_and_merge_remote(repo, credentials)
+ def fetch_and_merge_remote(repo)
repo.fetch('origin', [repo.head.name], credentials: credentials)
their_branch = repo.branches["origin/master"] or return
@@ -31,4 +30,17 @@ class GithubRepo < Oxidized::Hook
update_ref: "HEAD"
})
end
+
+ private
+
+ def credentials
+ @credentials ||= if cfg.has_key?('username') && cfg.has_key?('password')
+ log "Using https auth", :debug
+ Rugged::Credentials::UserPassword.new(username: cfg.username, password: cfg.password)
+ else
+ log "Using ssh auth", :debug
+ Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
+ end
+ end
+
end
diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb
index 07296f3..a3b4953 100644
--- a/spec/githubrepo_spec.rb
+++ b/spec/githubrepo_spec.rb
@@ -4,30 +4,50 @@ require 'oxidized/hook/githubrepo'
describe Oxidized::Node do
before(:each) do
- asetus = Asetus.new
- asetus.cfg.output.git.repo = 'foo.git'
- asetus.cfg.hooks.github_repo_hook.remote_repo = 'https://github.com/blah/blah.git'
- asetus.cfg.hooks.github_repo_hook.username = 'username'
- asetus.cfg.hooks.github_repo_hook.password = 'password'
- GithubRepo.any_instance.stubs(:cfg).returns(asetus.cfg.hooks.github_repo_hook)
- Oxidized.stubs(:asetus).returns(asetus)
- repo = mock()
+ Oxidized.asetus = Asetus.new
+ Oxidized.config.output.git.repo = 'foo.git'
+
+ @credentials = mock()
+
remote = mock()
- remote.expects(:url).returns('github.com/foo.git')
- remote.expects(:push).returns(true)
- repo.expects(:remotes).returns({'origin' => remote})
- repo.expects(:path).returns('foo.git')
+ remote.expects(:url).returns('https://github.com/username/foo.git')
+ remote.expects(:push).with(['refs/heads/master'], credentials: @credentials).returns(true)
+
repo_head = mock()
- repo_head.expects(:name).twice.returns('origin/master')
+ repo_head.expects(:name).twice.returns('refs/heads/master')
+
+ repo = mock()
+ repo.expects(:path).returns('foo.git')
+ repo.expects(:remotes).returns({'origin' => remote})
repo.expects(:head).twice.returns(repo_head)
- repo.expects(:fetch).returns(true)
+ repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: @credentials).returns(true)
repo.expects(:branches).returns({})
+
Rugged::Repository.expects(:new).with('foo.git').returns(repo)
end
describe "#run_hook" do
- it "will push to the remote repository" do
+ it "will push to the remote repository using https" do
+ Oxidized.config.hooks.github_repo_hook.remote_repo = 'https://github.com/username/foo.git'
+ Oxidized.config.hooks.github_repo_hook.username = 'username'
+ Oxidized.config.hooks.github_repo_hook.password = 'password'
+
+ Rugged::Credentials::UserPassword.expects(:new).with(username: 'username', password: 'password').returns(@credentials)
+
+ gr = GithubRepo.new
+ gr.cfg = Oxidized.config.hooks.github_repo_hook
+
+ gr.run_hook(nil).must_equal true
+ end
+
+ it "will push to the remote repository using ssh" do
+ Oxidized.config.hooks.github_repo_hook.remote_repo = 'git@github.com:username/foo.git'
+
+ Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(@credentials)
+
gr = GithubRepo.new
+ gr.cfg = Oxidized.config.hooks.github_repo_hook
+
gr.run_hook(nil).must_equal true
end
end