Skip to content
Merged
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
39 changes: 38 additions & 1 deletion __tests__/utils/diff.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {test, expect} from 'vitest'
import {parseGitDiff} from '../../src/utils/diff'
import {parseGitDiff, parsePatch} from '../../src/utils/diff'
import {getFixturePath} from '../fixtures/util'
import * as fs from 'fs'

Expand All @@ -10,3 +10,40 @@ test('should parse Git diff', async function () {

expect(output).toMatchSnapshot()
})

test('parsePatch should return empty array for empty patch', () => {
expect(parsePatch('')).toEqual([])
})

test('parsePatch should parse single hunk patch', () => {
const patch = `@@ -0,0 +1,3 @@
+line 1
+line 2
+line 3`
expect(parsePatch(patch)).toEqual([1, 2, 3])
})

test('parsePatch should parse patch with context and deletions', () => {
const patch = `@@ -1,5 +1,6 @@
unchanged line 1
-deleted line
+added line 1
+added line 2
unchanged line 2
unchanged line 3`
// Line 2 and 3 are the added lines (after unchanged line 1 at position 1)
expect(parsePatch(patch)).toEqual([2, 3])
})

test('parsePatch should parse patch with multiple hunks', () => {
const patch = `@@ -1,3 +1,4 @@
line 1
+new line 2
line 3
line 4
@@ -10,3 +11,4 @@
line 11
+new line 12
line 13`
expect(parsePatch(patch)).toEqual([2, 12])
})
50 changes: 42 additions & 8 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/utils/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ interface FileDiff {
deletedLines: number[]
}

/**
* Parse a single file's patch (from GitHub's listFiles API) to extract added line numbers.
* The patch format contains only hunk headers (@@) and changes, not the full diff header.
*/
export function parsePatch(patch: string): number[] {
const addedLines: number[] = []
const lines = patch.split('\n')
let additionCurrentLineNumber = 0
let seenHeaderLine = false

for (const line of lines) {
if (line.startsWith('@@')) {
seenHeaderLine = true
const lineInfo = getLineInfoFromHeaderLine(line)
additionCurrentLineNumber = lineInfo.additionStartingLineNumber
} else if (line.startsWith('+') && seenHeaderLine) {
addedLines.push(additionCurrentLineNumber)
additionCurrentLineNumber++
} else if (line.startsWith('-') && seenHeaderLine) {
// Deleted line - don't increment addition line number
} else if (seenHeaderLine) {
// Context line
additionCurrentLineNumber++
}
}

return addedLines
}

export function parseGitDiff(diffOutput: string): FileDiff[] {
const fileDiffs: FileDiff[] = []
const lines = diffOutput.split('\n')
Expand Down
32 changes: 21 additions & 11 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import * as diff from './diff'
import {parsePatch} from './diff'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should check, because there may be a function we could remove or this should go back to *.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseGitDiff is no longer called except in tests. Should we keep it or remove it?

import * as github from '@actions/github'
import {
CoverageFile,
Expand Down Expand Up @@ -28,17 +28,27 @@ export class GithubUtil {

async getPullRequestDiff(): Promise<PullRequestFiles> {
const pull_number = github.context.issue.number
const response = await this.client.rest.pulls.get({
...github.context.repo,
pull_number,
mediaType: {
format: 'diff'
}
})
const fileLines = diff.parseGitDiff(response.data as unknown as string)
const prFiles: PullRequestFiles = {}
for (const item of fileLines) {
prFiles[item.filename] = coalesceLineNumbers(item.addedLines)

// Use paginated listFiles API to handle PRs with more than 300 files
const iterator = this.client.paginate.iterator(
this.client.rest.pulls.listFiles,
{
...github.context.repo,
pull_number,
per_page: 100
}
)

for await (const response of iterator) {
for (const file of response.data) {
if (file.patch) {
const addedLines = parsePatch(file.patch)
if (addedLines.length > 0) {
prFiles[file.filename] = coalesceLineNumbers(addedLines)
}
}
}
}

return prFiles
Expand Down
Loading