diff options
-rw-r--r-- | lib/oxidized/hook/githubrepo.rb | 14 | ||||
-rw-r--r-- | spec/githubrepo_spec.rb | 25 |
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb new file mode 100644 index 0000000..85adaab --- /dev/null +++ b/lib/oxidized/hook/githubrepo.rb @@ -0,0 +1,14 @@ +class GithubRepo < Oxidized::Hook + def validate_cfg! + cfg.has_key?('remote_repo') or raise KeyError, 'remote_repo is required' + end + + def run_hook(ctx) + credentials = Rugged::Credentials::SshKeyFromAgent.new(username: 'git') + 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}" + remote.push(['refs/heads/master'], credentials: credentials) + end +end diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb new file mode 100644 index 0000000..c98244d --- /dev/null +++ b/spec/githubrepo_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' +require 'rugged' +require 'oxidized/hook/githubrepo' + +describe Oxidized::Node do + before(:each) do + asetus = Asetus.new + asetus.cfg.output.git.repo = 'foo.git' + Oxidized.stubs(:asetus).returns(asetus) + repo = 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') + Rugged::Repository.expects(:new).with('foo.git').returns(repo) + end + + describe "#run_hook" do + it "will push to the remote repository" do + gr = GithubRepo.new + gr.run_hook(nil).must_equal true + end + end +end |