diff --git a/app/controllers/at_controller.rb b/app/controllers/at_controller.rb index 66cbf27b8..81d678261 100644 --- a/app/controllers/at_controller.rb +++ b/app/controllers/at_controller.rb @@ -29,6 +29,8 @@ class AtController < ApplicationController case type when "Issue" find_issue(id) + whne 'TrainingTask' + find_training_task(id) when 'Project' find_project(id) when 'Course' @@ -69,6 +71,13 @@ class AtController < ApplicationController at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id } end + def find_training_task(id) + training_task = TrainingTask.find(id) + journals = training_task.journals + at_persons = journals.map(&:user) + training_task.project.users + at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id } + end + def find_project(id) return [] if id.to_i<0 at_persons = Project.find(id).users diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index e91b17212..26137b584 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -379,7 +379,9 @@ class ProjectsController < ApplicationController def settings # 顶部导航 @project_menu_type = 10 - + if @project.is_child_training_project? + return render_404 + end # 修改查看消息状态 applied_messages = ForgeMessage.where("user_id =? and project_id =? and forge_message_type =? and viewed =?", User.current.id, @project, "AppliedProject", 0) applied_messages.update_all(:viewed => true) @@ -765,6 +767,14 @@ class ProjectsController < ApplicationController end end + def add_script + if @project.update_attribute(:script, params[:project_script]) + @notice = "脚本添加成功" + else + @notice = "脚本添加失败" + end + end + def modules @project.enabled_module_names = params[:enabled_module_names] flash[:notice] = l(:notice_successful_update) @@ -807,24 +817,33 @@ class ProjectsController < ApplicationController def training_project_execute jobName = "#{@project.id}" - pipeLine = Base64.encode64(@project.script) - params = {:jobName => "#{jobName}", :pipeLine => "#{pipeLine}"} - uri = URI.parse("http://106.75.33.219:9999/jenkins-exec/api/createJob") - begin - respond_message = uri_exec uri, params - @project.update_attribute(:training_status, 1) if respond_message.message - return - rescue - @message = "failure" + pipeLine = "#{Base64.encode64(@project.script)}" + uri = URI("http://123.59.135.74:9999/jenkins-exec/api/createJob") + res = Net::HTTP.post_form(uri, {jobName: jobName, pipeLine: pipeLine}).body + if res.code == 0 + @project.update_attribute(:training_status, 1) + else + flash[:notice] = "启动失败" end end - def training_task_execute - positon = params[:positon].to_i + def training_project_update + jobName = "#{@project.id}" + pipeLine = "#{Base64.encode64(@project.script)}" + uri = URI("http://123.59.135.74:9999/jenkins-exec/api/updateJob") + res = Net::HTTP.post_form(uri, {jobName: jobName, pipeLine: pipeLine}).body + if res.code == 0 + @project.update_attribute(:training_status, 1) + else + flash[:notice] = "启动失败" + end + end + + def task_execute taskId = params[:training_task_id] - jobName = @project.job_name + jobName = @project.forked_from_project_id params = {:jobName => "#{jobName}", :taskId => "#{taskId}"} - uri = URI.parse("http://106.75.33.219:9999/jenkins-exec/api/buildJob") + uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob") begin task = TrainingTask.find(taskId) respond_message = uri_exec uri, params @@ -837,13 +856,22 @@ class ProjectsController < ApplicationController end def uri_exec uri, params - res = Net::HTTP.post_form(uri, params) - res.body + Net::HTTP.post_form(uri, {jobName: '222e', pipeLine: base64}).body + + res = Net::HTTP.post_form(uri, params).body + end + + # 需要传Jobname(项目ID),Task ID, Stage + def training_task_status + task_id = params[:task_id] + stage = params[:stage].to_i + training_task = TrainingTask.find(task_id) + training_task.update_attribute(:status => stage) + end # 资源库fork弹框 def forked_pop - @task = params[:task] respond_to do |format| format.js end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index e279e46bd..50dbaec4d 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -148,12 +148,12 @@ class RepositoriesController < ApplicationController s = Trustie::Gitlab::Sync.new s.sync_user(User.current) ensure - logger.error "Synv user failed ==>#{User.current.id}" + logger.error "Syn user failed ==>#{User.current.id}" end end gproject = g.fork(@project.gpid, User.current.gid) if gproject - new_training_project = copy_project(@project, gproject) + new_training_project = copu_project_and_module(@project, gproject) forked_count = @project.forked_count.to_i + 1 @project.update_attributes(:forked_count => forked_count) # 发布实训任务,只发布实训任务的第一个 @@ -163,6 +163,43 @@ class RepositoriesController < ApplicationController end end + def copu_project_and_module tproject, gproject + project = Project.new + project.name = tproject.name + project.is_public = tproject.is_public + project.status = tproject.status + project.description = tproject.description + project.hidden_repo = tproject.hidden_repo + project.user_id = User.current.id + project.project_type = 0 + project.project_new_type = tproject.project_new_type + project.gpid = gproject.id + project.forked_from_project_id = tproject.id + project.enabled_module_names = tproject.enabled_module_names + if project.save + project.update_attribute(:training_status,1) + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => User.current, :roles => [r]) + if ProjectScore.where("project_id=?", project.id).first.nil? + ProjectScore.create(:project_id => project.id, :score => false) + end + project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => project.id) + user_grades = UserGrade.create(:user_id => User.current.id, :project_id => project.id) + Rails.logger.debug "UserGrade created: #{user_grades.to_json}" + project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type,:grade => 0) + Rails.logger.debug "ProjectStatus created: #{project_status.to_json}" + project.members << m + project.project_infos << project_info + copy_repository(project, gproject) + return project + else + respond_to do |format| + format.html { render :action => 'forked', :layout => 'base_projects'} + format.api { render_validation_errors(@project) } + end + end + end + # REDO: 如果实训项目还没有创建任务的时候应该跳出 def publish_training_tasks original_project, new_training_project original_task = TrainingTask.where(:project_id => original_project.id, :position => 1).first @@ -221,19 +258,19 @@ class RepositoriesController < ApplicationController project.members << m project.project_infos << project_info copy_repository(project, gproject) - # respond_to do |format| - # format.html { - # flash[:notice] = l(:notice_successful_create) - # if params[:continue] - # attrs = {:parent_id => project.parent_id}.reject {|k,v| v.nil?} - # redirect_to new_project_url(attrs, :course => '0') - # else - # redirect_to project_path(project) - # end - # } - # format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => project.id) } - # format.js - # end + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_create) + if params[:continue] + attrs = {:parent_id => project.parent_id}.reject {|k,v| v.nil?} + redirect_to new_project_url(attrs, :course => '0') + else + redirect_to project_path(project) + end + } + format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => project.id) } + format.js + end return project else respond_to do |format| diff --git a/app/controllers/training_tasks_controller.rb b/app/controllers/training_tasks_controller.rb index 9c84b08f8..1cf775da2 100644 --- a/app/controllers/training_tasks_controller.rb +++ b/app/controllers/training_tasks_controller.rb @@ -59,7 +59,7 @@ class TrainingTasksController < ApplicationController def index # 顶部导航 @project_menu_type = 11 - @training_tasks = TrainingTask.where(:project_id => @project.id).order('updated_at desc') + @training_tasks = TrainingTask.where(:project_id => @project.id).order('position desc') @training_tasks_count = @training_tasks.count @limit = 10 @is_remote = true @@ -88,6 +88,7 @@ class TrainingTasksController < ApplicationController @journal = Journal.new(:journalized => @training_task) respond_to do |format| format.html + format.js end end @@ -358,19 +359,19 @@ class TrainingTasksController < ApplicationController jour = Journal.new jour.user_id = User.current.id jour.notes = params[:notes] - jour.journalized = @issue + jour.journalized = @training_task jour.save_attachments(params[:attachments]) jour.save - update_user_activity(@issue.class,@issue.id) - update_forge_activity(@issue.class,@issue.id) - @allowed_statuses = @issue.new_statuses_allowed_to(User.current) + update_user_activity(@training_task.class, @training_task.id) + update_forge_activity(@training_task.class, @training_task.id) + # @allowed_statuses = @training_task.new_statuses_allowed_to(User.current) @user_activity_id = params[:user_activity_id] @priorities = IssuePriority.active respond_to do |format| # Issue详情单独处理 if params[:is_issue_show] - format.js{redirect_to issue_path(@issue)} + format.js{ redirect_to training_task_path(@training_task) } else format.js end @@ -397,7 +398,7 @@ class TrainingTasksController < ApplicationController #对某个journ回复,显示回复框 def reply - @issue = Issue.find(params[:id]) + @training_task = TrainingTask.find(params[:id]) @jour = Journal.find(params[:journal_id]) respond_to do |format| format.js @@ -408,25 +409,25 @@ class TrainingTasksController < ApplicationController def add_reply if User.current.logged? jour = Journal.find(params[:journal_id]) - @issue = Issue.find params[:id] - @project = @issue.project - @allowed_statuses = @issue.new_statuses_allowed_to(User.current) + @training_task = TrainingTask.find params[:id] + @project = @training_task.project + @allowed_statuses = @training_task.new_statuses_allowed_to(User.current) @priorities = IssuePriority.active new_jour = Journal.new new_jour.user_id = User.current.id new_jour.reply_id = params[:journal_id] new_jour.parent_id = jour.id new_jour.notes = params[:content] - new_jour.journalized = @issue + new_jour.journalized = @training_task new_jour.save_attachments(params[:attachments]) # new_jour = @issue.journals.build(:user_id => User.current.id, :reply_id => params[:journal_id], :notes => params[:content], :parent_id => jour.id) @user_activity_id = params[:user_activity_id] if new_jour.save - update_user_activity(@issue.class,@issue.id) - update_forge_activity(@issue.class,@issue.id) + update_user_activity(@training_task.class,@issue.id) + update_forge_activity(@training_task.class,@issue.id) respond_to do |format| if params[:is_issue_show] - format.js{redirect_to issue_path(@issue)} + format.js{ redirect_to project_training_task_path(@training_task) } else format.js end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index c87cdcca5..a7fef3dd7 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -544,7 +544,7 @@ module ProjectsHelper result = "manage_versions" elsif user.allowed_to?(:manage_repository, @project) result = "manage_repository" - elsif user.admin? + elsif is_project_manager?(user.id, @project.id) result = "training_task" end result diff --git a/app/helpers/training_tasks_helper.rb b/app/helpers/training_tasks_helper.rb index 4096b1cb0..bb52e8a79 100644 --- a/app/helpers/training_tasks_helper.rb +++ b/app/helpers/training_tasks_helper.rb @@ -1,2 +1,44 @@ module TrainingTasksHelper + include ApplicationHelper + include TagsHelper + require 'iconv' + + # Returns the textual representation of a journal details + # as an array of strings + def details_to_strings(details, no_html=false, options={}) + options[:only_path] = (options[:only_path] == false ? false : true) + options[:token] = options[:token] if options[:token] + strings = [] + values_by_field = {} + details.each do |detail| + + if detail.property == 'cf' + field_id = detail.prop_key + field = CustomField.find_by_id(field_id) + if field && field.multiple? + values_by_field[field_id] ||= {:added => [], :deleted => []} + if detail.old_value + values_by_field[field_id][:deleted] << detail.old_value + end + if detail.value + values_by_field[field_id][:added] << detail.value + end + next + end + end + strings << show_detail(detail, no_html, options) + + end + values_by_field.each do |field_id, changes| + detail = JournalDetail.new(:property => 'cf', :prop_key => field_id) + if changes[:added].any? + detail.value = changes[:added] + strings << show_detail(detail, no_html, options) + elsif changes[:deleted].any? + detail.old_value = changes[:deleted] + strings << show_detail(detail, no_html, options) + end + end + strings + end end diff --git a/app/models/journal.rb b/app/models/journal.rb index f14484a31..822d7beca 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -23,7 +23,8 @@ class Journal < ActiveRecord::Base belongs_to :journalized, :polymorphic => true,:touch => true # added as a quick fix to allow eager loading of the polymorphic association # since always associated to an issue, for now - belongs_to :issue, :foreign_key => :journalized_id,:touch => true + belongs_to :issue, :foreign_key => :journalized_id, :touch => true + belongs_to :training_task, :foreign_key => :journalized_id, :touch => true belongs_to :user has_many :details, :class_name => "JournalDetail", :dependent => :delete_all @@ -192,27 +193,44 @@ class Journal < ActiveRecord::Base def act_as_forge_message receivers = [] # 直接回复 - if self.user_id != self.issue.author_id - receivers << self.issue.author_id - end - if self.user_id != self.issue.assigned_to_id && self.issue.assigned_to_id != self.issue.author_id # 指派人不是自己的话,则给指派人发送 - receivers << self.issue.assigned_to_id - end - receivers.each do |r| - self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.issue.project_id, :viewed => false) + if self.journalized_type == 'Issue' + if self.user_id != self.issue.author_id + receivers << self.issue.author_id + end + if self.user_id != self.issue.assigned_to_id && self.issue.assigned_to_id != self.issue.author_id # 指派人不是自己的话,则给指派人发送 + receivers << self.issue.assigned_to_id + end + receivers.each do |r| + self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.issue.project_id, :viewed => false) + end + elsif self.journalized_type == 'TrainingTask' + if self.user_id != self.training_task.author_id + receivers << self.training_task.author_id + end + receivers.each do |r| + self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.training_task.project, :viewed => false) + end end end # 更新用户分数 -by zjc def be_user_score + if (self.journalized_type == 'Issue') #新建了缺陷留言且留言不为空,不为空白 - if !self.notes.nil? && self.notes.gsub(' ','') != '' - #协同得分加分 - UserScore.joint(:post_issue_message, self.user,self.issue.author,self, { message_id: self.id }) - update_messges_for_issue(self.user,1) - update_messges_for_issue(self.user,2,self.issue.project) + if !self.notes.nil? && self.notes.gsub(' ','') != '' + #协同得分加分 + UserScore.joint(:post_issue_message, self.user,self.issue.author,self, { message_id: self.id }) + update_messges_for_issue(self.user,1) + update_messges_for_issue(self.user,2,self.issue.project) + end + elsif( self.journalized_type == 'TrainingTask') + if !self.notes.nil? && self.notes.gsub(' ','') != '' + #协同得分加分 + UserScore.joint(:post_issue_message, self.user, self.training_task.author, self, { message_id: self.id }) + update_messges_for_issue(self.user, 1) + update_messges_for_issue(self.user, 2, self.training_task.project) + end end - end # 减少用户分数 -by zjc def down_user_score @@ -237,12 +255,18 @@ class Journal < ActiveRecord::Base # issue留言总数更新 def add_journals_count - if !self.issue.project.nil? && self.journalized_type == "Issue" && !self.issue.project.project_score.nil? - project = self.issue.project - project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + if self.journalized_type == 'Issue' + if !self.issue.project.nil? && self.journalized_type == "Issue" && !self.issue.project.project_score.nil? + project = self.issue.project + project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + end + elsif self.journalized_type == 'TrainingTask' + if !self.training_task.project.nil? && self.journalized_type == "TrainingTask" && !self.training_task.project.project_score.nil? + project = self.training_task.project + project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + end end end - # 回复issue的时候,更新issue的时候 def update_issue_time if self.journalized_type == "Issue" diff --git a/app/models/project.rb b/app/models/project.rb index 1421ebc8e..cd8ea3d7b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -160,6 +160,7 @@ class Project < ActiveRecord::Base after_create :create_board_sync,:acts_as_forge_activities, :create_project_ealasticsearch_index before_destroy :delete_all_members,:delete_project_ealasticsearch_index after_update :update_project_ealasticsearch_index + def remove_references_before_destroy return if self.id.nil? Watcher.delete_all ['watchable_id = ?', id] @@ -194,6 +195,7 @@ class Project < ActiveRecord::Base scope :project_entities, -> { where(project_type: ProjectType_project) } scope :course_entities, -> { where(project_type: ProjectType_course) } scope :indexable,lambda { where('is_public = 1')} #用于elastic建索引的scope + def self.search(query) __elasticsearch__.search( { @@ -220,6 +222,17 @@ class Project < ActiveRecord::Base } ) end + + # 判断项目是否为实训项目 + def is_training_project? + (self.enabled_modules.where("name = 'training_tasks'").empty? && self.training_status == 0) ? false : true + end + + # 判断项目是否为开启实训的项目(eg:学生开启实训) + def is_child_training_project? + self.training_status == 1 ? true :false + end + def new_course self.where('project_type = ?', 1) end diff --git a/app/views/layouts/_base_project_top.html.erb b/app/views/layouts/_base_project_top.html.erb index 0c59da6cf..6d26bc124 100644 --- a/app/views/layouts/_base_project_top.html.erb +++ b/app/views/layouts/_base_project_top.html.erb @@ -15,9 +15,12 @@ -
提示文字
<%= link_to "开启实训", training_project_execute_project_path(@project) %>
- -