socialforge/app/controllers/games_controller.rb

203 lines
6.6 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class GamesController < ApplicationController
layout "base_myshixun"
skip_before_filter :verify_authenticity_token, :only => [:file_update]
before_filter :find_myshixun, :only => [:index]
before_filter :find_game, :only => [:show, :game_build, :entry,:next_step, :outputs_show, :file_edit, :file_update, :game_status]
before_filter :find_repository, :only => [:show, :entry, :file_edit, :file_update]
before_filter :allowd_manager
include ApplicationHelper
def index
@games = @myshixun.games
@games_count = @games.count
respond_to do |format|
format.html
format.js
end
end
# mushixun的版本库必须创建时就创建
# 首次进入版本库自动打开文件
# path"" && path: @game.path
def show
game_path = (@game.path.blank? ? @game.path : @game.path.strip)
@rev = @rev.nil? ? "master" : @rev
@git_url = git_repository_url(@myshixun, "Myshixun")
@type = params[:type]
# 默认打开文件
if @path == "" && !game_path.nil? && !@repository.cat(game_path, @rev).blank? && @type != "root"
@path = game_path
@file_open = true
@content = @repository.cat(@path, @rev)
else
@entries = @repository.entries(@path, @rev)
end
@latest_output = @game.latest_output.try(:out_put)
outputs = @game.outputs
@had_done = 1 if (@myshixun.games.count == @game.stage && @game.status ==2)
if outputs.count == 0
@results = []
else
@results = outputs.map{|result| [result.code, result.id]}
end
respond_to do |format|
format.html
format.js
end
rescue Exception => e
flash[:error] = e.message
end
# 代码预览
def entry
entry_and_raw(false)
@content = @repository.cat(@path, @rev)
if is_entry_text_data?(@content, @path)
render :layout => 'base_myshixun'
end
end
def file_edit
entry_and_raw(false)
@content = @repository.cat(@path, @rev).rstrip
# respond_to do |format|
# format.js
# end
end
def file_update
@g = Gitlab.client
@g.edit_file(@myshixun.gpid, :content => params[:content], :file_path => @path, :branch_name => @rev, :commit_message => "shixun exec")
respond_to do |format|
format.js{redirect_to entry_myshixun_game_path(@game, :myshixun_id => @myshixun, :path => @path)}
end
rescue Exception => e
puts e.message
end
# json调用显示左边的隐藏具体详情@game 字段 msg
# 刷新右边的输出结果 @game out_put
def outputs_show
@game = Output.find(params[:game_output_id])
respond_to do |format|
format.js
end
end
# status 0: 未提交测评或者提交测评失败后报错1中间状态还没返回值2返回值并成功
def game_build
gitUrl = git_repository_url(@myshixun, "Myshixun")
gitUrl = Base64.encode64(gitUrl)
taskId = params[:id]
jobName = "myshixun_#{@myshixun.id}"
@game.update_attribute(:status, 1)
testCode = {}
test_sets = @game.test_sets
unless test_sets.blank?
test_sets.each_with_index do |test_set, index|
testCode.store("testCode_#{index}",test_set.try(:output))
end
end
testCode = testCode.to_json
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
step = @game.stage
params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}", :testCode => "#{testCode}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/api/buildJob")
res = uri_exec uri, params
render :json => {data:"success"}
end
def game_status
outputs = @game.outputs
if outputs.count == 0
outputs = ""
else
outputs = outputs.map{|result| [result.code, result.id]}
end
had_done = 1 if (@myshixun.games.count == @game.stage && @game.status ==2)
latest_output = @game.latest_output.try(:out_put)
render :json => {status: @game.status, output: latest_output, results: outputs, had_done: had_done}
end
# 自动推送下一个任务
def next_step
render_403 if @game.status != 2
next_game = @game.next_game
next_game.update_attribute(:status, 0) if next_game.status == 3
respond_to do |format|
format.js{redirect_to myshixun_game_path(next_game, :myshixun_id => @myshixun.id)}
end
end
private
def entry_and_raw(is_raw)
@entry = @repository.entry(@path, @rev)
(show_error_not_found; return) unless @entry
# If the entry is a dir, show the browser
(show; return) if @entry.is_dir?
@content = @repository.cat(@path, @rev)
(show_error_not_found; return) unless @content
if is_raw || (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || !is_entry_text_data?(@content, @path)
# Force the download
send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
send_type = Redmine::MimeType.of(@path)
send_opt[:type] = send_type.to_s if send_type
send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
send_data @content, send_opt
else
# Prevent empty lines when displaying a file with Windows style eol
# TODO: UTF-16
# Is this needs? AttachmentsController reads file simply.
@content.gsub!("\r\n", "\n")
@changeset = @repository.find_changeset_by_name(@rev)
end
end
def is_entry_text_data?(ent, path)
# UTF-16 contains "\x00".
# It is very strict that file contains less than 30% of ascii symbols
# in non Western Europe.
return true if Redmine::MimeType.is_type?('text', path)
# Ruby 1.8.6 has a bug of integer divisions.
# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
return false if ent.is_binary_data?
true
end
def find_repository
@repository = @myshixun.repository
render_404 if @myshixun.gpid.nil?
@path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
@g = Gitlab.client
@g_project = @g.project(@myshixun.gpid)
@g_default_branch = @g_project.default_branch
# gitlab端获取默认分支
@rev = params[:rev].blank? ? @g_default_branch : params[:rev].to_s.strip
rescue ActiveRecord::RecordNotFound
render_404
end
def allowd_manager
render_403 unless (User.current.manager_of_myshixun?(@myshixun) || User.current.admin?)
end
# Find myshixun of id params[:id]
def find_myshixun
myshixun_id = params[:myshixun_id] || (params[:game] && params[:game][:myshixun_id])
@myshixun = Myshixun.find(myshixun_id)
rescue ActiveRecord::RecordNotFound
render_404
end
def find_game
@game = Game.find(params[:id])
@myshixun = @game.myshixun
rescue ActiveRecord::RecordNotFound
render_404
end
end