diff --git a/app/controllers/api/v1/projects/pipelines_controller.rb b/app/controllers/api/v1/projects/pipelines_controller.rb index 3be80d7a2..f6cf4d910 100644 --- a/app/controllers/api/v1/projects/pipelines_controller.rb +++ b/app/controllers/api/v1/projects/pipelines_controller.rb @@ -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 diff --git a/app/controllers/api/v1/projects/sca_controller.rb b/app/controllers/api/v1/projects/sca_controller.rb new file mode 100644 index 000000000..b5106dd71 --- /dev/null +++ b/app/controllers/api/v1/projects/sca_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/issue_tags_controller.rb b/app/controllers/issue_tags_controller.rb index da9f16f71..306e3deee 100644 --- a/app/controllers/issue_tags_controller.rb +++ b/app/controllers/issue_tags_controller.rb @@ -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 diff --git a/app/controllers/sync_forge_controller.rb b/app/controllers/sync_forge_controller.rb index 58256bf79..8c8d23ef3 100644 --- a/app/controllers/sync_forge_controller.rb +++ b/app/controllers/sync_forge_controller.rb @@ -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! diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 60cded236..7261c4df2 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -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 diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 870d5a439..d2d88f89b 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -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}" diff --git a/app/services/api/pm/issues/create_service.rb b/app/services/api/pm/issues/create_service.rb index 3003869ea..65f570612 100644 --- a/app/services/api/pm/issues/create_service.rb +++ b/app/services/api/pm/issues/create_service.rb @@ -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 diff --git a/app/services/api/v1/issues/create_service.rb b/app/services/api/v1/issues/create_service.rb index b0056aced..3fe7625c9 100644 --- a/app/services/api/v1/issues/create_service.rb +++ b/app/services/api/v1/issues/create_service.rb @@ -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 diff --git a/app/services/api/v1/issues/journals/create_service.rb b/app/services/api/v1/issues/journals/create_service.rb index c04011610..abf7d1924 100644 --- a/app/services/api/v1/issues/journals/create_service.rb +++ b/app/services/api/v1/issues/journals/create_service.rb @@ -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 diff --git a/app/services/api/v1/journals/create_service.rb b/app/services/api/v1/journals/create_service.rb index 591a3f11a..d8a644c44 100644 --- a/app/services/api/v1/journals/create_service.rb +++ b/app/services/api/v1/journals/create_service.rb @@ -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 diff --git a/app/services/api/v1/projects/sca/base_service.rb b/app/services/api/v1/projects/sca/base_service.rb new file mode 100644 index 000000000..53428141a --- /dev/null +++ b/app/services/api/v1/projects/sca/base_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/projects/sca/create_project_service.rb b/app/services/api/v1/projects/sca/create_project_service.rb new file mode 100644 index 000000000..7c866903e --- /dev/null +++ b/app/services/api/v1/projects/sca/create_project_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/projects/sca/create_report_service.rb b/app/services/api/v1/projects/sca/create_report_service.rb new file mode 100644 index 000000000..e38ec1450 --- /dev/null +++ b/app/services/api/v1/projects/sca/create_report_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/projects/sca/list_branch_service.rb b/app/services/api/v1/projects/sca/list_branch_service.rb new file mode 100644 index 000000000..8fad772ef --- /dev/null +++ b/app/services/api/v1/projects/sca/list_branch_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/projects/sca/policies_service.rb b/app/services/api/v1/projects/sca/policies_service.rb new file mode 100644 index 000000000..541a80ddc --- /dev/null +++ b/app/services/api/v1/projects/sca/policies_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/api/v1/projects/sca/show_project_service.rb b/app/services/api/v1/projects/sca/show_project_service.rb new file mode 100644 index 000000000..244c6301a --- /dev/null +++ b/app/services/api/v1/projects/sca/show_project_service.rb @@ -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 + diff --git a/app/services/gitea/hooks/create_service.rb b/app/services/gitea/hooks/create_service.rb index 8b8d7f2c3..ebf8ecdc8 100644 --- a/app/services/gitea/hooks/create_service.rb +++ b/app/services/gitea/hooks/create_service.rb @@ -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 diff --git a/app/services/gitea/oauth2/create_service.rb b/app/services/gitea/oauth2/create_service.rb index 1fa93b7a8..459b8c635 100644 --- a/app/services/gitea/oauth2/create_service.rb +++ b/app/services/gitea/oauth2/create_service.rb @@ -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", diff --git a/app/services/gitea/pull_request/create_service.rb b/app/services/gitea/pull_request/create_service.rb index a911feaea..85dc585e8 100644 --- a/app/services/gitea/pull_request/create_service.rb +++ b/app/services/gitea/pull_request/create_service.rb @@ -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 diff --git a/app/services/repositories/create_service.rb b/app/services/repositories/create_service.rb index 38b2e2cd9..193ac6067 100644 --- a/app/services/repositories/create_service.rb +++ b/app/services/repositories/create_service.rb @@ -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 diff --git a/app/views/api/v1/projects/sca/create.json.jbuilder b/app/views/api/v1/projects/sca/create.json.jbuilder new file mode 100644 index 000000000..86a3f08fd --- /dev/null +++ b/app/views/api/v1/projects/sca/create.json.jbuilder @@ -0,0 +1 @@ +json.data @result \ No newline at end of file diff --git a/app/views/api/v1/projects/sca/index.json.jbuilder b/app/views/api/v1/projects/sca/index.json.jbuilder new file mode 100644 index 000000000..86a3f08fd --- /dev/null +++ b/app/views/api/v1/projects/sca/index.json.jbuilder @@ -0,0 +1 @@ +json.data @result \ No newline at end of file diff --git a/app/views/projects/_project_detail.json.jbuilder b/app/views/projects/_project_detail.json.jbuilder index 5c702ad7b..b0962db25 100644 --- a/app/views/projects/_project_detail.json.jbuilder +++ b/app/views/projects/_project_detail.json.jbuilder @@ -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 diff --git a/app/views/projects/show.json.jbuilder b/app/views/projects/show.json.jbuilder index 505c9ae4e..aa66808b4 100644 --- a/app/views/projects/show.json.jbuilder +++ b/app/views/projects/show.json.jbuilder @@ -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 diff --git a/app/views/repositories/detail.json.jbuilder b/app/views/repositories/detail.json.jbuilder index 8508be91c..ffe57c249 100644 --- a/app/views/repositories/detail.json.jbuilder +++ b/app/views/repositories/detail.json.jbuilder @@ -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) diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index c4f3e7c68..61ee6d3e2 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -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 diff --git a/config/routes/api.rb b/config/routes/api.rb index d8f6c98c2..66296d18b 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -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 diff --git a/db/migrate/20250904021726_add_sca_project_id_to_projects.rb b/db/migrate/20250904021726_add_sca_project_id_to_projects.rb new file mode 100644 index 000000000..2a97beae5 --- /dev/null +++ b/db/migrate/20250904021726_add_sca_project_id_to_projects.rb @@ -0,0 +1,5 @@ +class AddScaProjectIdToProjects < ActiveRecord::Migration[5.2] + def change + add_column :projects, :sca_project_id, :integer + end +end