Commit 56bf4a7f authored by Hariom Verma's avatar Hariom Verma

Add time_change in Issue, MR and Note webhook

Include the time changed in webhooks when spending time on Issues,
MRs and Epics; in machine and human readable format.

Changelog: added
parent 1eac4bbb
...@@ -468,9 +468,11 @@ module Issuable ...@@ -468,9 +468,11 @@ module Issuable
if self.respond_to?(:total_time_spent) if self.respond_to?(:total_time_spent)
old_total_time_spent = old_associations.fetch(:total_time_spent, total_time_spent) old_total_time_spent = old_associations.fetch(:total_time_spent, total_time_spent)
old_time_change = old_associations.fetch(:time_change, time_change)
if old_total_time_spent != total_time_spent if old_total_time_spent != total_time_spent
changes[:total_time_spent] = [old_total_time_spent, total_time_spent] changes[:total_time_spent] = [old_total_time_spent, total_time_spent]
changes[:time_change] = [old_time_change, time_change]
end end
end end
end end
......
...@@ -33,11 +33,11 @@ module TimeTrackable ...@@ -33,11 +33,11 @@ module TimeTrackable
return if @time_spent == 0 return if @time_spent == 0
if @time_spent == :reset @timelog = if @time_spent == :reset
reset_spent_time reset_spent_time
else else
add_or_subtract_spent_time add_or_subtract_spent_time
end end
end end
alias_method :spend_time=, :spend_time alias_method :spend_time=, :spend_time
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
...@@ -50,6 +50,14 @@ module TimeTrackable ...@@ -50,6 +50,14 @@ module TimeTrackable
Gitlab::TimeTrackingFormatter.output(total_time_spent) Gitlab::TimeTrackingFormatter.output(total_time_spent)
end end
def time_change
@timelog&.time_spent.to_i # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def human_time_change
Gitlab::TimeTrackingFormatter.output(time_change)
end
def human_time_estimate def human_time_estimate
Gitlab::TimeTrackingFormatter.output(time_estimate) Gitlab::TimeTrackingFormatter.output(time_estimate)
end end
......
...@@ -433,6 +433,7 @@ class IssuableBaseService < ::BaseProjectService ...@@ -433,6 +433,7 @@ class IssuableBaseService < ::BaseProjectService
milestone: issuable.try(:milestone) milestone: issuable.try(:milestone)
} }
associations[:total_time_spent] = issuable.total_time_spent if issuable.respond_to?(:total_time_spent) associations[:total_time_spent] = issuable.total_time_spent if issuable.respond_to?(:total_time_spent)
associations[:time_change] = issuable.time_change if issuable.respond_to?(:time_change)
associations[:description] = issuable.description associations[:description] = issuable.description
associations[:reviewers] = issuable.reviewers.to_a if issuable.allows_reviewers? associations[:reviewers] = issuable.reviewers.to_a if issuable.allows_reviewers?
......
...@@ -308,8 +308,10 @@ X-Gitlab-Event: Issue Hook ...@@ -308,8 +308,10 @@ X-Gitlab-Event: Issue Hook
"duplicated_to_id": null, "duplicated_to_id": null,
"time_estimate": 0, "time_estimate": 0,
"total_time_spent": 0, "total_time_spent": 0,
"time_change": 0,
"human_total_time_spent": null, "human_total_time_spent": null,
"human_time_estimate": null, "human_time_estimate": null,
"human_time_change": null,
"weight": null, "weight": null,
"iid": 23, "iid": 23,
"url": "http://example.com/diaspora/issues/23", "url": "http://example.com/diaspora/issues/23",
......
...@@ -7,6 +7,7 @@ module Gitlab ...@@ -7,6 +7,7 @@ module Gitlab
assignees assignees
labels labels
total_time_spent total_time_spent
time_change
].freeze ].freeze
def self.safe_hook_attributes def self.safe_hook_attributes
...@@ -43,7 +44,9 @@ module Gitlab ...@@ -43,7 +44,9 @@ module Gitlab
description: absolute_image_urls(issue.description), description: absolute_image_urls(issue.description),
url: Gitlab::UrlBuilder.build(issue), url: Gitlab::UrlBuilder.build(issue),
total_time_spent: issue.total_time_spent, total_time_spent: issue.total_time_spent,
time_change: issue.time_change,
human_total_time_spent: issue.human_total_time_spent, human_total_time_spent: issue.human_total_time_spent,
human_time_change: issue.human_time_change,
human_time_estimate: issue.human_time_estimate, human_time_estimate: issue.human_time_estimate,
assignee_ids: issue.assignee_ids, assignee_ids: issue.assignee_ids,
assignee_id: issue.assignee_ids.first, # This key is deprecated assignee_id: issue.assignee_ids.first, # This key is deprecated
......
...@@ -37,6 +37,7 @@ module Gitlab ...@@ -37,6 +37,7 @@ module Gitlab
assignees assignees
labels labels
total_time_spent total_time_spent
time_change
].freeze ].freeze
alias_method :merge_request, :object alias_method :merge_request, :object
...@@ -50,7 +51,9 @@ module Gitlab ...@@ -50,7 +51,9 @@ module Gitlab
last_commit: merge_request.diff_head_commit&.hook_attrs, last_commit: merge_request.diff_head_commit&.hook_attrs,
work_in_progress: merge_request.work_in_progress?, work_in_progress: merge_request.work_in_progress?,
total_time_spent: merge_request.total_time_spent, total_time_spent: merge_request.total_time_spent,
time_change: merge_request.time_change,
human_total_time_spent: merge_request.human_total_time_spent, human_total_time_spent: merge_request.human_total_time_spent,
human_time_change: merge_request.human_time_change,
human_time_estimate: merge_request.human_time_estimate, human_time_estimate: merge_request.human_time_estimate,
assignee_ids: merge_request.assignee_ids, assignee_ids: merge_request.assignee_ids,
assignee_id: merge_request.assignee_ids.first, # This key is deprecated assignee_id: merge_request.assignee_ids.first, # This key is deprecated
......
...@@ -42,8 +42,10 @@ RSpec.describe Gitlab::HookData::IssueBuilder do ...@@ -42,8 +42,10 @@ RSpec.describe Gitlab::HookData::IssueBuilder do
it 'includes additional attrs' do it 'includes additional attrs' do
expect(data).to include(:total_time_spent) expect(data).to include(:total_time_spent)
expect(data).to include(:time_change)
expect(data).to include(:human_time_estimate) expect(data).to include(:human_time_estimate)
expect(data).to include(:human_total_time_spent) expect(data).to include(:human_total_time_spent)
expect(data).to include(:human_time_change)
expect(data).to include(:assignee_ids) expect(data).to include(:assignee_ids)
expect(data).to include(:state) expect(data).to include(:state)
expect(data).to include('labels' => [label.hook_attrs]) expect(data).to include('labels' => [label.hook_attrs])
......
...@@ -57,8 +57,10 @@ RSpec.describe Gitlab::HookData::MergeRequestBuilder do ...@@ -57,8 +57,10 @@ RSpec.describe Gitlab::HookData::MergeRequestBuilder do
expect(data).to include(:last_commit) expect(data).to include(:last_commit)
expect(data).to include(:work_in_progress) expect(data).to include(:work_in_progress)
expect(data).to include(:total_time_spent) expect(data).to include(:total_time_spent)
expect(data).to include(:time_change)
expect(data).to include(:human_time_estimate) expect(data).to include(:human_time_estimate)
expect(data).to include(:human_total_time_spent) expect(data).to include(:human_total_time_spent)
expect(data).to include(:human_time_change)
end end
context 'when the MR has an image in the description' do context 'when the MR has an image in the description' do
......
...@@ -715,6 +715,12 @@ RSpec.describe Issuable do ...@@ -715,6 +715,12 @@ RSpec.describe Issuable do
expect(issue.total_time_spent).to eq(1800) expect(issue.total_time_spent).to eq(1800)
end end
it 'stores the time change' do
spend_time(1800)
expect(issue.time_change).to eq(1800)
end
it 'updates issues updated_at' do it 'updates issues updated_at' do
issue issue
...@@ -735,6 +741,12 @@ RSpec.describe Issuable do ...@@ -735,6 +741,12 @@ RSpec.describe Issuable do
expect(issue.total_time_spent).to eq(900) expect(issue.total_time_spent).to eq(900)
end end
it 'stores negative time change' do
spend_time(-900)
expect(issue.time_change).to eq(-900)
end
context 'when time to subtract exceeds the total time spent' do context 'when time to subtract exceeds the total time spent' do
it 'raise a validation error' do it 'raise a validation error' do
Timecop.travel(1.minute.from_now) do Timecop.travel(1.minute.from_now) do
......
...@@ -297,6 +297,7 @@ RSpec.describe MergeRequests::UpdateService, :mailer do ...@@ -297,6 +297,7 @@ RSpec.describe MergeRequests::UpdateService, :mailer do
reviewers: [], reviewers: [],
milestone: nil, milestone: nil,
total_time_spent: 0, total_time_spent: 0,
time_change: 0,
description: "FYI #{user2.to_reference}" description: "FYI #{user2.to_reference}"
} }
) )
......
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