From d0ef36e273b77c9d4b9f3f25f65d207dd513156f Mon Sep 17 00:00:00 2001 From: xxq250 Date: Wed, 5 Nov 2025 11:26:21 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E4=BB=93=E5=BA=93=E4=B8=AD=E5=B7=B2?= =?UTF-8?q?=E6=9C=89=E6=B5=81=E6=B0=B4=E7=BA=BF=E6=96=87=E4=BB=B6=E4=B8=8D?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=8A=A0=E5=85=A5=E5=88=B0=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/pm/pipelines_controller.rb | 32 +++++++------- .../api/v1/projects/pipelines_controller.rb | 44 ++++++++++++------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/app/controllers/api/pm/pipelines_controller.rb b/app/controllers/api/pm/pipelines_controller.rb index 1ad0fe085..3f7f752e3 100644 --- a/app/controllers/api/pm/pipelines_controller.rb +++ b/app/controllers/api/pm/pipelines_controller.rb @@ -19,21 +19,23 @@ class Api::Pm::PipelinesController < Api::Pm::BaseController file = file_info.workflow_id project = Project.find_by(gpid: file_info.repo_id) next if project.blank? - unless db_files.include?(".gitea/workflows/#{file}") - pipeline = Action::Pipeline.find_or_initialize_by(pipeline_name: file.to_s.gsub(".yml", "").gsub(".yaml", ""), - file_name: ".gitea/workflows/#{file}", - branch: project.default_branch, - disable: false, - project_id: project.id) - interactor = Repositories::EntriesInteractor.call(@owner, project.identifier, ".gitea/workflows/#{file}", ref: project.default_branch) - if interactor.success? - pipeline.yaml = decode64_content(interactor.result, @owner, project.repository, project.default_branch, nil) - end - pipeline.user_id = current_user.id - pipeline.save - # 导入的流水线统一先禁用 - $gitea_hat_client.post_repos_actions_disable(project&.owner&.login, project&.identifier, {query: {workflow: file}}) rescue nil - end + # unless db_files.include?(".gitea/workflows/#{file}") + # pipeline = Action::Pipeline.find_or_initialize_by(pipeline_name: file.to_s.gsub(".yml", "").gsub(".yaml", ""), + # file_name: ".gitea/workflows/#{file}", + # branch: project.default_branch, + # disable: false, + # project_id: project.id) + # interactor = Repositories::EntriesInteractor.call(@owner, project.identifier, ".gitea/workflows/#{file}", ref: project.default_branch) + # if interactor.success? + # pipeline.yaml = decode64_content(interactor.result, @owner, project.repository, project.default_branch, nil) + # file = interactor.result + # pipeline.sha = file['content']['sha'] + # end + # pipeline.user_id = current_user.id + # pipeline.save + # # 导入的流水线统一先禁用 + # $gitea_hat_client.post_repos_actions_disable(project&.owner&.login, project&.identifier, {query: {workflow: file}}) rescue nil + # end last_action_run = action_runs.where(repo_id: project.gpid).where(workflow_id: file).order(updated: :desc).first last_action_run_json = last_action_run.present? ? { id: last_action_run.id, diff --git a/app/controllers/api/v1/projects/pipelines_controller.rb b/app/controllers/api/v1/projects/pipelines_controller.rb index d42a7e7de..558da2a9f 100644 --- a/app/controllers/api/v1/projects/pipelines_controller.rb +++ b/app/controllers/api/v1/projects/pipelines_controller.rb @@ -11,22 +11,22 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController db_files = pipelines.pluck(:file_name) @run_result = [] @files.map { |i| i['name'] }.each do |file| - unless db_files.include?(".gitea/workflows/#{file}") - pipeline = Action::Pipeline.find_or_initialize_by(pipeline_name: file.to_s.gsub(".yml", "").gsub(".yaml", ""), - file_name: ".gitea/workflows/#{file}", - branch: @project.default_branch, - is_graphic_design: false, - disable: false, - project_id: @project.id) - interactor = Repositories::EntriesInteractor.call(@owner, @project.identifier, ".gitea/workflows/#{file}", ref: @project.default_branch) - if interactor.success? - pipeline.yaml = decode64_content(interactor.result, @owner, @project.repository, @project.default_branch, nil) - end - pipeline.user_id = current_user.id - pipeline.save - # 导入的流水线统一先禁用 - $gitea_hat_client.post_repos_actions_disable(@project&.owner&.login, @project&.identifier, {query: {workflow: file}}) rescue nil - end + # unless db_files.include?(".gitea/workflows/#{file}") + # pipeline = Action::Pipeline.find_or_initialize_by(pipeline_name: file.to_s.gsub(".yml", "").gsub(".yaml", ""), + # file_name: ".gitea/workflows/#{file}", + # branch: @project.default_branch, + # is_graphic_design: false, + # disable: false, + # project_id: @project.id) + # interactor = Repositories::EntriesInteractor.call(@owner, @project.identifier, ".gitea/workflows/#{file}", ref: @project.default_branch) + # if interactor.success? + # pipeline.yaml = decode64_content(interactor.result, @owner, @project.repository, @project.default_branch, nil) + # end + # pipeline.user_id = current_user.id + # pipeline.save + # # 导入的流水线统一先禁用 + # $gitea_hat_client.post_repos_actions_disable(@project&.owner&.login, @project&.identifier, {query: {workflow: file}}) rescue nil + # end last_action_run = @action_runs.where(workflow_id: file).order(updated: :desc).first last_action_run_json = last_action_run.present? ? { id: last_action_run.id, @@ -80,12 +80,20 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController tip_exception("已经存在#{params[:pipeline_name]}流水线!") if has_pipeline.present? if @pipeline.yaml.present? sha = get_pipeline_file_sha(@pipeline.file_name, @pipeline.branch) - if sha.present? + new_file_sha = get_pipeline_file_sha(".gitea/workflows/#{params[:pipeline_name]}.yml", @pipeline.branch) + # 重命名的新文件存在时,直接修改新文件new_file_sha, 同时删除老文件 + if sha.present? && new_file_sha.present? + interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("update", params[:pipeline_name]).merge(sha: new_file_sha, from_path: ".gitea/workflows/#{params[:pipeline_name]}.yml")) + tip_exception(interactor.error) unless interactor.success? + del_interactor = Gitea::DeleteFileInteractor.call(current_user.gitea_token, @owner.login, del_content_params(sha).merge(identifier: @project.identifier)) + tip_exception(del_interactor.error) unless del_interactor.success? + elsif sha.present? && new_file_sha.blank? 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 end + @pipeline.user_id = current_user.id @pipeline.pipeline_name = params[:pipeline_name] @pipeline.file_name = ".gitea/workflows/#{@pipeline.pipeline_name}.yml" @pipeline.branch = params[:branch] || @project.default_branch @@ -113,7 +121,9 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController interactor = sha.present? ? Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("update").merge(sha: sha)) : Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("create")) tip_exception(interactor.error) unless interactor.success? file = interactor.result + @pipeline.user_id = current_user.id if @pipeline.user_id.blank? @pipeline.pipeline_type = @pipeline.json.present? ? 2 : 1 + @pipeline.sha = sha.present? ? sha : file['content']['sha'] @pipeline.save render_ok({ pipeline_yaml: pipeline_yaml, pipeline_name: @pipeline.pipeline_name, file_name: @pipeline.file_name, sha: sha.present? ? sha : file['content']['sha'] }) end