diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index c9d41c0a..4feaa3d0 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -53,12 +53,15 @@ class WelcomeController < ApplicationController local_shixun = LocalShixun.create!(name: shixun.name, description: shixun.description, user_id: User.current.id, status: 0, trainee: shixun.trainee, webssh: shixun.webssh, multi_webssh: shixun.multi_webssh, can_copy: shixun.can_copy, identifier: generate_identifier, shixun_id: shixun.id, - use_scope: shixun.use_scope, visits: 1, evaluate_script: shixun.evaluate_script) + use_scope: shixun.use_scope, visits: 1, evaluate_script: shixun.evaluate_script, + local_giturl: shixun.git_url) # 同步镜像 if shixun.mirror_repositories.present? shixun.mirror_repositories.each do |mirror| - LocalMirrorRepository.create!(:local_shixun_id => local_shixun.id, :mirror_repository_id => mirror.id) + # 本地版的mirror id和线上的可能不一样,所以按名字取,然后再存 + # local_mirror = MirrorRepository.where(type_name: mirror.type_name).first.try(:id) + LocalMirrorRepository.create!(:local_shixun_id => local_shixun.id, :mirror_repository_id => mirror.id, type_name: mirror.type_name) end end @@ -97,10 +100,10 @@ class WelcomeController < ApplicationController end end end - render :json => {status: 0, message: "同步成功"} + render :json => {status: 0, message: "success"} rescue Exception => e logger.error("shixun_local_in ##{e.message}") - render :json => {status: -1, message: "同步失败,#{e.message}"} + render :json => {status: -1, message: "error,#{e.message}"} raise ActiveRecord::Rollback end @@ -123,7 +126,8 @@ class WelcomeController < ApplicationController local_mirrors = LocalMirrorRepository.where(local_shixun_id: local_shixun.id) if local_mirrors.present? local_mirrors.each do |local_mirror| - ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => local_mirror.mirror_repository_id) + local_mirror_new = MirrorRepository.where(type_name: local_mirror.try(:type_name)).first + ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => local_mirror_new.id) end end @@ -147,6 +151,16 @@ class WelcomeController < ApplicationController raise "版本库创建失败" if shixun.gpid.blank? # 若和gitlab没同步成功,则抛出异常 g = Gitlab.client shixun.update_column(:git_url, g.project(shixun.gpid).path_with_namespace) + local_git_path = local_shixun.local_giturl.split('/').last if local_shixun.local_giturl.present? + new_giturl = "http://educoder:xinhu1ji2qu3@"+g.project(shixun.gpid).http_url_to_repo.split('//').last + # 如果有目录才执行 + if system("cd tmp/repositories/#{local_git_path}") + system("cd tmp/repositories/#{local_git_path};git remote remove origin;git remote add origin #{new_giturl}; + git add .;git commit -m '..';git push origin master") + end + + + # http://Hjqreturn:xinhu1ji2qu3@bdgit.educoder.net/Hjqreturn/pgfqe6ch8.git (fetch) # 同步关卡信息 local_challenges = LocalChallenge.where(local_shixun_id: local_shixun.id) diff --git a/app/views/common/index.html.erb b/app/views/common/index.html.erb index 0d7f90fd..d99476a6 100644 --- a/app/views/common/index.html.erb +++ b/app/views/common/index.html.erb @@ -1 +1 @@ -Educoder
\ No newline at end of file +Educoder
\ No newline at end of file diff --git a/db/migrate/20190507013619_add_local_giturl_to_local_shixuns.rb b/db/migrate/20190507013619_add_local_giturl_to_local_shixuns.rb new file mode 100644 index 00000000..83589808 --- /dev/null +++ b/db/migrate/20190507013619_add_local_giturl_to_local_shixuns.rb @@ -0,0 +1,5 @@ +class AddLocalGiturlToLocalShixuns < ActiveRecord::Migration + def change + add_column :local_shixuns, :local_giturl, :string + end +end diff --git a/db/migrate/20190507031028_add_type_name_to_local_mirror_repositories.rb b/db/migrate/20190507031028_add_type_name_to_local_mirror_repositories.rb new file mode 100644 index 00000000..ce6ff3b3 --- /dev/null +++ b/db/migrate/20190507031028_add_type_name_to_local_mirror_repositories.rb @@ -0,0 +1,5 @@ +class AddTypeNameToLocalMirrorRepositories < ActiveRecord::Migration + def change + add_column :local_mirror_repositories, :type_name, :string + end +end