From cb9f2678877c90e8b37229c490f8506a244ef38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=B1=E5=91=B1=E5=91=B1?= Date: Wed, 26 Nov 2025 14:44:48 +0800 Subject: [PATCH] add render_client with huawei code_check --- .../api/v1/projects/code_checks_controller.rb | 100 +++++++++++------- app/services/huawei_cloud/code_check.rb | 10 +- 2 files changed, 71 insertions(+), 39 deletions(-) diff --git a/app/controllers/api/v1/projects/code_checks_controller.rb b/app/controllers/api/v1/projects/code_checks_controller.rb index cb31558a2..c63e5eaa2 100644 --- a/app/controllers/api/v1/projects/code_checks_controller.rb +++ b/app/controllers/api/v1/projects/code_checks_controller.rb @@ -10,96 +10,111 @@ class Api::V1::Projects::CodeChecksController < Api::V1::BaseController render_ok({check:true}) end def create - @result = @client.create_task(params["branch"]) - render :index + render_client do + @result = @client.create_task(params["branch"]) + end end def run_task - @result = @client.run_task(params["branch"],params["task_id"]) - render :index + render_client do + @result = @client.run_task(params["branch"],params["task_id"]) + end end def query_task - @result = @client.query_task(params["task_id"]) + render_client do + @result = @client.query_task(params["task_id"]) + end render :index end #----- 缺陷管理 ------ def defects_summary allowed_query = %w[job_id merge_id] query_params, _ = filter_params(allowed_query_keys: allowed_query) - @result = @client.defects_summary(query_params) - render :index + render_client do + @result = @client.defects_summary(query_params) + end end def metrics_summary - @result = @client.metrics_summary - render :index + render_client do + @result = @client.metrics_summary + end end def defects_detail allowed_query = %w[offset limit status_ids severity delay_status] query_params, _ = filter_params(allowed_query_keys: allowed_query) - Rails.logger.info "defects_detail controller " - Rails.logger.info query_params - @result = @client.defects_detail(query_params) - render :index + + render_client do + @result = @client.defects_detail(query_params) + end end def defects_statistic - @result = @client.defects_statistic - render :index + render_client do + @result = @client.defects_statistic + end end def defect_status #put allowed_body = %w[defect_id defect_status] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.defect_status(body_params) - render :index + render_client do + @result = @client.defect_status(body_params) + end end #------ 代码问题 ---- def issue_status #post allowed_body = %w[mergeId jobId status comment mergeKey operator] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.issue_status(body_params) - render :index + render_client do + @result = @client.issue_status(body_params) + end end def issue_list_by_filter #post allowed_body = %w[mergeId jobId pageSize page languages ruleIds authors isNew statusIds severities delayStatus fileNames userTags cwes] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.issue_list_by_filter(body_params) - render :index + render_client do + @result = @client.issue_list_by_filter(body_params) + end end def issue_filter #post allowed_body = %w[mergeId jobId facets languages ruleIds authors isNew statusIds severities delayStatus fileNames userTags cwes] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.issue_filter(body_params) - render :index + render_client do + @result = @client.issue_filter(body_params) + end end def defect_metric_trend #get allowed_query = %w[start_date end_date dimension] query_params, _ = filter_params(allowed_query_keys: allowed_query) - @result = @client.defect_metric_trend(query_params) - render :index + render_client do + @result = @client.defect_metric_trend(query_params) + end end def criterion_rule allowed_query = %w[criterion_rule_id] query_params, _ = filter_params(allowed_query_keys: allowed_query) - @result = @client.criterion_rule(query_params) - render :index + render_client do + @result = @client.criterion_rule(query_params) + end end def generate_pdf - @result = @client.generate_pdf - render :index + render_client do + @result = @client.generate_pdf + end end def query_generate_pdf - @result = @client.query_generate_pdf(params[:async_job_id]) - render :index + render_client do + @result = @client.query_generate_pdf(params[:async_job_id]) + end end def download_pdf @@ -117,22 +132,25 @@ class Api::V1::Projects::CodeChecksController < Api::V1::BaseController def measure_total allowed_body = %w[jobId search] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.measure_total(body_params) - render :index + render_client do + @result = @client.measure_total(body_params) + end end def related_duplicate_blocks allowed_query = %w[job_id file_path block_id duplication_type] query_params, _ = filter_params(allowed_query_keys: allowed_query) - @result = @client.related_duplicate_blocks(query_params) - render :index + render_client do + @result = @client.related_duplicate_blocks(query_params) + end end def measure_list allowed_body = %w[filterType page pageSize] _, body_params = filter_params(allowed_body_keys: allowed_body) - @result = @client.measure_list(body_params) - render :index + render_client do + @result = @client.measure_list(body_params) + end end private @@ -143,4 +161,12 @@ class Api::V1::Projects::CodeChecksController < Api::V1::BaseController [query_params, body_params] end + def render_client + @code, @result = yield # 执行传入的 block + if @code == "200" + render :index + else + render_error(@result["error_msg"] + @result["error_code"]) + end + end end \ No newline at end of file diff --git a/app/services/huawei_cloud/code_check.rb b/app/services/huawei_cloud/code_check.rb index f1fd2c53f..0d2432469 100644 --- a/app/services/huawei_cloud/code_check.rb +++ b/app/services/huawei_cloud/code_check.rb @@ -231,9 +231,15 @@ module HuaweiCloud end response = http.request(request) begin - JSON.parse(response.body) + result = JSON.parse(response.body) + response_code = if result["error_code"].blank? + response.code + else + "400" + end + [response_code, result] rescue JSON::ParserError, TypeError - response.body + [response.code, response.body] end rescue => e -- 2.34.1