BigW Consortium Gitlab

triggers_controller.rb 2.02 KB
Newer Older
1
class Projects::TriggersController < Projects::ApplicationController
2
  before_action :authorize_admin_build!
3 4 5
  before_action :authorize_manage_trigger!, except: [:index, :create]
  before_action :authorize_admin_trigger!, only: [:edit, :update]
  before_action :trigger, only: [:take_ownership, :edit, :update, :destroy]
6 7 8

  layout 'project_settings'

9 10 11 12
  def index
    redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project)
  end

13
  def create
14
    @trigger = project.triggers.create(trigger_params.merge(owner: current_user))
15 16

    if @trigger.valid?
17
      flash[:notice] = 'Trigger was created successfully.'
18
    else
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      flash[:alert] = 'You could not create a new trigger.'
    end

    redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project)
  end

  def take_ownership
    if trigger.update(owner: current_user)
      flash[:notice] = 'Trigger was re-assigned.'
    else
      flash[:alert] = 'You could not take ownership of trigger.'
    end

    redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project)
  end

  def edit
  end

  def update
39
    if trigger.update(trigger_params)
40 41 42
      redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project), notice: 'Trigger was successfully updated.'
    else
      render action: "edit"
43 44 45 46
    end
  end

  def destroy
47 48 49 50 51
    if trigger.destroy
      flash[:notice] = "Trigger removed."
    else
      flash[:alert] = "Could not remove the trigger."
    end
52

53
    redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project)
54 55 56 57
  end

  private

58 59 60 61 62 63 64 65
  def authorize_manage_trigger!
    access_denied! unless can?(current_user, :manage_trigger, trigger)
  end

  def authorize_admin_trigger!
    access_denied! unless can?(current_user, :admin_trigger, trigger)
  end

66
  def trigger
67 68 69
    @trigger ||= project.triggers.find(params[:id]) || render_404
  end

70
  def trigger_params
71
    params.require(:trigger).permit(
Kamil Trzcinski committed
72 73
      :description,
      trigger_schedule_attributes: [:id, :active, :cron, :cron_timezone, :ref]
74
    )
75 76
  end
end