From 77999f75df4fe0e8aca12475e95a102c3396128e Mon Sep 17 00:00:00 2001 From: Karol Date: Sat, 24 Sep 2022 15:24:12 +0200 Subject: [PATCH] set proglang based on exenv try to guess exenv based on proglang --- .../proforma_service/convert_exercise_to_task.rb | 8 +++++++- .../proforma_service/convert_task_to_exercise.rb | 16 +++++++++++++++- .../convert_exercise_to_task_spec.rb | 14 ++++++++------ .../convert_task_to_exercise_spec.rb | 14 ++++++++++++-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/app/services/proforma_service/convert_exercise_to_task.rb b/app/services/proforma_service/convert_exercise_to_task.rb index 937f1898..0acb062a 100644 --- a/app/services/proforma_service/convert_exercise_to_task.rb +++ b/app/services/proforma_service/convert_exercise_to_task.rb @@ -23,7 +23,7 @@ module ProformaService title: @exercise.title, description: @exercise.description, internal_description: nil, - # proglang: proglang, where can we get this information? + proglang: proglang, files: task_files, tests: tests, uuid: uuid, @@ -44,6 +44,12 @@ module ProformaService ) end + def proglang + regex = %r{^openhpi/co_execenv_(?[^:]*):(?[^-]*)(?>-.*)?$} + match = regex.match @exercise.execution_environment.docker_image + match ? {name: match[:language], version: match[:version]} : nil + end + def uuid @exercise.update(uuid: SecureRandom.uuid) if @exercise.uuid.nil? @exercise.uuid diff --git a/app/services/proforma_service/convert_task_to_exercise.rb b/app/services/proforma_service/convert_task_to_exercise.rb index 7ecf7881..a0cbeaf7 100644 --- a/app/services/proforma_service/convert_task_to_exercise.rb +++ b/app/services/proforma_service/convert_task_to_exercise.rb @@ -26,12 +26,26 @@ module ProformaService allow_file_creation: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_file_creation)), allow_auto_completion: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_auto_completion)), expected_difficulty: @task.meta_data[:CodeOcean]&.dig(:expected_difficulty), - execution_environment_id: @task.meta_data[:CodeOcean]&.dig(:execution_environment_id), + execution_environment_id: execution_environment_id, files: files ) end + def execution_environment_id + from_meta_data = @task.meta_data[:CodeOcean]&.dig(:execution_environment_id) + return from_meta_data if from_meta_data + return nil unless @task.proglang + + ex_envs_with_name_and_version = ExecutionEnvironment.where('docker_image ilike ?', "%#{@task.proglang[:name]}%#{@task.proglang[:version]}%") + return ex_envs_with_name_and_version.first.id if ex_envs_with_name_and_version.any? + + ex_envs_with_name = ExecutionEnvironment.where('docker_image like ?', "%#{@task.proglang[:name]}%") + return ex_envs_with_name.first.id if ex_envs_with_name.any? + + nil + end + def string_to_bool(str) return true if str == 'true' return false if str == 'false' diff --git a/spec/services/proforma_service/convert_exercise_to_task_spec.rb b/spec/services/proforma_service/convert_exercise_to_task_spec.rb index f43d2f41..bf9f5b40 100644 --- a/spec/services/proforma_service/convert_exercise_to_task_spec.rb +++ b/spec/services/proforma_service/convert_exercise_to_task_spec.rb @@ -19,22 +19,19 @@ RSpec.describe ProformaService::ConvertExerciseToTask do let(:convert_to_task) { described_class.new(exercise: exercise) } let(:exercise) do create(:dummy, + execution_environment: execution_environment, instructions: 'instruction', uuid: SecureRandom.uuid, files: files + tests) end let(:files) { [] } let(:tests) { [] } + let(:execution_environment) { create(:java) } it 'creates a task with all basic attributes' do expect(task).to have_attributes( title: exercise.title, description: exercise.description, - # internal_description: exercise.instructions, - # proglang: { - # name: exercise.execution_environment.language, - # version: exercise.execution_environment.version - # }, uuid: exercise.uuid, language: described_class::DEFAULT_LANGUAGE, meta_data: { @@ -48,13 +45,18 @@ RSpec.describe ProformaService::ConvertExerciseToTask do files: {}, }, }, - # parent_uuid: exercise.clone_relations.first&.origin&.uuid, files: [], tests: [], model_solutions: [] ) end + context 'when exercise has execution_environment with correct docker-image name' do + it 'creates a task with the correct proglang attribute' do + expect(task).to have_attributes(proglang: {name: 'java', version: '8'}) + end + end + context 'when exercise has a mainfile' do let(:files) { [file] } let(:file) { build(:file) } diff --git a/spec/services/proforma_service/convert_task_to_exercise_spec.rb b/spec/services/proforma_service/convert_task_to_exercise_spec.rb index bf19bb3e..22b649da 100644 --- a/spec/services/proforma_service/convert_task_to_exercise_spec.rb +++ b/spec/services/proforma_service/convert_task_to_exercise_spec.rb @@ -32,7 +32,7 @@ describe ProformaService::ConvertTaskToExercise do Proforma::Task.new( title: 'title', description: 'description', - # proglang: {name: 'proglang-name', version: 'proglang-version'}, + proglang: {name: 'python', version: '3.4'}, uuid: 'uuid', parent_uuid: 'parent_uuid', language: 'language', @@ -57,7 +57,7 @@ describe ProformaService::ConvertTaskToExercise do allow_file_creation: allow_file_creation, allow_auto_completion: allow_auto_completion, expected_difficulty: expected_difficulty, - execution_environment_id: execution_environment.id, + execution_environment_id: execution_environment&.id, files: files_meta_data, }, } @@ -87,6 +87,16 @@ describe ProformaService::ConvertTaskToExercise do ) end + context 'when execution environment is not set in meta_data' do + let(:execution_environment) { nil } + + before { create(:python) } + + it 'sets the execution_environment based on proglang name and value' do + expect(convert_to_exercise_service).to have_attributes(execution_environment: have_attributes(name: 'Python 3.4')) + end + end + context 'when task has a file' do let(:files) { [file] } let(:file) do