Browse Source

Merge remote-tracking branch 'origin/standalone_develop' into standalone_develop

pull/313/head
xiaoxiaoqiong 4 years ago
parent
commit
4a23878185
10 changed files with 119 additions and 40 deletions
  1. +17
    -17
      app/controllers/ci/cloud_accounts_controller.rb
  2. +13
    -12
      app/controllers/ci/pipelines_controller.rb
  3. +3
    -3
      app/controllers/ci/projects_controller.rb
  4. +3
    -3
      app/controllers/ci/secrets_controller.rb
  5. +3
    -3
      app/controllers/ci/templates_controller.rb
  6. +13
    -1
      app/controllers/notices_controller.rb
  7. +11
    -0
      app/jobs/send_template_message_job.rb
  8. +51
    -0
      app/models/message_template/custom_tip.rb
  9. +4
    -0
      app/models/repository.rb
  10. +1
    -1
      app/views/organizations/projects/index.json.jbuilder

+ 17
- 17
app/controllers/ci/cloud_accounts_controller.rb View File

@@ -14,12 +14,12 @@ class Ci::CloudAccountsController < Ci::BaseController

def create
flag, msg = check_bind_cloud_account!
return render_error(msg) if flag === true
return tip_exception(msg) if flag === true

ActiveRecord::Base.transaction do
@cloud_account = bind_account!
if @cloud_account.blank?
render_error('激活失败, 请检查你的云服务器信息是否正确.')
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
@@ -27,17 +27,17 @@ class Ci::CloudAccountsController < Ci::BaseController
end
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def activate
return render_error('请先认证') unless current_user.ci_certification?
return tip_exception('请先认证') unless current_user.ci_certification?

begin
@cloud_account = Ci::CloudAccount.find params[:id]
ActiveRecord::Base.transaction do
if @repo
return render_error('该项目已经激活') if @repo.repo_active?
return tip_exception('该项目已经激活') if @repo.repo_active?
@repo.activate!(@project)
else
@repo = Ci::Repo.auto_create!(@ci_user, @project)
@@ -50,7 +50,7 @@ class Ci::CloudAccountsController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end
end

@@ -59,39 +59,39 @@ class Ci::CloudAccountsController < Ci::BaseController

def bind
flag, msg = check_bind_cloud_account!
return render_error(msg) if flag === true
return tip_exception(msg) if flag === true

ActiveRecord::Base.transaction do
@cloud_account = bind_account!
if @cloud_account.blank?
render_error('激活失败, 请检查你的云服务器信息是否正确.')
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
end
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def trustie_bind
account = params[:account].to_s
return render_error("account不能为空.") if account.blank?
return tip_exception("account不能为空.") if account.blank?

flag, msg = check_trustie_bind_cloud_account!
return render_error(msg) if flag === true
return tip_exception(msg) if flag === true

ActiveRecord::Base.transaction do
@cloud_account = trustie_bind_account!
if @cloud_account.blank?
render_error('激活失败, 请检查你的云服务器信息是否正确.')
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
end
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def unbind
@@ -107,18 +107,18 @@ class Ci::CloudAccountsController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def oauth_grant
password = params[:password].to_s
return render_error('你输入的密码不正确.') unless current_user.check_password?(password)
return tip_exception('你输入的密码不正确.') unless current_user.check_password?(password)

oauth = current_user.oauths.last
return render_error("服务器出小差了.") if oauth.blank?
return tip_exception("服务器出小差了.") if oauth.blank?

result = gitea_oauth_grant!(password, oauth)
return render_error('授权失败.') unless result === true
return tip_exception('授权失败.') unless result === true
current_user.set_drone_step!(User::DEVOPS_CERTIFICATION)
end



+ 13
- 12
app/controllers/ci/pipelines_controller.rb View File

@@ -30,7 +30,7 @@ class Ci::PipelinesController < Ci::BaseController
ActiveRecord::Base.transaction do
size = Ci::Pipeline.where('branch=? and identifier=? and owner=?', params[:branch], params[:repo], params[:owner]).size
if size > 0
render_error("#{params[:branch]}分支已经存在流水线!")
tip_exception("#{params[:branch]}分支已经存在流水线!")
return
end
pipeline = Ci::Pipeline.new(pipeline_name: params[:pipeline_name], file_name: params[:file_name],owner: params[:owner],
@@ -53,7 +53,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok({id: pipeline.id})
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

# 在代码库创建文件
@@ -81,6 +81,7 @@ class Ci::PipelinesController < Ci::BaseController
repo_branch: pipeline.branch,
repo_config: pipeline.file_name
}
Rails.logger.info("########create_params===#{create_params.to_json}")
repo = Ci::Repo.create_repo(create_params)
repo
end
@@ -118,7 +119,7 @@ class Ci::PipelinesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def destroy
@@ -132,7 +133,7 @@ class Ci::PipelinesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def content
@@ -182,7 +183,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def update_stage
@@ -192,7 +193,7 @@ class Ci::PipelinesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def delete_stage
@@ -205,7 +206,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def update_stage_index(pipeline_id, show_index, diff)
@@ -229,7 +230,7 @@ class Ci::PipelinesController < Ci::BaseController
unless steps.empty?
steps.each do |step|
unless step[:template_id]
render_error('请选择模板!')
tip_exception('请选择模板!')
return
end
if !step[:id]
@@ -246,7 +247,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def create_stage_step
@@ -262,7 +263,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def update_stage_step
@@ -279,7 +280,7 @@ class Ci::PipelinesController < Ci::BaseController
render_ok
end
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def delete_stage_step
@@ -289,6 +290,6 @@ class Ci::PipelinesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end
end

+ 3
- 3
app/controllers/ci/projects_controller.rb View File

@@ -30,12 +30,12 @@ class Ci::ProjectsController < Ci::BaseController
@file = interactor.result
render_result(1, "更新成功")
else
render_error(interactor.error)
tip_exception(interactor.error)
end
end

def activate
return render_error('你还未认证') unless current_user.ci_certification?
return tip_exception('你还未认证') unless current_user.ci_certification?

begin
ActiveRecord::Base.transaction do
@@ -60,7 +60,7 @@ class Ci::ProjectsController < Ci::BaseController
end

def deactivate
return render_error('该项目已经取消激活') if !@repo.repo_active?
return tip_exception('该项目已经取消激活') if !@repo.repo_active?

@project.update_column(:open_devops, false)
@repo.deactivate_repos!


+ 3
- 3
app/controllers/ci/secrets_controller.rb View File

@@ -20,14 +20,14 @@ class Ci::SecretsController < Ci::BaseController
if result["id"]
render_ok
else
render_error(result["message"])
tip_exception(result["message"])
end
else
result = Ci::Drone::API.new(@ci_user.user_hash, ci_drone_url, params[:owner], params[:repo], options).create_secret
if result["id"]
render_ok
else
render_error(result["message"])
tip_exception(result["message"])
end
end
end
@@ -39,7 +39,7 @@ class Ci::SecretsController < Ci::BaseController
Ci::Drone::API.new(@ci_user.user_hash, ci_drone_url, params[:owner], params[:repo], {name: name}).delete_secret
render_ok
else
render_error("参数名不能为空")
tip_exception("参数名不能为空")
end
rescue Exception => ex
render_ok


+ 3
- 3
app/controllers/ci/templates_controller.rb View File

@@ -50,7 +50,7 @@ class Ci::TemplatesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def update
@@ -63,7 +63,7 @@ class Ci::TemplatesController < Ci::BaseController
)
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

def destroy
@@ -73,7 +73,7 @@ class Ci::TemplatesController < Ci::BaseController
end
render_ok
rescue Exception => ex
render_error(ex.message)
tip_exception(ex.message)
end

#======流水线模板查询=====#


+ 13
- 1
app/controllers/notices_controller.rb View File

@@ -1,7 +1,7 @@
class NoticesController < ApplicationController

def create
tip_exception("参数有误") if params["source"].blank?
return tip_exception("参数有误") if params["source"].blank?
user_id = params[:user_id]

if params["source"] == "CompetitionBegin"
@@ -13,9 +13,21 @@ class NoticesController < ApplicationController
elsif params["source"] == "CompetitionReview"
competition_id = params[:competition_id]
SendTemplateMessageJob.perform_later('CompetitionReview', user_id, competition_id)
elsif params["source"] == "CustomTip"
users_id = params[:users_id]
props = params[:props].to_unsafe_hash
return tip_exception("参数有误") unless props.is_a?(Hash) && users_id.is_a?(Array)
template_id = params[:template_id]
SendTemplateMessageJob.perform_later('CustomTip', users_id, template_id, props)
else
tip_exception("#{params["source"]}未配置")
end
render_ok
end


private
def params_props
params.require(:notice).permit(:props)
end
end

+ 11
- 0
app/jobs/send_template_message_job.rb View File

@@ -4,6 +4,17 @@ class SendTemplateMessageJob < ApplicationJob
def perform(source, *args)
Rails.logger.info "SendTemplateMessageJob [args] #{args}"
case source
when 'CustomTip'
receivers_id, template_id, props = args[0], args[1], args[2]
template = MessageTemplate.find_by_id(template_id)
return unless template.present?
receivers = User.where(id: receivers_id)
receivers_string, content, notification_url = MessageTemplate::CustomTip.get_message_content(receivers, template, props)
Notice::Write::CreateService.call(receivers_string, content, notification_url, source, {receivers_id: receivers_id, template_id: template_id, props: props})
receivers.find_each do |receiver|
receivers_email_string, email_title, email_content = MessageTemplate::CustomTip.get_email_message_content(receiver, template, props)
Notice::Write::EmailCreateService.call(receivers_email_string, email_title, email_content)
end
when 'FollowTip'
watcher_id = args[0]
watcher = Watcher.find_by_id(watcher_id)


+ 51
- 0
app/models/message_template/custom_tip.rb View File

@@ -0,0 +1,51 @@
# == Schema Information
#
# Table name: message_templates
#
# id :integer not null, primary key
# type :string(255)
# sys_notice :text(65535)
# email :text(65535)
# created_at :datetime not null
# updated_at :datetime not null
# notification_url :string(255)
# email_title :string(255)
#

# 统一模板(

class MessageTemplate::CustomTip < MessageTemplate
# MessageTemplate::CustomTip.get_message_content(User.where(login: 'yystopf'), "hahah")
def self.get_message_content(receivers, template, props={})
return '', '', '' if receivers.blank? || template.blank?
content = template.sys_notice
notification_url = template.notification_url
props.each do |k, v|
content.gsub!("{#{k}}", v)
notification_url.gsub!("{#{k}}", v)
end
return receivers_string(receivers), content, notification_url
rescue => e
Rails.logger.info("MessageTemplate::CustomTip.get_message_content [ERROR] #{e}")
return '', '', ''
end

def self.get_email_message_content(receiver, template, props={})
return '', '', '' if receiver.blank? || template.blank?
title = template.email_title
content = template.email
props.each do |k, v|
title.gsub!("{#{k}}", v)
content.gsub!("{#{k}}", v)
end
content.gsub!('{receiver}', receiver&.real_name)
title.gsub!('{platform}', PLATFORM)
content.gsub!('{platform}', PLATFORM)

return receiver&.mail, title, content
rescue => e
Rails.logger.info("MessageTemplate::CustomTip.get_email_message_content [ERROR] #{e}")
return '', '', ''
end
end

+ 4
- 0
app/models/repository.rb View File

@@ -48,6 +48,10 @@ class Repository < ApplicationRecord
self.identifier.parameterize
end

def url
self['url'].blank? ? "#{Rails.application.config_for(:configuration)['platform_url']}/#{self.owner&.login}/#{self.identifier}.git" : self['url']
end

# with repository is mirror
def set_mirror!
self.build_mirror(status: Mirror.statuses[:waiting]).save


+ 1
- 1
app/views/organizations/projects/index.json.jbuilder View File

@@ -1,6 +1,6 @@
json.total_count @projects.total_count
json.projects @projects.each do |project|
json.(project, :id, :name, :identifier, :description, :forked_count, :praises_count, :forked_from_project_id)
json.(project, :id, :name, :identifier, :description, :forked_count, :praises_count, :forked_from_project_id, :is_public)
json.mirror_url project.repository&.mirror_url
json.type project.numerical_for_project_type
json.praised project.praised_by?(current_user)


Loading…
Cancel
Save