Skip to content

Commit

Permalink
Add SCM credential support for EmbeddedAnsible
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Aug 2, 2019
1 parent e530796 commit 5c791c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 4 additions & 3 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class GitRepository < ApplicationRecord
include AuthenticationMixin
belongs_to :authentication

GIT_REPO_DIRECTORY = Rails.root.join('data/git_repos')

Expand Down Expand Up @@ -158,9 +159,9 @@ def handling_worktree_errors
def worktree_params
params = {:path => directory_name}
params[:certificate_check] = method(:self_signed_cert_cb) if verify_ssl == OpenSSL::SSL::VERIFY_NONE
if authentications.any?
params[:username] = default_authentication.userid
params[:password] = default_authentication.password
if (auth = authentication || default_authentication)
params[:username] = auth.userid
params[:password] = auth.password
end
params
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScri
default_value_for :scm_type, "git"
default_value_for :scm_branch, "master"

belongs_to :git_repository, :dependent => :destroy
belongs_to :git_repository, :autosave => true, :dependent => :destroy
before_validation :sync_git_repository

include ManageIQ::Providers::EmbeddedAnsible::CrudCommon

Expand Down Expand Up @@ -46,14 +47,27 @@ def raw_delete_in_provider
end

def git_repository
super || begin
transaction do
update!(:git_repository => GitRepository.create!(:url => scm_url))
(super || (ensure_git_repository && super)).tap { |r| sync_git_repository(r) }
end

private def ensure_git_repository
transaction do
repo = GitRepository.create!(:url => scm_url)
if new_record?
self.git_repository_id = repo.id
else
update_columns(:git_repository_id => repo.id)
end
super
end
end

private def sync_git_repository(git_repository = nil)
return unless name_changed? || scm_url_changed? || authentication_id_changed?

git_repository ||= self.git_repository
git_repository.attributes = {:name => name, :url => scm_url, :authentication_id => authentication_id}
end

def sync
update_attributes!(:status => "running")
transaction do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def files_in_repository(git_repo_dir)

expect(Notification).to receive(:create!).with(notify_creation_args)
expect(Notification).to receive(:create!).with(sync_notification_args)
expect(GitRepository).to receive(:create!).and_raise(::Rugged::NetworkError)
allow_any_instance_of(GitRepository).to receive(:with_worktree).and_raise(::Rugged::NetworkError)

expect do
described_class.create_in_provider(manager.id, params)
Expand All @@ -166,7 +166,7 @@ def files_in_repository(git_repo_dir)
expect(result.last_updated_on).to be_an(Time)
expect(result.last_update_error).to start_with("Rugged::NetworkError")

expect(GitRepository).to receive(:create!).and_call_original
allow_any_instance_of(GitRepository).to receive(:with_worktree).and_call_original

result.sync

Expand Down

0 comments on commit 5c791c4

Please sign in to comment.