From a626c212f9e64434b828dadcb0cb3bd879beb612 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Tue, 16 Feb 2016 18:56:30 -0200 Subject: fix `githubrepo` hook for groups repositories remotes when there was a repository per group we could not use the hook for pushing since we only had **one** remote configured. This PR configures each repository its own remote based in the `groups` config, as follows: ``` --- groups: first: remote_repo: git@gitlab.lab:oxidized/first.git second: remote_repo: git@gitlab.lab:oxidized/second.git ``` --- lib/oxidized/hook/githubrepo.rb | 16 +++++++- spec/githubrepo_spec.rb | 86 ++++++++++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 20 deletions(-) diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb index d10b51e..80ae665 100644 --- a/lib/oxidized/hook/githubrepo.rb +++ b/lib/oxidized/hook/githubrepo.rb @@ -4,9 +4,9 @@ class GithubRepo < Oxidized::Hook end def run_hook(ctx) - repo = Rugged::Repository.new(Oxidized.config.output.git.repo) + repo = Rugged::Repository.new(ctx.node.repo) log "Pushing local repository(#{repo.path})..." - remote = repo.remotes['origin'] || repo.remotes.create('origin', cfg.remote_repo) + remote = repo.remotes['origin'] || repo.remotes.create('origin', remote_repo(ctx.node)) log "to remote: #{remote.url}" fetch_and_merge_remote(repo) @@ -54,4 +54,16 @@ class GithubRepo < Oxidized::Hook end end + def remote_repo(node) + if node.group.nil? || single_repo? + cfg.remote_repo + else + Oxidized.config.groups[node.group].remote_repo + end + end + + def single_repo? + Oxidized.config.git.single_repo? + end + end diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index 71681ed..36207d3 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -2,9 +2,10 @@ require 'spec_helper' require 'rugged' require 'oxidized/hook/githubrepo' -describe Oxidized::Node do +describe Oxidized::GithubRepo do let(:credentials) { mock() } let(:remote) { mock() } + let(:remotes) { mock() } let(:repo_head) { mock() } let(:repo) { mock() } let(:gr) { GithubRepo.new } @@ -69,31 +70,80 @@ describe Oxidized::Node do end describe "#run_hook" do - before(:each) do - remote.expects(:url).returns('https://github.com/username/foo.git') - remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + let(:group) { nil } + let(:ctx) { OpenStruct.new(node: node) } + let(:node) do + Oxidized::Node.new(ip: '127.0.0.1', group: group, model: 'junos', output: 'output') + end + + before do repo_head.expects(:name).twice.returns('refs/heads/master') repo.expects(:head).twice.returns(repo_head) repo.expects(:path).returns('foo.git') - repo.expects(:remotes).returns({'origin' => remote}) repo.expects(:fetch).with('origin', ['refs/heads/master'], credentials: credentials).returns(Hash.new(0)) - Rugged::Repository.expects(:new).with('foo.git').returns(repo) end - 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.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(nil).must_equal true + describe 'when there is only one repository and no groups' do + before do + remote.expects(:url).returns('https://github.com/username/foo.git') + remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + repo.expects(:remotes).returns({'origin' => remote}) + Rugged::Repository.expects(:new).with('foo.git').returns(repo) + end + + 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.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).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.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end 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.cfg = Oxidized.config.hooks.github_repo_hook - gr.run_hook(nil).must_equal true + describe "when there are groups" do + let(:group) { 'ggrroouupp' } + + before do + Rugged::Credentials::SshKeyFromAgent.expects(:new).with(username: 'git').returns(credentials) + Rugged::Repository.expects(:new).with(repository).returns(repo) + Oxidized.config.groups.ggrroouupp.remote_repo = 'ggrroouupp#remote_repo' + Oxidized.config.hooks.github_repo_hook.remote_repo = 'github_repo_hook#remote_repo' + Oxidized.config.output.git.single_repo = single_repo + + repo.expects(:remotes).twice.returns(remotes) + remotes.expects(:[]).with('origin').returns(nil) + remotes.expects(:create).with('origin', 'ggrroouupp#remote_repo').returns(remote) + remote.expects(:url).returns('url') + remote.expects(:push).with(['refs/heads/master'], credentials: credentials).returns(true) + end + + describe 'when there are several repositories' do + let(:repository) { './ggrroouupp.git' } + let(:single_repo) { nil } + + it 'will push to the node group repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end + + describe 'when is a single repository' do + let(:repository) { 'foo.git' } + let(:single_repo) { true } + + it 'will push to the correct repository' do + gr.cfg = Oxidized.config.hooks.github_repo_hook + gr.run_hook(ctx).must_equal true + end + end end end end -- cgit v1.2.1