summaryrefslogtreecommitdiff
path: root/app/controllers/projects/triggers_controller.rb
blob: e86adddd77fc18dbf2f070d950c7c2802931155f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Projects::TriggersController < Projects::ApplicationController
  before_action :authorize_admin_build!
  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]

  layout 'project_settings'

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

  def create
    @trigger = project.triggers.create(trigger_params.merge(owner: current_user))

    if @trigger.valid?
      flash[:notice] = 'Trigger was created successfully.'
    else
      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
    if trigger.update(trigger_params)
      redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project), notice: 'Trigger was successfully updated.'
    else
      render action: "edit"
    end
  end

  def destroy
    if trigger.destroy
      flash[:notice] = "Trigger removed."
    else
      flash[:alert] = "Could not remove the trigger."
    end

    redirect_to namespace_project_settings_ci_cd_path(@project.namespace, @project), status: 302
  end

  private

  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

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

  def trigger_params
    params.require(:trigger).permit(
      :description,
      trigger_schedule_attributes: [:id, :active, :cron, :cron_timezone, :ref]
    )
  end
end