增加chain的积分操作和上链操作

This commit is contained in:
sylor_huang@126.com 2020-05-12 18:15:55 +08:00
parent 27cc8ab96d
commit 30a8367d09
6 changed files with 198 additions and 27 deletions

View File

@ -104,8 +104,14 @@ class IssuesController < ApplicationController
normal_status(-1, "标题不能为空")
elsif params[:subject].to_s.size > 255
normal_status(-1, "标题不能超过255个字符")
elsif (params[:issue_type].to_s == "2") && params[:token].to_i == 0
normal_status(-1, "悬赏的奖金必须大于0")
elsif (params[:issue_type].to_s == "2")
return normal_status(-1, "悬赏的奖金必须大于0")if params[:token].to_i == 0
#查看当前用户的积分
query_params = {
type: "user"
}.merge(tokens_params(@project))
user_tokens = Gitea::Repository::Hooks::QueryService.new(query_params).call
return normal_status(-1, "悬赏的奖金不足") if user_tokens[:value].to_i < params[:token].to_i
else
issue_params = issue_send_params(params)
@ -133,6 +139,16 @@ class IssuesController < ApplicationController
parent_container_id: @project.id, parent_container_type: "Project",
tiding_type: 'issue', status: 0)
end
#为悬赏任务时, 扣除当前用户的积分
if params[:issue_type].to_s == "2"
change_params = {
change_type: "minusToken",
tokens: params[:token]
}.merge(tokens_params(@proeject))
ChangeTokenJob.perform_later(change_params)
end
@issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
normal_status(0, "创建成功")
else
@ -150,6 +166,7 @@ class IssuesController < ApplicationController
def update
issue_params = issue_send_params(params).except("issue_classify", "author_id", "project_id")
return normal_status(-1, "您没有权限修改token") if @issue.will_save_change_to_token? && @issue.user_id !== current_user&.id
if params[:issue_tag_ids].present? && !@issue&.issue_tags_relates.where(issue_tag_id: params[:issue_tag_ids]).exists?
@issue&.issue_tags_relates&.destroy_all
params[:issue_tag_ids].each do |tag|
@ -193,6 +210,21 @@ class IssuesController < ApplicationController
@issue.issue_times.update_all(end_time: Time.now)
end
if @issue.issue_type.to_s == "2"
#表示修改token值
if @issue.saved_change_to_attribute("token")
last_token = @issue.token_was
change_token = last_token - @issue.token
change_type = change_token > 0 ? "addToken" : "minusToken"
change_params = {
change_type: change_type,
tokens: change_token.abs
}.merge(tokens_params(@proeject))
ChangeTokenJob.perform_later(change_params)
end
end
@issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id)
normal_status(0, "更新成功")
else
@ -243,6 +275,9 @@ class IssuesController < ApplicationController
def close_issue
type = params[:status_id].to_i || 5
return normal_status(-1, "悬赏工单不允许再次打开") if @issue.issue_type.to_s == "2" && @issue.status_id.to_i == 5
return normal_status(-1, "您没有权限操作") if @issue.issue_type.to_s == "2" && (@issue.user_id !== current_user&.id || !current_user&.admin?)
if type == 5
message = "关闭"
old_message = "重新开启"
@ -255,6 +290,17 @@ class IssuesController < ApplicationController
@issue&.project_trends&.update_all(action_type: "close")
@issue.issue_times.update_all(end_time: Time.now)
if @issue.issue_type.to_s == "2"
tokens = @issue.token
change_params = {
change_type: "addToken",
tokens: tokens,
ownername: project.owner.try(:login),
reponame: project.try(:identifer),
username: @issue.get_assign_user.try(:login) #指派人增加积分
}
ChangeTokenJob.perform_later(change_params)
end
else
@issue&.project_trends&.update_all(action_type: "create")
end
@ -264,7 +310,6 @@ class IssuesController < ApplicationController
close_message = "close_pr"
end
@issue.custom_journal_detail(close_message,old_message, "#{message}", current_user&.id)
normal_status(0, message)
else
normal_status(-1, "操作失败")
@ -381,4 +426,13 @@ class IssuesController < ApplicationController
project_id: @project.id
}
end
def tokens_params(project)
{
ownername: project.owner.try(:login),
reponame: project.try(:identifer),
username: current_user.try(:login)
}
end
end

View File

@ -124,7 +124,22 @@ class RepositoriesController < ApplicationController
end
def repo_hook
Rails.logger.info("#####______222________########")
hook_type = request.headers["X-Gitea-Event"].to_s # 获取推送的方式
ownername = @project.owner.try(:login)
reponame = @project.identifier
username = current_user.try(:login)
user_params = {
"ownername": ownername,
"username": username,
"reponame": reponame
}
uploadPushInfo = hook_params(hook_type, params).merge(user_params)
chain_params = {
type: hook_type,
uploadPushInfo: uploadPushInfo
}.merge(user_params)
ProjectCreateChainJob.perform_later(chain_params)
@project.update_attribute(:token, @project.token + uploadPushInfo[:modificationLines].to_i)
end
private
@ -151,4 +166,28 @@ class RepositoriesController < ApplicationController
}
end
def hook_params(hook_type, params)
if hook_type == "push"
# TODO hook返回的记录中暂时没有文件代码数量的增减暂时根据 commits数量来计算
uploadPushInfo = {
"shas": params["commits"].map{|c| c["id"]} if params["commits"].present?,
"branch": params["ref"].to_s.split("/").last,
"modificationLines": params["commits"].length
}
elsif hook_type == "pull_request" && params["action"].to_s == "closed" #合并请求合并后才会有上链操作
uploadPushInfo = {
"source_branch": params["head"]["ref"].to_s.split("/").last,
"target_branch": params["base"]["ref"].to_s.split("/").last,
"source_project_id": params["head"]["repo_id"].to_i, #现在是为gitea上仓库的id
"target_project_id": params["base"]["repo_id"].to_i,
"shas": [params["pull_request"]["merge_commit_sha"], params["pull_request"]["merge_base"]],
"modificationLines": 1 #pull_request中没有commits数量
}
else
uploadPushInfo = {}
end
return uploadPushInfo
end
end

View File

@ -0,0 +1,21 @@
class ChangeTokenJob < ApplicationJob
queue_as :default
def perform(change_params)
status = 0
5.times do |i|
if status == 200
Rails.logger.info("########_change_user_toke_success_user:__#{change_params[:username]}_try:_#{i+1}_")
break
else
Rails.logger.info("########_change_user_toke_try:_#{i+1}_")
chain_status = system("chain #{change_params[:change_type]} #{change_params[:ownername]} #{change_params[:reponame]} #{change_params[:username]} #{change_params[:tokens].to_i}")
status = chain_status[:status].to_i
end
end
unless status == 200
Rails.logger.info("########_change_user_toke_failed__change_params:__#{change_parame}_")
end
end
end

View File

@ -1,8 +1,10 @@
class ProjectCreateChainJob < ApplicationJob
queue_as :default
def perform(owner_login, reponame)
def perform(chain_params)
status = 0
chain_type = chain_params[:type].to_s
reponame = chain_params[:reponame]
5.times do |i|
if status == 200
@ -10,10 +12,31 @@ class ProjectCreateChainJob < ApplicationJob
break
else
Rails.logger.info("########_repository__#{reponame}______start_to_create_chain__try:_#{i+1}_")
create_chain = system("chain trustieCreate #{owner_login} #{reponame}")
status = create_chain[:status].to_i
Rails.logger.info("########_repository__#{reponame}__create_chain:___#{create_chain}____failed_to_create_chain__")
if chain_type == "create"
chain_status = create_chain(chain_params)
elsif chain_type == "push"
chain_status = push_chain(chain_params)
else
chain_status = {status: 200}
end
status = chain_status[:status].to_i
end
end
unless status == 200
Rails.logger.info("########_repository__#{reponame}__create_chain:___#{chain_status}____failed_to_create_chain__")
end
end
private
#创建项目的上链操作
def create_chain(chain_params)
system("chain trustieCreate #{chain_params[:ownername]} #{chain_params[:reponame]}")
end
# push项目的上链操作
def push_chain(chain_params)
system("chain trustiePush #{chain_params[:ownername]} #{chain_params[:reponame]} #{chain_params[:username]} #{chain_params[:uploadPushInfo]}")
end
end

View File

@ -0,0 +1,32 @@
class Gitea::Repository::Hooks::QueryService < Gitea::ClientService
#查询用户的积分/贡献
#query_params = {
#type: "user", #"user/members/percent"
#ownername: ownername,
#reponame: reponame,
#username: username
#}
attr_reader :query_params
def initialize(query_params)
@query_params = query_params
end
def call
query_type = query_params[:type] || "user"
if query_type == "user" #查询单个用户的积分
query_result = system("chain query #{query_params[:ownername]} #{query_params[:reponame]} #{query_params[:username]}")
#response {status:int, message:string, value:int}
elsif query_type == "members" #查询项目全部用户的积分
query_result = system("chain getAllInfo #{query_params[:ownername]} #{query_params[:reponame]} ")
#response {status:int, message:string, value:jsonObject}
else #查询用户在项目的贡献大小
query_result = system("chain getContributionPercent #{query_params[:ownername]} #{query_params[:reponame]} #{query_params[:username]}")
#response {status:int, message:string, percent:int, allTokenSum:int, personalTokens:int}
end
query_result
end
end

View File

@ -14,24 +14,26 @@ class Repositories::CreateService < ApplicationService
gitea_repository = Gitea::Repository::CreateService.new(user.gitea_token, gitea_repository_params).call
sync_project(@repository, gitea_repository)
sync_repository(@repository, gitea_repository)
Rails.logger.info("#######________reuqest_domain____#########{EduSetting.get("host_name")}")
#if project.project_type == "common"
#hook_params = {
# active: true,
# type: "gitea"
# branch_filter: "",
# config: {
# content_type: "application/json",
# url: "#{EduSetting.get("host_name")}/repositories/#{project.id}/repo_hooks",
# http_method: "post"
#},
#events: ["create", "pull", "push"],
#}
#Gitea::Repository::Hooks::CreateService.new(user, @repository.try(:identifier), hook_params).call
#end
# 托管项目创建上链操作
ProjectCreateChainJob.perform_later(user.try(:login), @repository.try(:identifier)) if project.project_type == "common"
if project.project_type == "common"
hook_params = {
active: true,
type: "gitea"
branch_filter: "",
config: {
content_type: "application/json",
url: "#{EduSetting.get("host_name")}/repositories/#{project.id}/repo_hooks.json",
http_method: "post"
},
events: ["create", "pull", "push"],
}
chain_params = {
type: "create",
ownername: user.try(:login),
reponame: @repository.try(:identifier)
}
ProjectCreateChainJob.perform_later(chain_params) #创建上链操作
Gitea::Repository::Hooks::CreateService.new(user, @repository.try(:identifier), hook_params).call #创建gitea的hook功能
end
end
@repository
end