This commit is contained in:
呱呱呱 2025-09-04 17:39:58 +08:00
parent 55034cebdd
commit 3492689d9b
18 changed files with 139 additions and 66 deletions

View File

@ -2,13 +2,17 @@ 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)
@result_object = Api::V1::Projects::Sca::CreateProjectService.call(@project)
end
def show
@result_object = Api::V1::Projects::Tags::GetService.call(@project, params[:name], current_user&.gitea_token)
end
def policies
end
before_action :require_operate_above, only: [:destroy]
def destroy

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

@ -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?
@ -108,7 +108,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

@ -1,37 +1,5 @@
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
class Api::V1::Projects::Sca::BaseService < ApplicationService
private
def base_url
Rails.application.config_for(:configuration)["sca"]["domain"] + Rails.application.config_for(:configuration)["sca"]["base_url"]
end
@ -40,23 +8,15 @@ class Api::V1::Projects::Sca::CreateService < ApplicationService
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
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 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 = {} )
def sca_get_request(url, params = {} )
begin
uri = URI.parse(base_url + url)
uri.query = URI.encode_www_form(params) if params && !params.empty?
@ -80,7 +40,7 @@ class Api::V1::Projects::Sca::CreateService < ApplicationService
end
end
def post_json(url, params = {})
def sca_post_request(url, params = {})
begin
uri = URI.parse(base_url + url)
@ -104,4 +64,21 @@ class Api::V1::Projects::Sca::CreateService < ApplicationService
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,21 @@
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)
@project.update(sca_project_id:data[:json]["id"])
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,16 @@
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": base_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,5 @@
class AddScaProjectIdToProjects < ActiveRecord::Migration[5.2]
def change
add_column :projects, :sca_project_id, :integer
end
end