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
6b2b3d7f
Commit
6b2b3d7f
authored
Aug 01, 2018
by
Jan Provaznik
Committed by
Sean McGivern
Aug 01, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resource event model
parent
bd659f70
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
251 additions
and
1 deletion
+251
-1
app/models/concerns/label_eventable.rb
app/models/concerns/label_eventable.rb
+16
-0
app/models/issue.rb
app/models/issue.rb
+1
-0
app/models/merge_request.rb
app/models/merge_request.rb
+1
-0
app/models/resource_label_event.rb
app/models/resource_label_event.rb
+35
-0
app/services/resource_events/change_labels_service.rb
app/services/resource_events/change_labels_service.rb
+43
-0
changelogs/unreleased/jprovazn-resource-events.yml
changelogs/unreleased/jprovazn-resource-events.yml
+5
-0
db/migrate/20180726172057_create_resource_label_events.rb
db/migrate/20180726172057_create_resource_label_events.rb
+18
-0
db/schema.rb
db/schema.rb
+19
-1
spec/factories/resource_label_events.rb
spec/factories/resource_label_events.rb
+10
-0
spec/lib/gitlab/import_export/all_models.yml
spec/lib/gitlab/import_export/all_models.yml
+2
-0
spec/models/resource_label_event_spec.rb
spec/models/resource_label_event_spec.rb
+48
-0
spec/services/resource_events/change_labels_service_spec.rb
spec/services/resource_events/change_labels_service_spec.rb
+53
-0
No files found.
app/models/concerns/label_eventable.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
# == LabelEventable concern
#
# Contains functionality related to objects that support adding/removing labels.
#
# This concern is not used yet, it will be used for:
# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
module
LabelEventable
extend
ActiveSupport
::
Concern
included
do
has_many
:resource_label_events
end
end
app/models/issue.rb
View file @
6b2b3d7f
...
...
@@ -12,6 +12,7 @@ class Issue < ActiveRecord::Base
include
TimeTrackable
include
ThrottledTouch
include
IgnorableColumn
include
LabelEventable
ignore_column
:assignee_id
,
:branch_name
,
:deleted_at
...
...
app/models/merge_request.rb
View file @
6b2b3d7f
...
...
@@ -10,6 +10,7 @@ class MergeRequest < ActiveRecord::Base
include
EachBatch
include
ThrottledTouch
include
Gitlab
::
Utils
::
StrongMemoize
include
LabelEventable
ignore_column
:locked_at
,
:ref_fetched
,
...
...
app/models/resource_label_event.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
# This model is not used yet, it will be used for:
# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
class
ResourceLabelEvent
<
ActiveRecord
::
Base
belongs_to
:user
belongs_to
:issue
belongs_to
:merge_request
belongs_to
:label
validates
:user
,
presence:
true
,
on: :create
validates
:label
,
presence:
true
,
on: :create
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
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_events/change_labels_service.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
# This service is not used yet, it will be used for:
# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
module
ResourceEvents
class
ChangeLabelsService
attr_reader
:resource
,
:user
def
initialize
(
resource
,
user
)
@resource
,
@user
=
resource
,
user
end
def
execute
(
added_labels:
[],
removed_labels:
[])
label_hash
=
{
resource_column
(
resource
)
=>
resource
.
id
,
user_id:
user
.
id
,
created_at:
Time
.
now
}
labels
=
added_labels
.
map
do
|
label
|
label_hash
.
merge
(
label_id:
label
.
id
,
action:
ResourceLabelEvent
.
actions
[
'add'
])
end
labels
+=
removed_labels
.
map
do
|
label
|
label_hash
.
merge
(
label_id:
label
.
id
,
action:
ResourceLabelEvent
.
actions
[
'remove'
])
end
Gitlab
::
Database
.
bulk_insert
(
ResourceLabelEvent
.
table_name
,
labels
)
end
private
def
resource_column
(
resource
)
case
resource
when
Issue
:issue_id
when
MergeRequest
:merge_request_id
else
raise
ArgumentError
,
"Unknown resource type
#{
resource
.
class
.
name
}
"
end
end
end
end
changelogs/unreleased/jprovazn-resource-events.yml
0 → 100644
View file @
6b2b3d7f
---
title
:
Add new model for tracking label events.
merge_request
:
author
:
type
:
added
db/migrate/20180726172057_create_resource_label_events.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
class
CreateResourceLabelEvents
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
def
change
create_table
:resource_label_events
,
id: :bigserial
do
|
t
|
t
.
integer
:action
,
null:
false
t
.
references
:issue
,
null:
true
,
index:
true
,
foreign_key:
{
on_delete: :cascade
}
t
.
references
:merge_request
,
null:
true
,
index:
true
,
foreign_key:
{
on_delete: :cascade
}
t
.
references
:label
,
index:
true
,
foreign_key:
{
on_delete: :nullify
}
t
.
references
:user
,
index:
true
,
foreign_key:
{
on_delete: :nullify
}
t
.
datetime_with_timezone
:created_at
,
null:
false
end
end
end
db/schema.rb
View file @
6b2b3d7f
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2018072
2103201
)
do
ActiveRecord
::
Schema
.
define
(
version:
2018072
6172057
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -1787,6 +1787,20 @@ ActiveRecord::Schema.define(version: 20180722103201) do
add_index
"remote_mirrors"
,
[
"last_successful_update_at"
],
name:
"index_remote_mirrors_on_last_successful_update_at"
,
using: :btree
add_index
"remote_mirrors"
,
[
"project_id"
],
name:
"index_remote_mirrors_on_project_id"
,
using: :btree
create_table
"resource_label_events"
,
id: :bigserial
,
force: :cascade
do
|
t
|
t
.
integer
"action"
,
null:
false
t
.
integer
"issue_id"
t
.
integer
"merge_request_id"
t
.
integer
"label_id"
t
.
integer
"user_id"
t
.
datetime_with_timezone
"created_at"
,
null:
false
end
add_index
"resource_label_events"
,
[
"issue_id"
],
name:
"index_resource_label_events_on_issue_id"
,
using: :btree
add_index
"resource_label_events"
,
[
"label_id"
],
name:
"index_resource_label_events_on_label_id"
,
using: :btree
add_index
"resource_label_events"
,
[
"merge_request_id"
],
name:
"index_resource_label_events_on_merge_request_id"
,
using: :btree
add_index
"resource_label_events"
,
[
"user_id"
],
name:
"index_resource_label_events_on_user_id"
,
using: :btree
create_table
"routes"
,
force: :cascade
do
|
t
|
t
.
integer
"source_id"
,
null:
false
t
.
string
"source_type"
,
null:
false
...
...
@@ -2337,6 +2351,10 @@ ActiveRecord::Schema.define(version: 20180722103201) do
add_foreign_key
"push_event_payloads"
,
"events"
,
name:
"fk_36c74129da"
,
on_delete: :cascade
add_foreign_key
"releases"
,
"projects"
,
name:
"fk_47fe2a0596"
,
on_delete: :cascade
add_foreign_key
"remote_mirrors"
,
"projects"
,
on_delete: :cascade
add_foreign_key
"resource_label_events"
,
"issues"
,
on_delete: :cascade
add_foreign_key
"resource_label_events"
,
"labels"
,
on_delete: :nullify
add_foreign_key
"resource_label_events"
,
"merge_requests"
,
on_delete: :cascade
add_foreign_key
"resource_label_events"
,
"users"
,
on_delete: :nullify
add_foreign_key
"services"
,
"projects"
,
name:
"fk_71cce407f9"
,
on_delete: :cascade
add_foreign_key
"snippets"
,
"projects"
,
name:
"fk_be41fd4bb7"
,
on_delete: :cascade
add_foreign_key
"subscriptions"
,
"projects"
,
on_delete: :cascade
...
...
spec/factories/resource_label_events.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
FactoryBot
.
define
do
factory
:resource_label_event
do
user
{
issue
.
project
.
creator
}
action
:add
label
issue
end
end
spec/lib/gitlab/import_export/all_models.yml
View file @
6b2b3d7f
...
...
@@ -7,6 +7,7 @@ issues:
-
updated_by
-
milestone
-
notes
-
resource_label_events
-
label_links
-
labels
-
last_edited_by
...
...
@@ -76,6 +77,7 @@ merge_requests:
-
updated_by
-
milestone
-
notes
-
resource_label_events
-
label_links
-
labels
-
last_edited_by
...
...
spec/models/resource_label_event_spec.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
require
'rails_helper'
RSpec
.
describe
ResourceLabelEvent
,
type: :model
do
subject
{
build
(
:resource_label_event
)
}
let
(
:issue
)
{
create
(
:issue
)
}
let
(
:merge_request
)
{
create
(
:merge_request
)
}
describe
'associations'
do
it
{
is_expected
.
to
belong_to
(
:user
)
}
it
{
is_expected
.
to
belong_to
(
:issue
)
}
it
{
is_expected
.
to
belong_to
(
:merge_request
)
}
it
{
is_expected
.
to
belong_to
(
:label
)
}
end
describe
'validations'
do
it
{
is_expected
.
to
be_valid
}
it
{
is_expected
.
to
validate_presence_of
(
:label
)
}
it
{
is_expected
.
to
validate_presence_of
(
:user
)
}
describe
'Issuable validation'
do
it
'is invalid if issue_id and merge_request_id are missing'
do
subject
.
attributes
=
{
issue:
nil
,
merge_request:
nil
}
expect
(
subject
).
to
be_invalid
end
it
'is invalid if issue_id and merge_request_id are set'
do
subject
.
attributes
=
{
issue:
issue
,
merge_request:
merge_request
}
expect
(
subject
).
to
be_invalid
end
it
'is valid if only issue_id is set'
do
subject
.
attributes
=
{
issue:
issue
,
merge_request:
nil
}
expect
(
subject
).
to
be_valid
end
it
'is valid if only merge_request_id is set'
do
subject
.
attributes
=
{
merge_request:
merge_request
,
issue:
nil
}
expect
(
subject
).
to
be_valid
end
end
end
end
spec/services/resource_events/change_labels_service_spec.rb
0 → 100644
View file @
6b2b3d7f
# frozen_string_literal: true
require
'spec_helper'
describe
ResourceEvents
::
ChangeLabelsService
do
set
(
:project
)
{
create
(
:project
)
}
set
(
:author
)
{
create
(
:user
)
}
let
(
:resource
)
{
create
(
:issue
,
project:
project
)
}
describe
'.change_labels'
do
subject
{
described_class
.
new
(
resource
,
author
).
execute
(
added_labels:
added
,
removed_labels:
removed
)
}
let
(
:labels
)
{
create_list
(
:label
,
2
,
project:
project
)
}
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