Commit 50cb6eca authored by Aleksei Lipniagov's avatar Aleksei Lipniagov Committed by Kamil Trzciński

Use 'gitlab_chronic_duration' gem

Replace 'chronic_duration' to 'gitlab_chronic_duration', to make
relevant method calls thread-safe.
parent 6db9cbfe
...@@ -265,7 +265,7 @@ gem 'fast_blank' ...@@ -265,7 +265,7 @@ gem 'fast_blank'
# Parse time & duration # Parse time & duration
gem 'chronic', '~> 0.10.2' gem 'chronic', '~> 0.10.2'
gem 'chronic_duration', '~> 0.10.6' gem 'gitlab_chronic_duration', '~> 0.10.6.1'
gem 'webpack-rails', '~> 0.9.10' gem 'webpack-rails', '~> 0.9.10'
gem 'rack-proxy', '~> 0.6.0' gem 'rack-proxy', '~> 0.6.0'
......
...@@ -136,8 +136,6 @@ GEM ...@@ -136,8 +136,6 @@ GEM
childprocess (0.9.0) childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11) ffi (~> 1.0, >= 1.0.11)
chronic (0.10.2) chronic (0.10.2)
chronic_duration (0.10.6)
numerizer (~> 0.1.1)
chunky_png (1.3.5) chunky_png (1.3.5)
citrus (3.0.2) citrus (3.0.2)
claide (1.0.3) claide (1.0.3)
...@@ -356,6 +354,8 @@ GEM ...@@ -356,6 +354,8 @@ GEM
rubocop-gitlab-security (~> 0.1.0) rubocop-gitlab-security (~> 0.1.0)
rubocop-performance (~> 1.1.0) rubocop-performance (~> 1.1.0)
rubocop-rspec (~> 1.19) rubocop-rspec (~> 1.19)
gitlab_chronic_duration (0.10.6.1)
numerizer (~> 0.1.1)
gitlab_omniauth-ldap (2.1.1) gitlab_omniauth-ldap (2.1.1)
net-ldap (~> 0.16) net-ldap (~> 0.16)
omniauth (~> 1.3) omniauth (~> 1.3)
...@@ -1089,7 +1089,6 @@ DEPENDENCIES ...@@ -1089,7 +1089,6 @@ DEPENDENCIES
carrierwave (~> 1.3) carrierwave (~> 1.3)
charlock_holmes (~> 0.7.5) charlock_holmes (~> 0.7.5)
chronic (~> 0.10.2) chronic (~> 0.10.2)
chronic_duration (~> 0.10.6)
commonmarker (~> 0.17) commonmarker (~> 0.17)
concurrent-ruby (~> 1.1) concurrent-ruby (~> 1.1)
connection_pool (~> 2.0) connection_pool (~> 2.0)
...@@ -1141,6 +1140,7 @@ DEPENDENCIES ...@@ -1141,6 +1140,7 @@ DEPENDENCIES
gitlab-peek (~> 0.0.1) gitlab-peek (~> 0.0.1)
gitlab-sidekiq-fetcher (= 0.5.2) gitlab-sidekiq-fetcher (= 0.5.2)
gitlab-styles (~> 2.7) gitlab-styles (~> 2.7)
gitlab_chronic_duration (~> 0.10.6.1)
gitlab_omniauth-ldap (~> 2.1.1) gitlab_omniauth-ldap (~> 2.1.1)
gon (~> 6.2) gon (~> 6.2)
google-api-client (~> 0.23) google-api-client (~> 0.23)
......
---
title: Use `ChronicDuration` in a thread-safe way
merge_request: 32817
author:
type: fixed
# frozen_string_literal: true # frozen_string_literal: true
ChronicDuration.raise_exceptions = true ChronicDuration.raise_exceptions = true
ChronicDuration.prepend Gitlab::Patch::ChronicDuration
# frozen_string_literal: true
# Fixes a bug where parsing months doesn't take into account
# the ChronicDuration.days_per_week setting
#
# We can remove this when we do a refactor and push upstream in
# https://gitlab.com/gitlab-org/gitlab-ce/issues/66637
module Gitlab
module Patch
module ChronicDuration
extend ActiveSupport::Concern
class_methods do
def duration_units_seconds_multiplier(unit)
return 0 unless duration_units_list.include?(unit)
case unit
when 'months'
3600 * ::ChronicDuration.hours_per_day * ::ChronicDuration.days_per_month
else
super
end
end
# ChronicDuration#output uses 1mo = 4w as the conversion so we do the same here.
# We do need to add a special case for the default days_per_week value because
# we want to retain existing behavior for the default case
def days_per_month
::ChronicDuration.days_per_week == 7 ? 30 : ::ChronicDuration.days_per_week * 4
end
end
end
end
end
...@@ -4,37 +4,38 @@ module Gitlab ...@@ -4,37 +4,38 @@ module Gitlab
module TimeTrackingFormatter module TimeTrackingFormatter
extend self extend self
def parse(string) # We may want to configure it through project settings in a future version.
with_custom_config do CUSTOM_DAY_AND_WEEK_LENGTH = { hours_per_day: 8, days_per_month: 20 }.freeze
string = string.sub(/\A-/, '')
seconds = ChronicDuration.parse(string, default_unit: 'hours') rescue nil def parse(string)
seconds *= -1 if seconds && Regexp.last_match string = string.sub(/\A-/, '')
seconds
end seconds =
begin
ChronicDuration.parse(
string,
CUSTOM_DAY_AND_WEEK_LENGTH.merge(default_unit: 'hours'))
rescue
nil
end
seconds *= -1 if seconds && Regexp.last_match
seconds
end end
def output(seconds) def output(seconds)
with_custom_config do ChronicDuration.output(
ChronicDuration.output(seconds, format: :short, limit_to_hours: limit_to_hours_setting, weeks: true) rescue nil seconds,
end CUSTOM_DAY_AND_WEEK_LENGTH.merge(
format: :short,
limit_to_hours: limit_to_hours_setting,
weeks: true))
rescue
nil
end end
private private
def with_custom_config
# We may want to configure it through project settings in a future version.
ChronicDuration.hours_per_day = 8
ChronicDuration.days_per_week = 5
result = yield
ChronicDuration.hours_per_day = 24
ChronicDuration.days_per_week = 7
result
end
def limit_to_hours_setting def limit_to_hours_setting
Gitlab::CurrentSettings.time_tracking_limit_to_hours Gitlab::CurrentSettings.time_tracking_limit_to_hours
end end
......
require 'fast_spec_helper' require 'fast_spec_helper'
require 'chronic_duration' require 'gitlab_chronic_duration'
require 'support/helpers/stub_feature_flags' require 'support/helpers/stub_feature_flags'
require_dependency 'active_model' require_dependency 'active_model'
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Patch::ChronicDuration do
subject { ChronicDuration.parse('1mo') }
it 'uses default conversions' do
expect(subject).to eq(2_592_000)
end
context 'with custom conversions' do
before do
ChronicDuration.hours_per_day = 8
ChronicDuration.days_per_week = 5
end
after do
ChronicDuration.hours_per_day = 24
ChronicDuration.days_per_week = 7
end
it 'uses custom conversions' do
expect(subject).to eq(576_000)
end
end
end
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