fixed 仓库中已有流水线文件不读取加入到列表

This commit is contained in:
xxq250 2025-11-05 11:26:21 +08:00
parent e3dd23da3f
commit d0ef36e273
2 changed files with 44 additions and 32 deletions

View File

@ -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,

View File

@ -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