diff --git a/app/controllers/api/pm/pipelines_controller.rb b/app/controllers/api/pm/pipelines_controller.rb new file mode 100644 index 000000000..00a85f948 --- /dev/null +++ b/app/controllers/api/pm/pipelines_controller.rb @@ -0,0 +1,71 @@ +class Api::Pm::PipelinesController < Api::Pm::BaseController + include RepositoriesHelper + before_action :require_operate_above, except: [:upload_results, :run_results] + + def index + @owner = Owner.find_by(login: params[:owner_id].to_s) || Owner.find_by(id: params[:owner_id].to_s) + tip_exception('组织未找到') if @owner.blank? + project_ids = @owner.projects.ids + project_gpids = @owner.projects.pluck(:gpid) + action_runs = Gitea::ActionRun.where(owner_id: @owner.gitea_uid) + group_data = action_runs.where(status: [1,2]).group(:workflow_id, :status).count + pipelines = Action::Pipeline.where(project_id: project_ids).order(updated_at: :desc) + run_files = Gitea::ActionRun.where(owner_id: @owner.gitea_uid).group(:workflow_id) + db_files = pipelines.pluck(:file_name) + @run_result = [] + run_files.each do |file_info| + file = file_info.workflow_id + project = Project.find_by(gpid: file.repo_id) + next if project.blank? + unless db_files.include?(".gitea/workflows/#{file}") + pipeline = Action::Pipeline.new(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, + schedule: last_action_run.schedule_id > 0, + title: last_action_run.title, + index: last_action_run.index, + status: last_action_run.status, + started: last_action_run.started, + stopped: last_action_run.stopped, + length: last_action_run.stopped-last_action_run.started, + created: last_action_run.created, + updated: last_action_run.updated, + } : {} + + total = 0 + success = 0 + failure = 0 + group_data.each do |k,v| + total += v if k[0] == file + success += v if k[0] == file && k[1] == 1 + failure += v if k[0] == file && k[1] == 2 + end + @run_result << { + filename: ".gitea/workflows/#{file}", + total: total, + success: success, + failure: failure + }.merge(last_action_run_json) + end + Rails.logger.info("@run_result======#{@run_result}") + @disabled_workflows = Gitea::RepoUnit.where(repo_id: project_gpids, type: 10).where("config is not null") + @pipelines = Action::Pipeline.where(project_id: @project.id).order(updated_at: :desc) + @pipelines = paginate @pipelines + end + +end diff --git a/app/views/api/pm/pipelines/index.json.jbuilder b/app/views/api/pm/pipelines/index.json.jbuilder new file mode 100644 index 000000000..1f9201334 --- /dev/null +++ b/app/views/api/pm/pipelines/index.json.jbuilder @@ -0,0 +1,15 @@ +json.status 0 +json.message "success" + +json.pipelines @pipelines.each do |pipeline| + json.extract! pipeline, :id, :pipeline_name, :pipeline_status, :description, :file_name, :is_graphic_design, + :repo_name, :repo_identifier, :branch, :event, :sha, :disable, :json, :yaml, :created_at, :updated_at + repo_config = @disabled_workflows.select { |config| config.repo_id = pipeline.project.gpid } + if repo_config.present? + json.disable JSON.parse(repo_config.first.config)["DisabledWorkflows"].to_s.include?(pipeline.file_name.to_s.gsub(".gitea/workflows/", "")) + else + json.disable pipeline.disable + end + json.pipeline_type pipeline.pipeline_type + json.run_data @run_result.select { |result| result[:filename] == pipeline.file_name }.first +end diff --git a/config/routes/api.rb b/config/routes/api.rb index 629ea93f7..be80bac3f 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -51,6 +51,7 @@ defaults format: :json do end end resources :action_runs, only: [:index] + resources :pipelines, only: [:index] end namespace :v1 do