qianyi datas

This commit is contained in:
sylor_huang@126.com 2020-07-09 16:46:57 +08:00
parent 1bb8060b37
commit b340de89c7
7 changed files with 324 additions and 5 deletions

View File

@ -40,7 +40,7 @@ gem 'oauth2'
#导出为pdf
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
gem 'request_store'
# gem 'request_store'
#gem 'iconv'
# markdown 转html
gem 'redcarpet', '~> 3.4'

View File

@ -1,8 +1,13 @@
class AdminConstraint
def matches?(request)
laboratory = Laboratory.first
return false unless request.session[:"#{laboratory.try(:identifier).split('.').first}_user_id"]
user = User.find request.session[:"#{laboratory.try(:identifier).split('.').first}_user_id"]
user && user.admin?
if Rails.env.development?
true
else
laboratory = Laboratory.first
return false unless request.session[:"#{laboratory.try(:identifier).split('.').first}_user_id"]
user = User.find request.session[:"#{laboratory.try(:identifier).split('.').first}_user_id"]
user && user.admin?
end
end
end

View File

@ -0,0 +1,194 @@
class SyncForgeController < ApplicationController
before_action :check_token
def create
sync_params = params[:sync_params]
#以前已同步的项目,那么肯定存在仓库
if Project.exists?(id: sync_params[:id], identifier: sync_params[:identifier])
project = Project.find_by(id: sync_params[:id])
check_sync_project(project, sync_params)
else #新建项目
project_user = User.where(login: sync_params[:owner_login]).first.id
project_params = {
identifier: sync_params[:identifier],
user_id: user_id,
is_public: sync_params[:is_public]
}
project = Projects::CreateService.new(project_user, project_params).call
if project.present?
project.project_score.create!( sync_params[:project_score]) if sync_params[:project_score]
SyncRepositoryJob.perform_later(project.repository, sync_params[:repository_params]) if sync_params[:repository_params]
check_new_project(project, sync_params)
end
end
end
def sync_users
params.permit!
users_params = params[:sync_params]
users_params.each do |u|
unless User.exists?(login: u[:user_params][:login])
new_user = User.new(u[:user_params])
username = new_user.login
password = "12345678"
ActiveRecord::Base.transaction do
interactor = Gitea::RegisterInteractor.call({username: username, email: new_user.mail, password: password})
if interactor.success?
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.new(username, password).call
new_user.gitea_token = result['sha1']
new_user.gitea_uid = gitea_user['id']
if new_user.save!
new_user.user_extension.create!(u[:user_extensions]) if u[:user_extensions].present?
normal_status(1, "created_succrss")
end
else
normal_status(-1, "created_failed")
end
end
end
end
rescue Exception => e
normal_status(-1, e.message)
end
private
def check_sync_project(project,sync_params)
if sync_params[:repository_params].present? #仓库存在
change_project_score(project, sync_params[:project_score], sync_params[:repository_params]) #更新project_score
end
change_project_issues(project, sync_params[:issues],project.id)
change_project_members(project, sync_params[:members])
change_project_versions(project, sync_params[:project_versions])
change_project_watchers(project, sync_params[:project_watchers])
change_project_praises(project, sync_params[:praise_trends])
end
def check_new_project(project,sync_params)
sync_projects_params = {
type: "Project",
ids: sync_params[:id],
token: get_token,
sync_params: sync_params,
new_project_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params)
end
def change_project_praises(project, praises)
forge_praises_ids = project&.praise_treads&.select(:id)&.pluck(:id)
diff_target_ids = praises[:ids] - forge_praises_ids
if diff_target_ids.size > 0
sync_projects_params = {
type: "PraiseTread",
ids: diff_target_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params)
end
end
#检查repository和project_score
def change_project_score(project, project_scores, repository_params)
pre_project_score = project.project_score
if pre_project_score.present?
change_num = 0
project_scores.each do |k,v|
unless pre_project_score.send("#{k}") == v
change_num += 1
pre_project_score[:"#{k}"] = v
end
if k == "changeset_num" && v.to_i > pre_project_score.changeset_num.to_i && repository_params[:url].present?
SyncRepositoryJob.perform_later(project.repository, repository_params)
end
end
pre_project_score.save! if change_num > 0 #如果 project_score有变化则更新
else
project.project_score.create!(project_scores)
end
end
def change_project_issues(project, old_issues_params,project_id)
forge_issue_ids = project&.issues&.select(:id)&.pluck(:id)
forge_journal_ids = Journal.select([:id, :journalized_id, :journalized_type]).where(journalized_id: forge_issue_ids).pluck(:id)
diff_issue_ids = old_issues_params[:issue_params][:ids] - forge_issue_ids
sync_projects_params = {}
if diff_issue_ids.size == 0 #issue数量一样判断评论是否有增减
diff_journal_ids = old_issues_params[:issue_params][:journals][:ids] - forge_journal_ids
unless diff_journal_ids.size == 0
sync_projects_params = {
type: "Journal",
ids: diff_journal_ids,
token: get_token,
parent_id: project_id
}
end
else
sync_projects_params = {
type: "Issue",
ids: diff_issue_ids,
token: get_token,
parent_id: project_id
}
end
SyncProjectsJob.perform_later(sync_projects_params) if sync_projects_params.present?
end
def change_project_watchers(project, watchers)
forge_watchers_ids = project&.watchers&.select(:id)&.pluck(:id)
diff_target_ids = watchers[:ids] - forge_watchers_ids
if diff_target_ids.size > 0
sync_projects_params = {
type: "Watcher",
ids: diff_target_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params)
end
end
def change_project_versions(project, versions)
forge_version_ids = project&.versions&.select(:id)&.pluck(:id)
diff_version_ids = versions[:ids] - forge_version_ids
if diff_version_ids.size > 0
sync_projects_params = {
type: "Version",
ids: diff_version_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params)
end
end
def change_project_members(project, members)
forge_member_ids = project&.members&.select(:id)&.pluck(:id)
diff_member_ids = members[:ids] - forge_member_ids
if diff_member_ids.size > 0
sync_projects_params = {
type: "Member",
ids: diff_member_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params)
end
end
def check_token
sync_params = params[:sync_params][0]
unless sync_params[:token] && sync_params[:token] == get_token
render json: {message: "token_errors"}
end
end
def get_token
"34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e"
end
end

View File

@ -0,0 +1,69 @@
class SyncProjectsJob < ApplicationJob
queue_as :default
require 'uri'
require 'net/http'
def perform(sync_params)
SyncLog.sync_log.info("==========begin to sync #{sync_params[:type]} to forge============")
begin
gitea_main = "https://trustie.net"
if request.subdomain === 'testforgeplus'
gitea_main = "https://ucloudtest.trustie.net/"
end
url = "#{gitea_main}/sync_forges" #trustie上的相关路由
sync_json = {
"sync_params": sync_params
}
uri = URI.parse(url)
if api_host
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true
response = http.send_request('GET', uri.path, sync_params, {'Content-Type' => 'application/json'})
if response.status == 200
target_jsons = response.body
if target_jsons.present? && sync_params[:type]
create_target(eval(target_jsons), sync_params[:type].to_s)
end
else
SyncLog.sync_log.info("==========bsync_user_to_forge_failed #{sync_params[:type]}============")
end
end
rescue => e
SyncLog.sync_log.info("==========bsync_user_to_forge_failed #{sync_params[:type]}============errors:#{e}")
end
end
def create_target(target_jsons, target_type)
target_jsons.each do |re|
u_id = User.select(:id, :login).where(login: re[:user_login]).pluck(:id).first
new_target = target_type.constantize.new(re[:target_params].merge(user_id: u_id))
if target_type == "Project"
create_target(re[:issues_params], "Issue") if re[:issues_params].present?
create_target(re[:member_params], "Member") if re[:member_params].present?
create_target(re[:versions_params], "Version") if re[:versions_params].present?
create_target(re[:watcher_params], "Watcher") if re[:watcher_params].present?
create_target(re[:praise_treads], "PraiseTread") if re[:praise_treads].present?
end
if target_type == "Issue"
assing_u_id = User.select(:id, :login).where(login: re[:assign_login]).pluck(:id).first
new_target.assigned_to_id = assing_u_id
if re[:journals].present?
create_target(re[:journals], "Journal")
end
end
if new_target.save!
if re[:journal_details].present?
re[:journal_details].each do |j|
JournalDetail.create!(j[:journal_detail].merge(journal_id: new_target.id))
end
end
end
end
end
end

View File

@ -0,0 +1,40 @@
class SyncRepositoryJob < ApplicationJob
queue_as :default
#同步 trustie的仓库
def perform(repository, repository_params)
#创建临时文件夹 clone 并强推代码
SyncLog.sync_log("=================begin to sync repository=====================")
path = "#{Rails.root}/public/cache_repository"
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
image_url = repository_params[:url]
g_default_branch = repository_params[:default_branch]
image_repo_name = image_url.to_s.split('/')&.last&.chomp('.git')
check_clone = system("cd #{path} and git clone #{image_url}")
gitea_main = "testgitea.trustie.net"
if request.subdomain === 'testforgeplus'
gitea_main = "testgitea2.trustie.net"
elsif request.subdomain === 'forge'
gitea_main = "gitea.trustie.net"
end
if check_clone
new_gitlab_url = "http://root:_Trustie_10010@#{gitea_main}/#{repository.user.login}/#{repository.identifier}.git"
shell_remote_1 = system("cd #{path}/#{image_repo_name} && git remote set-url origin #{new_gitlab_url}")
shell5 = system("cd #{path}/#{image_repo_name} && git checkout #{g_default_branch} && git push --force --set-upstream origin #{g_default_branch}")
if !shell5
SyncLog.sync_log("++++++++++++++++++force_push_erros++++++++++++++++++##{path}/#{image_repo_name}++++++new_gitlab_url+++#{new_gitlab_url}")
end
else
SyncLog.sync_log("++++++++++++++++++check_clone_erros++++++++++++++++++#{image_repo_name}")
end
SyncLog.sync_log("=================end to sync repository=====================#{image_repo_name}")
end
end

6
app/models/sync_log.rb Normal file
View File

@ -0,0 +1,6 @@
class SyncLog
def self.sync_log(message=nil)
@my_log ||= Logger.new("#{Rails.root}/log/sync.log")
@my_log.debug(message) unless message.nil?
end
end

View File

@ -16,6 +16,11 @@ Rails.application.routes.draw do
resources :edu_settings
scope '/api' do
resources :sync_forge, only: [:create] do
collection do
post :sync_users
end
end
resources :composes do
resources :compose_projects, only: [:create, :destroy]
end