diff --git a/app/controllers/api/pm/action_runs_controller.rb b/app/controllers/api/pm/action_runs_controller.rb new file mode 100644 index 000000000..0e69d56a9 --- /dev/null +++ b/app/controllers/api/pm/action_runs_controller.rb @@ -0,0 +1,48 @@ +class Api::Pm::ActionRunsController < Api::Pm::BaseController + before_action :require_login + + def index + render_error('请输入workflows') if params[:workflows].blank? + @owner = Owner.find_by(login: params[:owner_id].to_s) || Owner.find_by(id: params[:owner_id].to_s) + render_error('组织未找到') if @owner.blank? + action_runs = Gitea::ActionRun.where(owner_id: @owner.gitea_uid) + group_data = action_runs.where(workflow_id: params[:workflows].to_s.split(",")).group(:workflow_id, :status).count + @result = [] + params[:workflows].to_s.split(",").each do |file| + 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 + + @result << { + name: file, + last_action_run: last_action_run_json, + history: { + total: total, + success: success, + failure: failure + } + } + end + render :json => @result + end + +end \ No newline at end of file diff --git a/app/controllers/api/v1/projects/actions/actions_controller.rb b/app/controllers/api/v1/projects/actions/actions_controller.rb index 5a34847e2..1e9177144 100644 --- a/app/controllers/api/v1/projects/actions/actions_controller.rb +++ b/app/controllers/api/v1/projects/actions/actions_controller.rb @@ -3,8 +3,8 @@ class Api::V1::Projects::Actions::ActionsController < Api::V1::Projects::Actions def new_index @files = $gitea_client.get_repos_contents_by_owner_repo_filepath(@project&.owner&.login, @project&.identifier, ".gitea/workflows") puts @files - @action_runs = Gitea::ActionRun.where(repo_id: @project.gpid, status: [1,2]) - group_data = @action_runs.group(:workflow_id, :status).count + @action_runs = Gitea::ActionRun.where(repo_id: @project.gpid) + group_data = @action_runs.where(status: [1,2]).group(:workflow_id, :status).count @result = [] @files.map{|i|i['name']}.each do |file| last_action_run = @action_runs.where(workflow_id: file).order(updated: :desc).first diff --git a/config/routes/api.rb b/config/routes/api.rb index ec8031fd8..629ea93f7 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -50,6 +50,7 @@ defaults format: :json do get :polyline end end + resources :action_runs, only: [:index] end namespace :v1 do