Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lua/gitlab/actions/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ M.get_line_numbers_for_range = function(old_line, new_line, start_line_code, end
local old_start_line, new_start_line = indicators_common.parse_line_code(start_line_code)
local old_end_line, new_end_line = indicators_common.parse_line_code(end_line_code)
if old_line ~= nil and old_start_line ~= 0 then
local range = old_end_line - old_start_line
local range = (old_start_line and old_end_line) and (old_end_line - old_start_line) or 0
return (old_line - range), old_line, false
elseif new_line ~= nil then
local range = new_end_line - new_start_line
local range = (new_start_line and new_end_line) and (new_end_line - new_start_line) or 0
-- Force start_line to be greater than 0
-- TODO: use `math.max(new_line - range, 1)` instead
local start_line = (new_line - range > 0) and (new_line - range) or 1
Expand Down
15 changes: 7 additions & 8 deletions lua/gitlab/indicators/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ M.filter_placeable_discussions = function()
end

---Parse old and new line from a line code like "3f454a98e586d1aa0d322e19afd5e67e08f2d3c8_10_44".
---@param line_code string A SHA hash of the file name and line numbers before and after change
---@return integer The line number before the change
---@return integer The line number after the change
---GitLab leaves a number empty when the line has no counterpart, e.g. "3f454a98...__44".
---@param line_code? string A SHA hash of the file name and line numbers before and after change
---@return integer? The line number before the change
---@return integer? The line number after the change
M.parse_line_code = function(line_code)
local line_code_regex = "%w+_(%d+)_(%d+)"
local old_line, new_line = line_code:match(line_code_regex)
old_line = tonumber(old_line) --[[@as integer]]
new_line = tonumber(new_line) --[[@as integer]]
return old_line, new_line
local line_code_regex = "%w+_(%d*)_(%d*)"
local old_line, new_line = (line_code or ""):match(line_code_regex)
return tonumber(old_line), tonumber(new_line)
end

---Return true if discussion/draft belongs to the old file, otherwise false.
Expand Down
45 changes: 45 additions & 0 deletions tests/spec/line_code_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local indicators_common = require("gitlab.indicators.common")
local actions_common = require("gitlab.actions.common")

local sha = "3f454a98e586d1aa0d322e19afd5e67e08f2d3c8"

describe("indicators/common.parse_line_code", function()
it("Parses both line numbers", function()
assert.are.same({ 10, 44 }, { indicators_common.parse_line_code(sha .. "_10_44") })
end)

it("Returns nil for an empty old line number", function()
assert.are.same({ nil, 44 }, { indicators_common.parse_line_code(sha .. "__44") })
end)

it("Returns nil for an empty new line number", function()
assert.are.same({ 10, nil }, { indicators_common.parse_line_code(sha .. "_10_") })
end)

it("Returns nil for a missing line code", function()
assert.are.same({ nil, nil }, { indicators_common.parse_line_code(nil) })
end)
end)

describe("actions/common.get_line_numbers_for_range", function()
it("Computes a range on the new SHA", function()
assert.are.same(
{ 40, 44, true },
{ actions_common.get_line_numbers_for_range(nil, 44, sha .. "_1_40", sha .. "_1_44") }
)
end)

it("Falls back to a single line when a line code has an empty number", function()
assert.are.same(
{ 44, 44, true },
{ actions_common.get_line_numbers_for_range(nil, 44, sha .. "__40", sha .. "_1_") }
)
end)

it("Falls back to a single line on the old SHA when a line code has an empty number", function()
assert.are.same(
{ 12, 12, false },
{ actions_common.get_line_numbers_for_range(12, nil, sha .. "_10_", sha .. "__5") }
)
end)
end)