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
5bc54ca4
Commit
5bc54ca4
authored
Aug 16, 2018
by
Felipe Artur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a serializer to render diff lines
parent
472f2d56
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
99 additions
and
13 deletions
+99
-13
app/controllers/projects/blob_controller.rb
app/controllers/projects/blob_controller.rb
+1
-1
app/serializers/diff_file_entity.rb
app/serializers/diff_file_entity.rb
+2
-2
app/serializers/diff_line_entity.rb
app/serializers/diff_line_entity.rb
+14
-0
app/serializers/diff_line_parallel_entity.rb
app/serializers/diff_line_parallel_entity.rb
+6
-0
app/serializers/diff_line_serializer.rb
app/serializers/diff_line_serializer.rb
+5
-0
app/serializers/discussion_entity.rb
app/serializers/discussion_entity.rb
+1
-1
lib/gitlab/diff/line.rb
lib/gitlab/diff/line.rb
+3
-9
spec/fixtures/api/schemas/entities/diff_line.json
spec/fixtures/api/schemas/entities/diff_line.json
+14
-0
spec/fixtures/api/schemas/entities/diff_line_parallel.json
spec/fixtures/api/schemas/entities/diff_line_parallel.json
+11
-0
spec/serializers/diff_file_entity_spec.rb
spec/serializers/diff_file_entity_spec.rb
+17
-0
spec/serializers/diff_line_serializer_spec.rb
spec/serializers/diff_line_serializer_spec.rb
+25
-0
No files found.
app/controllers/projects/blob_controller.rb
View file @
5bc54ca4
...
@@ -127,7 +127,7 @@ class Projects::BlobController < Projects::ApplicationController
...
@@ -127,7 +127,7 @@ class Projects::BlobController < Projects::ApplicationController
add_match_line
add_match_line
render
json:
@lines
render
json:
DiffLineSerializer
.
new
.
represent
(
@lines
)
end
end
def
add_match_line
def
add_match_line
...
...
app/serializers/diff_file_entity.rb
View file @
5bc54ca4
...
@@ -135,12 +135,12 @@ class DiffFileEntity < Grape::Entity
...
@@ -135,12 +135,12 @@ class DiffFileEntity < Grape::Entity
end
end
# Used for inline diffs
# Used for inline diffs
expose
:highlighted_diff_lines
,
if:
->
(
diff_file
,
_
)
{
diff_file
.
text?
}
do
|
diff_file
|
expose
:highlighted_diff_lines
,
using:
DiffLineEntity
,
if:
->
(
diff_file
,
_
)
{
diff_file
.
text?
}
do
|
diff_file
|
diff_file
.
diff_lines_for_serializer
diff_file
.
diff_lines_for_serializer
end
end
# Used for parallel diffs
# Used for parallel diffs
expose
:parallel_diff_lines
,
if:
->
(
diff_file
,
_
)
{
diff_file
.
text?
}
expose
:parallel_diff_lines
,
using:
DiffLineParallelEntity
,
if:
->
(
diff_file
,
_
)
{
diff_file
.
text?
}
def
current_user
def
current_user
request
.
current_user
request
.
current_user
...
...
app/serializers/diff_line_entity.rb
0 → 100644
View file @
5bc54ca4
# frozen_string_literal: true
class
DiffLineEntity
<
Grape
::
Entity
expose
:line_code
expose
:type
expose
:old_line
expose
:new_line
expose
:text
expose
:meta_positions
,
as: :meta_data
expose
:rich_text
do
|
line
|
line
.
rich_text
||
CGI
.
escapeHTML
(
line
.
text
)
end
end
app/serializers/diff_line_parallel_entity.rb
0 → 100644
View file @
5bc54ca4
# frozen_string_literal: true
class
DiffLineParallelEntity
<
Grape
::
Entity
expose
:left
,
using:
DiffLineEntity
expose
:right
,
using:
DiffLineEntity
end
app/serializers/diff_line_serializer.rb
0 → 100644
View file @
5bc54ca4
# frozen_string_literal: true
class
DiffLineSerializer
<
BaseSerializer
entity
DiffLineEntity
end
app/serializers/discussion_entity.rb
View file @
5bc54ca4
...
@@ -43,7 +43,7 @@ class DiscussionEntity < Grape::Entity
...
@@ -43,7 +43,7 @@ class DiscussionEntity < Grape::Entity
project_merge_request_discussion_path
(
discussion
.
project
,
discussion
.
noteable
,
discussion
)
project_merge_request_discussion_path
(
discussion
.
project
,
discussion
.
noteable
,
discussion
)
end
end
expose
:truncated_diff_lines
,
if:
->
(
d
,
_
)
{
d
.
diff_discussion?
&&
d
.
on_text?
&&
(
d
.
expanded?
||
render_truncated_diff_lines?
)
}
expose
:truncated_diff_lines
,
using:
DiffLineEntity
,
if:
->
(
d
,
_
)
{
d
.
diff_discussion?
&&
d
.
on_text?
&&
(
d
.
expanded?
||
render_truncated_diff_lines?
)
}
expose
:image_diff_html
,
if:
->
(
d
,
_
)
{
d
.
diff_discussion?
&&
d
.
on_image?
}
do
|
discussion
|
expose
:image_diff_html
,
if:
->
(
d
,
_
)
{
d
.
diff_discussion?
&&
d
.
on_image?
}
do
|
discussion
|
diff_file
=
discussion
.
diff_file
diff_file
=
discussion
.
diff_file
...
...
lib/gitlab/diff/line.rb
View file @
5bc54ca4
...
@@ -79,16 +79,10 @@ module Gitlab
...
@@ -79,16 +79,10 @@ module Gitlab
}
}
end
end
# We have to keep this here since it is still used for conflict resolution
# Conflict::File#as_json renders json diff lines in sections
def
as_json
(
opts
=
nil
)
def
as_json
(
opts
=
nil
)
{
DiffLineSerializer
.
new
.
represent
(
self
)
line_code:
line_code
,
type:
type
,
old_line:
old_line
,
new_line:
new_line
,
text:
text
,
rich_text:
rich_text
||
CGI
.
escapeHTML
(
text
),
meta_data:
meta_positions
}
end
end
private
private
...
...
spec/fixtures/api/schemas/entities/diff_line.json
0 → 100644
View file @
5bc54ca4
{
"type"
:
"object"
,
"required"
:
[
"type"
],
"properties"
:
{
"line_code"
:
{
"type"
:
[
"string"
,
"null"
]
},
"type"
:
{
"type"
:
[
"string"
,
"null"
]
},
"old_line"
:
{
"type"
:
[
"integer"
,
"null"
]
},
"new_line"
:
{
"type"
:
[
"integer"
,
"null"
]
},
"text"
:
{
"type"
:
[
"string"
]
},
"rich_text"
:
{
"type"
:
[
"string"
]
},
"meta_data"
:
{
"type"
:
[
"object"
,
"null"
]
}
},
"additionalProperties"
:
false
}
spec/fixtures/api/schemas/entities/diff_line_parallel.json
0 → 100644
View file @
5bc54ca4
{
"required"
:
[
"left"
,
"right"
],
"properties"
:
{
"left"
:
{
"$ref"
:
"diff_line.json"
},
"right"
:
{
"$ref"
:
"diff_line.json"
}
},
"additionalProperties"
:
false
}
spec/serializers/diff_file_entity_spec.rb
View file @
5bc54ca4
...
@@ -67,4 +67,21 @@ describe DiffFileEntity do
...
@@ -67,4 +67,21 @@ describe DiffFileEntity do
end
end
end
end
end
end
context
'#parallel_diff_lines'
do
it
'exposes parallel diff lines correctly'
do
response
=
subject
lines
=
response
[
:parallel_diff_lines
]
# make sure at least one line is present for each side
expect
(
lines
.
map
{
|
line
|
line
[
:right
]
}.
compact
).
to
be_present
expect
(
lines
.
map
{
|
line
|
line
[
:left
]
}.
compact
).
to
be_present
# make sure all lines are in correct format
lines
.
each
do
|
parallel_line
|
expect
(
parallel_line
[
:left
].
as_json
).
to
match_schema
(
'entities/diff_line'
)
if
parallel_line
[
:left
]
expect
(
parallel_line
[
:right
].
as_json
).
to
match_schema
(
'entities/diff_line'
)
if
parallel_line
[
:right
]
end
end
end
end
end
spec/serializers/diff_line_serializer_spec.rb
0 → 100644
View file @
5bc54ca4
require
'spec_helper'
describe
DiffLineSerializer
do
let
(
:line
)
{
Gitlab
::
Diff
::
Line
.
new
(
'hello world'
,
'new'
,
1
,
nil
,
1
)
}
let
(
:serializer
)
{
described_class
.
new
.
represent
(
line
)
}
describe
'#to_json'
do
subject
{
serializer
.
to_json
}
it
'matches the schema'
do
expect
(
subject
).
to
match_schema
(
'entities/diff_line'
)
end
context
'when lines are parallel'
do
let
(
:right_line
)
{
Gitlab
::
Diff
::
Line
.
new
(
'right line'
,
'new'
,
1
,
nil
,
1
)
}
let
(
:left_line
)
{
Gitlab
::
Diff
::
Line
.
new
(
'left line'
,
'match'
,
1
,
nil
,
1
)
}
let
(
:parallel_line
)
{
[{
right:
right_line
,
left:
left_line
}]
}
let
(
:serializer
)
{
described_class
.
new
.
represent
(
parallel_line
,
{},
DiffLineParallelEntity
)
}
it
'matches the schema'
do
expect
(
subject
).
to
match_schema
(
'entities/diff_line_parallel'
)
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