From fa3b93c5046f23ebbb2e82ac4944357683f8c5d8 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Wed, 9 Dec 2015 22:53:16 -0500 Subject: let user configure which auth method to use --- lib/oxidized/hook/githubrepo.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lib') 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 -- cgit v1.2.1