diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-10-13 15:52:41 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-10-13 15:52:41 +0000 |
commit | b0f98a2e8e7fde23666d57675f9340fa453d04a8 (patch) | |
tree | 996423598883ead8b05f163733467bae4a19c945 /lib | |
parent | b6abad9094c39c3c27f72c17f931ef71fdfdcde6 (diff) | |
parent | 945e0684afc3c22ac658dbb68005cb2ebf5ac51c (diff) | |
download | gitlab-ce-b0f98a2e8e7fde23666d57675f9340fa453d04a8.tar.gz |
Merge branch 'gitlab-ee-1312-time-spent-at' into 'master'
added date parameter for time tracking
See merge request gitlab-org/gitlab-ce!14866
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/quick_actions/spend_time_and_date_separator.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/gitlab/quick_actions/spend_time_and_date_separator.rb b/lib/gitlab/quick_actions/spend_time_and_date_separator.rb new file mode 100644 index 00000000000..3f52402b31f --- /dev/null +++ b/lib/gitlab/quick_actions/spend_time_and_date_separator.rb @@ -0,0 +1,54 @@ +module Gitlab + module QuickActions + # This class takes spend command argument + # and separates date and time from spend command arguments if it present + # example: + # spend_command_time_and_date = "15m 2017-01-02" + # SpendTimeAndDateSeparator.new(spend_command_time_and_date).execute + # => [900, Mon, 02 Jan 2017] + # if date doesn't present return time with current date + # in other cases return nil + class SpendTimeAndDateSeparator + DATE_REGEX = /(\d{2,4}[\/\-.]\d{1,2}[\/\-.]\d{1,2})/ + + def initialize(spend_command_arg) + @spend_arg = spend_command_arg + end + + def execute + return if @spend_arg.blank? + return [get_time, DateTime.now.to_date] unless date_present? + return unless valid_date? + + [get_time, get_date] + end + + private + + def get_time + raw_time = @spend_arg.gsub(DATE_REGEX, '') + Gitlab::TimeTrackingFormatter.parse(raw_time) + end + + def get_date + string_date = @spend_arg.match(DATE_REGEX)[0] + Date.parse(string_date) + end + + def date_present? + DATE_REGEX =~ @spend_arg + end + + def valid_date? + string_date = @spend_arg.match(DATE_REGEX)[0] + date = Date.parse(string_date) rescue nil + + date_past_or_today?(date) + end + + def date_past_or_today?(date) + date&.past? || date&.today? + end + end + end +end |