From 8aa844749fa969ae1d2f60a739fde28b5debe32d Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Fri, 3 Feb 2023 09:11:45 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=AF=B9token=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=9D=9E=E7=A9=BA=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/forms/projects/create_form.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/forms/projects/create_form.rb b/app/forms/projects/create_form.rb index ff9008b23..c9fcb44e6 100644 --- a/app/forms/projects/create_form.rb +++ b/app/forms/projects/create_form.rb @@ -16,8 +16,8 @@ class Projects::CreateForm < BaseForm check_project_language(project_language_id) check_project_name(user_id, name) unless name.blank? check_repository_name(user_id, repository_name) unless repository_name.blank? - check_blockchain_token_all(blockchain_token_all) - check_blockchain_init_token(blockchain_init_token) + check_blockchain_token_all(blockchain_token_all) unless blockchain_token_all.blank? + check_blockchain_init_token(blockchain_init_token) unless blockchain_init_token.blank? end def check_license From 7aca2e487ed17bc2dfc10a20957a960c0bf576f7 Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Fri, 3 Feb 2023 09:23:31 +0800 Subject: [PATCH 2/6] change find by name to find by identifier --- app/models/concerns/watchable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/watchable.rb b/app/models/concerns/watchable.rb index db00088db..71c194ed5 100644 --- a/app/models/concerns/watchable.rb +++ b/app/models/concerns/watchable.rb @@ -32,9 +32,9 @@ module Watchable end def contribution_perc - project_name = params[:project_name] + project_identifier = params[:project_identifier] user_login = params[:id] - @project = Project.find_by_name(project_name) + @project = Project.find_by_identifier(project_identifier) @user = User.find_by_login(user_login) def cal_perc(count_user, count_all) From 7b1b03ab41f3e59695e4dad34b12b3f85207ab6d Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Fri, 3 Feb 2023 13:19:27 +0800 Subject: [PATCH 3/6] change push_activity_2_blockchain by querying projects with identifier --- app/controllers/application_controller.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0efbd8900..d9c08aada 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1378,7 +1378,8 @@ class ApplicationController < ActionController::Base end pull_request_id = model['id'] - reponame = project['name'] + identifier = project['identifier'] + reponame = project['reponame'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1402,7 +1403,10 @@ class ApplicationController < ActionController::Base updated_at = model['updated_at'] # 查询pull request对应的commit信息 - commits = Gitea::PullRequest::CommitsService.call(ownername, reponame, model['gitea_number']) + commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number']) + if commits.nil? + return false # 获取pr中变更的commit信息失败 + end commit_shas = [] commits.each do |c| commit_shas << c["Sha"] @@ -1439,7 +1443,8 @@ class ApplicationController < ActionController::Base end pull_request_id = model['id'] - reponame = project['name'] + identifier = project['identifier'] + reponame = project['reponame'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1450,7 +1455,10 @@ class ApplicationController < ActionController::Base updated_at = model['updated_at'] # 查询pull request对应的commit信息 - commits = Gitea::PullRequest::CommitsService.call(ownername, reponame, model['gitea_number']) + commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number']) + if commits.nil? + return false # 获取pr中变更的commit信息失败 + end commit_shas = [] commits.each do |c| commit_shas << c["Sha"] From 11023cbc709ad6343e43a549481c9b7ed7350d2e Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Fri, 3 Feb 2023 14:07:39 +0800 Subject: [PATCH 4/6] change reponame to identifier --- app/controllers/application_controller.rb | 324 +--------------------- 1 file changed, 12 insertions(+), 312 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d9c08aada..95d1e6f44 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -958,305 +958,7 @@ class ApplicationController < ActionController::Base # author: zxh # blockchain相关项目活动调用函数 # return true: 表示上链操作成功; return false: 表示上链操作失败 - # 北大数瑞的被注释掉 - #def push_activity_2_blockchain(activity_type, model) - # if activity_type == "issue_create" - # - # project_id = model['project_id'] - # project = Project.find(project_id) - # if project['use_blockchain'] == 0 || project['use_blockchain'] == false - # # 无需执行上链操作 - # return true - # end - # - # id = model['id'] - # - # owner_id = project['user_id'] - # owner = User.find(owner_id) - # ownername = owner['login'] - # reponame = project['name'] - # - # author_id = project['user_id'] - # author = User.find(author_id) - # username = author['login'] - # - # action = 'opened' - # - # title = model['subject'] - # content = model['description'] - # created_at = model['created_on'] - # updated_at = model['updated_on'] - # - # # 调用区块链接口 - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB1", - # "operation": "putIssue", - # "arg": { - # "issue_id": id, - # "repo_id": project_id, - # "reponame": reponame, - # "ownername": ownername, - # "username": username, - # "action": action, - # "title": title, - # "content": content, - # "created_at": created_at, - # "updated_at": updated_at - # }.to_json - # }.to_json - # success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # return success_blockchain - # - # elsif activity_type == "issue_comment_create" - # issue_comment_id = model['id'] - # issue_id = model['journalized_id'] - # parent_id = model['parent_id'].nil? ? "" : model['parent_id'] - # - # issue = Issue.find(issue_id) - # issue_classify = issue['issue_classify'] # issue或pull_request - # project_id = issue['project_id'] - # project = Project.find(project_id) - # - # if project['use_blockchain'] == 0 || project['use_blockchain'] == false - # # 无需执行上链操作 - # return true - # end - # - # reponame = project['name'] - # owner_id = project['user_id'] - # owner = User.find(owner_id) - # ownername = owner['login'] - # - # author_id = model['user_id'] - # author = User.find(author_id) - # username = author['login'] - # - # action = 'created' - # - # content = model['notes'] - # created_at = model['created_on'] - # - # if issue_classify == "issue" - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB1", - # "operation": "putIssueComment", - # "arg": { - # "issue_comment_id": issue_comment_id, - # "issue_id": issue_id, - # "parent_id": parent_id, - # "reponame": reponame, - # "ownername": ownername, - # "username": username, - # "action": action, - # "content": content, - # "created_at": created_at, - # }.to_json - # }.to_json - # elsif issue_classify == "pull_request" - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB0", - # "operation": "putPullRequestComment", - # "arg": { - # "pull_request_comment_id": issue_comment_id, - # "pull_request_id": issue_id, - # "parent_id": parent_id, - # "reponame": reponame, - # "ownername": ownername, - # "username": username, - # "action": action, - # "content": content, - # "created_at": created_at, - # }.to_json - # }.to_json - # end - # - # # 调用区块链接口 - # success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # return success_blockchain - # elsif activity_type == "pull_request_create" - # # 调用区块链接口 - # project_id = model['project_id'] - # project = Project.find(project_id) - # if project['use_blockchain'] == 0 || project['use_blockchain'] == false - # # 无需执行上链操作 - # return true - # end - # - # pull_request_id = model['id'] - # reponame = project['name'] - # owner_id = project['user_id'] - # owner = User.find(owner_id) - # ownername = owner['login'] - # - # action = 'opened' - # - # title = model['title'] - # content = model['body'] - # - # source_branch = model['head'] - # source_repo_id = model['fork_project_id'].nil? ? project_id : model['fork_project_id'] - # - # target_branch = model['base'] - # target_repo_id = project_id - # - # author_id = model['user_id'] - # author = User.find(author_id) - # username = author['login'] - # - # created_at = model['created_at'] - # updated_at = model['updated_at'] - # - # # 查询pull request对应的commit信息 - # commits = Gitea::PullRequest::CommitsService.call(ownername, reponame, model['gitea_number']) - # commit_shas = [] - # commits.each do |c| - # commit_shas << c["Sha"] - # end - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB0", - # "operation": "putPullRequest", - # "arg": { - # "pull_request_id": pull_request_id, - # "repo_id": project_id, - # "ownername": ownername, - # "reponame": reponame, - # "username": username, - # "action": action, - # "title": title, - # "content": content, - # "source_branch": source_branch, - # "target_branch": target_branch, - # "commit_shas": commit_shas, - # "created_at": created_at, - # "updated_at": updated_at - # }.to_json - # }.to_json - # success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # return success_blockchain - # elsif activity_type == "pull_request_merge" - # - # # 调用区块链接口 - # project_id = model['project_id'] - # project = Project.find(project_id) - # if project['use_blockchain'] == 0 || project['use_blockchain'] == false - # # 无需执行上链操作 - # return true - # end - # - # pull_request_id = model['id'] - # reponame = project['name'] - # owner_id = project['user_id'] - # owner = User.find(owner_id) - # ownername = owner['login'] - # - # action = 'merged' - # - # # 查询pull request对应的commit信息 - # commits = Gitea::PullRequest::CommitsService.call(ownername, reponame, model['gitea_number']) - # commit_shas = [] - # commits.each do |c| - # commit_shas << c["Sha"] - # end - # - # # 将pull request相关信息写入链上 - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB0", - # "operation": "putPullRequest", - # "arg": { - # "pull_request_id": pull_request_id, - # "repo_id": project_id, - # "ownername": ownername, - # "reponame": reponame, - # "username": username, - # "action": action, - # "title": title, - # "content": content, - # "source_branch": source_branch, - # "target_branch": target_branch, - # "commit_shas": commit_shas, - # "created_at": created_at, - # "updated_at": updated_at - # }.to_json - # }.to_json - # success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # - # - # # 将commit相关信息写入链上 - # commit_shas.each do |commit_sha| - # commit_diff = Gitea::Commit::DiffService.call(ownername, reponame, commit_sha) - # commit = Gitea::Commit::InfoService.call(ownername, reponame, commit_sha) - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB1", - # "operation": "putCommit", - # "arg": { - # "commit_hash": commit_sha, - # "repo_id": project_id, - # "author": commit['author']['login'], - # "author_email": commit['author']['email'], - # "committer": commit['committer']['login'], - # "committer_email": commit['committer']['email'], - # "author_time": commit['commit']['author']['date'], - # "committer_time": commit['commit']['committer']['date'], - # "message": commit['commit']['message'], - # "diff": commit_diff['Files'] - # }.to_json - # }.to_json - # success_blockchain_commit = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # success_blockchain = success_blockchain & success_blockchain_commit - # end - # return success_blockchain - # - # elsif activity_type == "pull_request_refuse" - # - # # 调用区块链接口 - # project_id = model['project_id'] - # project = Project.find(project_id) - # if project['use_blockchain'] == 0 || project['use_blockchain'] == false - # # 无需执行上链操作 - # return true - # end - # - # pull_request_id = model['id'] - # reponame = project['name'] - # owner_id = project['user_id'] - # owner = User.find(owner_id) - # ownername = owner['login'] - # - # action = 'refused' - # - # # 将pull request相关信息写入链上 - # param = { - # "action": "executeContract", - # "contractID": "RepositoryDB0", - # "operation": "putPullRequest", - # "arg": { - # "pull_request_id": pull_request_id, - # "repo_id": project_id, - # "ownername": ownername, - # "reponame": reponame, - # "username": username, - # "action": action, - # "title": title, - # "content": content, - # "source_branch": source_branch, - # "target_branch": target_branch, - # "created_at": created_at, - # "updated_at": updated_at - # }.to_json - # }.to_json - # success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - # return success_blockchain - # end - #end - - - def push_activity_2_blockchain(activity_type, model) + def push_activity_2_blockchain(activity_type, model) if activity_type == "issue_create" project_id = model['project_id'] @@ -1271,7 +973,7 @@ class ApplicationController < ActionController::Base owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] - reponame = project['name'] + identifier = project['identifier'] author_id = project['user_id'] author = User.find(author_id) @@ -1290,7 +992,7 @@ class ApplicationController < ActionController::Base "issue_id": "gitlink-" + id.to_s, "repo_id": "gitlink-" + project_id.to_s, "issue_number": 0, # 暂时不需要改字段 - "reponame": reponame, + "reponame": identifier, "ownername": ownername, "username": username, "action": action, @@ -1317,7 +1019,7 @@ class ApplicationController < ActionController::Base return true end - reponame = project['name'] + identifier = project['identifier'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1340,7 +1042,7 @@ class ApplicationController < ActionController::Base "issue_id": "gitlink-" + issue_id.to_s, "repo_id": "gitlink-" + project.id.to_s, "parent_id": parent_id.to_s, - "reponame": reponame, + "reponame": identifier, "ownername": ownername, "username": username, "action": action, @@ -1356,7 +1058,7 @@ class ApplicationController < ActionController::Base "pull_request_id": "gitlink-" + issue_id.to_s, "parent_id": parent_id.to_s, "repo_id": "gitlink-" + project.id.to_s, - "reponame": reponame, + "reponame": identifier, "ownername": ownername, "username": username, "action": action, @@ -1379,7 +1081,6 @@ class ApplicationController < ActionController::Base pull_request_id = model['id'] identifier = project['identifier'] - reponame = project['reponame'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1417,7 +1118,7 @@ class ApplicationController < ActionController::Base "pull_request_number": 0, # trustie没有该字段 "repo_id": "gitlink-" + project_id.to_s, "ownername": ownername, - "reponame": reponame, + "reponame": identifier, "username": username, "action": action, "title": title, @@ -1444,7 +1145,6 @@ class ApplicationController < ActionController::Base pull_request_id = model['id'] identifier = project['identifier'] - reponame = project['reponame'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1471,7 +1171,7 @@ class ApplicationController < ActionController::Base "pull_request_number": 0, # trustie没有该字段 "repo_id": "gitlink-" + project_id.to_s, "ownername": ownername, - "reponame": reponame, + "reponame": identifier, "username": username, "action": action, "title": title, @@ -1489,8 +1189,8 @@ class ApplicationController < ActionController::Base # 将commit相关信息写入链上 commit_shas.each do |commit_sha| - commit_diff = Gitea::Commit::DiffService.call(ownername, reponame, commit_sha, owner['gitea_token']) - commit = Gitea::Commit::InfoService.call(ownername, reponame, commit_sha, owner['gitea_token']) + commit_diff = Gitea::Commit::DiffService.call(ownername, identifier, commit_sha, owner['gitea_token']) + commit = Gitea::Commit::InfoService.call(ownername, identifier, commit_sha, owner['gitea_token']) param = { "request-type": "upload commit info", "commit_hash": commit_sha, @@ -1520,7 +1220,7 @@ class ApplicationController < ActionController::Base end pull_request_id = model['id'] - reponame = project['name'] + identifier = project['identifier'] owner_id = project['user_id'] owner = User.find(owner_id) ownername = owner['login'] @@ -1534,7 +1234,7 @@ class ApplicationController < ActionController::Base "pull_request_number": 0, # trustie没有该字段 "repo_id": "gitlink-" + project_id.to_s, "ownername": ownername, - "reponame": reponame, + "reponame": identifier, "username": username, "action": action, "title": title, From b1ef05e0bef04995a6e2e1dac88584e5d23861c5 Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Fri, 3 Feb 2023 14:08:22 +0800 Subject: [PATCH 5/6] change reponame to identifier --- app/controllers/users_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7f33dbf25..c17249461 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -646,9 +646,9 @@ class UsersController < ApplicationController def blockchain_get_unclosed_issue_list ownername = params["ownername"] - reponame = params["reponame"] + identifier = params["reponame"] owner = User.find_by(login: ownername) - project = Project.find_by(user_id: owner.id, name: reponame) + project = Project.find_by(user_id: owner.id, identifier: identifier) unclosed_issues = Issue.where(project_id: project.id, issue_classify: "issue").where.not(status_id: 5) results = [] unclosed_issues.each do |i| From 06a679424560f5d35e8c903639b3c3c9493dd962 Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Sat, 4 Feb 2023 11:06:04 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86blockchain=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 133 +++++++----------- app/controllers/issues_controller.rb | 20 +-- app/controllers/journals_controller.rb | 7 +- app/controllers/pull_requests_controller.rb | 50 +++---- app/controllers/users_controller.rb | 26 ++-- app/helpers/tag_chosen_helper.rb | 3 +- app/models/concerns/watchable.rb | 2 +- app/queries/application_query.rb | 68 ++++----- .../blockchain/balance_query_one_project.rb | 8 +- app/queries/blockchain/repo_basic_info.rb | 6 +- app/services/application_service.rb | 73 ++++------ app/services/blockchain/create_issue.rb | 3 +- app/services/blockchain/fix_issue.rb | 3 +- .../blockchain/invoke_blockchain_api.rb | 34 +++++ app/services/blockchain/transfer_service.rb | 15 +- 15 files changed, 188 insertions(+), 263 deletions(-) create mode 100644 app/services/blockchain/invoke_blockchain_api.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 95d1e6f44..d754a4552 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -895,66 +895,6 @@ class ApplicationController < ActionController::Base HotSearchKeyword.add(keyword) end - - # author: zxh - # blockchain存证api - #def invoke_blockchain_api(uri, params) - # begin - # uri = URI.parse(URI.encode(uri.strip)) - # res = Net::HTTP.start(uri.host, uri.port) do |http| - # req = Net::HTTP::Post.new(uri) - # req['Content-Type'] = 'application/json' - # req.body = params - # http.request(req) - # end - # if res.code.to_i != 200 - # puts '区块链接口请求失败.' - # return false - # else - # res_body = JSON.parse(res.body) - # if res_body.has_key?("data") && JSON.parse(res_body["data"]).has_key?("status") && JSON.parse(res_body["data"])['status'] == "Success" - # else - # puts '区块链接口请求出错.' - # return false - # end - # end - # - # return true - # rescue Exception => e - # puts '区块链接口请求失败.' - # return false - # end - #end - - def invoke_blockchain_api(uri, params) - begin - uri = URI.parse(URI.encode(uri.strip)) - res = Net::HTTP.start(uri.host, uri.port) do |http| - req = Net::HTTP::Post.new(uri) - req['Content-Type'] = 'application/json' - req.body = params - http.request(req) - end - if res.code.to_i != 200 - puts '区块链接口请求失败.' - return false - else - res_body = JSON.parse(res.body) - if res_body.has_key?("status") && res_body["status"] == 0 - else - puts '区块链接口请求出错.' - return false - end - end - - return true - rescue Exception => e - puts '区块链接口请求失败.' - return false - end - end - - # author: zxh # blockchain相关项目活动调用函数 # return true: 表示上链操作成功; return false: 表示上链操作失败 @@ -987,7 +927,7 @@ class ApplicationController < ActionController::Base updated_at = model['updated_on'] # 调用区块链接口 - param = { + params = { "request-type": "upload issue info", "issue_id": "gitlink-" + id.to_s, "repo_id": "gitlink-" + project_id.to_s, @@ -1001,8 +941,12 @@ class ApplicationController < ActionController::Base "created_at": created_at, "updated_at": updated_at }.to_json - success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return success_blockchain + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 10 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end elsif activity_type == "issue_comment_create" issue_comment_id = model['id'] @@ -1016,7 +960,7 @@ class ApplicationController < ActionController::Base if project['use_blockchain'] == 0 || project['use_blockchain'] == false # 无需执行上链操作 - return true + return end identifier = project['identifier'] @@ -1034,7 +978,7 @@ class ApplicationController < ActionController::Base created_at = model['created_on'] if issue_classify == "issue" - param = { + params = { "request-type": "upload issue comment info", "issue_comment_id": "gitlink-" + issue_comment_id.to_s, "issue_comment_number": 0, # 暂时不需要 @@ -1050,7 +994,7 @@ class ApplicationController < ActionController::Base "created_at": created_at, }.to_json elsif issue_classify == "pull_request" - param = { + params = { "request-type": "upload pull request comment info", "pull_request_comment_id": "gitlink-" + issue_comment_id.to_s, "pull_request_comment_number": 0, # 不考虑该字段 @@ -1068,15 +1012,19 @@ class ApplicationController < ActionController::Base end # 调用区块链接口 - success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return success_blockchain + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 10 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end elsif activity_type == "pull_request_create" # 调用区块链接口 project_id = model['project_id'] project = Project.find(project_id) if project['use_blockchain'] == 0 || project['use_blockchain'] == false # 无需执行上链操作 - return true + return end pull_request_id = model['id'] @@ -1106,13 +1054,13 @@ class ApplicationController < ActionController::Base # 查询pull request对应的commit信息 commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number']) if commits.nil? - return false # 获取pr中变更的commit信息失败 + raise Error, "区块链接口请求失败" # 获取pr中变更的commit信息失败 end commit_shas = [] commits.each do |c| commit_shas << c["Sha"] end - param = { + params = { "request-type": "upload pull request info", "pull_request_id": "gitlink-" + pull_request_id.to_s, "pull_request_number": 0, # trustie没有该字段 @@ -1131,16 +1079,19 @@ class ApplicationController < ActionController::Base "created_at": created_at, "updated_at": updated_at }.to_json - success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return success_blockchain + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 9 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end elsif activity_type == "pull_request_merge" - # 调用区块链接口 project_id = model['project_id'] project = Project.find(project_id) if project['use_blockchain'] == 0 || project['use_blockchain'] == false # 无需执行上链操作 - return true + return end pull_request_id = model['id'] @@ -1157,7 +1108,7 @@ class ApplicationController < ActionController::Base # 查询pull request对应的commit信息 commits = Gitea::PullRequest::CommitsService.call(ownername, identifier, model['gitea_number']) if commits.nil? - return false # 获取pr中变更的commit信息失败 + raise Error, "区块链接口请求失败" # 获取pr中变更的commit信息失败 end commit_shas = [] commits.each do |c| @@ -1165,7 +1116,7 @@ class ApplicationController < ActionController::Base end # 将pull request相关信息写入链上 - param = { + params = { "request-type": "upload pull request info", "pull_request_id": "gitlink-" + pull_request_id.to_s, "pull_request_number": 0, # trustie没有该字段 @@ -1184,14 +1135,19 @@ class ApplicationController < ActionController::Base "created_at": created_at, "updated_at": updated_at }.to_json - success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 9 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end # 将commit相关信息写入链上 commit_shas.each do |commit_sha| commit_diff = Gitea::Commit::DiffService.call(ownername, identifier, commit_sha, owner['gitea_token']) commit = Gitea::Commit::InfoService.call(ownername, identifier, commit_sha, owner['gitea_token']) - param = { + params = { "request-type": "upload commit info", "commit_hash": commit_sha, "repo_id": "gitlink-" + project_id.to_s, @@ -1204,10 +1160,13 @@ class ApplicationController < ActionController::Base "content": commit['commit']['message'], "commit_diff": commit_diff['Files'].to_s }.to_json - success_blockchain_commit = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - success_blockchain = success_blockchain & success_blockchain_commit + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 7 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end end - return success_blockchain elsif activity_type == "pull_request_refuse" @@ -1228,7 +1187,7 @@ class ApplicationController < ActionController::Base action = 'refused' # 将pull request相关信息写入链上 - param = { + params = { "request-type": "upload pull request info", "pull_request_id": "gitlink-" + pull_request_id.to_s, "pull_request_number": 0, # trustie没有该字段 @@ -1247,8 +1206,12 @@ class ApplicationController < ActionController::Base "created_at": created_at, "updated_at": updated_at }.to_json - success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return success_blockchain + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 9 + raise Error, resp_body['message'] + elsif resp_body['status'] != 0 + raise Error, "区块链接口请求失败." + end end end def find_atme_receivers diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 68567be19..41af03c26 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -160,22 +160,14 @@ class IssuesController < ApplicationController # author: zxh # 扣除发起人的token - blockchain_result = Blockchain::CreateIssue.call(user_id: @issue.author_id, project_id: @issue.project_id, token_num: @issue.blockchain_token_num) - if blockchain_result = false - normal_status(-1, "创建失败") - raise ActiveRecord::Rollback - else - end - #render json: {status: 0, message: "创建成功", id: @issue.id} - # 调用上链API存证 - success_blockchain = push_activity_2_blockchain("issue_create", @issue) - if success_blockchain == false - normal_status(-1, "创建失败") - raise ActiveRecord::Rollback - else - render json: {status: 0, message: "创建成功", id: @issue.id} + if @issue.blockchain_token_num > 0 + Blockchain::CreateIssue.call(user_id: @issue.author_id, project_id: @issue.project_id, token_num: @issue.blockchain_token_num) end + # 调用上链API存证 + push_activity_2_blockchain("issue_create", @issue) + + render json: {status: 0, message: "创建成功", id: @issue.id} else normal_status(-1, "创建失败") end diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index 029214f4c..ccf61e2cc 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -51,14 +51,9 @@ class JournalsController < ApplicationController # author: zxh # 调用上链API - success_blockchain = push_activity_2_blockchain("issue_comment_create", journal) - if success_blockchain == false - normal_status(-1, "评论失败") - raise ActiveRecord::Rollback - end + push_activity_2_blockchain("issue_comment_create", journal) render :json => { status: 0, message: "评论成功", id: journal.id} - # normal_status(0, "评论成功") else normal_status(-1, "评论失败") raise ActiveRecord::Rollback diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index a30fd4930..778038aec 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -78,11 +78,8 @@ class PullRequestsController < ApplicationController # author: zxh # 调用上链API - success_blockchain = push_activity_2_blockchain("pull_request_create", @pull_request) - if success_blockchain == false - render_error("create pull request error: cannot save to blockchain") - raise ActiveRecord::Rollback - end + push_activity_2_blockchain("pull_request_create", @pull_request) + else render_error("create pull request error: #{@gitea_pull_request[:status]}") raise ActiveRecord::Rollback @@ -166,20 +163,16 @@ class PullRequestsController < ApplicationController colsed = PullRequests::CloseService.call(@owner, @repository, @pull_request, current_user) # author: zxh # 调用上链API - success_blockchain = push_activity_2_blockchain("pull_request_refuse", @pull_request) - if success_blockchain == false - normal_status(-1, "拒绝失败") - raise ActiveRecord::Rollback + push_activity_2_blockchain("pull_request_refuse", @pull_request) + + if colsed === true + @pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::CLOSE) + # 合并请求下issue处理为关闭 + @issue&.update_attributes!({status_id:5}) + SendTemplateMessageJob.perform_later('PullRequestClosed', current_user.id, @pull_request.id) if Site.has_notice_menu? + normal_status(1, "已拒绝") else - if colsed === true - @pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::CLOSE) - # 合并请求下issue处理为关闭 - @issue&.update_attributes!({status_id:5}) - SendTemplateMessageJob.perform_later('PullRequestClosed', current_user.id, @pull_request.id) if Site.has_notice_menu? - normal_status(1, "已拒绝") - else - normal_status(-1, '合并失败') - end + normal_status(-1, '合并失败') end rescue => e normal_status(-1, e.message) @@ -224,13 +217,7 @@ class PullRequestsController < ApplicationController # author: zxh # 调用上链API - success_blockchain = push_activity_2_blockchain("pull_request_merge", @pull_request) - if success_blockchain == false - normal_status(-1, "合并失败") - raise ActiveRecord::Rollback - else - end - + push_activity_2_blockchain("pull_request_merge", @pull_request) # 查看是否fix了相关issue,如果fix就转账 if params["fix_issue_id"].nil? || params["fix_issue_id"] == "" @@ -248,20 +235,17 @@ class PullRequestsController < ApplicationController normal_status(-1, "关联issue失败") raise ActiveRecord::Rollback else - project = Project.find_by(user_id: owner.id, name: params["project_id"]) + project = Project.find_by(user_id: owner.id, identifier: params["project_id"]) if project.nil? normal_status(-1, "关联issue失败") raise ActiveRecord::Rollback else author_id = pr.user_id - result = Blockchain::FixIssue.call({user_id: author_id.to_s, project_id: project.id.to_s, token_num: token_num}) - if result == false - normal_status(-1, "关联issue失败") - raise ActiveRecord::Rollback - else - # update issue to state 5 - issue.update(status_id: 5) + if token_num > 0 + Blockchain::FixIssue.call({user_id: author_id.to_s, project_id: project.id.to_s, token_num: token_num}) end + # update issue to state 5 + issue.update(status_id: 5) end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c17249461..78f7fa343 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -92,7 +92,7 @@ class UsersController < ApplicationController end if @project['use_blockchain'] == true or @project['use_blockchain'] == 1 - balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id})[:balance] + balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": user_id, "project_id": project_id}) balance_all = Blockchain::RepoBasicInfo.call({"project_id": project_id})["cur_supply"] scores = { "final" => cal_perc(balance_user, balance_all), @@ -440,12 +440,10 @@ class UsersController < ApplicationController normal_status(-1, "创建者无法找到") else p = Project.find_by(user_id: owner.id, identifier: params['project_identifier']) - results = Blockchain::BalanceQueryOneProject.call({"user_id": params['user_id'].to_i, "project_id": p.id.to_i}) - render json: { status: results[:status], balance: results[:balance]} + balance = Blockchain::BalanceQueryOneProject.call({"user_id": params['user_id'].to_i, "project_id": p.id.to_i}) + render json: { status: 0, balance: balance} end else - puts "~!@~!@" + params['user_id'].to_s - puts current_user.id.to_s normal_status(-1, "缺少权限") end @@ -455,16 +453,15 @@ class UsersController < ApplicationController def blockchain_transfer #is_current_admin_user = User.current.logged? && (current_user&.admin? || current_user.id == params['payer_id'].to_i) is_current_admin_user = true + if is_current_admin_user - results = Blockchain::TransferService.call(params) - if results == true - render json: { status: 2 } # 重新查询余额 - else - normal_status(-1, "转账失败") - end + Blockchain::TransferService.call(params) + render json: {status: 2} # 重新查询余额 else normal_status(-1, "缺少权限") end + rescue Exception => e + normal_status(-1, e.to_s) end # exchange money @@ -640,7 +637,12 @@ class UsersController < ApplicationController def blockchain_get_issue_token_num issue_id = params["issue_id"]['orderId'].to_i issue = Issue.find_by(id: issue_id) - render json: {"blockchain_token_num": issue.blockchain_token_num} + project = Project.find(issue.project_id) + if project[:use_blockchain] + render json: {"blockchain_token_num": issue.blockchain_token_num} + else + render json: {"blockchain_token_num": -1} # 未使用blockchain + end end diff --git a/app/helpers/tag_chosen_helper.rb b/app/helpers/tag_chosen_helper.rb index 520b868fa..784efcf29 100644 --- a/app/helpers/tag_chosen_helper.rb +++ b/app/helpers/tag_chosen_helper.rb @@ -21,7 +21,8 @@ module TagChosenHelper "issue_tag": render_issue_tags(project), "issue_type": render_issue_species, "all_issues": all_issues, - "branches": render_branches(project) + "branches": render_branches(project), + "use_blockchain": project['use_blockchain'] } end diff --git a/app/models/concerns/watchable.rb b/app/models/concerns/watchable.rb index 71c194ed5..9eb0172d7 100644 --- a/app/models/concerns/watchable.rb +++ b/app/models/concerns/watchable.rb @@ -42,7 +42,7 @@ module Watchable end if @project['use_blockchain'] == true or @project['use_blockchain'] == 1 - balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": @user.id, "project_id": @project.id})[:balance] + balance_user = Blockchain::BalanceQueryOneProject.call({"user_id": @user.id, "project_id": @project.id}) balance_all = Blockchain::RepoBasicInfo.call({"project_id": @project.id})["cur_supply"] score = cal_perc(balance_user, balance_all) else diff --git a/app/queries/application_query.rb b/app/queries/application_query.rb index 5fd13055d..fdef1b71d 100644 --- a/app/queries/application_query.rb +++ b/app/queries/application_query.rb @@ -7,70 +7,52 @@ class ApplicationQuery params[key].to_s.strip.presence end - - # author: zxh - # add blockchain related functions in application_query - - def invoke_blockchain_api(uri, params) - begin - uri = URI.parse(URI.encode(uri.strip)) - res = Net::HTTP.start(uri.host, uri.port) do |http| - req = Net::HTTP::Post.new(uri) - req['Content-Type'] = 'application/json' - req.body = params - http.request(req) - end - if res.code.to_i != 200 - puts '区块链接口请求失败.' - return false - else - res_body = JSON.parse(res.body) - if res_body.has_key?("status") && res_body["status"] == 0 - else - puts '区块链接口请求出错.' - return false - end - end - - return res_body - rescue Exception => e - puts '区块链接口请求失败.' - return false - end - end - - # find all the repos that a user has tokens def find_repo_with_token(user_id) - param = { + params = { "request-type": "query user balance of all repos", "username": user_id.to_s }.to_json - resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList'] - return token_list + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] != 0 + raise "区块链接口请求失败." + else + token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList'] + return token_list + end end # find one repo that a user has tokens def find_one_balance(user_id, project_id) - param = { + # return 3 statuses: UnknownErr/ResUserNotExisted/Success + params = { "request-type": "query user balance of single repo", "username": user_id.to_s, "token_name": project_id.to_s }.to_json - resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return resp_body + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 0 + return resp_body['balance'] + elsif resp_body['status'] == 100 + return 0 # 找不到用户返回0 + else + raise "区块链接口请求失败." + end end # query the basic info of a repository def find_blockchain_repo_info(project_id) - param = { + params = { "request-type": "query repo basic info", "token_name": project_id.to_s }.to_json - resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return resp_body + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 0 + return resp_body + else + raise "区块链接口请求失败." + end end end \ No newline at end of file diff --git a/app/queries/blockchain/balance_query_one_project.rb b/app/queries/blockchain/balance_query_one_project.rb index 7e5dfabc9..c4ec19e21 100644 --- a/app/queries/blockchain/balance_query_one_project.rb +++ b/app/queries/blockchain/balance_query_one_project.rb @@ -7,12 +7,6 @@ class Blockchain::BalanceQueryOneProject < ApplicationQuery end def call - balance = find_one_balance(params[:user_id].to_s, params[:project_id].to_s) - if balance == false - return false - else - results = {"status": balance['status'], "balance": balance['balance']} - results - end + find_one_balance(params[:user_id].to_s, params[:project_id].to_s) end end diff --git a/app/queries/blockchain/repo_basic_info.rb b/app/queries/blockchain/repo_basic_info.rb index 716a67ba3..df17b7a04 100644 --- a/app/queries/blockchain/repo_basic_info.rb +++ b/app/queries/blockchain/repo_basic_info.rb @@ -8,10 +8,6 @@ class Blockchain::RepoBasicInfo < ApplicationQuery def call info = find_blockchain_repo_info(params[:project_id].to_s) - if info == false - return false - else - return info - end + return info end end diff --git a/app/services/application_service.rb b/app/services/application_service.rb index 672c2bae2..f4ac75261 100644 --- a/app/services/application_service.rb +++ b/app/services/application_service.rb @@ -19,39 +19,6 @@ class ApplicationService ActiveModel::Type::Boolean.new.cast str end - - # author: zxh - # blockchain创建项目相关api - - def invoke_blockchain_api(uri, params) - begin - uri = URI.parse(URI.encode(uri.strip)) - res = Net::HTTP.start(uri.host, uri.port) do |http| - req = Net::HTTP::Post.new(uri) - req['Content-Type'] = 'application/json' - req.body = params - http.request(req) - end - if res.code.to_i != 200 - puts '区块链接口请求失败.' - return false - else - res_body = JSON.parse(res.body) - if res_body.has_key?("status") && res_body["status"] == 0 - else - puts '区块链接口请求出错.' - return false - end - end - - return true - rescue Exception => e - puts '区块链接口请求失败.' - return false - end - end - - # params: params from index.js page def create_repo_on_blockchain(params, project) username = params['user_id'].to_s @@ -59,54 +26,72 @@ class ApplicationService total_supply = params['blockchain_token_all'].to_i token_balance = [[username, (total_supply * params['blockchain_init_token'].to_i / 100).to_i]] - param = { + params = { "request-type": "create repo", "username": username, "token_name": token_name, "total_supply": total_supply, "token_balance": token_balance }.to_json - invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] != 0 + raise "区块链接口请求失败." + end end def transfer_balance_on_blockchain(payer, payee, token_name, amount) - param = { + params = { "request-type": "transfer amount", "payer": payer, "payee": payee, "token_name": token_name, "amount": amount }.to_json - results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return results + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 105 + raise resp_body['message'] + elsif resp_body['status'] != 0 + raise "区块链接口请求失败." + else + end end def lock_balance_on_blockchain(username, tokenname, amount) - param = { + params = { "request-type": "lock user balance", "username": username, "token_name": tokenname, "amount": amount }.to_json - results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return results + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 103 + raise resp_body['message'] + elsif resp_body['status'] != 0 + raise "区块链接口请求失败." + else + end end def unlock_balance_on_blockchain(username, tokenname, amount) - param = { + params = { "request-type": "unlock user balance", "username": username, "token_name": tokenname, "amount": amount }.to_json - results = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) - return results + resp_body = Blockchain::InvokeBlockchainApi.call(params) + if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 104 + raise resp_body['message'] + elsif resp_body['status'] != 0 + raise "区块链接口请求失败." + else + end end def phone_mail_type value value =~ /^1\d{10}$/ ? 1 : 0 diff --git a/app/services/blockchain/create_issue.rb b/app/services/blockchain/create_issue.rb index 286f6eda8..c3ab5ad49 100644 --- a/app/services/blockchain/create_issue.rb +++ b/app/services/blockchain/create_issue.rb @@ -13,8 +13,7 @@ class Blockchain::CreateIssue < ApplicationService amount = @params[:token_num].to_i # 调用token锁仓函数 - results = lock_balance_on_blockchain(username, token_name, amount) - return results + lock_balance_on_blockchain(username, token_name, amount) end rescue => e puts "转账失败: #{e.message}" diff --git a/app/services/blockchain/fix_issue.rb b/app/services/blockchain/fix_issue.rb index 573db88e2..332202792 100644 --- a/app/services/blockchain/fix_issue.rb +++ b/app/services/blockchain/fix_issue.rb @@ -13,8 +13,7 @@ class Blockchain::FixIssue < ApplicationService amount = @params[:token_num].to_i # 调用token锁仓函数 - results = unlock_balance_on_blockchain(username, token_name, amount) - return results + unlock_balance_on_blockchain(username, token_name, amount) end rescue => e puts "关联issue失败: #{e.message}" diff --git a/app/services/blockchain/invoke_blockchain_api.rb b/app/services/blockchain/invoke_blockchain_api.rb new file mode 100644 index 000000000..d485e171c --- /dev/null +++ b/app/services/blockchain/invoke_blockchain_api.rb @@ -0,0 +1,34 @@ +class Blockchain::InvokeBlockchainApi < ApplicationService + # 调用blockchain + + attr_reader :params + + def initialize(params) + @params = params + end + + def call + begin + uri = Blockchain.blockchain_config[:api_url] + uri = URI.parse(URI.encode(uri.strip)) + res = Net::HTTP.start(uri.host, uri.port) do |http| + req = Net::HTTP::Post.new(uri) + req['Content-Type'] = 'application/json' + req.body = @params + http.request(req) + end + if res.code.to_i != 200 + raise "区块链接口请求失败." + else + res_body = JSON.parse(res.body) + if res_body.has_key?("status") + else + raise "区块链接口请求失败." + end + end + return res_body + rescue Exception => e + raise "区块链接口请求失败." + end + end +end \ No newline at end of file diff --git a/app/services/blockchain/transfer_service.rb b/app/services/blockchain/transfer_service.rb index 6b4e2c191..4f7425e5d 100644 --- a/app/services/blockchain/transfer_service.rb +++ b/app/services/blockchain/transfer_service.rb @@ -8,25 +8,23 @@ class Blockchain::TransferService < ApplicationService def call ActiveRecord::Base.transaction do + transfer_amount = params['transfer_amount'] + if (Float(transfer_amount) rescue false) == false or transfer_amount.to_i < 0 or Float(transfer_amount) != transfer_amount.to_i + raise Error, "请输入正确的转账金额" + end transfer_amount = params['transfer_amount'].to_i transfer_login = params['transfer_login'] payer = params['payer_id'].to_s payee = User.find_by(login: transfer_login) if payee.nil? - # normal_status(-1, "未找到用户") - return false + raise Error, "未找到接收转账的用户" else payee = payee.id.to_s token_name = params['project_id'].to_s # 调用token转移函数 - results = transfer_balance_on_blockchain(payer, payee, token_name, transfer_amount) - return results + transfer_balance_on_blockchain(payer, payee, token_name, transfer_amount) end end - rescue => e - puts "转账失败: #{e.message}" - # normal_status(-1, e.message) - return false end private @@ -34,4 +32,5 @@ class Blockchain::TransferService < ApplicationService def no_use puts "this function does not have any usage" end + end \ No newline at end of file