修改token值

This commit is contained in:
sylor_huang@126.com 2020-06-08 17:56:32 +08:00
parent 26bc53ad26
commit 83d516da04
10 changed files with 239 additions and 132 deletions

View File

@ -105,13 +105,19 @@ class IssuesController < ApplicationController
elsif params[:subject].to_s.size > 255 elsif params[:subject].to_s.size > 255
normal_status(-1, "标题不能超过255个字符") normal_status(-1, "标题不能超过255个字符")
elsif (params[:issue_type].to_s == "2") elsif (params[:issue_type].to_s == "2")
return normal_status(-1, "悬赏的奖金必须大于0")if params[:token].to_i == 0 return normal_status(-1, "悬赏的奖金必须大于0") if params[:token].to_i == 0
#查看当前用户的积分 #查看当前用户的积分
query_params = { query_params = {
type: "user" type: "query",
}.merge(tokens_params(@project)) chain_params: {
user_tokens = Gitea::Repository::Hooks::QueryService.new(query_params).call reponame: project.try(:identifer),
return normal_status(-1, "您的token值不足") if user_tokens[:value].to_i < params[:token].to_i username: current_user.try(:login)
}
}
response = Gitea::Chain::ChainGetService.new(query_params).call
return normal_status(-1, "获取token失败请稍后重试") if response.status != 200
return normal_status(-1, "您的token值不足") if response.body["balance"].to_i < params[:token].to_i
else else
issue_params = issue_send_params(params) issue_params = issue_send_params(params)
@ -143,10 +149,14 @@ class IssuesController < ApplicationController
#为悬赏任务时, 扣除当前用户的积分 #为悬赏任务时, 扣除当前用户的积分
if params[:issue_type].to_s == "2" if params[:issue_type].to_s == "2"
change_params = { change_params = {
change_type: "minusToken", type: "minus",
tokens: params[:token] chain_params: {
}.merge(tokens_params(@project)) amount: params[:token],
ChangeTokenJob.perform_later(change_params) reponame: @project.try(:identifer),
username: current_user.try(:login)
}
}
PostChainJob.perform_later(change_params)
end end
@issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create") @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
@ -216,16 +226,18 @@ class IssuesController < ApplicationController
if @issue.saved_change_to_attribute("token") if @issue.saved_change_to_attribute("token")
last_token = @issue.token_was last_token = @issue.token_was
change_token = last_token - @issue.token change_token = last_token - @issue.token
change_type = change_token > 0 ? "addToken" : "minusToken" change_type = change_token > 0 ? "add" : "minus"
change_params = { change_params = {
change_type: change_type, type: change_type,
tokens: change_token.abs chain_params: {
}.merge(tokens_params(@proeject)) amount: change_token.abs,
ChangeTokenJob.perform_later(change_params) reponame: @project.try(:identifer),
username: current_user.try(:login)
}
}
PostChainJob.perform_later(change_params)
end end
end end
@issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id) @issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id)
normal_status(0, "更新成功") normal_status(0, "更新成功")
else else
@ -252,6 +264,7 @@ class IssuesController < ApplicationController
def destroy def destroy
if @issue.destroy if @issue.destroy
normal_status(0, "删除成功") normal_status(0, "删除成功")
else else
normal_status(-1, "删除失败") normal_status(-1, "删除失败")
@ -272,7 +285,6 @@ class IssuesController < ApplicationController
end end
def series_update def series_update
update_hash = {} update_hash = {}
update_hash.merge!(assigned_to_id: params[:assigned_to_id]) if params[:assigned_to_id].present? update_hash.merge!(assigned_to_id: params[:assigned_to_id]) if params[:assigned_to_id].present?
update_hash.merge!(fixed_version_id: params[:fixed_version_id]) if params[:fixed_version_id].present? update_hash.merge!(fixed_version_id: params[:fixed_version_id]) if params[:fixed_version_id].present?
@ -323,18 +335,18 @@ class IssuesController < ApplicationController
if @issue.update_attribute(:status_id, type) if @issue.update_attribute(:status_id, type)
if type == 5 if type == 5
@issue&.project_trends&.update_all(action_type: "close") @issue&.project_trends&.update_all(action_type: "close")
@issue.issue_times.update_all(end_time: Time.now) @issue.issue_times.update_all(end_time: Time.now)
if @issue.issue_type.to_s == "2" if @issue.issue_type.to_s == "2"
tokens = @issue.token tokens = @issue.token
change_params = { change_params = {
change_type: "addToken", type: "add",
tokens: tokens, chain_params: {
ownername: project.owner.try(:login), amount: tokens
reponame: project.try(:identifer), reponame: @project.try(:identifer),
username: @issue.get_assign_user.try(:login) #指派人增加积分 username: @issue.get_assign_user.try(:login)
}
} }
ChangeTokenJob.perform_later(change_params) PostChainJob.perform_later(change_params)
end end
if @issue.issue_classify.to_s == "pull_request" if @issue.issue_classify.to_s == "pull_request"
@issue&.pull_request&.update_attribute(:status, 2) @issue&.pull_request&.update_attribute(:status, 2)
@ -467,13 +479,4 @@ class IssuesController < ApplicationController
project_id: @project.id project_id: @project.id
} }
end end
def tokens_params(project)
{
ownername: project.owner.try(:login),
reponame: project.try(:identifer),
username: current_user.try(:login)
}
end
end end

View File

@ -106,10 +106,10 @@ class RepositoriesController < ApplicationController
} }
uploadPushInfo = hook_params(hook_type, params).merge(user_params) uploadPushInfo = hook_params(hook_type, params).merge(user_params)
chain_params = { chain_params = {
type: hook_type, type: "upload",
uploadPushInfo: uploadPushInfo chain_params: uploadPushInfo
}.merge(user_params) }
ProjectCreateChainJob.perform_later(chain_params) PostChainJob.perform_later(chain_params)
@project.update_attribute(:token, @project.token + uploadPushInfo[:modificationLines].to_i) @project.update_attribute(:token, @project.token + uploadPushInfo[:modificationLines].to_i)
end end
@ -162,9 +162,9 @@ class RepositoriesController < ApplicationController
if hook_type == "push" if hook_type == "push"
# TODO hook返回的记录中暂时没有文件代码数量的增减暂时根据 commits数量来计算 # TODO hook返回的记录中暂时没有文件代码数量的增减暂时根据 commits数量来计算
uploadPushInfo = { uploadPushInfo = {
"shas": params["commits"].present? ? params["commits"].map{|c| c["id"]} : "", "sha": params["commits"].present? ? params["commits"].last. : "",
"branch": params["ref"].to_s.split("/").last, "branch": params["ref"].to_s.split("/").last,
"modificationLines": params["commits"].length "modification_lines": params["commits"].length
} }
elsif hook_type == "pull_request" && params["action"].to_s == "closed" #合并请求合并后才会有上链操作 elsif hook_type == "pull_request" && params["action"].to_s == "closed" #合并请求合并后才会有上链操作
uploadPushInfo = { uploadPushInfo = {
@ -172,8 +172,8 @@ class RepositoriesController < ApplicationController
"target_branch": params["base"]["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 "source_project_id": params["head"]["repo_id"].to_i, #现在是为gitea上仓库的id
"target_project_id": params["base"]["repo_id"].to_i, "target_project_id": params["base"]["repo_id"].to_i,
"shas": [params["pull_request"]["merge_commit_sha"], params["pull_request"]["merge_base"]], "sha": [params["pull_request"]["merge_commit_sha"], params["pull_request"]["merge_base"]],
"modificationLines": 1 #pull_request中没有commits数量 "modification_lines": 1 #pull_request中没有commits数量
} }
else else
uploadPushInfo = {} uploadPushInfo = {}

View File

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

View File

@ -0,0 +1,25 @@
class PostChainJob < ApplicationJob
queue_as :default
#注 pull_request的 api文档有问题字段未明确所以暂时关于pr的参数应该有问题。6-8hs
def perform(chain_params)
status = false
chain_type = chain_params[:type].to_s
reponame = chain_params[:chain_params][:reponame]
5.times do |i|
if status
break
else
response = Gitea::Chain::ChainPostService(chain_params)
if response.status == 200
reponse_body = response&.body
messages = reponse_body.present? ? JSON.parse(reponse_body) : "success"
status = true
Rails.logger.info("################_repository__#{reponame}______create_chain_success_try:_#{i+1}_message__:#{messages}__")
else
Rails.logger.info("########_repository__#{reponame}______create_chain_failed__try:_#{i+1}_")
end
end
end
end
end

View File

@ -1,46 +1,48 @@
class ProjectCreateChainJob < ApplicationJob class ProjectCreateChainJob < ApplicationJob
queue_as :default queue_as :default
def perform(chain_params) #可以去掉
status = false
chain_type = chain_params[:type].to_s
reponame = chain_params[:reponame]
5.times do |i| # def perform(chain_params)
if status # status = false
Rails.logger.info("########_repository__#{reponame}______create_chain_success__try:_#{i+1}_") # chain_type = chain_params[:type].to_s
break # reponame = chain_params[:reponame]
else
Rails.logger.info("########_repository__#{reponame}______start_to_create_chain__try:_#{i+1}_")
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
Rails.logger.info("########_response__chain_status__#{chain_status}__")
status = chain_status[:status].to_i # 5.times do |i|
end # if status
end # Rails.logger.info("########_repository__#{reponame}______create_chain_success__try:_#{i+1}_")
# break
# else
# Rails.logger.info("########_repository__#{reponame}______start_to_create_chain__try:_#{i+1}_")
# 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
# Rails.logger.info("########_response__chain_status__#{chain_status}__")
unless status == 200 # status = chain_status[:status].to_i
Rails.logger.info("########_repository__#{reponame}__create_chain:___#{chain_status}____failed_to_create_chain__") # end
end # end
end
private # unless status == 200
# Rails.logger.info("########_repository__#{reponame}__create_chain:___#{chain_status}____failed_to_create_chain__")
# end
# end
#创建项目的上链操作 # private
def create_chain(chain_params)
result = `chain trustieCreate #{chain_params[:ownername]} #{chain_params[:reponame]}`
return eval(result)
end
# push项目的上链操作 # #创建项目的上链操作
def push_chain(chain_params) # def create_chain(chain_params)
result = `chain trustiePush #{chain_params[:ownername]} #{chain_params[:reponame]} #{chain_params[:username]} #{chain_params[:uploadPushInfo]}` # result = `chain trustieCreate #{chain_params[:ownername]} #{chain_params[:reponame]}`
return eval(result) # return eval(result)
end # end
# # push项目的上链操作
# def push_chain(chain_params)
# result = `chain trustiePush #{chain_params[:ownername]} #{chain_params[:reponame]} #{chain_params[:username]} #{chain_params[:uploadPushInfo]}`
# return eval(result)
# end
end end

View File

@ -0,0 +1,29 @@
class Gitea::Chain::ChainGetService < Gitea::ChainService
attr_reader :params
def initialize(params)
@params = params
end
def call
get(url, request_params)
end
private
def request_params
Hash.new.merge(data: params["chain_params"])
end
def url
chain_type = params["type"].to_s
case chain_type
when "query"
"/repos/amount/query".freeze
else
"".freeze
end
end
end

View File

@ -0,0 +1,31 @@
class Gitea::Chain::ChainPostService < Gitea::ChainService
attr_reader :params
def initialize(params)
@params = params
end
def call
post(url, request_params)
end
private
def request_params
Hash.new.merge(data: params["chain_params"])
end
def url
chain_type = params["type"].to_s
case chain_type
when "create"
"/repos/create".freeze
when "upload"
"/repos/commit/upload".freeze
else #由于目前的api文档操作post请求除了create/upload都是在/repos/amount/*,所以以下简化了
"/repos/amount/#{chain_type}".freeze
end
end
end

View File

@ -0,0 +1,47 @@
class Gitea::ChainService < ApplicationService
attr_reader :url, :params
def initialize(options={})
@url = options[:url]
@params = options[:params]
end
def post(url, params={})
Rails.logger.info("######_____api____request_url_______###############{request_url}")
Rails.logger.info("######_____api____request_params_______###############{params}")
conn.post do |req|
req.url "#{request_url}"
req.body = params[:data].to_json
end
end
def get(url, params={})
conn.get do |req|
req.url = "#{request_url}"
req.body = params[:data].to_json
end
end
private
def conn(auth={})
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.response :logger # 显示日志
req.adapter Faraday.default_adapter
end
end
@client
end
def domain
Rails.application.config_for(:configuration)['chain_base']
end
def request_url
[domain, url].join('').freeze
end
end

View File

@ -1,32 +0,0 @@
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 = `chain query #{query_params[:ownername]} #{query_params[:reponame]} #{query_params[:username]}`
#response {status:int, message:string, value:int}
elsif query_type == "members" #查询项目全部用户的积分
query_result = `chain getAllInfo #{query_params[:ownername]} #{query_params[:reponame]}`
#response {status:int, message:string, value:jsonObject}
else #查询用户在项目的贡献大小
query_result = `chain getContributionPercent #{query_params[:ownername]} #{query_params[:reponame]} #{query_params[:username]}`
#response {status:int, message:string, percent:int, allTokenSum:int, personalTokens:int}
end
eval(query_result)
end
end

View File

@ -15,14 +15,16 @@ class Repositories::CreateService < ApplicationService
sync_project(@repository, gitea_repository) sync_project(@repository, gitea_repository)
sync_repository(@repository, gitea_repository) sync_repository(@repository, gitea_repository)
if project.project_type == "common" if project.project_type == "common"
chain_params = { chain_params = {
type: "create", type: "create",
ownername: user.try(:login), chain_params:{
reponame: @repository.try(:id) username: user.try(:login),
reponame: @repository.try(:identifier),
token_name: @repository.try(:identifier),
total_supply: 0
}
} }
# ProjectCreateChainJob.perform_later(chain_params) #创建上链操作 PostChainJob.perform_later(chain_params) #创建上链操作
end end
end end
@repository @repository