summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-04-07 04:02:19 +0900
committerKamil Trzcinski <ayufan@ayufan.eu>2017-04-07 16:17:27 +0200
commit2548c155eed13c806951a6303c85dbb0c5772ca3 (patch)
tree0df8ddb462937a6cb4043b2412442a099b2513a9
parent7ccaa27f899dfbc96b23d98ee415b5daf80dd336 (diff)
downloadgitlab-ce-2548c155eed13c806951a6303c85dbb0c5772ca3.tar.gz
Add form for scheduled trigger
-rw-r--r--app/controllers/projects/settings/ci_cd_controller.rb1
-rw-r--r--app/controllers/projects/triggers_controller.rb10
-rw-r--r--app/models/ci/trigger.rb13
-rw-r--r--app/models/ci/trigger_schedule.rb8
-rw-r--r--app/views/projects/triggers/_form.html.haml23
-rw-r--r--app/views/projects/triggers/_index.html.haml2
-rw-r--r--app/views/projects/triggers/_trigger.html.haml6
7 files changed, 59 insertions, 4 deletions
diff --git a/app/controllers/projects/settings/ci_cd_controller.rb b/app/controllers/projects/settings/ci_cd_controller.rb
index 6f009d61950..0ae4b4d3945 100644
--- a/app/controllers/projects/settings/ci_cd_controller.rb
+++ b/app/controllers/projects/settings/ci_cd_controller.rb
@@ -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
diff --git a/app/controllers/projects/triggers_controller.rb b/app/controllers/projects/triggers_controller.rb
index c47198c5eb6..5ceeeb1c454 100644
--- a/app/controllers/projects/triggers_controller.rb
+++ b/app/controllers/projects/triggers_controller.rb
@@ -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
diff --git a/app/models/ci/trigger.rb b/app/models/ci/trigger.rb
index 0a89f3e0640..225b6253616 100644
--- a/app/models/ci/trigger.rb
+++ b/app/models/ci/trigger.rb
@@ -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
diff --git a/app/models/ci/trigger_schedule.rb b/app/models/ci/trigger_schedule.rb
index 10ea381ee31..d8a2770ea61 100644
--- a/app/models/ci/trigger_schedule.rb
+++ b/app/models/ci/trigger_schedule.rb
@@ -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
diff --git a/app/views/projects/triggers/_form.html.haml b/app/views/projects/triggers/_form.html.haml
index 5f708b3a2ed..169f6c43225 100644
--- a/app/views/projects/triggers/_form.html.haml
+++ b/app/views/projects/triggers/_form.html.haml
@@ -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"
diff --git a/app/views/projects/triggers/_index.html.haml b/app/views/projects/triggers/_index.html.haml
index cc74e50a5e3..84e945ee0df 100644
--- a/app/views/projects/triggers/_index.html.haml
+++ b/app/views/projects/triggers/_index.html.haml
@@ -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
diff --git a/app/views/projects/triggers/_trigger.html.haml b/app/views/projects/triggers/_trigger.html.haml
index 9b5f63ae81a..87d7c741bd6 100644
--- a/app/views/projects/triggers/_trigger.html.haml
+++ b/app/views/projects/triggers/_trigger.html.haml
@@ -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?"