2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SystemNotes
|
|
|
|
class TimeTrackingService < ::SystemNotes::BaseService
|
2022-08-27 11:52:29 +05:30
|
|
|
# Called when the start_date or due_date of an Issue/WorkItem is changed
|
2020-03-13 15:44:24 +05:30
|
|
|
#
|
2022-08-27 11:52:29 +05:30
|
|
|
# start_date - Start date being assigned, or nil
|
2020-03-13 15:44:24 +05:30
|
|
|
# due_date - Due date being assigned, or nil
|
|
|
|
#
|
|
|
|
# Example Note text:
|
|
|
|
#
|
|
|
|
# "removed due date"
|
|
|
|
#
|
|
|
|
# "changed due date to September 20, 2018"
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
# "changed start date to September 20, 2018 and changed due date to September 25, 2018"
|
2020-03-13 15:44:24 +05:30
|
|
|
#
|
|
|
|
# Returns the created Note object
|
2022-08-27 11:52:29 +05:30
|
|
|
def change_start_date_or_due_date(changed_dates = {})
|
|
|
|
return if changed_dates.empty?
|
|
|
|
|
|
|
|
# Using instance_of because WorkItem < Issue. We don't want to track work item updates as issue updates
|
|
|
|
if noteable.instance_of?(Issue) && changed_dates.key?('due_date')
|
|
|
|
issue_activity_counter.track_issue_due_date_changed_action(author: author)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
work_item_activity_counter.track_work_item_date_changed_action(author: author) if noteable.is_a?(WorkItem)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
create_note(
|
|
|
|
NoteSummary.new(noteable, project, author, changed_date_body(changed_dates), action: 'start_date_or_due_date')
|
|
|
|
)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Called when the estimated time of a Noteable is changed
|
|
|
|
#
|
|
|
|
# time_estimate - Estimated time
|
|
|
|
#
|
|
|
|
# Example Note text:
|
|
|
|
#
|
|
|
|
# "removed time estimate"
|
|
|
|
#
|
|
|
|
# "changed time estimate to 3d 5h"
|
|
|
|
#
|
|
|
|
# Returns the created Note object
|
|
|
|
def change_time_estimate
|
|
|
|
parsed_time = Gitlab::TimeTrackingFormatter.output(noteable.time_estimate)
|
|
|
|
body = if noteable.time_estimate == 0
|
|
|
|
"removed time estimate"
|
|
|
|
else
|
|
|
|
"changed time estimate to #{parsed_time}"
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
issue_activity_counter.track_issue_time_estimate_changed_action(author: author) if noteable.is_a?(Issue)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Called when the spent time of a Noteable is changed
|
|
|
|
#
|
|
|
|
# time_spent - Spent time
|
|
|
|
#
|
|
|
|
# Example Note text:
|
|
|
|
#
|
|
|
|
# "removed time spent"
|
|
|
|
#
|
|
|
|
# "added 2h 30m of time spent"
|
|
|
|
#
|
|
|
|
# Returns the created Note object
|
|
|
|
def change_time_spent
|
|
|
|
time_spent = noteable.time_spent
|
|
|
|
|
|
|
|
if time_spent == :reset
|
|
|
|
body = "removed time spent"
|
|
|
|
else
|
2021-06-08 01:23:25 +05:30
|
|
|
spent_at = noteable.spent_at&.to_date
|
2020-03-13 15:44:24 +05:30
|
|
|
parsed_time = Gitlab::TimeTrackingFormatter.output(time_spent.abs)
|
|
|
|
action = time_spent > 0 ? 'added' : 'subtracted'
|
|
|
|
|
|
|
|
text_parts = ["#{action} #{parsed_time} of time spent"]
|
2021-06-08 01:23:25 +05:30
|
|
|
text_parts << "at #{spent_at}" if spent_at && spent_at != DateTime.current.to_date
|
2020-03-13 15:44:24 +05:30
|
|
|
body = text_parts.join(' ')
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
issue_activity_counter.track_issue_time_spent_changed_action(author: author) if noteable.is_a?(Issue)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
# Called when a timelog is added to an issuable
|
|
|
|
#
|
|
|
|
# timelog - Added timelog
|
|
|
|
#
|
|
|
|
# Example Note text:
|
|
|
|
#
|
|
|
|
# "subtracted 1h 15m of time spent"
|
|
|
|
#
|
|
|
|
# "added 2h 30m of time spent"
|
|
|
|
#
|
|
|
|
# Returns the created Note object
|
|
|
|
def created_timelog(timelog)
|
|
|
|
time_spent = timelog.time_spent
|
|
|
|
spent_at = timelog.spent_at&.to_date
|
|
|
|
parsed_time = Gitlab::TimeTrackingFormatter.output(time_spent.abs)
|
|
|
|
action = time_spent > 0 ? 'added' : 'subtracted'
|
|
|
|
|
|
|
|
text_parts = ["#{action} #{parsed_time} of time spent"]
|
|
|
|
text_parts << "at #{spent_at}" if spent_at && spent_at != DateTime.current.to_date
|
|
|
|
body = text_parts.join(' ')
|
|
|
|
|
|
|
|
issue_activity_counter.track_issue_time_spent_changed_action(author: author) if noteable.is_a?(Issue)
|
|
|
|
|
|
|
|
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def remove_timelog(timelog)
|
|
|
|
time_spent = timelog.time_spent
|
|
|
|
spent_at = timelog.spent_at&.to_date
|
|
|
|
|
|
|
|
parsed_time = Gitlab::TimeTrackingFormatter.output(time_spent)
|
|
|
|
|
|
|
|
body = "deleted #{parsed_time} of spent time"
|
|
|
|
body += " from #{spent_at}" if spent_at
|
|
|
|
|
|
|
|
create_note(NoteSummary.new(noteable, project, author, body, action: 'time_tracking'))
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
private
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
def changed_date_body(changed_dates)
|
|
|
|
%w[start_date due_date].each_with_object([]) do |date_field, word_array|
|
|
|
|
next unless changed_dates.key?(date_field)
|
|
|
|
|
|
|
|
word_array << 'and' if word_array.any?
|
|
|
|
|
|
|
|
word_array << message_for_changed_date(changed_dates, date_field)
|
|
|
|
end.join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_for_changed_date(changed_dates, date_key)
|
|
|
|
changed_date = changed_dates[date_key].last
|
|
|
|
readable_date = date_key.humanize.downcase
|
|
|
|
|
|
|
|
if changed_date.nil?
|
|
|
|
"removed #{readable_date}"
|
|
|
|
else
|
|
|
|
"changed #{readable_date} to #{changed_date.to_s(:long)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def issue_activity_counter
|
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def work_item_activity_counter
|
|
|
|
Gitlab::UsageDataCounters::WorkItemActivityUniqueCounter
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|