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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
c729d9da
Commit
c729d9da
authored
Mar 15, 2017
by
Jarka Kadlecova
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create metadata when creating system notes
parent
1c3c7fb2
Changes
13
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
269 additions
and
75 deletions
+269
-75
app/models/note.rb
app/models/note.rb
+1
-1
app/models/system_note_metadata.rb
app/models/system_note_metadata.rb
+1
-1
app/services/note_summary.rb
app/services/note_summary.rb
+20
-0
app/services/system_note_service.rb
app/services/system_note_service.rb
+41
-38
changelogs/unreleased/24784-system-notes-meta-data.yml
changelogs/unreleased/24784-system-notes-meta-data.yml
+4
-0
db/migrate/20170314082049_create_system_note_metadata.rb
db/migrate/20170314082049_create_system_note_metadata.rb
+7
-3
db/schema.rb
db/schema.rb
+1
-1
spec/factories/system_note_metadata.rb
spec/factories/system_note_metadata.rb
+1
-1
spec/lib/gitlab/import_export/all_models.yml
spec/lib/gitlab/import_export/all_models.yml
+1
-0
spec/models/note_spec.rb
spec/models/note_spec.rb
+0
-1
spec/models/system_note_metadata_spec.rb
spec/models/system_note_metadata_spec.rb
+4
-4
spec/services/note_summary_spec.rb
spec/services/note_summary_spec.rb
+44
-0
spec/services/system_note_service_spec.rb
spec/services/system_note_service_spec.rb
+144
-25
No files found.
app/models/note.rb
View file @
c729d9da
...
...
@@ -37,7 +37,7 @@ class Note < ActiveRecord::Base
has_many
:todos
,
dependent: :destroy
has_many
:events
,
as: :target
,
dependent: :destroy
has_one
:system_note_metadata
,
dependent: :destroy
has_one
:system_note_metadata
delegate
:gfm_reference
,
:local_reference
,
to: :noteable
delegate
:name
,
to: :project
,
prefix:
true
...
...
app/models/system_note_metadata.rb
View file @
c729d9da
...
...
@@ -5,7 +5,7 @@ class SystemNoteMetadata < ActiveRecord::Base
]
.
freeze
validates
:note
,
presence:
true
validates
:
ic
on
,
inclusion:
ICON_TYPES
,
allow_nil:
true
validates
:
acti
on
,
inclusion:
ICON_TYPES
,
allow_nil:
true
belongs_to
:note
end
app/services/note_summary.rb
0 → 100644
View file @
c729d9da
class
NoteSummary
attr_reader
:note
attr_reader
:metadata
def
initialize
(
noteable
,
project
,
author
,
body
,
action:
nil
,
commit_count:
nil
)
@note
=
{
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
}
@metadata
=
{
action:
action
,
commit_count:
commit_count
}.
compact
set_commit_params
if
note
[
:noteable
].
is_a?
(
Commit
)
end
def
metadata?
metadata
.
present?
end
def
set_commit_params
note
.
merge!
(
noteable_type:
'Commit'
,
commit_id:
note
[
:noteable
].
id
)
note
[
:noteable
]
=
nil
end
end
app/services/system_note_service.rb
View file @
c729d9da
...
...
@@ -26,7 +26,7 @@ module SystemNoteService
body
<<
new_commit_summary
(
new_commits
).
join
(
"
\n
"
)
body
<<
"
\n\n
[Compare with previous version](
#{
diff_comparison_url
(
noteable
,
project
,
oldrev
)
}
)"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'commit'
,
commit_count:
total_count
)
)
end
# Called when the assignee of a Noteable is changed or removed
...
...
@@ -46,7 +46,7 @@ module SystemNoteService
def
change_assignee
(
noteable
,
project
,
author
,
assignee
)
body
=
assignee
.
nil?
?
'removed assignee'
:
"assigned to
#{
assignee
.
to_reference
}
"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'assignee'
)
)
end
# Called when one or more labels on a Noteable are added and/or removed
...
...
@@ -86,7 +86,7 @@ module SystemNoteService
body
<<
' '
<<
'label'
.
pluralize
(
labels_count
)
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'label'
)
)
end
# Called when the milestone of a Noteable is changed
...
...
@@ -106,7 +106,7 @@ module SystemNoteService
def
change_milestone
(
noteable
,
project
,
author
,
milestone
)
body
=
milestone
.
nil?
?
'removed milestone'
:
"changed milestone to
#{
milestone
.
to_reference
(
project
)
}
"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'milestone'
)
)
end
# Called when the estimated time of a Noteable is changed
...
...
@@ -132,7 +132,7 @@ module SystemNoteService
"changed time estimate to
#{
parsed_time
}
"
end
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'time_tracking'
)
)
end
# Called when the spent time of a Noteable is changed
...
...
@@ -161,7 +161,7 @@ module SystemNoteService
body
=
"
#{
action
}
#{
parsed_time
}
of time spent"
end
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'time_tracking'
)
)
end
# Called when the status of a Noteable is changed
...
...
@@ -183,53 +183,57 @@ module SystemNoteService
body
=
status
.
dup
body
<<
" via
#{
source
.
gfm_reference
(
project
)
}
"
if
source
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'status'
)
)
end
# Called when 'merge when pipeline succeeds' is executed
def
merge_when_pipeline_succeeds
(
noteable
,
project
,
author
,
last_commit
)
body
=
"enabled an automatic merge when the pipeline for
#{
last_commit
.
to_reference
(
project
)
}
succeeds"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'merge'
)
)
end
# Called when 'merge when pipeline succeeds' is canceled
def
cancel_merge_when_pipeline_succeeds
(
noteable
,
project
,
author
)
body
=
'canceled the automatic merge'
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'merge'
)
)
end
def
remove_merge_request_wip
(
noteable
,
project
,
author
)
body
=
'unmarked as a **Work In Progress**'
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'title'
)
)
end
def
add_merge_request_wip
(
noteable
,
project
,
author
)
body
=
'marked as a **Work In Progress**'
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'title'
)
)
end
def
add_merge_request_wip_from_commit
(
noteable
,
project
,
author
,
commit
)
body
=
"marked as a **Work In Progress** from
#{
commit
.
to_reference
(
project
)
}
"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'title'
)
)
end
def
self
.
resolve_all_discussions
(
merge_request
,
project
,
author
)
body
=
"resolved all discussions"
create_note
(
noteable:
merge_request
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
merge_request
,
project
,
author
,
body
,
action:
'discussion'
)
)
end
def
discussion_continued_in_issue
(
discussion
,
project
,
author
,
issue
)
body
=
"created
#{
issue
.
to_reference
}
to continue this discussion"
note_attributes
=
discussion
.
reply_attributes
.
merge
(
project:
project
,
author:
author
,
note:
body
)
note_attributes
[
:type
]
=
note_attributes
.
delete
(
:note_type
)
create_note
(
note_attributes
)
note_params
=
discussion
.
reply_attributes
.
merge
(
project:
project
,
author:
author
,
note:
body
)
note_params
[
:type
]
=
note_params
.
delete
(
:note_type
)
note
=
Note
.
create
(
note_params
.
merge
(
system:
true
))
note
.
system_note_metadata
=
SystemNoteMetadata
.
new
({
action:
'discussion'
})
note
end
# Called when the title of a Noteable is changed
...
...
@@ -253,7 +257,8 @@ module SystemNoteService
marked_new_title
=
Gitlab
::
Diff
::
InlineDiffMarker
.
new
(
new_title
).
mark
(
new_diffs
,
mode: :addition
,
markdown:
true
)
body
=
"changed title from **
#{
marked_old_title
}
** to **
#{
marked_new_title
}
**"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'title'
))
end
# Called when the confidentiality changes
...
...
@@ -269,7 +274,8 @@ module SystemNoteService
# Returns the created Note object
def
change_issue_confidentiality
(
issue
,
project
,
author
)
body
=
issue
.
confidential
?
'made the issue confidential'
:
'made the issue visible to everyone'
create_note
(
noteable:
issue
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
issue
,
project
,
author
,
body
,
action:
'confidentiality'
))
end
# Called when a branch in Noteable is changed
...
...
@@ -288,7 +294,8 @@ module SystemNoteService
# Returns the created Note object
def
change_branch
(
noteable
,
project
,
author
,
branch_type
,
old_branch
,
new_branch
)
body
=
"changed
#{
branch_type
}
branch from `
#{
old_branch
}
` to `
#{
new_branch
}
`"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'branch'
))
end
# Called when a branch in Noteable is added or deleted
...
...
@@ -314,7 +321,8 @@ module SystemNoteService
end
body
=
"
#{
verb
}
#{
branch_type
}
branch `
#{
branch
}
`"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'branch'
))
end
# Called when a branch is created from the 'new branch' button on a issue
...
...
@@ -325,7 +333,8 @@ module SystemNoteService
link
=
url_helpers
.
namespace_project_compare_url
(
project
.
namespace
,
project
,
from:
project
.
default_branch
,
to:
branch
)
body
=
"created branch [`
#{
branch
}
`](
#{
link
}
)"
create_note
(
noteable:
issue
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
issue
,
project
,
author
,
body
,
action:
'branch'
))
end
# Called when a Mentionable references a Noteable
...
...
@@ -349,23 +358,12 @@ module SystemNoteService
return
if
cross_reference_disallowed?
(
noteable
,
mentioner
)
gfm_reference
=
mentioner
.
gfm_reference
(
noteable
.
project
)
note_options
=
{
project:
noteable
.
project
,
author:
author
,
note:
cross_reference_note_content
(
gfm_reference
)
}
if
noteable
.
is_a?
(
Commit
)
note_options
.
merge!
(
noteable_type:
'Commit'
,
commit_id:
noteable
.
id
)
else
note_options
[
:noteable
]
=
noteable
end
body
=
cross_reference_note_content
(
gfm_reference
)
if
noteable
.
is_a?
(
ExternalIssue
)
noteable
.
project
.
issues_tracker
.
create_cross_reference_note
(
noteable
,
mentioner
,
author
)
else
create_note
(
note_options
)
create_note
(
NoteSummary
.
new
(
noteable
,
noteable
.
project
,
author
,
body
,
action:
'cross_reference'
)
)
end
end
...
...
@@ -444,7 +442,8 @@ module SystemNoteService
def
change_task_status
(
noteable
,
project
,
author
,
new_task
)
status_label
=
new_task
.
complete?
?
Taskable
::
COMPLETED
:
Taskable
::
INCOMPLETE
body
=
"marked the task **
#{
new_task
.
source
}
** as
#{
status_label
}
"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'task'
))
end
# Called when noteable has been moved to another project
...
...
@@ -466,7 +465,8 @@ module SystemNoteService
cross_reference
=
noteable_ref
.
to_reference
(
project
)
body
=
"moved
#{
direction
}
#{
cross_reference
}
"
create_note
(
noteable:
noteable
,
project:
project
,
author:
author
,
note:
body
)
create_note
(
NoteSummary
.
new
(
noteable
,
project
,
author
,
body
,
action:
'moved'
))
end
private
...
...
@@ -482,8 +482,11 @@ module SystemNoteService
end
end
def
create_note
(
args
=
{})
Note
.
create
(
args
.
merge
(
system:
true
))
def
create_note
(
note_summary
)
note
=
Note
.
create
(
note_summary
.
note
.
merge
(
system:
true
))
note
.
system_note_metadata
=
SystemNoteMetadata
.
new
(
note_summary
.
metadata
)
if
note_summary
.
metadata?
note
end
def
cross_reference_note_prefix
...
...
changelogs/unreleased/24784-system-notes-meta-data.yml
0 → 100644
View file @
c729d9da
---
title
:
Add metadata to system notes
merge_request
:
9964
author
:
db/migrate/20170314082049_create_system_note_metadata.rb
View file @
c729d9da
...
...
@@ -5,15 +5,19 @@ class CreateSystemNoteMetadata < ActiveRecord::Migration
disable_ddl_transaction!
def
change
def
up
create_table
:system_note_metadata
do
|
t
|
t
.
references
:note
,
null:
false
t
.
integer
:commit_count
t
.
string
:
ic
on
t
.
string
:
acti
on
t
.
timestamps
null:
false
end
add_concurrent_foreign_key
:system_note_metadata
,
:notes
,
column: :note_id
,
on_delete: :cascade
add_concurrent_foreign_key
:system_note_metadata
,
:notes
,
column: :note_id
end
def
down
drop_table
:system_note_metadata
end
end
db/schema.rb
View file @
c729d9da
...
...
@@ -1078,7 +1078,7 @@ ActiveRecord::Schema.define(version: 20170317203554) do
create_table
"system_note_metadata"
,
force: :cascade
do
|
t
|
t
.
integer
"note_id"
,
null:
false
t
.
integer
"commit_count"
t
.
string
"
ic
on"
t
.
string
"
acti
on"
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
end
...
...
spec/factories/system_note_metadata.rb
View file @
c729d9da
FactoryGirl
.
define
do
factory
:system_note_metadata
do
note
ic
on
'merge'
acti
on
'merge'
end
end
spec/lib/gitlab/import_export/all_models.yml
View file @
c729d9da
...
...
@@ -29,6 +29,7 @@ notes:
-
resolved_by
-
todos
-
events
-
system_note_metadata
label_links
:
-
target
-
label
...
...
spec/models/note_spec.rb
View file @
c729d9da
...
...
@@ -9,7 +9,6 @@ describe Note, models: true do
it
{
is_expected
.
to
belong_to
(
:author
).
class_name
(
'User'
)
}
it
{
is_expected
.
to
have_many
(
:todos
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:system_note_metadata
).
dependent
(
:destroy
)
}
end
describe
'modules'
do
...
...
spec/models/system_note_metadata_spec.rb
View file @
c729d9da
...
...
@@ -8,17 +8,17 @@ describe SystemNoteMetadata, models: true do
describe
'validation'
do
it
{
is_expected
.
to
validate_presence_of
(
:note
)
}
context
'when
ic
on type is invalid'
do
context
'when
acti
on type is invalid'
do
subject
do
build
(
:system_note_metadata
,
note:
build
(
:note
),
ic
on:
'invalid_type'
)
build
(
:system_note_metadata
,
note:
build
(
:note
),
acti
on:
'invalid_type'
)
end
it
{
is_expected
.
to
be_invalid
}
end
context
'when
ic
on type is valid'
do
context
'when
acti
on type is valid'
do
subject
do
build
(
:system_note_metadata
,
note:
build
(
:note
),
ic
on:
'merge'
)
build
(
:system_note_metadata
,
note:
build
(
:note
),
acti
on:
'merge'
)
end
it
{
is_expected
.
to
be_valid
}
...
...
spec/services/note_summary_spec.rb
0 → 100644
View file @
c729d9da
require
'spec_helper'
describe
NoteSummary
,
services:
true
do
let
(
:project
)
{
build
(
:empty_project
)
}
let
(
:noteable
)
{
build
(
:issue
)
}
let
(
:user
)
{
build
(
:user
)
}
def
create_note_summary
described_class
.
new
(
noteable
,
project
,
user
,
'note'
,
action:
'icon'
,
commit_count:
5
)
end
describe
'#metadata?'
do
it
'returns true when metadata present'
do
expect
(
create_note_summary
.
metadata?
).
to
be_truthy
end
it
'returns false when metadata not present'
do
expect
(
described_class
.
new
(
noteable
,
project
,
user
,
'note'
).
metadata?
).
to
be_falsey
end
end
describe
'#note'
do
it
'returns note hash'
do
expect
(
create_note_summary
.
note
).
to
eq
(
noteable:
noteable
,
project:
project
,
author:
user
,
note:
'note'
)
end
context
'when noteable is a commit'
do
let
(
:noteable
)
{
build
(
:commit
)
}
it
'returns note hash specific to commit'
do
expect
(
create_note_summary
.
note
).
to
eq
(
noteable:
nil
,
project:
project
,
author:
user
,
note:
'note'
,
noteable_type:
'Commit'
,
commit_id:
noteable
.
id
)
end
end
end
describe
'#metadata'
do
it
'returns metadata hash'
do
expect
(
create_note_summary
.
metadata
).
to
eq
(
action:
'icon'
,
commit_count:
5
)
end
end
end
spec/services/system_note_service_spec.rb
View file @
c729d9da
This diff is collapsed.
Click to expand it.
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