forked from Trustie/forgeplus
add sca
This commit is contained in:
parent
1dc3052891
commit
55034cebdd
|
|
@ -0,0 +1,22 @@
|
|||
class Api::V1::Projects::ScaController < Api::V1::BaseController
|
||||
before_action :require_public_and_member_above, only: [:index, :show]
|
||||
|
||||
def create
|
||||
@result_object = Api::V1::Projects::Sca::CreateService.call(@project)
|
||||
end
|
||||
|
||||
def show
|
||||
@result_object = Api::V1::Projects::Tags::GetService.call(@project, params[:name], current_user&.gitea_token)
|
||||
end
|
||||
|
||||
before_action :require_operate_above, only: [:destroy]
|
||||
|
||||
def destroy
|
||||
@result_object = Api::V1::Projects::Tags::DeleteService.call(@project, params[:name], current_user&.gitea_token)
|
||||
if @result_object
|
||||
return render_ok
|
||||
else
|
||||
return render_error('删除标签失败!')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
class Api::V1::Projects::Sca::CreateService < ApplicationService
|
||||
def initialize(project)
|
||||
@project = project
|
||||
@owner = project&.owner.login
|
||||
@repo = project&.identifier
|
||||
end
|
||||
|
||||
def initialize_sca_project
|
||||
params = {
|
||||
"org_id": user_info["default_org"],
|
||||
"team_id_list": [team_id],
|
||||
"name": @repo,
|
||||
"provider": "gitea_hs",
|
||||
"external_id": "#{@owner}&|&#{@repo}",
|
||||
"description": @project.description
|
||||
}
|
||||
post_json("projects/", params)
|
||||
end
|
||||
|
||||
def run_sca_scan
|
||||
params = {
|
||||
"org_id": user_info["default_org"],
|
||||
"team_id_list": [team_id],
|
||||
"name": @repo,
|
||||
"provider": "gitea_hs",
|
||||
"external_id": "#{@owner}&|&#{@repo}",
|
||||
"description": @project.description
|
||||
}
|
||||
post_json("projects/", params)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def base_url
|
||||
Rails.application.config_for(:configuration)["sca"]["domain"] + Rails.application.config_for(:configuration)["sca"]["base_url"]
|
||||
end
|
||||
|
||||
def auth_token
|
||||
Rails.application.config_for(:configuration)["sca"]["token"]
|
||||
end
|
||||
|
||||
def user_info
|
||||
if Rails.cache.fetch("sca_user").nil?
|
||||
data = get_json("user/", params = {})
|
||||
Rails.cache.write("sca_user",data[:body], expires_in: 1.day, force: true)
|
||||
data[:json]
|
||||
else
|
||||
JSON.parse Rails.cache.fetch("sca_user")
|
||||
end
|
||||
end
|
||||
|
||||
def team_id
|
||||
data = get_json("orgs/#{user_info["default_org"]}/teams/", params = {})
|
||||
default_team = data[:json]["results"].find { |team| team["is_default"] }
|
||||
default_team ? default_team["id"] : nil
|
||||
end
|
||||
|
||||
def get_json(url, params = {} )
|
||||
begin
|
||||
uri = URI.parse(base_url + url)
|
||||
uri.query = URI.encode_www_form(params) if params && !params.empty?
|
||||
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
http.use_ssl = (uri.scheme == "https")
|
||||
|
||||
request = Net::HTTP::Get.new(uri.request_uri)
|
||||
request["Content-Type"] = "application/json"
|
||||
request["Authorization"] = auth_token if auth_token
|
||||
|
||||
response = http.request(request)
|
||||
|
||||
{
|
||||
code: response.code.to_i,
|
||||
body: response.body,
|
||||
json: (JSON.parse(response.body) rescue nil)
|
||||
}
|
||||
rescue
|
||||
raise Error, "SCA服务器错误,请联系系统管理员!"
|
||||
end
|
||||
end
|
||||
|
||||
def post_json(url, params = {})
|
||||
begin
|
||||
uri = URI.parse(base_url + url)
|
||||
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
http.use_ssl = (uri.scheme == "https")
|
||||
|
||||
request = Net::HTTP::Post.new(uri.request_uri)
|
||||
request["Content-Type"] = "application/json"
|
||||
request["Authorization"] = auth_token if auth_token
|
||||
request.body = params.to_json if params && !params.empty?
|
||||
|
||||
response = http.request(request)
|
||||
|
||||
{
|
||||
code: response.code.to_i,
|
||||
body: response.body,
|
||||
json: (JSON.parse(response.body) rescue nil)
|
||||
}
|
||||
rescue
|
||||
raise Error, "SCA服务器错误,请联系系统管理员!"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Reference in New Issue