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
0c82bf04
Commit
0c82bf04
authored
Mar 30, 2020
by
Gary Holtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds a great refactoring idea and clarified specs
parent
6bfbc3c6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
18 deletions
+46
-18
app/serializers/diffs_entity.rb
app/serializers/diffs_entity.rb
+8
-9
spec/serializers/diffs_entity_spec.rb
spec/serializers/diffs_entity_spec.rb
+38
-9
No files found.
app/serializers/diffs_entity.rb
View file @
0c82bf04
...
@@ -80,20 +80,19 @@ class DiffsEntity < Grape::Entity
...
@@ -80,20 +80,19 @@ class DiffsEntity < Grape::Entity
private
private
def
commit_ids
def
commit_ids
strong_memoize
(
:commit_ids
)
do
@commit_ids
||=
merge_request
.
commits
.
collect
(
&
:id
)
[
nil
]
+
merge_request
.
commits
.
collect
(
&
:id
)
+
[
nil
]
end
end
end
def
commit_with_neighbors
(
id
)
def
commit_neighbors
(
commit_id
)
commits
=
commit_ids
.
each_cons
(
3
).
find
{
|
prev_commit
,
commit
,
next_commit
|
commit
==
id
}
index
=
commit_ids
.
index
(
commit_id
)
{
prev_commit_id:
commits
.
first
,
next_commit_id:
commits
.
last
}
if
commits
return
[]
unless
index
[(
index
>
0
?
commit_ids
[
index
-
1
]
:
nil
),
commit_ids
[
index
+
1
]]
end
end
def
commit_options
(
options
)
def
commit_options
(
options
)
neighbors
=
commit_with_neighbors
(
options
[
:commit
]
&
.
id
)
prev_commit_id
,
next_commit_id
=
*
commit_neighbors
(
options
[
:commit
]
&
.
id
)
prev_commit_id
=
neighbors
&
.
dig
(
:prev_commit_id
)
next_commit_id
=
neighbors
&
.
dig
(
:next_commit_id
)
options
.
merge
(
options
.
merge
(
type: :full
,
type: :full
,
...
...
spec/serializers/diffs_entity_spec.rb
View file @
0c82bf04
...
@@ -29,21 +29,50 @@ describe DiffsEntity do
...
@@ -29,21 +29,50 @@ describe DiffsEntity do
context
"when a commit_id is passed"
do
context
"when a commit_id is passed"
do
let
(
:commits
)
{
[
nil
]
+
merge_request
.
commits
+
[
nil
]
}
let
(
:commits
)
{
[
nil
]
+
merge_request
.
commits
+
[
nil
]
}
let
(
:commit
)
{
commits
.
compact
.
sample
}
let
(
:entity
)
do
let
(
:entity
)
do
described_class
.
new
(
merge_request_diffs
.
first
.
diffs
,
request:
request
,
merge_request:
merge_request
,
merge_request_diffs:
merge_request_diffs
,
commit:
commit
)
described_class
.
new
(
merge_request_diffs
.
first
.
diffs
,
request:
request
,
merge_request:
merge_request
,
merge_request_diffs:
merge_request_diffs
,
commit:
@commit
)
end
end
subject
{
entity
.
as_json
}
context
"when the passed commit is not the first or last in the group"
do
it
'includes commit references for previous and next'
do
it
'includes commit references for previous and next'
do
expect
(
subject
[
:commit
]).
to
include
(
:prev_commit_id
,
:next_commit_id
)
@commit
=
commits
.
third
check_neighbor_commits
(
commits
,
@commit
)
end
end
context
"when the passed commit is the first in the group"
do
it
'includes commit references for nil and next'
do
@commit
=
commits
.
compact
.
last
check_neighbor_commits
(
commits
,
@commit
)
end
end
context
"when the passed commit is the last in the group"
do
it
'includes commit references for previous and nil'
do
@commit
=
commits
.
compact
.
first
check_neighbor_commits
(
commits
,
@commit
)
end
end
end
end
private
def
check_neighbor_commits
(
commits
,
commit
)
index
=
commits
.
index
(
commit
)
index
=
commits
.
index
(
commit
)
prev_commit
=
commits
[
index
-
1
]
&
.
id
prev_commit
=
commits
[
index
-
1
]
&
.
id
next_commit
=
commits
[
index
+
1
]
&
.
id
next_commit
=
commits
[
index
+
1
]
&
.
id
expect
(
subject
[
:commit
]).
to
include
(
:prev_commit_id
,
:next_commit_id
)
expect
(
subject
[
:commit
][
:prev_commit_id
]).
to
eq
(
prev_commit
)
expect
(
subject
[
:commit
][
:prev_commit_id
]).
to
eq
(
prev_commit
)
expect
(
subject
[
:commit
][
:next_commit_id
]).
to
eq
(
next_commit
)
expect
(
subject
[
:commit
][
:next_commit_id
]).
to
eq
(
next_commit
)
end
end
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