From ffa4c6e870009516d22e09e23b74d549e56c383b Mon Sep 17 00:00:00 2001 From: xxq250 Date: Wed, 19 Mar 2025 11:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E6=B0=B4=E7=BA=BF=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=94=AF=E6=8C=81=E9=87=8D=E5=90=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/projects/pipelines_controller.rb | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/projects/pipelines_controller.rb b/app/controllers/api/v1/projects/pipelines_controller.rb index c123bb3fa..af83fb15e 100644 --- a/app/controllers/api/v1/projects/pipelines_controller.rb +++ b/app/controllers/api/v1/projects/pipelines_controller.rb @@ -71,9 +71,19 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController end def create - size = Action::Pipeline.where(pipeline_name: params[:pipeline_name], project_id: @project.id).size - tip_exception("已经存在#{params[:pipeline_name]}流水线!") if size > 0 - @pipeline = Action::Pipeline.new(pipeline_name: params[:pipeline_name], project_id: @project.id) + @pipeline = params[:id].present? ? Action::Pipeline.find(params[:id]) : Action::Pipeline.find_or_initialize_by(pipeline_name: params[:pipeline_name], project_id: @project.id) + if @pipeline.pipeline_name != params[:pipeline_name] + has_pipeline = Action::Pipeline.where(pipeline_name: params[:pipeline_name], project_id: @project.id).where.not(id: @pipeline.id) + 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? + interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params("update", params[:pipeline_name]).merge(sha: sha, from_path: "#{@pipeline.pipeline_name}")) + tip_exception(interactor.error) unless interactor.success? + end + end + end + @pipeline.pipeline_name = params[:pipeline_name] @pipeline.file_name = ".gitea/workflows/#{@pipeline.pipeline_name}.yml" @pipeline.branch = params[:branch] || @project.default_branch @pipeline.is_graphic_design = params[:pipeline_type] == 2 ? true : false @@ -93,6 +103,7 @@ 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.pipeline_type = @pipeline.json.present? ? 2 : 1 @pipeline.save render_ok({ pipeline_yaml: pipeline_yaml, pipeline_name: params[:pipeline_name], file_name: @pipeline.file_name, sha: sha.present? ? sha : file['content']['sha'] }) end @@ -203,9 +214,9 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController end end - def content_params(opt) + def content_params(opt, rename_file=nil) { - filepath: ".gitea/workflows/#{@pipeline.pipeline_name}.yml", + filepath: ".gitea/workflows/#{rename_file.present? ? rename_file : @pipeline.pipeline_name}.yml", branch: @pipeline.branch, new_branch: @pipeline.branch, content: opt == "create" ? Base64.encode64(@pipeline.yaml).gsub(/\n/, '') : @pipeline.yaml,