| @@ -105,13 +105,19 @@ class IssuesController < ApplicationController | |||
| elsif params[:subject].to_s.size > 255 | |||
| normal_status(-1, "标题不能超过255个字符") | |||
| 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 = { | |||
| type: "user" | |||
| }.merge(tokens_params(@project)) | |||
| user_tokens = Gitea::Repository::Hooks::QueryService.new(query_params).call | |||
| return normal_status(-1, "您的token值不足") if user_tokens[:value].to_i < params[:token].to_i | |||
| type: "query", | |||
| chain_params: { | |||
| reponame: project.try(:identifer), | |||
| 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 | |||
| issue_params = issue_send_params(params) | |||
| @@ -143,10 +149,14 @@ class IssuesController < ApplicationController | |||
| #为悬赏任务时, 扣除当前用户的积分 | |||
| if params[:issue_type].to_s == "2" | |||
| change_params = { | |||
| change_type: "minusToken", | |||
| tokens: params[:token] | |||
| }.merge(tokens_params(@project)) | |||
| ChangeTokenJob.perform_later(change_params) | |||
| type: "minus", | |||
| chain_params: { | |||
| amount: params[:token], | |||
| reponame: @project.try(:identifer), | |||
| username: current_user.try(:login) | |||
| } | |||
| } | |||
| PostChainJob.perform_later(change_params) | |||
| end | |||
| @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") | |||
| last_token = @issue.token_was | |||
| change_token = last_token - @issue.token | |||
| change_type = change_token > 0 ? "addToken" : "minusToken" | |||
| change_type = change_token > 0 ? "add" : "minus" | |||
| change_params = { | |||
| change_type: change_type, | |||
| tokens: change_token.abs | |||
| }.merge(tokens_params(@proeject)) | |||
| ChangeTokenJob.perform_later(change_params) | |||
| type: change_type, | |||
| chain_params: { | |||
| amount: change_token.abs, | |||
| reponame: @project.try(:identifer), | |||
| username: current_user.try(:login) | |||
| } | |||
| } | |||
| PostChainJob.perform_later(change_params) | |||
| end | |||
| end | |||
| @issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id) | |||
| normal_status(0, "更新成功") | |||
| else | |||
| @@ -252,6 +264,7 @@ class IssuesController < ApplicationController | |||
| def destroy | |||
| if @issue.destroy | |||
| normal_status(0, "删除成功") | |||
| else | |||
| normal_status(-1, "删除失败") | |||
| @@ -272,7 +285,6 @@ class IssuesController < ApplicationController | |||
| end | |||
| def series_update | |||
| update_hash = {} | |||
| 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? | |||
| @@ -323,18 +335,18 @@ class IssuesController < ApplicationController | |||
| if @issue.update_attribute(:status_id, type) | |||
| if type == 5 | |||
| @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) #指派人增加积分 | |||
| type: "add", | |||
| chain_params: { | |||
| amount: tokens | |||
| reponame: @project.try(:identifer), | |||
| username: @issue.get_assign_user.try(:login) | |||
| } | |||
| } | |||
| ChangeTokenJob.perform_later(change_params) | |||
| PostChainJob.perform_later(change_params) | |||
| end | |||
| if @issue.issue_classify.to_s == "pull_request" | |||
| @issue&.pull_request&.update_attribute(:status, 2) | |||
| @@ -467,13 +479,4 @@ 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 | |||
| @@ -106,10 +106,10 @@ class RepositoriesController < ApplicationController | |||
| } | |||
| uploadPushInfo = hook_params(hook_type, params).merge(user_params) | |||
| chain_params = { | |||
| type: hook_type, | |||
| uploadPushInfo: uploadPushInfo | |||
| }.merge(user_params) | |||
| ProjectCreateChainJob.perform_later(chain_params) | |||
| type: "upload", | |||
| chain_params: uploadPushInfo | |||
| } | |||
| PostChainJob.perform_later(chain_params) | |||
| @project.update_attribute(:token, @project.token + uploadPushInfo[:modificationLines].to_i) | |||
| end | |||
| @@ -162,9 +162,9 @@ class RepositoriesController < ApplicationController | |||
| if hook_type == "push" | |||
| # TODO hook返回的记录中,暂时没有文件代码数量的增减,暂时根据 commits数量来计算 | |||
| 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, | |||
| "modificationLines": params["commits"].length | |||
| "modification_lines": params["commits"].length | |||
| } | |||
| elsif hook_type == "pull_request" && params["action"].to_s == "closed" #合并请求合并后才会有上链操作 | |||
| uploadPushInfo = { | |||
| @@ -172,8 +172,8 @@ class RepositoriesController < ApplicationController | |||
| "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数量 | |||
| "sha": [params["pull_request"]["merge_commit_sha"], params["pull_request"]["merge_base"]], | |||
| "modification_lines": 1 #pull_request中没有commits数量 | |||
| } | |||
| else | |||
| uploadPushInfo = {} | |||
| @@ -1,22 +1,22 @@ | |||
| class ChangeTokenJob < ApplicationJob | |||
| queue_as :default | |||
| # 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 = `chain #{change_params[:change_type]} #{change_params[:ownername]} #{change_params[:reponame]} #{change_params[:username]} #{change_params[:tokens].to_i}` | |||
| chain_status = eval(chain_status) | |||
| status = chain_status[:status].to_i | |||
| end | |||
| end | |||
| unless status == 200 | |||
| Rails.logger.info("########_change_user_toke_failed__change_params:__#{change_params}_") | |||
| end | |||
| end | |||
| # 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 = `chain #{change_params[:change_type]} #{change_params[:ownername]} #{change_params[:reponame]} #{change_params[:username]} #{change_params[:tokens].to_i}` | |||
| # chain_status = eval(chain_status) | |||
| # status = chain_status[:status].to_i | |||
| # end | |||
| # end | |||
| # unless status == 200 | |||
| # Rails.logger.info("########_change_user_toke_failed__change_params:__#{change_params}_") | |||
| # end | |||
| # end | |||
| end | |||
| @@ -0,0 +1,25 @@ | |||
| class PostChainJob < ApplicationJob | |||
| queue_as :default | |||
| #注 pull_request的 api文档有问题,字段未明确,所以暂时关于pr的参数应该有问题。6-8,hs | |||
| 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 | |||
| @@ -1,46 +1,48 @@ | |||
| class ProjectCreateChainJob < ApplicationJob | |||
| queue_as :default | |||
| def perform(chain_params) | |||
| status = false | |||
| chain_type = chain_params[:type].to_s | |||
| reponame = chain_params[:reponame] | |||
| 5.times do |i| | |||
| if status | |||
| 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}__") | |||
| 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) | |||
| result = `chain trustieCreate #{chain_params[:ownername]} #{chain_params[:reponame]}` | |||
| return eval(result) | |||
| 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 | |||
| #可以去掉 | |||
| # def perform(chain_params) | |||
| # status = false | |||
| # chain_type = chain_params[:type].to_s | |||
| # reponame = chain_params[:reponame] | |||
| # 5.times do |i| | |||
| # if status | |||
| # 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}__") | |||
| # 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) | |||
| # result = `chain trustieCreate #{chain_params[:ownername]} #{chain_params[:reponame]}` | |||
| # return eval(result) | |||
| # 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 | |||
| @@ -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 | |||
| @@ -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 | |||
| @@ -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 | |||
| @@ -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 | |||
| @@ -15,14 +15,16 @@ class Repositories::CreateService < ApplicationService | |||
| sync_project(@repository, gitea_repository) | |||
| sync_repository(@repository, gitea_repository) | |||
| if project.project_type == "common" | |||
| chain_params = { | |||
| type: "create", | |||
| ownername: user.try(:login), | |||
| reponame: @repository.try(:id) | |||
| chain_params:{ | |||
| 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 | |||
| @repository | |||