add 存证信息

This commit is contained in:
nigel007 2021-05-13 22:27:25 +08:00
parent ac2974f7e0
commit 6d399fe8d5
5 changed files with 446 additions and 144 deletions

View File

@ -889,6 +889,304 @@ class ApplicationController < ActionController::Base
# author: zxh # author: zxh
# blockchain相关项目活动调用函数 # blockchain相关项目活动调用函数
# return true: 表示上链操作成功; return false: 表示上链操作失败 # 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
# # 无需执行上链操作
# 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
# # 无需执行上链操作
# 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
# # 无需执行上链操作
# 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['gpid'])
# 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
# # 无需执行上链操作
# 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['gpid'])
# 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
# # 无需执行上链操作
# 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" if activity_type == "issue_create"
@ -919,12 +1217,10 @@ class ApplicationController < ActionController::Base
# 调用区块链接口 # 调用区块链接口
param = { param = {
"action": "executeContract", "request-type": "upload issue info",
"contractID": "RepositoryDB1", "issue_id": "trustie-" + id.to_s,
"operation": "putIssue", "repo_id": "trustie-" + project_id.to_s,
"arg": { "issue_number": 0, # 暂时不需要改字段
"issue_id": id,
"repo_id": project_id,
"reponame": reponame, "reponame": reponame,
"ownername": ownername, "ownername": ownername,
"username": username, "username": username,
@ -934,7 +1230,6 @@ class ApplicationController < ActionController::Base
"created_at": created_at, "created_at": created_at,
"updated_at": updated_at "updated_at": updated_at
}.to_json }.to_json
}.to_json
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
return success_blockchain return success_blockchain
@ -969,13 +1264,13 @@ class ApplicationController < ActionController::Base
if issue_classify == "issue" if issue_classify == "issue"
param = { param = {
"action": "executeContract", "request-type": "upload issue comment info",
"contractID": "RepositoryDB1", "issue_comment_id": "trustie-" + issue_comment_id.to_s,
"operation": "putIssueComment", "issue_comment_number": 0, # 暂时不需要
"arg": { "issue_number": 0, # 暂时不需要
"issue_comment_id": issue_comment_id, "issue_id": "trustie-" + issue_id.to_s,
"issue_id": issue_id, "repo_id": "trustie-" + project.id.to_s,
"parent_id": parent_id, "parent_id": parent_id.to_s,
"reponame": reponame, "reponame": reponame,
"ownername": ownername, "ownername": ownername,
"username": username, "username": username,
@ -983,16 +1278,15 @@ class ApplicationController < ActionController::Base
"content": content, "content": content,
"created_at": created_at, "created_at": created_at,
}.to_json }.to_json
}.to_json
elsif issue_classify == "pull_request" elsif issue_classify == "pull_request"
param = { param = {
"action": "executeContract", "request-type": "upload pull request comment info",
"contractID": "RepositoryDB0", "pull_request_comment_id": "trustie-" + issue_comment_id.to_s,
"operation": "putPullRequestComment", "pull_request_comment_number": 0, # 不考虑该字段
"arg": { "pull_request_number": 0, # 不考虑该字段
"pull_request_comment_id": issue_comment_id, "pull_request_id": "trustie-" + issue_id.to_s,
"pull_request_id": issue_id, "parent_id": parent_id.to_s,
"parent_id": parent_id, "repo_id": "trustie-" + project.id.to_s,
"reponame": reponame, "reponame": reponame,
"ownername": ownername, "ownername": ownername,
"username": username, "username": username,
@ -1000,7 +1294,6 @@ class ApplicationController < ActionController::Base
"content": content, "content": content,
"created_at": created_at, "created_at": created_at,
}.to_json }.to_json
}.to_json
end end
# 调用区块链接口 # 调用区块链接口
@ -1046,12 +1339,10 @@ class ApplicationController < ActionController::Base
commit_shas << c["Sha"] commit_shas << c["Sha"]
end end
param = { param = {
"action": "executeContract", "request-type": "upload pull request info",
"contractID": "RepositoryDB0", "pull_request_id": "trustie-" + pull_request_id.to_s,
"operation": "putPullRequest", "pull_request_number": 0, # trustie没有该字段
"arg": { "repo_id": "trustie-" + project_id.to_s,
"pull_request_id": pull_request_id,
"repo_id": project_id,
"ownername": ownername, "ownername": ownername,
"reponame": reponame, "reponame": reponame,
"username": username, "username": username,
@ -1060,11 +1351,12 @@ class ApplicationController < ActionController::Base
"content": content, "content": content,
"source_branch": source_branch, "source_branch": source_branch,
"target_branch": target_branch, "target_branch": target_branch,
"reviewers": [], # trustie没有该字段
"commit_shas": commit_shas, "commit_shas": commit_shas,
"merge_user": "", # trustie没有该字段
"created_at": created_at, "created_at": created_at,
"updated_at": updated_at "updated_at": updated_at
}.to_json }.to_json
}.to_json
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
return success_blockchain return success_blockchain
elsif activity_type == "pull_request_merge" elsif activity_type == "pull_request_merge"
@ -1094,12 +1386,10 @@ class ApplicationController < ActionController::Base
# 将pull request相关信息写入链上 # 将pull request相关信息写入链上
param = { param = {
"action": "executeContract", "request-type": "upload pull request info",
"contractID": "RepositoryDB0", "pull_request_id": "trustie-" + pull_request_id.to_s,
"operation": "putPullRequest", "pull_request_number": 0, # trustie没有该字段
"arg": { "repo_id": "trustie-" + project_id.to_s,
"pull_request_id": pull_request_id,
"repo_id": project_id,
"ownername": ownername, "ownername": ownername,
"reponame": reponame, "reponame": reponame,
"username": username, "username": username,
@ -1108,11 +1398,12 @@ class ApplicationController < ActionController::Base
"content": content, "content": content,
"source_branch": source_branch, "source_branch": source_branch,
"target_branch": target_branch, "target_branch": target_branch,
"reviewers": [], # trustie没有该字段
"commit_shas": commit_shas, "commit_shas": commit_shas,
"merge_user": "", # trustie没有该字段
"created_at": created_at, "created_at": created_at,
"updated_at": updated_at "updated_at": updated_at
}.to_json }.to_json
}.to_json
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
@ -1121,21 +1412,17 @@ class ApplicationController < ActionController::Base
commit_diff = Gitea::Commit::DiffService.call(ownername, reponame, commit_sha) commit_diff = Gitea::Commit::DiffService.call(ownername, reponame, commit_sha)
commit = Gitea::Commit::InfoService.call(ownername, reponame, commit_sha) commit = Gitea::Commit::InfoService.call(ownername, reponame, commit_sha)
param = { param = {
"action": "executeContract", "request-type": "upload commit info",
"contractID": "RepositoryDB1",
"operation": "putCommit",
"arg": {
"commit_hash": commit_sha, "commit_hash": commit_sha,
"repo_id": project_id, "repo_id": "trustie-" + project_id.to_s,
"author": commit['author']['login'], "author": commit['author']['login'],
"author_email": commit['author']['email'], "author_email": commit['author']['email'],
"committer": commit['committer']['login'], "committer": commit['committer']['login'],
"committer_email": commit['committer']['email'], "committer_email": commit['committer']['email'],
"author_time": commit['commit']['author']['date'], "author_time": commit['commit']['author']['date'],
"committer_time": commit['commit']['committer']['date'], "committer_time": commit['commit']['committer']['date'],
"message": commit['commit']['message'], "content": commit['commit']['message'],
"diff": commit_diff['Files'] "commit_diff": commit_diff['Files'].to_s
}.to_json
}.to_json }.to_json
success_blockchain_commit = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) success_blockchain_commit = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
success_blockchain = success_blockchain & success_blockchain_commit success_blockchain = success_blockchain & success_blockchain_commit
@ -1162,12 +1449,10 @@ class ApplicationController < ActionController::Base
# 将pull request相关信息写入链上 # 将pull request相关信息写入链上
param = { param = {
"action": "executeContract", "request-type": "upload pull request info",
"contractID": "RepositoryDB0", "pull_request_id": "trustie-" + pull_request_id.to_s,
"operation": "putPullRequest", "pull_request_number": 0, # trustie没有该字段
"arg": { "repo_id": "trustie-" + project_id.to_s,
"pull_request_id": pull_request_id,
"repo_id": project_id,
"ownername": ownername, "ownername": ownername,
"reponame": reponame, "reponame": reponame,
"username": username, "username": username,
@ -1176,10 +1461,12 @@ class ApplicationController < ActionController::Base
"content": content, "content": content,
"source_branch": source_branch, "source_branch": source_branch,
"target_branch": target_branch, "target_branch": target_branch,
"reviewers": [], # trustie没有该字段
"commit_shas": commit_shas,
"merge_user": "", # trustie没有该字段
"created_at": created_at, "created_at": created_at,
"updated_at": updated_at "updated_at": updated_at
}.to_json }.to_json
}.to_json
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param) success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
return success_blockchain return success_blockchain
end end

View File

@ -143,17 +143,16 @@ class IssuesController < ApplicationController
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
else else
end 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} #render json: {status: 0, message: "创建成功", id: @issue.id}
#end # 调用上链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}
end
render json: {status: 0, message: "创建成", id: @issue.id}
else else
normal_status(-1, "创建失败") normal_status(-1, "创建失败")
end end

View File

@ -58,14 +58,13 @@ class PullRequestsController < ApplicationController
# author: zxh # author: zxh
# 调用上链API # 调用上链API
#success_blockchain = push_activity_2_blockchain("pull_request_create", @pull_request) success_blockchain = push_activity_2_blockchain("pull_request_create", @pull_request)
#if success_blockchain == false if success_blockchain == false
# render_error("create pull request error: cannot save to blockchain") render_error("create pull request error: cannot save to blockchain")
# raise ActiveRecord::Rollback raise ActiveRecord::Rollback
#else else
# render_ok
#end
render_ok render_ok
end
else else
render_error("create pull request error: #{@gitea_pull_request[:status]}") render_error("create pull request error: #{@gitea_pull_request[:status]}")
@ -135,13 +134,12 @@ class PullRequestsController < ApplicationController
colsed === true ? normal_status(1, "已拒绝") : normal_status(-1, '合并失败') colsed === true ? normal_status(1, "已拒绝") : normal_status(-1, '合并失败')
# author: zxh # author: zxh
# 调用上链API # 调用上链API
#success_blockchain = push_activity_2_blockchain("pull_request_refuse", @pull_request) success_blockchain = push_activity_2_blockchain("pull_request_refuse", @pull_request)
#if success_blockchain == false if success_blockchain == false
# normal_status(-1, "拒绝失败") normal_status(-1, "拒绝失败")
# raise ActiveRecord::Rollback raise ActiveRecord::Rollback
#else else
# normal_status(1, "已拒绝") end
#end
rescue => e rescue => e
normal_status(-1, e.message) normal_status(-1, e.message)
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
@ -175,14 +173,13 @@ class PullRequestsController < ApplicationController
# author: zxh # author: zxh
# 调用上链API # 调用上链API
#success_blockchain = push_activity_2_blockchain("pull_request_merge", @pull_request) success_blockchain = push_activity_2_blockchain("pull_request_merge", @pull_request)
#if success_blockchain == false if success_blockchain == false
# normal_status(-1, "合并失败") normal_status(-1, "合并失败")
# raise ActiveRecord::Rollback raise ActiveRecord::Rollback
#else else
# normal_status(1, "合并成功") end
#end
#
# 查看是否fix了相关issue如果fix就转账 # 查看是否fix了相关issue如果fix就转账
if params["fix_issue_id"].nil? || params["fix_issue_id"] == "" if params["fix_issue_id"].nil? || params["fix_issue_id"] == ""
@ -190,6 +187,7 @@ class PullRequestsController < ApplicationController
issue = Issue.find_by(id: params["fix_issue_id"]) issue = Issue.find_by(id: params["fix_issue_id"])
if issue.nil? if issue.nil?
normal_status(-1, "关联issue失败") normal_status(-1, "关联issue失败")
raise ActiveRecord::Rollback
else else
token_num = issue.blockchain_token_num token_num = issue.blockchain_token_num
token_num = token_num.nil? ? 0 : token_num token_num = token_num.nil? ? 0 : token_num
@ -197,15 +195,18 @@ class PullRequestsController < ApplicationController
pr = PullRequest.find_by(id: params["pull_request"]["id"]) pr = PullRequest.find_by(id: params["pull_request"]["id"])
if owner.nil? || pr.nil? if owner.nil? || pr.nil?
normal_status(-1, "关联issue失败") normal_status(-1, "关联issue失败")
raise ActiveRecord::Rollback
else else
project = Project.find_by(user_id: owner.id, name: params["project_id"]) project = Project.find_by(user_id: owner.id, name: params["project_id"])
if project.nil? if project.nil?
normal_status(-1, "关联issue失败") normal_status(-1, "关联issue失败")
raise ActiveRecord::Rollback
else else
author_id = pr.user_id 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}) result = Blockchain::FixIssue.call({user_id: author_id.to_s, project_id: project.id.to_s, token_num: token_num})
if result == false if result == false
normal_status(-1, "关联issue失败") normal_status(-1, "关联issue失败")
raise ActiveRecord::Rollback
else else
# update issue to state 5 # update issue to state 5
issue.update(status_id: 5) issue.update(status_id: 5)

View File

@ -459,6 +459,20 @@ class UsersController < ApplicationController
render json: {"blockchain_token_num": issue.blockchain_token_num} render json: {"blockchain_token_num": issue.blockchain_token_num}
end end
def blockchain_get_unclosed_issue_list
ownername = params["ownername"]
reponame = params["reponame"]
owner = User.find_by(login: ownername)
project = Project.find_by(user_id: owner.id, name: reponame)
unclosed_issues = Issue.where(project_id: project.id, issue_classify: "issue").where.not(status_id: 5)
results = []
unclosed_issues.each do |i|
results << [i.id, i.subject]
end
render json: {unclosed_issues: results}
end
# TODO 其他平台登录时同步修改gitea平台对应用户的密码 # TODO 其他平台登录时同步修改gitea平台对应用户的密码
# 该方法主要用于别的平台初次部署对接forge平台同步用户后gitea平台对应的用户密码与forge平台用户密码不一致是问题 # 该方法主要用于别的平台初次部署对接forge平台同步用户后gitea平台对应的用户密码与forge平台用户密码不一致是问题
def sync_gitea_pwd def sync_gitea_pwd

View File

@ -117,6 +117,7 @@ Rails.application.routes.draw do
post '/users/blockchain/verify_trade', to: 'users#blockchain_verify_trade' post '/users/blockchain/verify_trade', to: 'users#blockchain_verify_trade'
get '/users/blockchain/get_history_trades', to: 'users#blockchain_get_history_trades' get '/users/blockchain/get_history_trades', to: 'users#blockchain_get_history_trades'
post '/blockchain/issue/get_token_num', to: 'users#blockchain_get_issue_token_num' post '/blockchain/issue/get_token_num', to: 'users#blockchain_get_issue_token_num'
get '/projects/blockchain/get_unclosed_issue_list', to: 'users#blockchain_get_unclosed_issue_list'
resources :owners, only: [:index] resources :owners, only: [:index]