BigW Consortium Gitlab

triggers_controller.rb 1.81 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
  def index
10
    redirect_to project_settings_ci_cd_path(@project)
11 12
  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
      flash[:alert] = 'You could not create a new trigger.'
    end

22
    redirect_to project_settings_ci_cd_path(@project)
23 24 25 26 27 28 29 30 31
  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

32
    redirect_to project_settings_ci_cd_path(@project)
33 34 35 36 37 38
  end

  def edit
  end

  def update
39
    if trigger.update(trigger_params)
40
      redirect_to project_settings_ci_cd_path(@project), notice: 'Trigger was successfully updated.'
41 42
    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 project_settings_ci_cd_path(@project), status: 302
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(
72
      :description
73
    )
74 75
  end
end