Skip to content

Commit

Permalink
Adjusting line calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Feb 11, 2024
1 parent a17ac79 commit 5fa3b69
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
29 changes: 22 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,27 @@ export async function run(): Promise<void> {
const lines = (diffData as unknown as string).split('\n')
let foundMatches = false
let currentFile = ''

Check failure on line 66 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'currentFile' is never reassigned. Use 'const' instead

Check failure on line 66 in src/main.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'currentFile' is never reassigned. Use 'const' instead
let currentLineNumber = 0
let oldLineNumber = 0
let newLineNumber = 0

for (const line of lines) {
if (line.startsWith('+++ b/')) {
currentFile = line.substring('+++ b/'.length)
currentLineNumber = 0 // Reset line number for each new file
} else if (line.startsWith('+') || line.startsWith('-')) {
currentLineNumber++ // Count only added or removed lines
if (line.startsWith('@@')) {
// Parse and set the starting line numbers from the hunk header
const match = line.match(/-(\d+),\d+ \+(\d+),\d+/)
if (match) {
oldLineNumber = parseInt(match[1], 10) - 1
newLineNumber = parseInt(match[2], 10) - 1
}
} else {
// Increment the appropriate line number
if (line.startsWith('-')) {
oldLineNumber++
} else if (line.startsWith('+')) {
newLineNumber++
} else {
oldLineNumber++
newLineNumber++
}
}

if (
Expand Down Expand Up @@ -100,7 +113,7 @@ export async function run(): Promise<void> {
commit_id: context.payload.pull_request.head.sha,
path: currentFile,
side,
line: currentLineNumber
line: side === 'LEFT' ? oldLineNumber : newLineNumber
})
} else {
core.debug(`Match found but already commented`)
Expand Down

0 comments on commit 5fa3b69

Please sign in to comment.