diff --git a/Gemfile b/Gemfile index 056ce5d7..02789f5b 100644 --- a/Gemfile +++ b/Gemfile @@ -37,7 +37,7 @@ gem 'rest-client' gem 'rubyzip' gem 'mnemosyne-ruby' gem 'faraday' -gem 'proforma', git: 'git://github.com/openHPI/proforma.git' # use version not master +gem 'proforma', path: '../proforma' #, git: 'git://github.com/openHPI/proforma.git' # use version not master gem 'whenever', require: false gem 'rails-timeago' diff --git a/Gemfile.lock b/Gemfile.lock index 4f353f77..862b636c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,3 @@ -GIT - remote: git://github.com/openHPI/proforma.git - revision: 54a5bdf2bafebb548998c945ee68d099d85108df - specs: - proforma (0.1.0) - activemodel (~> 5.2.3) - activesupport (~> 5.2.3) - nokogiri (~> 1.10.2) - rubyzip (>= 1.2.2, < 2.1.0) - GIT remote: https://github.com/gosukiwi/tubesock revision: 86a5ca4f7d3c3a7b9a727ad91df3b9b4912eda39 @@ -17,6 +7,15 @@ GIT rack (>= 1.5.0) websocket (>= 1.1.0) +PATH + remote: ../proforma + specs: + proforma (0.1.0) + activemodel (~> 5.2.3) + activesupport (~> 5.2.3) + nokogiri (~> 1.10.2) + rubyzip (>= 1.2.2, < 2.1.0) + GEM remote: https://rubygems.org/ specs: diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index d2a90f98..77f35ba4 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -107,26 +107,7 @@ class ExercisesController < ApplicationController end def export_external_check - @codeharbor_link = current_user.codeharbor_link - conn = Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday| - faraday.options[:open_timeout] = 5 - faraday.options[:timeout] = 5 - - faraday.adapter Faraday.default_adapter - end - - codeharbor_check = begin - response = conn.post do |req| - req.headers['Content-Type'] = 'application/json' - req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key - req.body = {uuid: @exercise.uuid}.to_json - end - response_hash = JSON.parse(response.body, symbolize_names: true) - - {error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right)) - rescue Faraday::Error => e - {error: true, message: t('exercises.export_codeharbor.error', message: e.message)} - end + codeharbor_check = ExerciseService::CheckExternal.call(uuid: @exercise.uuid, codeharbor_link: current_user.codeharbor_link) render json: { message: codeharbor_check[:message], @@ -196,8 +177,12 @@ class ExercisesController < ApplicationController exercise.save! return render json: {}, status: 201 end - logger.info(exercise.errors.full_messages) - render json: {}, status: 400 + # logger.info(exercise.errors.full_messages) + # render json: {}, status: 400 + rescue Proforma::ProformaError + render json: t('exercises.export_codeharbor.export_errors.invalid'), status: 400 + rescue StandardError + render json: t('exercises.export_codeharbor.export_errors.internal_error'), status: 500 end def user_from_api_key diff --git a/app/services/exercise_service/check_external.rb b/app/services/exercise_service/check_external.rb new file mode 100644 index 00000000..1cce7614 --- /dev/null +++ b/app/services/exercise_service/check_external.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module ExerciseService + class CheckExternal < ServiceBase + def initialize(uuid:, codeharbor_link:) + @uuid = uuid + @codeharbor_link = codeharbor_link + end + + def execute + response = connection.post do |req| + req.headers['Content-Type'] = 'application/json' + req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key + req.body = {uuid: @uuid}.to_json + end + response_hash = JSON.parse(response.body, symbolize_names: true) + + {error: false}.merge(response_hash.slice(:message, :exercise_found, :update_right)) + rescue Faraday::Error => e + {error: true, message: t('exercises.export_exercise.error', message: e.message)} + end + + private + + def connection + Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday| + faraday.options[:open_timeout] = 5 + faraday.options[:timeout] = 5 + + faraday.adapter Faraday.default_adapter + end + end + end +end diff --git a/app/services/exercise_service/push_external.rb b/app/services/exercise_service/push_external.rb index b0d8723f..93dacb81 100644 --- a/app/services/exercise_service/push_external.rb +++ b/app/services/exercise_service/push_external.rb @@ -10,11 +10,7 @@ module ExerciseService def execute body = @zip.string begin - conn = Faraday.new(url: @codeharbor_link.push_url) do |faraday| - faraday.adapter Faraday.default_adapter - end - - response = conn.post do |request| + response = connection.post do |request| request.headers['Content-Type'] = 'application/zip' request.headers['Content-Length'] = body.length.to_s request.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key @@ -26,5 +22,13 @@ module ExerciseService return e.message end end + + private + + def connection + Faraday.new(url: @codeharbor_link.push_url) do |faraday| + faraday.adapter Faraday.default_adapter + end + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index b0a7ccd3..138fa28c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -337,6 +337,9 @@ en: create_new: Create new close: Close abort: Abort + export_errors: + invalid: Invalid exercise + internal_error: An internal error occurred on Codeharbor while importing the exercise. file_form: hints: feedback_message: This message is used as a hint for failing tests.