mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-17 10:15:57 +08:00
Merge pull request '代码溯源部分代码' (#9) from yystopf/forgeplus:standalone_develop into standalone_develop
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
# 代码溯源 查询检测结果
|
||||
class Trace::CheckResultService < Trace::ClientService
|
||||
|
||||
attr_accessor :token, :project_name, :file_name, :page_num, :page_size
|
||||
attr_accessor :token, :project, :file_name, :page_num, :page_size
|
||||
|
||||
def initialize(token, project_name=nil, file_name=nil, page_num=1, page_size=15)
|
||||
def initialize(token, project, file_name=nil, page_num=1, page_size=15)
|
||||
@token = token
|
||||
@project_name = project_name
|
||||
@project = project
|
||||
@file_name = file_name
|
||||
@page_num = page_num
|
||||
@page_size = page_size
|
||||
@@ -19,7 +19,7 @@ class Trace::CheckResultService < Trace::ClientService
|
||||
private
|
||||
def request_params
|
||||
{
|
||||
product_name: project_name,
|
||||
product_name: Digest::MD5.hexdigest(project&.id.to_s)[0...20],
|
||||
file_name: file_name,
|
||||
pageNum: page_num,
|
||||
pageSize: page_size,
|
||||
|
||||
@@ -11,26 +11,25 @@ class Trace::CheckService < Trace::ClientService
|
||||
end
|
||||
|
||||
def call
|
||||
result = authed_post(token, url, {data: request_params})
|
||||
result = http_authed_post(token, url, {data: request_params})
|
||||
reponse = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
def request_params
|
||||
repo = Gitea::Repository::GetService.call(project&.owner&.login, project&.identifier)
|
||||
repo = Gitea::Repository::GetService.call(project&.owner, project&.identifier)
|
||||
{
|
||||
product_name: project&.name,
|
||||
product_type: project&.category&.name,
|
||||
code_type: project&.language&.name,
|
||||
product_name: Digest::MD5.hexdigest(project&.id.to_s)[0...20],
|
||||
product_type: project&.project_category&.name || '其他',
|
||||
code_type: project&.project_language&.name || '其他',
|
||||
product_desc: project&.description,
|
||||
git_url: repo['clone_url'],
|
||||
if_branch: if_branch,
|
||||
branch_tag: branch_tag
|
||||
}
|
||||
}.compact
|
||||
end
|
||||
|
||||
def url
|
||||
"/user/check".freeze
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -12,6 +12,19 @@ class Trace::ClientService < ApplicationService
|
||||
conn.post(full_url(url), params[:data])
|
||||
end
|
||||
|
||||
def http_authed_post(token, url, params={})
|
||||
puts "[trace][POST] request params: #{params}"
|
||||
puts "[trace][POST] request token: #{token}"
|
||||
url = URI("#{full_url(url)}")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
http.read_timeout = 1200
|
||||
request = Net::HTTP::Post.new(url)
|
||||
request["Authorization"] = token
|
||||
form_data = params[:data].stringify_keys.to_a
|
||||
request.set_form form_data, 'multipart/form-data'
|
||||
http.request(request)
|
||||
end
|
||||
|
||||
def get(url, params={})
|
||||
puts "[trace][GET] request params: #{params}"
|
||||
conn.get do |req|
|
||||
@@ -100,11 +113,22 @@ class Trace::ClientService < ApplicationService
|
||||
end
|
||||
|
||||
def render_response(response)
|
||||
status = response.status
|
||||
body = JSON.parse(response&.body)
|
||||
if response.is_a?(Faraday::Response)
|
||||
status = response.status
|
||||
body = JSON.parse(response&.body)
|
||||
|
||||
log_error(status, body)
|
||||
log_error(status, body)
|
||||
|
||||
return [body["code"], body["data"], body["error"]]
|
||||
return [body["code"], body["data"], body["error"]]
|
||||
end
|
||||
|
||||
if response.is_a?(Net::HTTPOK)
|
||||
status = 200
|
||||
body = JSON.parse(response&.body)
|
||||
|
||||
log_error(status, body)
|
||||
|
||||
return [body["code"], body["data"], body["error"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,7 @@
|
||||
# 代码溯源 导出pdf
|
||||
require 'open-uri'
|
||||
require 'fileutils'
|
||||
|
||||
class Trace::PdfReportService < Trace::ClientService
|
||||
|
||||
attr_accessor :token, :task_id
|
||||
@@ -9,15 +12,23 @@ class Trace::PdfReportService < Trace::ClientService
|
||||
end
|
||||
|
||||
def call
|
||||
result = authed_get(token, url, request_params)
|
||||
response = render_response(result)
|
||||
content = open("#{domain}#{base_url}#{url}?task_id=#{task_id}", "Authorization" => token)
|
||||
if content.is_a?(Tempfile)
|
||||
check_file_path
|
||||
IO.copy_stream(content, "#{save_path}/#{task_id}.pdf")
|
||||
return {code: 200, download_url: "/trace_task_results/#{task_id}.pdf"}
|
||||
else
|
||||
return {code: 404}
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def request_params
|
||||
{
|
||||
task_id: task_id
|
||||
}
|
||||
def check_file_path
|
||||
FileUtils.mkdir_p save_path
|
||||
end
|
||||
|
||||
def save_path
|
||||
"public/trace_task_results"
|
||||
end
|
||||
|
||||
def url
|
||||
|
||||
Reference in New Issue
Block a user