Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
35b4cc41
Commit
35b4cc41
authored
Oct 11, 2019
by
Patrick Derichs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract time tracking system note service
Also move specs
parent
e0027b75
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
218 additions
and
126 deletions
+218
-126
app/services/system_note_service.rb
app/services/system_note_service.rb
+3
-26
app/services/system_notes/time_tracking_service.rb
app/services/system_notes/time_tracking_service.rb
+73
-0
spec/services/system_note_service_spec.rb
spec/services/system_note_service_spec.rb
+13
-100
spec/services/system_notes/time_tracking_service_spec.rb
spec/services/system_notes/time_tracking_service_spec.rb
+129
-0
No files found.
app/services/system_note_service.rb
View file @
35b4cc41
...
...
@@ -60,9 +60,7 @@ module SystemNoteService
#
# Returns the created Note object
def
change_due_date
(
noteable
,
project
,
author
,
due_date
)
body
=
due_date
?
"changed due date to
#{
due_date
.
to_s
(
:long
)
}
"
:
'removed due date'
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'due_date'
))
::
SystemNotes
::
TimeTrackingService
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_due_date
(
due_date
)
end
# Called when the estimated time of a Noteable is changed
...
...
@@ -80,14 +78,7 @@ module SystemNoteService
#
# Returns the created Note object
def
change_time_estimate
(
noteable
,
project
,
author
)
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
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'time_tracking'
))
::
SystemNotes
::
TimeTrackingService
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_time_estimate
end
# Called when the spent time of a Noteable is changed
...
...
@@ -105,21 +96,7 @@ module SystemNoteService
#
# Returns the created Note object
def
change_time_spent
(
noteable
,
project
,
author
)
time_spent
=
noteable
.
time_spent
if
time_spent
==
:reset
body
=
"removed time spent"
else
spent_at
=
noteable
.
spent_at
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
body
=
text_parts
.
join
(
' '
)
end
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'time_tracking'
))
::
SystemNotes
::
TimeTrackingService
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_time_spent
end
def
change_status
(
noteable
,
project
,
author
,
status
,
source
=
nil
)
...
...
app/services/system_notes/time_tracking_service.rb
0 → 100644
View file @
35b4cc41
# frozen_string_literal: true
module
SystemNotes
class
TimeTrackingService
<
::
SystemNotes
::
BaseService
# Called when the due_date of a Noteable is changed
#
# due_date - Due date being assigned, or nil
#
# Example Note text:
#
# "removed due date"
#
# "changed due date to September 20, 2018"
#
# Returns the created Note object
def
change_due_date
(
due_date
)
body
=
due_date
?
"changed due date to
#{
due_date
.
to_s
(
:long
)
}
"
:
'removed due date'
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'due_date'
))
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
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
spent_at
=
noteable
.
spent_at
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
body
=
text_parts
.
join
(
' '
)
end
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'time_tracking'
))
end
end
end
spec/services/system_note_service_spec.rb
View file @
35b4cc41
...
...
@@ -76,28 +76,14 @@ describe SystemNoteService do
end
describe
'.change_due_date'
do
subject
{
described_class
.
change_due_date
(
noteable
,
project
,
author
,
due_date
)
}
let
(
:due_date
)
{
double
}
let
(
:due_date
)
{
Date
.
today
}
it_behaves_like
'a note with overridable created_at'
it_behaves_like
'a system note'
do
let
(
:action
)
{
'due_date'
}
end
context
'when due date added'
do
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
"changed due date to
#{
Date
.
today
.
to_s
(
:long
)
}
"
end
it
'calls TimeTrackingService'
do
expect_next_instance_of
(
::
SystemNotes
::
TimeTrackingService
)
do
|
service
|
expect
(
service
).
to
receive
(
:change_due_date
).
with
(
due_date
)
end
context
'when due date removed'
do
let
(
:due_date
)
{
nil
}
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
'removed due date'
end
described_class
.
change_due_date
(
noteable
,
project
,
author
,
due_date
)
end
end
...
...
@@ -488,36 +474,12 @@ describe SystemNoteService do
end
describe
'.change_time_estimate'
do
subject
{
described_class
.
change_time_estimate
(
noteable
,
project
,
author
)
}
it_behaves_like
'a system note'
do
let
(
:action
)
{
'time_tracking'
}
it
'calls TimeTrackingService'
do
expect_next_instance_of
(
::
SystemNotes
::
TimeTrackingService
)
do
|
service
|
expect
(
service
).
to
receive
(
:change_time_estimate
)
end
context
'with a time estimate'
do
it
'sets the note text'
do
noteable
.
update_attribute
(
:time_estimate
,
277200
)
expect
(
subject
.
note
).
to
eq
"changed time estimate to 1w 4d 5h"
end
context
'when time_tracking_limit_to_hours setting is true'
do
before
do
stub_application_setting
(
time_tracking_limit_to_hours:
true
)
end
it
'sets the note text'
do
noteable
.
update_attribute
(
:time_estimate
,
277200
)
expect
(
subject
.
note
).
to
eq
"changed time estimate to 77h"
end
end
end
context
'without a time estimate'
do
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
"removed time estimate"
end
described_class
.
change_time_estimate
(
noteable
,
project
,
author
)
end
end
...
...
@@ -548,62 +510,13 @@ describe SystemNoteService do
end
describe
'.change_time_spent'
do
# We need a custom noteable in order to the shared examples to be green.
let
(
:noteable
)
do
mr
=
create
(
:merge_request
,
source_project:
project
)
mr
.
spend_time
(
duration:
360000
,
user_id:
author
.
id
)
mr
.
save!
mr
it
'calls TimeTrackingService'
do
expect_next_instance_of
(
::
SystemNotes
::
TimeTrackingService
)
do
|
service
|
expect
(
service
).
to
receive
(
:change_time_spent
)
end
subject
do
described_class
.
change_time_spent
(
noteable
,
project
,
author
)
end
it_behaves_like
'a system note'
do
let
(
:action
)
{
'time_tracking'
}
end
context
'when time was added'
do
it
'sets the note text'
do
spend_time!
(
277200
)
expect
(
subject
.
note
).
to
eq
"added 1w 4d 5h of time spent"
end
end
context
'when time was subtracted'
do
it
'sets the note text'
do
spend_time!
(
-
277200
)
expect
(
subject
.
note
).
to
eq
"subtracted 1w 4d 5h of time spent"
end
end
context
'when time was removed'
do
it
'sets the note text'
do
spend_time!
(
:reset
)
expect
(
subject
.
note
).
to
eq
"removed time spent"
end
end
context
'when time_tracking_limit_to_hours setting is true'
do
before
do
stub_application_setting
(
time_tracking_limit_to_hours:
true
)
end
it
'sets the note text'
do
spend_time!
(
277200
)
expect
(
subject
.
note
).
to
eq
"added 77h of time spent"
end
end
def
spend_time!
(
seconds
)
noteable
.
spend_time
(
duration:
seconds
,
user_id:
author
.
id
)
noteable
.
save!
end
end
describe
'.handle_merge_request_wip'
do
...
...
spec/services/system_notes/time_tracking_service_spec.rb
0 → 100644
View file @
35b4cc41
# frozen_string_literal: true
require
'spec_helper'
describe
::
SystemNotes
::
TimeTrackingService
do
let_it_be
(
:author
)
{
create
(
:user
)
}
let_it_be
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:noteable
)
{
create
(
:issue
,
project:
project
)
}
describe
'#change_due_date'
do
subject
{
described_class
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_due_date
(
due_date
)
}
let
(
:due_date
)
{
Date
.
today
}
it_behaves_like
'a note with overridable created_at'
it_behaves_like
'a system note'
do
let
(
:action
)
{
'due_date'
}
end
context
'when due date added'
do
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
"changed due date to
#{
due_date
.
to_s
(
:long
)
}
"
end
end
context
'when due date removed'
do
let
(
:due_date
)
{
nil
}
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
'removed due date'
end
end
end
describe
'.change_time_estimate'
do
subject
{
described_class
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_time_estimate
}
it_behaves_like
'a system note'
do
let
(
:action
)
{
'time_tracking'
}
end
context
'with a time estimate'
do
it
'sets the note text'
do
noteable
.
update_attribute
(
:time_estimate
,
277200
)
expect
(
subject
.
note
).
to
eq
"changed time estimate to 1w 4d 5h"
end
context
'when time_tracking_limit_to_hours setting is true'
do
before
do
stub_application_setting
(
time_tracking_limit_to_hours:
true
)
end
it
'sets the note text'
do
noteable
.
update_attribute
(
:time_estimate
,
277200
)
expect
(
subject
.
note
).
to
eq
"changed time estimate to 77h"
end
end
end
context
'without a time estimate'
do
it
'sets the note text'
do
expect
(
subject
.
note
).
to
eq
"removed time estimate"
end
end
end
describe
'.change_time_spent'
do
# We need a custom noteable in order to the shared examples to be green.
let
(
:noteable
)
do
mr
=
create
(
:merge_request
,
source_project:
project
)
mr
.
spend_time
(
duration:
360000
,
user_id:
author
.
id
)
mr
.
save!
mr
end
subject
do
described_class
.
new
(
noteable:
noteable
,
project:
project
,
author:
author
).
change_time_spent
end
it_behaves_like
'a system note'
do
let
(
:action
)
{
'time_tracking'
}
end
context
'when time was added'
do
it
'sets the note text'
do
spend_time!
(
277200
)
expect
(
subject
.
note
).
to
eq
"added 1w 4d 5h of time spent"
end
end
context
'when time was subtracted'
do
it
'sets the note text'
do
spend_time!
(
-
277200
)
expect
(
subject
.
note
).
to
eq
"subtracted 1w 4d 5h of time spent"
end
end
context
'when time was removed'
do
it
'sets the note text'
do
spend_time!
(
:reset
)
expect
(
subject
.
note
).
to
eq
"removed time spent"
end
end
context
'when time_tracking_limit_to_hours setting is true'
do
before
do
stub_application_setting
(
time_tracking_limit_to_hours:
true
)
end
it
'sets the note text'
do
spend_time!
(
277200
)
expect
(
subject
.
note
).
to
eq
"added 77h of time spent"
end
end
def
spend_time!
(
seconds
)
noteable
.
spend_time
(
duration:
seconds
,
user_id:
author
.
id
)
noteable
.
save!
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment