runners_controller.rb 2.04 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Admin::RunnersController < Admin::ApplicationController
4
  before_action :runner, except: [:index, :tag_list]
5 6

  def index
7 8
    finder = Admin::RunnersFinder.new(params: params)
    @runners = finder.execute
9
    @active_runners_count = Ci::Runner.online.count
10
    @sort = finder.sort_key
11 12 13
  end

  def show
14
    assign_builds_and_projects
15 16 17
  end

  def update
18
    if Ci::UpdateRunnerService.new(@runner).update(runner_params)
19 20 21 22 23 24 25
      respond_to do |format|
        format.js
        format.html { redirect_to admin_runner_path(@runner) }
      end
    else
      assign_builds_and_projects
      render 'show'
26 27 28 29 30 31
    end
  end

  def destroy
    @runner.destroy

Lin Jen-Shin's avatar
Lin Jen-Shin committed
32
    redirect_to admin_runners_path, status: :found
33 34 35
  end

  def resume
36
    if Ci::UpdateRunnerService.new(@runner).update(active: true)
37 38 39 40 41 42 43
      redirect_to admin_runners_path, notice: 'Runner was successfully updated.'
    else
      redirect_to admin_runners_path, alert: 'Runner was not updated.'
    end
  end

  def pause
44
    if Ci::UpdateRunnerService.new(@runner).update(active: false)
45 46 47 48 49 50
      redirect_to admin_runners_path, notice: 'Runner was successfully updated.'
    else
      redirect_to admin_runners_path, alert: 'Runner was not updated.'
    end
  end

51
  def tag_list
52 53 54
    tags = Autocomplete::ActsAsTaggableOn::TagsFinder.new(taggable_type: Ci::Runner, params: params).execute

    render json: ActsAsTaggableOn::TagSerializer.new.represent(tags)
55 56
  end

57 58 59 60 61 62 63
  private

  def runner
    @runner ||= Ci::Runner.find(params[:id])
  end

  def runner_params
64
    params.require(:runner).permit(Ci::Runner::FORM_EDITABLE)
65
  end
66

67
  # rubocop: disable CodeReuse/ActiveRecord
68
  def assign_builds_and_projects
69 70 71 72 73 74 75
    @builds = runner.builds.order('id DESC').first(30)
    @projects =
      if params[:search].present?
        ::Project.search(params[:search])
      else
        Project.all
      end
76

77 78 79
    @projects = @projects.where.not(id: runner.projects.select(:id)) if runner.projects.any?
    @projects = @projects.page(params[:page]).per(30)
  end
80
  # rubocop: enable CodeReuse/ActiveRecord
81
end