Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
f4be2d4e
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f4be2d4e
编写于
2月 26, 2019
作者:
H
Heinrich Lee Yu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "Merge branch '56726-fix-n+1-in-issues-and-merge-requests-api' into 'master'"
This reverts merge request !25042
上级
f5201a81
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
58 addition
and
51 deletion
+58
-51
app/models/concerns/issuable.rb
app/models/concerns/issuable.rb
+3
-3
app/models/issue.rb
app/models/issue.rb
+0
-4
app/views/shared/_issuable_meta_data.html.haml
app/views/shared/_issuable_meta_data.html.haml
+1
-1
changelogs/unreleased/56726-fix-n-1-in-issues-and-merge-requests-api.yml
...leased/56726-fix-n-1-in-issues-and-merge-requests-api.yml
+0
-5
lib/api/entities.rb
lib/api/entities.rb
+50
-22
spec/lib/gitlab/issuable_metadata_spec.rb
spec/lib/gitlab/issuable_metadata_spec.rb
+4
-4
spec/requests/api/merge_requests_spec.rb
spec/requests/api/merge_requests_spec.rb
+0
-12
未找到文件。
app/models/concerns/issuable.rb
浏览文件 @
f4be2d4e
...
...
@@ -28,7 +28,7 @@ module Issuable
# This object is used to gather issuable meta data for displaying
# upvotes, downvotes, notes and closing merge requests count for issues and merge requests
# lists avoiding n+1 queries and improving performance.
IssuableMeta
=
Struct
.
new
(
:upvotes
,
:downvotes
,
:
user_
notes_count
,
:merge_requests_count
)
IssuableMeta
=
Struct
.
new
(
:upvotes
,
:downvotes
,
:notes_count
,
:merge_requests_count
)
included
do
cache_markdown_field
:title
,
pipeline: :single_line
...
...
@@ -36,8 +36,8 @@ module Issuable
redact_field
:description
belongs_to
:author
,
class_name:
'User'
belongs_to
:updated_by
,
class_name:
'User'
belongs_to
:author
,
class_name:
"User"
belongs_to
:updated_by
,
class_name:
"User"
belongs_to
:last_edited_by
,
class_name:
'User'
belongs_to
:milestone
...
...
app/models/issue.rb
浏览文件 @
f4be2d4e
...
...
@@ -263,10 +263,6 @@ class Issue < ActiveRecord::Base
end
# rubocop: enable CodeReuse/ServiceClass
def
merge_requests_count
merge_requests_closing_issues
.
count
end
private
def
ensure_metrics
...
...
app/views/shared/_issuable_meta_data.html.haml
浏览文件 @
f4be2d4e
-
note_count
=
@issuable_meta_data
[
issuable
.
id
].
user_
notes_count
-
note_count
=
@issuable_meta_data
[
issuable
.
id
].
notes_count
-
issue_votes
=
@issuable_meta_data
[
issuable
.
id
]
-
upvotes
,
downvotes
=
issue_votes
.
upvotes
,
issue_votes
.
downvotes
-
issuable_url
=
@collection_type
==
"Issue"
?
issue_path
(
issuable
,
anchor:
'notes'
)
:
merge_request_path
(
issuable
,
anchor:
'notes'
)
...
...
changelogs/unreleased/56726-fix-n-1-in-issues-and-merge-requests-api.yml
已删除
100644 → 0
浏览文件 @
f5201a81
---
title
:
Fix N+1 query in Issues and MergeRequest API when issuable_metadata is present
merge_request
:
25042
author
:
Alex Koval
type
:
other
lib/api/entities.rb
浏览文件 @
f4be2d4e
...
...
@@ -468,17 +468,11 @@ module API
end
end
class
Issueable
Entity
<
Grape
::
Entity
class
Project
Entity
<
Grape
::
Entity
expose
:id
,
:iid
expose
(
:project_id
)
{
|
entity
|
entity
&
.
project
.
try
(
:id
)
}
expose
:title
,
:description
expose
:state
,
:created_at
,
:updated_at
# Avoids an N+1 query when metadata is included
def
issuable_metadata
(
subject
,
options
,
method
)
cached_subject
=
options
.
dig
(
:issuable_metadata
,
subject
.
id
)
(
cached_subject
||
subject
).
public_send
(
method
)
# rubocop: disable GitlabSecurity/PublicSend
end
end
class
Diff
<
Grape
::
Entity
...
...
@@ -521,35 +515,57 @@ module API
end
end
class
IssueBasic
<
Issueable
Entity
class
IssueBasic
<
Project
Entity
expose
:closed_at
expose
:closed_by
,
using:
Entities
::
UserBasic
expose
:labels
do
|
issue
|
expose
:labels
do
|
issue
,
options
|
# Avoids an N+1 query since labels are preloaded
issue
.
labels
.
map
(
&
:title
).
sort
end
expose
:milestone
,
using:
Entities
::
Milestone
expose
:assignees
,
:author
,
using:
Entities
::
UserBasic
expose
:assignee
,
using:
::
API
::
Entities
::
UserBasic
do
|
issue
|
expose
:assignee
,
using:
::
API
::
Entities
::
UserBasic
do
|
issue
,
options
|
issue
.
assignees
.
first
end
expose
(
:user_notes_count
)
{
|
issue
,
options
|
issuable_metadata
(
issue
,
options
,
:user_notes_count
)
}
expose
(
:merge_requests_count
)
{
|
issue
,
options
|
issuable_metadata
(
issue
,
options
,
:merge_requests_count
)
}
expose
(
:upvotes
)
{
|
issue
,
options
|
issuable_metadata
(
issue
,
options
,
:upvotes
)
}
expose
(
:downvotes
)
{
|
issue
,
options
|
issuable_metadata
(
issue
,
options
,
:downvotes
)
}
expose
:user_notes_count
expose
:upvotes
do
|
issue
,
options
|
if
options
[
:issuable_metadata
]
# Avoids an N+1 query when metadata is included
options
[
:issuable_metadata
][
issue
.
id
].
upvotes
else
issue
.
upvotes
end
end
expose
:downvotes
do
|
issue
,
options
|
if
options
[
:issuable_metadata
]
# Avoids an N+1 query when metadata is included
options
[
:issuable_metadata
][
issue
.
id
].
downvotes
else
issue
.
downvotes
end
end
expose
:due_date
expose
:confidential
expose
:discussion_locked
expose
:web_url
do
|
issue
|
expose
:web_url
do
|
issue
,
options
|
Gitlab
::
UrlBuilder
.
build
(
issue
)
end
expose
:time_stats
,
using:
'API::Entities::IssuableTimeStats'
do
|
issue
|
issue
end
expose
:merge_requests_count
do
|
issue
,
options
|
if
options
[
:issuable_metadata
]
# Avoids an N+1 query when metadata is included
options
[
:issuable_metadata
][
issue
.
id
].
merge_requests_count
else
issue
.
merge_requests_closing_issues
.
count
end
end
end
class
Issue
<
IssueBasic
...
...
@@ -612,14 +628,14 @@ module API
end
end
class
MergeRequestSimple
<
Issueable
Entity
class
MergeRequestSimple
<
Project
Entity
expose
:title
expose
:web_url
do
|
merge_request
,
options
|
Gitlab
::
UrlBuilder
.
build
(
merge_request
)
end
end
class
MergeRequestBasic
<
Issueable
Entity
class
MergeRequestBasic
<
Project
Entity
expose
:merged_by
,
using:
Entities
::
UserBasic
do
|
merge_request
,
_options
|
merge_request
.
metrics
&
.
merged_by
end
...
...
@@ -643,12 +659,23 @@ module API
MarkupHelper
.
markdown_field
(
entity
,
:description
)
end
expose
:target_branch
,
:source_branch
expose
(
:user_notes_count
)
{
|
merge_request
,
options
|
issuable_metadata
(
merge_request
,
options
,
:user_notes_count
)
}
expose
(
:upvotes
)
{
|
merge_request
,
options
|
issuable_metadata
(
merge_request
,
options
,
:upvotes
)
}
expose
(
:downvotes
)
{
|
merge_request
,
options
|
issuable_metadata
(
merge_request
,
options
,
:downvotes
)
}
expose
:upvotes
do
|
merge_request
,
options
|
if
options
[
:issuable_metadata
]
options
[
:issuable_metadata
][
merge_request
.
id
].
upvotes
else
merge_request
.
upvotes
end
end
expose
:downvotes
do
|
merge_request
,
options
|
if
options
[
:issuable_metadata
]
options
[
:issuable_metadata
][
merge_request
.
id
].
downvotes
else
merge_request
.
downvotes
end
end
expose
:author
,
:assignee
,
using:
Entities
::
UserBasic
expose
:source_project_id
,
:target_project_id
expose
:labels
do
|
merge_request
|
expose
:labels
do
|
merge_request
,
options
|
# Avoids an N+1 query since labels are preloaded
merge_request
.
labels
.
map
(
&
:title
).
sort
end
...
...
@@ -666,6 +693,7 @@ module API
end
expose
:diff_head_sha
,
as: :sha
expose
:merge_commit_sha
expose
:user_notes_count
expose
:discussion_locked
expose
:should_remove_source_branch?
,
as: :should_remove_source_branch
expose
:force_remove_source_branch?
,
as: :force_remove_source_branch
...
...
@@ -673,7 +701,7 @@ module API
# Deprecated
expose
:allow_collaboration
,
as: :allow_maintainer_to_push
,
if:
->
(
merge_request
,
_
)
{
merge_request
.
for_fork?
}
expose
:web_url
do
|
merge_request
|
expose
:web_url
do
|
merge_request
,
options
|
Gitlab
::
UrlBuilder
.
build
(
merge_request
)
end
...
...
spec/lib/gitlab/issuable_metadata_spec.rb
浏览文件 @
f4be2d4e
...
...
@@ -28,12 +28,12 @@ describe Gitlab::IssuableMetadata do
expect
(
data
.
count
).
to
eq
(
2
)
expect
(
data
[
issue
.
id
].
upvotes
).
to
eq
(
1
)
expect
(
data
[
issue
.
id
].
downvotes
).
to
eq
(
0
)
expect
(
data
[
issue
.
id
].
user_
notes_count
).
to
eq
(
0
)
expect
(
data
[
issue
.
id
].
notes_count
).
to
eq
(
0
)
expect
(
data
[
issue
.
id
].
merge_requests_count
).
to
eq
(
1
)
expect
(
data
[
closed_issue
.
id
].
upvotes
).
to
eq
(
0
)
expect
(
data
[
closed_issue
.
id
].
downvotes
).
to
eq
(
1
)
expect
(
data
[
closed_issue
.
id
].
user_
notes_count
).
to
eq
(
0
)
expect
(
data
[
closed_issue
.
id
].
notes_count
).
to
eq
(
0
)
expect
(
data
[
closed_issue
.
id
].
merge_requests_count
).
to
eq
(
0
)
end
end
...
...
@@ -51,12 +51,12 @@ describe Gitlab::IssuableMetadata do
expect
(
data
.
count
).
to
eq
(
2
)
expect
(
data
[
merge_request
.
id
].
upvotes
).
to
eq
(
1
)
expect
(
data
[
merge_request
.
id
].
downvotes
).
to
eq
(
1
)
expect
(
data
[
merge_request
.
id
].
user_
notes_count
).
to
eq
(
1
)
expect
(
data
[
merge_request
.
id
].
notes_count
).
to
eq
(
1
)
expect
(
data
[
merge_request
.
id
].
merge_requests_count
).
to
eq
(
0
)
expect
(
data
[
merge_request_closed
.
id
].
upvotes
).
to
eq
(
0
)
expect
(
data
[
merge_request_closed
.
id
].
downvotes
).
to
eq
(
0
)
expect
(
data
[
merge_request_closed
.
id
].
user_
notes_count
).
to
eq
(
0
)
expect
(
data
[
merge_request_closed
.
id
].
notes_count
).
to
eq
(
0
)
expect
(
data
[
merge_request_closed
.
id
].
merge_requests_count
).
to
eq
(
0
)
end
end
...
...
spec/requests/api/merge_requests_spec.rb
浏览文件 @
f4be2d4e
...
...
@@ -320,18 +320,6 @@ describe API::MergeRequests do
expect
(
json_response
.
first
[
'title'
]).
to
eq
merge_request_closed
.
title
expect
(
json_response
.
first
[
'id'
]).
to
eq
merge_request_closed
.
id
end
it
'avoids N+1 queries'
do
control
=
ActiveRecord
::
QueryRecorder
.
new
do
get
api
(
"/projects/
#{
project
.
id
}
/merge_requests"
,
user
)
end
.
count
create
(
:merge_request
,
author:
user
,
assignee:
user
,
source_project:
project
,
target_project:
project
,
created_at:
base_time
)
expect
do
get
api
(
"/projects/
#{
project
.
id
}
/merge_requests"
,
user
)
end
.
not_to
exceed_query_limit
(
control
)
end
end
describe
"GET /groups/:id/merge_requests"
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录