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
d07b179b
Commit
d07b179b
authored
Jul 30, 2018
by
Jan Provaznik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor fixes
parent
76607cfa
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
95 additions
and
28 deletions
+95
-28
app/models/concerns/label_eventable.rb
app/models/concerns/label_eventable.rb
+1
-3
app/models/resource_label_event.rb
app/models/resource_label_event.rb
+8
-12
app/services/resource_label_event_service.rb
app/services/resource_label_event_service.rb
+5
-2
changelogs/unreleased/jprovazn-resource-events.yml
changelogs/unreleased/jprovazn-resource-events.yml
+5
-0
ee/app/models/ee/resource_label_event.rb
ee/app/models/ee/resource_label_event.rb
+9
-6
ee/app/services/ee/resource_label_event_service.rb
ee/app/services/ee/resource_label_event_service.rb
+13
-0
ee/spec/models/ee/resource_label_event_spec.rb
ee/spec/models/ee/resource_label_event_spec.rb
+18
-0
spec/services/resource_label_event_service_spec.rb
spec/services/resource_label_event_service_spec.rb
+36
-5
No files found.
app/models/concerns/label_eventable.rb
View file @
d07b179b
...
...
@@ -2,9 +2,7 @@
# == LabelEventable concern
#
# Contains functionality related to objects that support adding/removing events.
#
# Used by Issue and MergeRequest.
# Contains functionality related to objects that support adding/removing labels.
#
module
LabelEventable
...
...
app/models/resource_label_event.rb
View file @
d07b179b
...
...
@@ -3,8 +3,6 @@
class
ResourceLabelEvent
<
ActiveRecord
::
Base
prepend
EE
::
ResourceLabelEvent
ISSUABLE_COLUMNS
=
%i(issue_id merge_request_id)
.
freeze
belongs_to
:user
belongs_to
:issue
belongs_to
:merge_request
...
...
@@ -12,28 +10,26 @@ class ResourceLabelEvent < ActiveRecord::Base
validates
:user
,
presence:
true
,
on: :create
validates
:label
,
presence:
true
,
on: :create
validate
:
issuable_id_is_present
validate
:
exactly_one_issuable
enum
action:
{
add:
1
,
remove:
2
}
def
self
.
issuable_columns
%i(issue_id merge_request_id)
.
freeze
end
def
issuable
issue
||
merge_request
end
private
def
issuable_columns
ISSUABLE_COLUMNS
end
def
issuable_id_is_present
ids
=
issuable_columns
.
find_all
{
|
attr
|
self
[
attr
]}
if
ids
.
size
!=
1
errors
.
add
(
:base
,
"Exactly one of
#{
issuable_columns
.
join
(
', '
)
}
is required"
)
def
exactly_one_issuable
if
self
.
class
.
issuable_columns
.
count
{
|
attr
|
self
[
attr
]
}
!=
1
errors
.
add
(
:base
,
"Exactly one of
#{
self
.
class
.
issuable_columns
.
join
(
', '
)
}
is required"
)
end
end
end
app/services/resource_label_event_service.rb
View file @
d07b179b
# frozen_string_literal: true
module
ResourceLabelEventService
prepend
EE
::
ResourceLabelEventService
extend
self
def
change_labels
(
resource
,
user
,
added_labels
,
removed_labels
)
...
...
@@ -23,9 +25,10 @@ module ResourceLabelEventService
private
def
resource_column
(
resource
)
if
resource
.
is_a?
(
Issue
)
case
resource
when
Issue
:issue_id
elsif
resource
.
is_a?
(
MergeRequest
)
when
MergeRequest
:merge_request_id
else
raise
ArgumentError
,
"Unknown resource type
#{
resource
.
class
.
name
}
"
...
...
changelogs/unreleased/jprovazn-resource-events.yml
0 → 100644
View file @
d07b179b
---
title
:
Add new model for tracking label events.
merge_request
:
author
:
type
:
added
ee/app/models/ee/resource_label_event.rb
View file @
d07b179b
...
...
@@ -5,16 +5,19 @@ module EE
extend
ActiveSupport
::
Concern
extend
::
Gitlab
::
Utils
::
Override
EE_ISSUABLE_COLUMNS
=
%i(epic_id)
.
freeze
prepended
do
belongs_to
:epic
end
class_methods
do
def
issuable_columns
%i(epic_id)
.
freeze
+
super
end
end
override
:issuable
def
issuable
epic
||
super
end
override
:issuable_columns
def
issuable_columns
EE_ISSUABLE_COLUMNS
+
super
end
end
end
ee/app/services/ee/resource_label_event_service.rb
0 → 100644
View file @
d07b179b
# frozen_string_literal: true
module
EE
module
ResourceLabelEventService
extend
ActiveSupport
::
Concern
extend
::
Gitlab
::
Utils
::
Override
override
:resource_column
def
resource_column
(
resource
)
resource
.
is_a?
(
Epic
)
?
:epic_id
:
super
end
end
end
ee/spec/models/ee/resource_label_event_spec.rb
0 → 100644
View file @
d07b179b
# frozen_string_literal: true
require
'rails_helper'
RSpec
.
describe
ResourceLabelEvent
,
type: :model
do
subject
{
build
(
:resource_label_event
)
}
let
(
:epic
)
{
create
(
:epic
)
}
describe
'validations'
do
describe
'Issuable validation'
do
it
'is valid if only epic_id is set'
do
subject
.
attributes
=
{
epic:
epic
,
issue:
nil
,
merge_request:
nil
}
expect
(
subject
).
to
be_valid
end
end
end
end
spec/services/resource_label_event_service_spec.rb
View file @
d07b179b
...
...
@@ -11,12 +11,43 @@ describe ResourceLabelEventService do
subject
{
described_class
.
change_labels
(
resource
,
author
,
added
,
removed
)
}
let
(
:labels
)
{
create_list
(
:label
,
2
,
project:
project
)
}
let
(
:added
)
{
[
labels
[
0
]]
}
let
(
:removed
)
{
[
labels
[
1
]]
}
it
'creates an event for each label in single query'
do
expect
(
Gitlab
::
Database
).
to
receive
(
:bulk_insert
).
once
.
and_call_original
expect
{
subject
}.
to
change
{
resource
.
resource_label_events
.
count
}.
from
(
0
).
to
(
2
)
def
expect_label_event
(
event
,
label
,
action
)
expect
(
event
.
user
).
to
eq
(
author
)
expect
(
event
.
label
).
to
eq
(
label
)
expect
(
event
.
action
).
to
eq
(
action
)
end
context
'when adding a label'
do
let
(
:added
)
{
[
labels
[
0
]]
}
let
(
:removed
)
{
[]
}
it
'creates new label event'
do
expect
{
subject
}.
to
change
{
resource
.
resource_label_events
.
count
}.
from
(
0
).
to
(
1
)
expect_label_event
(
resource
.
resource_label_events
.
first
,
labels
[
0
],
'add'
)
end
end
context
'when removing a label'
do
let
(
:added
)
{
[]
}
let
(
:removed
)
{
[
labels
[
1
]]
}
it
'creates new label event'
do
expect
{
subject
}.
to
change
{
resource
.
resource_label_events
.
count
}.
from
(
0
).
to
(
1
)
expect_label_event
(
resource
.
resource_label_events
.
first
,
labels
[
1
],
'remove'
)
end
end
context
'when both adding and removing labels'
do
let
(
:added
)
{
[
labels
[
0
]]
}
let
(
:removed
)
{
[
labels
[
1
]]
}
it
'creates all label events in a single query'
do
expect
(
Gitlab
::
Database
).
to
receive
(
:bulk_insert
).
once
.
and_call_original
expect
{
subject
}.
to
change
{
resource
.
resource_label_events
.
count
}.
from
(
0
).
to
(
2
)
end
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