-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure period of time during which continuous delivery is active.
- Loading branch information
1 parent
c494be6
commit f3dd257
Showing
15 changed files
with
457 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/assets/javascripts/shipit/continuous_delivery_schedule.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
$(document) | ||
.on "click", ".continuous-delivery-schedule [data-action='copy-to-all']", (event) -> | ||
form = event.target.closest("form"); | ||
|
||
mondayStart = form.elements.namedItem("continuous_delivery_schedule[monday_start]").value | ||
mondayEnd = form.elements.namedItem("continuous_delivery_schedule[monday_end]").value | ||
|
||
Array.from(form.elements).forEach (formElement) -> | ||
return unless formElement.type == "time" | ||
|
||
if formElement.name.endsWith("_start]") | ||
formElement.value = mondayStart | ||
|
||
if formElement.name.endsWith("_end]") | ||
formElement.value = mondayEnd |
42 changes: 42 additions & 0 deletions
42
app/controllers/shipit/continuous_delivery_schedules_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shipit | ||
class ContinuousDeliverySchedulesController < ShipitController | ||
before_action :load_stack | ||
|
||
def show | ||
@continuous_delivery_schedule = @stack.continuous_delivery_schedule || @stack.build_continuous_delivery_schedule | ||
end | ||
|
||
def update | ||
@continuous_delivery_schedule = @stack.continuous_delivery_schedule || @stack.build_continuous_delivery_schedule | ||
@continuous_delivery_schedule.assign_attributes(continuous_delivery_schedule_params) | ||
|
||
if @continuous_delivery_schedule.save | ||
flash[:success] = "Successfully updated" | ||
redirect_to(stack_continuous_delivery_schedule_path) | ||
else | ||
flash.now[:warning] = "Check form for errors" | ||
render(:show, status: :unprocessable_entity) | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_stack | ||
@stack = Stack.from_param!(params[:id]) | ||
end | ||
|
||
def continuous_delivery_schedule_params | ||
params.require(:continuous_delivery_schedule).permit( | ||
*Shipit::ContinuousDeliverySchedule::DAYS.flat_map do |day| | ||
[ | ||
"#{day}_start", | ||
"#{day}_end", | ||
"#{day}_enabled", | ||
] | ||
end | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shipit | ||
class ContinuousDeliverySchedule < Record | ||
belongs_to(:stack) | ||
|
||
DAYS = %w[sunday monday tuesday wednesday thursday friday saturday].freeze | ||
|
||
validates( | ||
*DAYS.map { |day| "#{day}_enabled" }, | ||
inclusion: [true, false], | ||
) | ||
|
||
validates( | ||
*DAYS.product([:start, :end]).map { |parts| parts.join("_") }, | ||
presence: true | ||
) | ||
|
||
validate(:validate_time_windows) | ||
|
||
DeploymentWindow = Struct.new(:starts_at, :ends_at, :enabled) do | ||
alias_method :enabled?, :enabled | ||
end | ||
|
||
def can_deploy?(now = Time.current) | ||
# Make sure time is in the default time zone so weekdays match what is | ||
# stored in the database. | ||
now = now.in_time_zone(Time.zone) | ||
|
||
deployment_window = get_deployment_window(now.to_date) | ||
|
||
deployment_window.enabled? && | ||
now >= deployment_window.starts_at && | ||
now <= deployment_window.ends_at | ||
end | ||
|
||
def get_deployment_window(date) | ||
wday_name = DAYS.fetch(date.wday) | ||
|
||
enabled = read_attribute("#{wday_name}_enabled") | ||
|
||
starts_at, ends_at = [:start, :end].map do |bound| | ||
raw_time = read_attribute("#{wday_name}_#{bound}") | ||
|
||
# `ActiveRecord::Type::Time` attributes are stored as timestamps | ||
# normalized to 2000-01-01 so they can't be used for comparisons without | ||
# having their dates adjusted. | ||
# https://github.com/rails/rails/blob/ec667e5f114df58087493096253541f1034815af/activemodel/lib/active_model/type/time.rb#L23 | ||
Time.zone.local( | ||
date.year, | ||
date.month, | ||
date.day, | ||
raw_time.hour, | ||
raw_time.min, | ||
) | ||
end | ||
|
||
DeploymentWindow.new( | ||
starts_at, | ||
# Includes the full minute in the configured range. This is required so | ||
# that a window configured to end at 17:59 actually ends at 17:59:59 | ||
# instead of 17:59:00. | ||
ends_at.at_end_of_minute, | ||
enabled, | ||
) | ||
end | ||
|
||
private | ||
|
||
# Make sure every `*_end` attribute comes after its matching `*_start` | ||
# attribute | ||
def validate_time_windows | ||
DAYS.each do |day| | ||
day_start, day_end = [:start, :end].map { |bound| read_attribute("#{day}_#{bound}") } | ||
|
||
next unless day_start && day_end | ||
|
||
next if day_start <= day_end | ||
|
||
errors.add("#{day}_end", :must_be_after_start, start: day_start.strftime("%I:%M %p")) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/views/shipit/continuous_delivery_schedules/show.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<%= render partial: 'shipit/stacks/header', locals: { stack: @stack } %> | ||
|
||
<div class="wrapper continuous-delivery-schedule"> | ||
<section> | ||
<header class="section-header"> | ||
<h2>Continuous Delivery Schedule (Stack #<%= @stack.id %>)</h2> | ||
</header> | ||
</section> | ||
<div class="setting-section"> | ||
<% if @continuous_delivery_schedule.errors.any? %> | ||
<div class="validation-errors"> | ||
<p>Validation errors prevented your schedule from being saved</p> | ||
<ul> | ||
<% @continuous_delivery_schedule.errors.full_messages.each do |full_message| %> | ||
<li><%= full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
<%= form_for(@continuous_delivery_schedule, url: stack_continuous_delivery_schedule_path, method: :patch) do |f| %> | ||
<table class="field-wrapper"> | ||
<tbody> | ||
<% Shipit::ContinuousDeliverySchedule::DAYS.rotate.each do |day| %> | ||
<tr> | ||
<td> | ||
<%= f.check_box("#{day}_enabled") %> | ||
</td> | ||
<td> | ||
<%= f.label("#{day}_enabled", day.titlecase) %> | ||
</td> | ||
<td> | ||
<%= f.time_field("#{day}_start", include_seconds: false) %> | ||
</td> | ||
<td>→</td> | ||
<td> | ||
<%= f.time_field("#{day}_end", include_seconds: false) %> | ||
</td> | ||
<td> | ||
<% if day == "monday" %> | ||
<button data-action="copy-to-all" type="button">Copy to all ↓</button> | ||
<% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<p> | ||
ℹ️ | ||
All times are in <%= Time.zone.name %> | ||
(the <a href="https://guides.rubyonrails.org/configuring.html#config-time-zone">default time zone</a>). | ||
</p> | ||
|
||
<div class="field-wrapper"> | ||
<%= f.submit("Save", class: "btn") %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrate/20240821003007_add_continuous_delivery_schedules.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class AddContinuousDeliverySchedules < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table(:continuous_delivery_schedules) do |t| | ||
t.references(:stack, null: false, index: { unique: true }) | ||
%w[sunday monday tuesday wednesday thursday friday saturday].each do |day| | ||
t.boolean("#{day}_enabled", null: false, default: true) | ||
t.time("#{day}_start", null: false, default: "00:00") | ||
t.time("#{day}_end", null: false, default: "23:59") | ||
end | ||
t.timestamps | ||
end | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
test/controllers/continuous_delivery_schedules_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
module Shipit | ||
class ContinuousDeliverySchedulesControllerTest < ActionController::TestCase | ||
setup do | ||
@routes = Shipit::Engine.routes | ||
@stack = shipit_stacks(:shipit) | ||
session[:user_id] = shipit_users(:walrus).id | ||
end | ||
|
||
def valid_params | ||
Shipit::ContinuousDeliverySchedule::DAYS.each_with_object({}) do |day, hash| | ||
hash[:"#{day}_enabled"] = "0" | ||
hash[:"#{day}_start"] = "09:00" | ||
hash[:"#{day}_end"] = "17:00" | ||
end | ||
end | ||
|
||
test "#show returns a 200 response" do | ||
get(:show, params: { id: @stack.to_param }) | ||
|
||
assert(response.ok?) | ||
end | ||
|
||
test "#update" do | ||
patch(:update, params: { | ||
id: @stack.to_param, | ||
continuous_delivery_schedule: { | ||
**valid_params, | ||
}, | ||
}) | ||
|
||
assert_redirected_to(stack_continuous_delivery_schedule_path(@stack)) | ||
assert_equal("Successfully updated", flash[:success]) | ||
|
||
schedule = @stack.continuous_delivery_schedule | ||
|
||
Shipit::ContinuousDeliverySchedule::DAYS.each do |day| | ||
refute(schedule.read_attribute("#{day}_enabled")) | ||
|
||
day_start = schedule.read_attribute("#{day}_start") | ||
assert_equal("09:00:00 AM", day_start.strftime("%r")) | ||
|
||
day_end = schedule.read_attribute("#{day}_end") | ||
assert_equal("05:00:00 PM", day_end.strftime("%r")) | ||
end | ||
end | ||
|
||
test "#update renders validation errors" do | ||
patch(:update, params: { | ||
id: @stack.to_param, | ||
continuous_delivery_schedule: { | ||
# Make Sunday end before it starts | ||
**valid_params.merge(sunday_end: "08:00"), | ||
}, | ||
}) | ||
|
||
assert_response(:unprocessable_entity) | ||
assert_equal("Check form for errors", flash[:warning]) | ||
elements = assert_select(".validation-errors") | ||
assert_includes(elements.sole.inner_text, "Sunday end must be after start (09:00 AM)") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.