Skip to content

TypeError when migrating merge request comments with missing line_range #250

@fluetanol

Description

@fluetanol

When migrating merge requests (especially already merged ones) to GitHub, the migration fails with a TypeError: Cannot read properties of null (reading 'start') error when processing comments that don't have line_range information.

Error Message

Could not create pull request: !13 - feature: 스페이스 구현[S13P31A208-127] TypeError: Cannot read properties of null (reading 'start') at Function.GithubHelper.createLineRef (C:\Users\SSAFY\Desktop\node-gitlab-2-github\src\githubHelper.ts:1711:29) at Function.GithubHelper.addMigrationLine (C:\Users\SSAFY\Desktop\node-gitlab-2-github\src\githubHelper.ts:1663:24) at GithubHelper. (C:\Users\SSAFY\Desktop\node-gitlab-2-github\src\githubHelper.ts:1463:38)

Root Cause

In githubHelper.ts at line 1711, the code assumes that position.line_range always exists and directly accesses position.line_range.start.type:

if (position.line_range.start.type === 'new') {
However, some GitLab comments may not have line_range information, particularly:
Single-line comments
General comments (not on code)
Comments on already merged merge requests
Older comment formats
Steps to Reproduce
Attempt to migrate a GitLab repository with merged merge requests
The migration tries to convert the MR to an issue (since it's already merged)
When processing comments with missing line_range, the error occurs
Proposed Solution
Add null-safety checks before accessing position.line_range.start:
let lineRef = `Commented on [${ref}](${repoLink}/compare/${base_sha}..${head_sha}${slug})\n\n`;

if (position.line_range && position.line_range.start && position.line_range.end) {
  if (position.line_range.start.type === 'new') {
    const startLine = position.line_range.start.new_line;
    const endLine = position.line_range.end.new_line;
    const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
    lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
  }
  else {
    const startLine = position.line_range.start.old_line;
    const endLine = position.line_range.end.old_line;
    const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
    lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
  }
}

return lineRef;

This gracefully handles cases where line_range is missing by falling back to the basic compare link, while still providing detailed line information when available.

Additional Context

This issue is related to GitLab API's complex handling of position[line_range] parameters in merge request discussions, as documented in GitLab issue #247521.

So when debugging, the position object for a single-line comment looks like this:
{
base_sha: '(data)',
start_sha: '(data)',
head_sha:'(data)',
old_path: '(pathdata)',
new_path: '(pathdata)',
position_type: 'text',
old_line: null,
new_line: 38, // Line information exists
line_range: null // But line_range is null
}
Note that line_range is null, but new_line contains valid line information (38).....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions