Commit 2548c155 authored by Shinya Maeda's avatar Shinya Maeda Committed by Kamil Trzcinski

Add form for scheduled trigger

parent 7ccaa27f
......@@ -27,6 +27,7 @@ module Projects
def define_triggers_variables
@triggers = @project.triggers
@trigger = Ci::Trigger.new
@trigger.build_trigger_schedule
end
def define_badges_variables
......
......@@ -68,10 +68,16 @@ class Projects::TriggersController < Projects::ApplicationController
end
def create_params
params.require(:trigger).permit(:description)
params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on,
trigger_schedule_attributes: [ :cron, :cron_timezone ]
)
end
def update_params
params.require(:trigger).permit(:description)
params.require(:trigger).permit(
:description, :ref, :trigger_schedule_on,
trigger_schedule_attributes: [ :cron, :cron_timezone ]
)
end
end
......@@ -14,8 +14,21 @@ module Ci
before_validation :set_default_values
accepts_nested_attributes_for :trigger_schedule
attr_accessor :trigger_schedule_on
def set_default_values
self.token = SecureRandom.hex(15) if self.token.blank?
if trigger_schedule_on.present?
if trigger_schedule_on.to_i == 1
self.trigger_schedule.project = project
self.trigger_schedule.trigger = self
else
self.trigger_schedule = nil
end
end
end
def last_trigger_request
......
......@@ -8,7 +8,7 @@ module Ci
belongs_to :project
belongs_to :trigger
delegate :ref, to: :trigger
delegate :ref, to: :trigger, allow_nil: true
validates :trigger, presence: { unless: :importing? }
validates :cron, cron: true, presence: { unless: :importing? }
......@@ -26,5 +26,11 @@ module Ci
rescue ActiveRecord::RecordInvalid
update_attribute(:next_run_at, nil) # update without validation
end
def real_next_run(worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'],
worker_time_zone: Time.zone.name)
Gitlab::Ci::CronParser.new(worker_cron, worker_time_zone)
.next_time_from(next_run_at)
end
end
end
......@@ -6,6 +6,27 @@
%label.label-light Token
%p.form-control-static= @trigger.token
.form-group
= f.label :key, "Description", class: "label-light"
= f.label :key, "Description (For extenral trigger and scheduled trigger)", class: "label-light"
= f.text_field :description, class: "form-control", required: true, title: 'Trigger description is required.', placeholder: "Trigger description"
= f.fields_for :trigger_schedule do |schedule_fields|
.form-group
= schedule_fields.label :cron, "Cron (For scheduled trigger)", class: "label-light"
= schedule_fields.text_field :cron, class: "form-control", title: 'Trigger Schedule cron is required.', placeholder: "0 1 * * *"
.form-group
= schedule_fields.label :cron_timezone, "Cron timezone (For scheduled trigger)", class: "label-light"
= schedule_fields.text_field :cron_timezone, class: "form-control", title: 'Trigger Schedule cron_timezone is required.', placeholder: "UTC"
.form-group
= f.label :ref, "Ref (For scheduled trigger)", class: "label-light"
= f.text_field :ref, class: "form-control", title: 'Trigger Schedule Ref is required.', placeholder: "master"
- if action_name == 'edit'
= f.hidden_field :trigger_schedule_on, :value => @trigger.trigger_schedule.present? ? 1 : 0
- else
.form-group
.checkbox
= f.label :trigger_schedule_on do
= f.check_box :trigger_schedule_on
%strong Register as scheduled trigger
.help-block
If checked, this trigger will be executed periodically according to `cron`, `cron_timezone` and `ref`
= link_to icon('question-circle'), help_page_path('user/project/pipelines/settings', anchor: 'visibility-of-pipelines')
= f.submit btn_text, class: "btn btn-save"
......@@ -22,6 +22,8 @@
%th
%strong Last used
%th
%strong Next run at
%th
= render partial: 'projects/triggers/trigger', collection: @triggers, as: :trigger
- else
%p.settings-message.text-center.append-bottom-default
......
......@@ -29,6 +29,12 @@
- else
Never
%td
- if trigger.trigger_schedule.present?
= trigger.trigger_schedule.real_next_run
- else
N/A (External trigger)
%td.text-right.trigger-actions
- take_ownership_confirmation = "By taking ownership you will bind this trigger to your user account. With this the trigger will have access to all your projects as if it was you. Are you sure?"
- revoke_trigger_confirmation = "By revoking a trigger you will break any processes making use of it. Are you sure?"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment