Skip to content

Commit aee652c

Browse files
authored
Merge pull request #252 from woutdenolf/fix_undefined_line_range
handle undefined position.line_range in case of comments on empty files
2 parents 6717ade + 452075a commit aee652c

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/githubHelper.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,16 +1681,22 @@ export class GithubHelper {
16811681
) {
16821682
return '';
16831683
}
1684+
16841685
const base_sha = position.base_sha;
1685-
let head_sha = position.head_sha;
1686+
const head_sha = position.head_sha;
1687+
16861688
var path = '';
16871689
var line = '';
16881690
var slug = '';
1691+
var ref = '';
1692+
1693+
const crypto = require('crypto');
1694+
16891695
if (
16901696
(position.new_line && position.new_path) ||
16911697
(position.old_line && position.old_path)
16921698
) {
1693-
var side;
1699+
var side = '';
16941700
if (!position.old_line || !position.old_path) {
16951701
side = 'R';
16961702
path = position.new_path;
@@ -1700,25 +1706,33 @@ export class GithubHelper {
17001706
path = position.old_path;
17011707
line = position.old_line;
17021708
}
1703-
const crypto = require('crypto');
17041709
const hash = crypto.createHash('sha256').update(path).digest('hex');
17051710
slug = `#diff-${hash}${side}${line}`;
1711+
ref = `${path} line ${line}`;
1712+
} else if (position.old_path) {
1713+
path = position.old_path;
1714+
const hash = crypto.createHash('sha256').update(path).digest('hex');
1715+
slug = `#diff-${hash}`;
1716+
ref = path;
1717+
} else {
1718+
ref = head_sha;
17061719
}
1707-
// Mention the file and line number. If we can't get this for some reason then use the commit id instead.
1708-
const ref = path && line ? `${path} line ${line}` : `${head_sha}`;
1720+
17091721
let lineRef = `Commented on [${ref}](${repoLink}/compare/${base_sha}..${head_sha}${slug})\n\n`;
17101722

1711-
if (position.line_range.start.type === 'new') {
1712-
const startLine = position.line_range.start.new_line;
1713-
const endLine = position.line_range.end.new_line;
1714-
const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
1715-
lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
1716-
}
1717-
else {
1718-
const startLine = position.line_range.start.old_line;
1719-
const endLine = position.line_range.end.old_line;
1720-
const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
1721-
lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
1723+
if (position.line_range) {
1724+
if (position.line_range.start.type === 'new') {
1725+
const startLine = position.line_range.start.new_line;
1726+
const endLine = position.line_range.end.new_line;
1727+
const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
1728+
lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
1729+
}
1730+
else {
1731+
const startLine = position.line_range.start.old_line;
1732+
const endLine = position.line_range.end.old_line;
1733+
const lineRange = (startLine !== endLine) ? `L${startLine}-L${endLine}` : `L${startLine}`;
1734+
lineRef += `${repoLink}/blob/${head_sha}/${path}#${lineRange}\n\n`;
1735+
}
17221736
}
17231737

17241738
return lineRef;

0 commit comments

Comments
 (0)