Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update scripts when copying from templates #3210

Merged
merged 15 commits into from
Jan 24, 2024
31 changes: 26 additions & 5 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ def templates
validate :project_directory_exist, on: [:create]
validate :project_template_invalid, on: [:create]

# the template you created this project from
attr_accessor :template
Oglopf marked this conversation as resolved.
Show resolved Hide resolved

def initialize(attributes = {})
@id = attributes.delete(:id)
@directory = attributes.delete(:directory)
Expand Down Expand Up @@ -194,15 +191,39 @@ def sync_template
return true if template.blank?

# Sync the template files over
oe, s = Open3.capture2e("rsync", "-a", "#{template}/", "#{project_dataroot}")
oe, s = Open3.capture2e(*rsync_args)
raise oe unless s.success?

true
save_new_scripts
rescue StandardError => e
errors.add(:save, "Failed to sync template: #{e.message}")
false
end

# When copying a project from a template, we need new Script objects
# that point to the _new_ project directory, not the template's directory.
# This creates them _and_ serializes them to yml in the new directory.
def save_new_scripts
dir = Script.scripts_dir(template)
Dir.glob("#{dir}/*/form.yml").map do |script_yml|
Script.from_yaml(script_yml, project_dataroot)
end.map do |script|
saved_successfully = script.save
errors.add(:save, script.errors.full_messages) unless saved_successfully

saved_successfully
end.none? do |saved_successfully|
saved_successfully == false
Oglopf marked this conversation as resolved.
Show resolved Hide resolved
end
end

def rsync_args
[
'rsync', '-a', '--exclude', 'scripts/*',
"#{template}/", project_dataroot.to_s
]
end

def project_directory_exist
if !directory.blank? && Project.lookup_table.map { |_id, directory| directory }.map(&:to_s).include?(directory.to_s)
errors.add(:directory, :used)
Expand Down
3 changes: 1 addition & 2 deletions apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,8 @@ def add_required_fields(form: [], attributes: {})
end

def add_script_to_form(form: [], attributes: {})
return if form.include?('auto_scripts')
form << 'auto_scripts' unless form.include?('auto_scripts')

form << 'auto_scripts'
attributes[:auto_scripts] = {
directory: project_dir
}
Expand Down