Merge remote-tracking branch 'origin/dev_osredm_server' into dev_osredm_server

This commit is contained in:
xxq250 2025-09-15 09:46:12 +08:00
commit 279735657c
28 changed files with 227 additions and 18 deletions

View File

@ -80,7 +80,7 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController
if @pipeline.yaml.present?
sha = get_pipeline_file_sha(@pipeline.file_name, @pipeline.branch)
if sha.present?
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("update", params[:pipeline_name]).merge(sha: sha, from_path: "#{@pipeline.pipeline_name}"))
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("update", params[:pipeline_name]).merge(sha: sha, from_path: ".gitea/workflows/#{@pipeline.pipeline_name}.yml"))
tip_exception(interactor.error) unless interactor.success?
end
end

View File

@ -0,0 +1,18 @@
class Api::V1::Projects::ScaController < Api::V1::BaseController
before_action :require_public_and_member_above
before_action :load_project
def index
@result = Api::V1::Projects::Sca::ShowProjectService.call(@project)
end
def create
result = Api::V1::Projects::Sca::CreateProjectService.call(@project)
if result
@result = Api::V1::Projects::Sca::ShowProjectService.call(@project)
else
{
auth_token: nil,
"url": nil,
}
end
end
end

View File

@ -35,7 +35,7 @@ class IssueTagsController < ApplicationController
begin
issue_tag = IssueTag.new(tag_params.merge(project_id: @project.id, user_id: current_user.id))
if issue_tag.save
# gitea_tag = Gitea::Labels::CreateService.new(current_user, @repository.try(:identifier), tag_params).call
# gitea_tag = Gitea::Labels::CreateProjectService.new(current_user, @repository.try(:identifier), tag_params).call
# if gitea_tag && issue_tag.update_attributes(gid: gitea_tag["id"], gitea_url: gitea_tag["url"])
normal_status(0, "项目标记创建成功!")
# else

View File

@ -45,7 +45,7 @@ class SyncForgeController < ApplicationController
# private: !sync_params[:is_public],
# name: sync_params[:name]
# }
# project = Projects::CreateService.new(project_user, project_params).call
# project = Projects::CreateProjectService.new(project_user, project_params).call
# if project.present?
# if sync_params[:project_score].present?
# sync_params.permit!

View File

@ -76,6 +76,7 @@ module ProjectsHelper
open_portrait: project.open_portrait,
ignore_id: project.ignore_id,
has_project_visitor: project.project_visitors.find_by(user_id: user.id).present?,
sca_project_id: project.sca_project_id
}).compact
render json: tmp_json

View File

@ -45,7 +45,7 @@ class UserAction < ApplicationRecord
def opt_user_name
user = User.find_by(id: self.user_id)
if user.present?
user&.login
user&.real_name
else
del_user = UserAction.find_by(action_type: "DestroyUser", action_id: self.user_id)
del_user.present? ? del_user.user.login : "不存在用户:#{user_id}"

View File

@ -54,7 +54,7 @@ class Api::Pm::Issues::CreateService < ApplicationService
load_attachments(attachment_ids) unless attachment_ids.blank?
load_issue_tags(issue_tag_ids) unless issue_tag_ids.blank?
load_atme_receivers(receivers_login) unless receivers_login.blank?
try_lock("Api::Pm::Issues::CreateService:#{project.id}") # 开始写数据,加锁
try_lock("Api::Pm::Issues::CreateProjectService:#{project.id}") # 开始写数据,加锁
@created_issue = Issue.new(issue_attributes)
@created_issue.pm_issue_type = @pm_issue_type
if @root_subject.present? && [4, 5].include?(@pm_issue_type.to_i)
@ -123,7 +123,7 @@ class Api::Pm::Issues::CreateService < ApplicationService
TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @created_issue&.id, current_user.id, {issue_tag_ids: [[], issue_tag_ids]}) unless issue_tag_ids.blank?
TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @created_issue&.id, current_user.id, {assigner_ids: [[], assigner_ids]}) unless assigner_ids.blank?
end
unlock("Api::Pm::Issues::CreateService:#{project.id}") # 结束写数据,解锁
unlock("Api::Pm::Issues::CreateProjectService:#{project.id}") # 结束写数据,解锁
end
return @created_issue

View File

@ -53,7 +53,7 @@ class Api::V1::Issues::CreateService < ApplicationService
load_attachments(attachment_ids) unless attachment_ids.blank?
load_issue_tags(issue_tag_ids) unless issue_tag_ids.blank?
load_atme_receivers(receivers_login) unless receivers_login.blank?
try_lock("Api::V1::Issues::CreateService:#{project.id}") # 开始写数据,加锁
try_lock("Api::V1::Issues::CreateProjectService:#{project.id}") # 开始写数据,加锁
@created_issue = Issue.new(issue_attributes)
build_author_participants
build_assigner_participants unless assigner_ids.blank?
@ -111,7 +111,7 @@ class Api::V1::Issues::CreateService < ApplicationService
TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @created_issue&.id, current_user.id, {issue_tag_ids: [[], issue_tag_ids]}) unless issue_tag_ids.blank?
TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @created_issue&.id, current_user.id, {assigner_ids: [[], assigner_ids]}) unless assigner_ids.blank?
end
unlock("Api::V1::Issues::CreateService:#{project.id}") # 结束写数据,解锁
unlock("Api::V1::Issues::CreateProjectService:#{project.id}") # 结束写数据,解锁
end
return @created_issue

View File

@ -29,7 +29,7 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService
load_attachments(attachment_ids) unless attachment_ids.nil?
load_atme_receivers(receivers_login) unless receivers_login.nil?
try_lock("Api::V1::Issues::Journals::CreateService:#{@issue.id}")
try_lock("Api::V1::Issues::Journals::CreateProjectService:#{@issue.id}")
@created_journal = @issue.journals.new(journal_attributes)
build_comment_participants
@ -44,7 +44,7 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService
# @信息发送
AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank?
TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueComment', @issue&.id, @current_user.id, @created_journal.id, 'created', {})
unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}")
unlock("Api::V1::Issues::Journals::CreateProjectService:#{@issue.id}")
@created_journal
end

View File

@ -29,14 +29,14 @@ class Api::V1::Journals::CreateService < ApplicationService
load_attachments(attachment_ids) unless attachment_ids.nil?
# load_atme_receivers(receivers_login) unless receivers_login.nil?
try_lock("Api::V1::Journals::CreateService:#{@container.id}")
try_lock("Api::V1::Journals::CreateProjectService:#{@container.id}")
@created_journal = @container.journals.new(journal_attributes)
@created_journal.attachments = @attachments unless attachment_ids.blank?
@created_journal.save!
# @container.save!
unlock("Api::V1::Journals::CreateService:#{@container.id}")
unlock("Api::V1::Journals::CreateProjectService:#{@container.id}")
@created_journal
end

View File

@ -0,0 +1,88 @@
class Api::V1::Projects::Sca::BaseService < ApplicationService
private
def base_url
domain_url + Rails.application.config_for(:configuration)["sca"]["base_url"]
end
def domain_url
Rails.application.config_for(:configuration)["sca"]["domain"]
end
def auth_token
Rails.application.config_for(:configuration)["sca"]["token"]
end
def login_token
params = {
"username": Rails.application.config_for(:configuration)["sca"]["username"],
"password": Rails.application.config_for(:configuration)["sca"]["password"]
}
sca_post_request("rest-auth/login/", params)
end
def sca_get_request(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 sca_post_request(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
def sca_user_info
if Rails.cache.fetch("sca_user").nil?
data = sca_get_request("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 sca_team_id
data = sca_get_request("orgs/#{sca_user_info["default_org"]}/teams/", params = {})
default_team = data[:json]["results"].find { |team| team["is_default"] }
default_team ? default_team["id"] : nil
end
end

View File

@ -0,0 +1,25 @@
class Api::V1::Projects::Sca::CreateProjectService < Api::V1::Projects::Sca::BaseService
def initialize(project)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
end
def call
params = {
"org_id": sca_user_info["default_org"],
"team_id_list": [sca_team_id],
"name": @repo,
"provider": "gitea_hs",
"external_id": "#{@owner}&|&#{@repo}",
"description": @project.description
}
data = sca_post_request("projects/", params)
if @project.update(sca_project_id:data[:json]["id"])
true
else
false
end
end
end

View File

@ -0,0 +1,19 @@
class Api::V1::Projects::Sca::CreateReportService < Api::V1::Projects::Sca::BaseService
def initialize(project)
@project = project
end
def call
params = {
"org_id": sca_user_info["default_org"],
"team_id_list": [sca_team_id],
"name": @repo,
"provider": "gitea_hs",
"external_id": "#{@owner}&|&#{@repo}",
"description": @project.description
}
data = sca_post_request("reports", params)
@project.update(sca_project_id:data[:json]["id"])
end
end

View File

@ -0,0 +1,17 @@
class Api::V1::Projects::Sca::ListBranchService < Api::V1::Projects::Sca::BaseService
def initialize(project)
@project = project
end
def call
#projects/18748/list-branches/?branch_name_query=&page=1&limit=5
#
params = {
"branch_name_query": nil,
"limit": 5,
"page": 1
}
sca_get_request("projects/#{ @project.sca_project_id }/list-branches/", params)
end
end

View File

@ -0,0 +1,14 @@
class Api::V1::Projects::Sca::PoliciesService < Api::V1::Projects::Sca::BaseService
def initialize()
end
def call
params = {
"org_id": sca_user_info["default_org"],
"limit": 1000,
"page": 1
}
sca_get_request("compliance/policies/", params)
end
end

View File

@ -0,0 +1,15 @@
class Api::V1::Projects::Sca::ShowProjectService < Api::V1::Projects::Sca::BaseService
def initialize(project)
@project = project
end
def call
#projects/18748/list-branches/?branch_name_query=&page=1&limit=5
{
auth_token:login_token,
"url": domain_url + "/org/#{sca_user_info["default_org"]}/projects/#{@project.sca_project_id}?engine=sca" ,
}
end
end

View File

@ -15,7 +15,7 @@ class Gitea::Hooks::CreateService < Gitea::ClientService
# "type": "gitea"
# }
# eg:
# Gitea::Hooks::CreateService.call(user.gitea_token, user.login, repo.identifier, body)
# Gitea::Hooks::CreateProjectService.call(user.gitea_token, user.login, repo.identifier, body)
def initialize(token, owner, repo, body)
@token = token
@owner = owner

View File

@ -9,7 +9,7 @@ class Gitea::Oauth2::CreateService < Gitea::ClientService
# "string"
# ]
# }
# ep: Gitea::OAuth2::CreateService.call(current_user.gitea_token, {name: 'oauth_name', redirect_uris: ['url']})
# ep: Gitea::OAuth2::CreateProjectService.call(current_user.gitea_token, {name: 'oauth_name', redirect_uris: ['url']})
# return values example:
# {
# "client_id": "string",

View File

@ -13,7 +13,7 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService
# base: 'master' // to branch 目标分支
# }
# 以上列子说明从develop分支合并到master分支
# Gitea::PullRequest::CreateService.call('token', '项目拥有者', '项目名称', params)
# Gitea::PullRequest::CreateProjectService.call('token', '项目拥有者', '项目名称', params)
# fork的项目向源项目发送pr例子如下
# 参数说明:
@ -27,7 +27,7 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService
# "title": "jasder用户向源项目发送pr"
# }
# 以上例子说明jasder用户fork的项目master分支向源项目的develop分支发送pr
# Gitea::PullRequest::CreateService.call('token', '源项目拥有者', '源项目名称', params)
# Gitea::PullRequest::CreateProjectService.call('token', '源项目拥有者', '源项目名称', params)
def initialize(token, owner, repo, params={})
@token = token
@owner = owner

View File

@ -43,10 +43,10 @@ class Repositories::CreateService < ApplicationService
rescue Gitea::Api::ServerError => e
if e.http_code.to_i == 404
if project.owner.is_a?(User)
# @gitea_repository = Gitea::Repository::CreateService.new(user.gitea_token, gitea_repository_params).call
# @gitea_repository = Gitea::Repository::CreateProjectService.new(user.gitea_token, gitea_repository_params).call
@gitea_repository = $gitea_client.post_user_repos({query: {token: user.gitea_token}, body: gitea_repository_params.to_json})
elsif project.owner.is_a?(Organization)
# @gitea_repository = Gitea::Organization::Repository::CreateService.call(user.gitea_token, project.owner.login, gitea_repository_params)
# @gitea_repository = Gitea::Organization::Repository::CreateProjectService.call(user.gitea_token, project.owner.login, gitea_repository_params)
@gitea_repository = $gitea_client.post_orgs_repos_by_org(project.owner.login, {query: {token: user.gitea_token}, body: gitea_repository_params.to_json})
end
else

View File

@ -0,0 +1 @@
json.data @result

View File

@ -0,0 +1 @@
json.data @result

View File

@ -54,6 +54,7 @@ json.language do
end
json.is_member project.member?(current_user.try(:id))
json.is_need_apply project.is_need_apply
json.sca_project_id project.sca_project_id
user_apply_signatures = project.apply_signatures.with_user_id(current_user.id)
json.user_apply_signatures user_apply_signatures do |signature|
json.id signature.id

View File

@ -6,6 +6,7 @@ json.description @project.description
json.repo_id @project.repository.id
json.repo_identifier @project.repository.identifier
json.is_need_apply @project.is_need_apply
json.sca_project_id @project.sca_project_id
user_apply_signatures = @project.apply_signatures.with_user_id(current_user.id)
json.user_apply_signatures user_apply_signatures do |signature|
json.id signature.id

View File

@ -28,6 +28,7 @@ json.open_devops @project.open_devops?
json.license_is_secret @project.license&.is_secret == true
json.updated_on @project.updated_on
json.time_ago time_from_now(@project.updated_on)
json.sca_project_id @project.sca_project_id
json.project_language @project.project_language&.name
json.topics @project.project_topics.each do |topic|
json.(topic, :id, :name)

View File

@ -19,6 +19,7 @@ json.type @project.numerical_for_project_type
json.open_devops @project.open_devops?
json.is_secret @project.is_secret
json.is_need_apply @project.is_need_apply
json.sca_project_id @project.sca_project_id
user_apply_signatures = @project.apply_signatures.with_user_id(current_user.id)
json.user_apply_signatures user_apply_signatures do |signature|
json.id signature.id

View File

@ -223,6 +223,7 @@ defaults format: :json do
end
end
end
resources :sca, only: [:index,:create]
resources :pipelines do
post :build_yaml, on: :collection
post :save_yaml, on: :collection

View File

@ -0,0 +1,5 @@
class AddScaProjectIdToProjects < ActiveRecord::Migration[5.2]
def change
add_column :projects, :sca_project_id, :integer
end
end