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
37 changes: 25 additions & 12 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.

13 changes: 7 additions & 6 deletions src/github/github-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PullRequest} from '../_namespaces/github'
import {context} from '@actions/github'
import {info} from "@actions/core";

const prEvents = ['pull_request', 'pull_request_review', 'pull_request_review_comment']

Expand Down Expand Up @@ -32,10 +33,10 @@ export function getPullRequestNumber(): number | undefined {
}

export function relativizePath(path: string): string {
let length = process.env.GITHUB_WORKSPACE?.length
if (!length) {
length = 'undefined'.length
}

return path.substring(length + 1)
// owner/repo-name
let repo = process.env.GITHUB_REPOSITORY ?? "undefined"
let repo_owner = process.env.GITHUB_REPOSITORY_OWNER ?? "undefined"
let repo_name = repo.substring(repo_owner.length + 1)
// path is in the format of ../workspace/{repo-name}/{RELATIVE_PATH}
return path.substring(path.lastIndexOf(repo_name) + repo_name.length + 1)
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function run(): Promise<void> {
}

function isInDiff(issue: IssueOccurrence, diffMap: DiffMap): boolean {
const diffHunks = diffMap.get(issue.mainEventFilePathname)
const diffHunks = diffMap.get(relativizePath(issue.mainEventFilePathname))

if (!diffHunks) {
return false
Expand Down
25 changes: 19 additions & 6 deletions src/reporting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {relativizePath} from './github/github-context'
import {IssueOccurrence} from './json-v7-schema'
import {info} from "@actions/core";

export const PRESENT = 'PRESENT'
export const NOT_PRESENT = 'NOT_PRESENT'
Expand Down Expand Up @@ -54,26 +55,38 @@ export function createIssueCommentMessage(issue: IssueOccurrence): string {
const message = createReviewCommentMessage(issue)
const relativePath = relativizePath(issue.mainEventFilePathname)


return `${message}
## Issue location
This issue was discovered outside the diff for this Pull Request. You can find it at:
[${relativePath}:${issue.mainEventLineNumber}](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/blob/${process.env.GITHUB_SHA}/${relativePath}#L${issue.mainEventLineNumber})
`
}

// naive Z-Table, can be optimized to terminate early
function zTable(input: string) {
let table = new Array<number>(input.length).fill(0)
for (let i = 1; i < input.length; i++) {
for (let j = 0; j < input.length - i; j ++) {
if (input.charAt(j) != input.charAt(i + j)) continue
table[i]++
}
}
return table
}

export function getDiffMap(rawDiff: string): DiffMap {
console.info('Gathering diffs...')
const diffMap = new Map()

let path = UNKNOWN_FILE
for (const line of rawDiff.split('\n')) {
if (line.startsWith('diff --git')) {
// TODO: Handle spaces in path
path = `${process.env.GITHUB_WORKSPACE}/${line.split(' ')[2].substring(2)}`
if (path === undefined) {
path = UNKNOWN_FILE
}

// this should give `diff --git a/path b/path`
// to get the file itself, we can use longest string match on `path b/path`
// since we definitely know paths are equal, path must be the longest string
path = line.substring("diff --git a/".length)
path = path.substring(0, Math.max(...zTable(path)))
diffMap.set(path, [])
}

Expand Down