Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
3183092c
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,体验更适合开发者的 AI 搜索 >>
提交
3183092c
编写于
1月 14, 2016
作者:
R
Rémy Coutable
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add pagination headers to already paginated API resources
上级
9f8c38bd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
52 addition
and
21 deletion
+52
-21
CHANGELOG
CHANGELOG
+1
-0
lib/api/helpers.rb
lib/api/helpers.rb
+19
-11
lib/api/notes.rb
lib/api/notes.rb
+3
-9
spec/requests/api/commit_status_spec.rb
spec/requests/api/commit_status_spec.rb
+5
-1
spec/requests/api/notes_spec.rb
spec/requests/api/notes_spec.rb
+4
-0
spec/support/api/pagination_shared_examples.rb
spec/support/api/pagination_shared_examples.rb
+20
-0
未找到文件。
CHANGELOG
浏览文件 @
3183092c
Please view this file on the master branch, on stable branches it's out of date.
v 8.4.0 (unreleased)
- Add pagination headers to already paginated API resources
- Autocomplete data is now always loaded, instead of when focusing a comment text area (Yorick Peterse)
- Improved performance of finding issues for an entire group (Yorick Peterse)
- Added custom application performance measuring system powered by InfluxDB (Yorick Peterse)
...
...
lib/api/helpers.rb
浏览文件 @
3183092c
...
...
@@ -97,11 +97,9 @@ module API
end
def
paginate
(
relation
)
per_page
=
params
[
:per_page
].
to_i
paginated
=
relation
.
page
(
params
[
:page
]).
per
(
per_page
)
add_pagination_headers
(
paginated
,
per_page
)
paginated
relation
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
].
to_i
).
tap
do
|
data
|
add_pagination_headers
(
data
)
end
end
def
authenticate!
...
...
@@ -327,16 +325,26 @@ module API
private
def
add_pagination_headers
(
paginated
,
per_page
)
def
add_pagination_headers
(
paginated_data
)
header
'X-Total'
,
paginated_data
.
total_count
.
to_s
header
'X-Total-Pages'
,
paginated_data
.
total_pages
.
to_s
header
'X-Per-Page'
,
paginated_data
.
limit_value
.
to_s
header
'X-Page'
,
paginated_data
.
current_page
.
to_s
header
'X-Next-Page'
,
paginated_data
.
next_page
.
to_s
header
'X-Prev-Page'
,
paginated_data
.
prev_page
.
to_s
header
'Link'
,
pagination_links
(
paginated_data
)
end
def
pagination_links
(
paginated_data
)
request_url
=
request
.
url
.
split
(
'?'
).
first
links
=
[]
links
<<
%(<#{request_url}?page=#{paginated
.current_page - 1}&per_page=#{per_page}>; rel="prev")
unless
paginated
.
first_page?
links
<<
%(<#{request_url}?page=#{paginated
.current_page + 1}&per_page=#{per_page}>; rel="next")
unless
paginated
.
last_page?
links
<<
%(<#{request_url}?page=1&per_page=#{p
er_pag
e}>; rel="first")
links
<<
%(<#{request_url}?page=#{paginated
.total_pages}&per_page=#{per_pag
e}>; rel="last")
links
<<
%(<#{request_url}?page=#{paginated
_data.current_page - 1}&per_page=#{paginated_data.limit_value}>; rel="prev")
unless
paginated_data
.
first_page?
links
<<
%(<#{request_url}?page=#{paginated
_data.current_page + 1}&per_page=#{paginated_data.limit_value}>; rel="next")
unless
paginated_data
.
last_page?
links
<<
%(<#{request_url}?page=1&per_page=#{p
aginated_data.limit_valu
e}>; rel="first")
links
<<
%(<#{request_url}?page=#{paginated
_data.total_pages}&per_page=#{paginated_data.limit_valu
e}>; rel="last")
header
'Link'
,
links
.
join
(
', '
)
links
.
join
(
', '
)
end
def
abilities
...
...
lib/api/notes.rb
浏览文件 @
3183092c
...
...
@@ -22,17 +22,11 @@ module API
@noteable
=
user_project
.
send
(
:"
#{
noteables_str
}
"
).
find
(
params
[
:"
#{
noteable_id_str
}
"
])
# We exclude notes that are cross-references and that cannot be viewed
# by the current user. By doing this exclusion at this level and not
# at the DB query level (which we cannot in that case), the current
# page can have less elements than :per_page even if
# there's more than one page.
# by the current user.
notes
=
# paginate() only works with a relation. This could lead to a
# mismatch between the pagination headers info and the actual notes
# array returned, but this is really a edge-case.
paginate
(
@noteable
.
notes
).
@noteable
.
notes
.
reject
{
|
n
|
n
.
cross_reference_not_visible_for?
(
current_user
)
}
present
notes
,
with:
Entities
::
Note
present
paginate
(
Kaminari
.
paginate_array
(
notes
))
,
with:
Entities
::
Note
end
# Get a single +noteable+ note
...
...
spec/requests/api/commit_status_spec.rb
浏览文件 @
3183092c
require
'spec_helper'
describe
API
::
API
,
api:
true
do
describe
API
::
CommitStatus
,
api:
true
do
include
ApiHelpers
let
(
:user
)
{
create
(
:user
)
}
let
(
:user2
)
{
create
(
:user
)
}
...
...
@@ -12,6 +12,10 @@ describe API::API, api: true do
let
(
:commit_status
)
{
create
(
:commit_status
,
commit:
ci_commit
)
}
describe
"GET /projects/:id/repository/commits/:sha/statuses"
do
it_behaves_like
'a paginated resources'
do
let
(
:request
)
{
get
api
(
"/projects/
#{
project
.
id
}
/repository/commits/
#{
commit
.
id
}
/statuses"
,
user
)
}
end
context
"reporter user"
do
let
(
:statuses_id
)
{
json_response
.
map
{
|
status
|
status
[
'id'
]
}
}
...
...
spec/requests/api/notes_spec.rb
浏览文件 @
3183092c
...
...
@@ -32,6 +32,10 @@ describe API::API, api: true do
before
{
project
.
team
<<
[
user
,
:reporter
]
}
describe
"GET /projects/:id/noteable/:noteable_id/notes"
do
it_behaves_like
'a paginated resources'
do
let
(
:request
)
{
get
api
(
"/projects/
#{
project
.
id
}
/issues/
#{
issue
.
id
}
/notes"
,
user
)
}
end
context
"when noteable is an Issue"
do
it
"should return an array of issue notes"
do
get
api
(
"/projects/
#{
project
.
id
}
/issues/
#{
issue
.
id
}
/notes"
,
user
)
...
...
spec/support/api/pagination_shared_examples.rb
0 → 100644
浏览文件 @
3183092c
# Specs for paginated resources.
#
# Requires an API request:
# let(:request) { get api("/projects/#{project.id}/repository/branches", user) }
shared_examples
'a paginated resources'
do
before
do
# Fires the request
request
end
it
'has pagination headers'
do
expect
(
response
.
headers
).
to
include
(
'X-Total'
)
expect
(
response
.
headers
).
to
include
(
'X-Total-Pages'
)
expect
(
response
.
headers
).
to
include
(
'X-Per-Page'
)
expect
(
response
.
headers
).
to
include
(
'X-Page'
)
expect
(
response
.
headers
).
to
include
(
'X-Next-Page'
)
expect
(
response
.
headers
).
to
include
(
'X-Prev-Page'
)
expect
(
response
.
headers
).
to
include
(
'Link'
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录