diff --git a/pr-review/src/config.ts b/pr-review/src/config.ts index 7237af6..466b7ad 100644 --- a/pr-review/src/config.ts +++ b/pr-review/src/config.ts @@ -17,7 +17,7 @@ if (process.env.NODE_ENV === "development") { // eslint-disable-next-line @typescript-eslint/no-for-in-array, no-restricted-syntax, guard-for-in for (const key in actionYaml.inputs) { const envKey = `INPUT_${key.toUpperCase()}` - const envValue = actionYaml.inputs[key].default + const envValue = actionYaml.inputs[key]?.default if (envValue && !Object.keys(process.env).includes(envKey)) { process.env[envKey] = envValue } diff --git a/pr-review/src/hunk-reader.ts b/pr-review/src/hunk-reader.ts index 683eaaa..1806efb 100644 --- a/pr-review/src/hunk-reader.ts +++ b/pr-review/src/hunk-reader.ts @@ -34,43 +34,50 @@ export function resolveHunkReferencesInComments(comments: AiReview["comments"], core.warning(`Could not find file for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`) } else { const hunkChangeMap = currentFile.chunks.flatMap(hunk => hunk.changes.map(change => ({ change, hunk }))) - let { change: startChange, hunk: startHunk } = hunkChangeMap[comment.start - 1] // eslint-disable-line prefer-const - let { change: endChange, hunk: endHunk } = hunkChangeMap[comment.end - 1] // eslint-disable-line prefer-const + const startEntry = hunkChangeMap[comment.start - 1] + const endEntry = hunkChangeMap[comment.end - 1] - if (!startHunk) { - core.warning(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`) + if (!startEntry || !endEntry) { + core.error(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`) } else { - if (startHunk !== endHunk) endChange = startHunk.changes.at(-1)! + const { change: startChange, hunk: startHunk } = startEntry + let { change: endChange, hunk: endHunk } = endEntry // eslint-disable-line prefer-const - const startSide = startChange.type !== "del" ? "RIGHT" : "LEFT" - const endSide = endChange.type !== "del" ? "RIGHT" : "LEFT" + if (!startHunk) { + core.warning(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`) + } else { + if (startHunk !== endHunk) endChange = startHunk.changes.at(-1)! - // get start line of the actual comment - let start: number - if (startChange.type === "normal") { - start = startChange.ln2 - } else if (startChange.type === "add" || startChange.type === "del") { - start = startChange.ln - } else throw new Error(`Unknown change type.`) + const startSide = startChange.type !== "del" ? "RIGHT" : "LEFT" + const endSide = endChange.type !== "del" ? "RIGHT" : "LEFT" - // get end line of the actual comment - let end: number - if (endChange.type === "normal") { - end = endChange.ln2 - } else if (endChange.type === "add" || endChange.type === "del") { - end = endChange.ln - } else throw new Error(`Unknown change type.`) - // make sure start and end are within the hunk - end = Math.min(end, endSide === "RIGHT" ? startHunk.newStart + startHunk.newLines - 1 : startHunk.oldStart + startHunk.oldLines - 1) + // get start line of the actual comment + let start: number + if (startChange.type === "normal") { + start = startChange.ln2 + } else if (startChange.type === "add" || startChange.type === "del") { + start = startChange.ln + } else throw new Error(`Unknown change type.`) - result.push({ - path: comment.path, - start_side: startSide !== endSide ? startSide : undefined, // only set start_side if it is a multi-line comment - side: startSide !== endSide ? endSide : startSide, - start_line: start !== end && start < end ? start : undefined, // only set start_line if it is a multi-line comment, start must be less than end - line: start !== end && start < end ? end : start, - body: comment.comment, - }) + // get end line of the actual comment + let end: number + if (endChange.type === "normal") { + end = endChange.ln2 + } else if (endChange.type === "add" || endChange.type === "del") { + end = endChange.ln + } else throw new Error(`Unknown change type.`) + // make sure start and end are within the hunk + end = Math.min(end, endSide === "RIGHT" ? startHunk.newStart + startHunk.newLines - 1 : startHunk.oldStart + startHunk.oldLines - 1) + + result.push({ + path: comment.path, + start_side: startSide !== endSide ? startSide : undefined, // only set start_side if it is a multi-line comment + side: startSide !== endSide ? endSide : startSide, + start_line: start !== end && start < end ? start : undefined, // only set start_line if it is a multi-line comment, start must be less than end + line: start !== end && start < end ? end : start, + body: comment.comment, + }) + } } } }) diff --git a/pr-review/src/main.ts b/pr-review/src/main.ts index 98486a8..9455f10 100644 --- a/pr-review/src/main.ts +++ b/pr-review/src/main.ts @@ -43,8 +43,8 @@ export async function run(config: Config): Promise { const regex = new RegExp(`^${markerStart}\\s+([\\s\\S]*?)${markerEnd}$`, "g") previousReviews.forEach(comment => { ;[...(comment.body?.matchAll(regex) ?? [])].forEach(match => { - const commentBase = match.groups!.base - const commentHead = match.groups!.head + const commentBase = match.groups!.base! + const commentHead = match.groups!.head! if (commentHead) base = commentHead !== head ? commentHead : commentBase }) }) diff --git a/pr-review/tsconfig.json b/pr-review/tsconfig.json index 2f26aac..87ed8f5 100644 --- a/pr-review/tsconfig.json +++ b/pr-review/tsconfig.json @@ -1,19 +1,7 @@ { "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../tsconfig.json", "compilerOptions": { - "target": "ES2022", - "module": "NodeNext", - "rootDir": "./src", - "moduleResolution": "NodeNext", - "baseUrl": "./", - "sourceMap": true, - "outDir": "./dist", - "noImplicitAny": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true, - "newLine": "lf" - }, - "exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"] + "rootDir": "./src" + } } diff --git a/pr-summary/src/config.ts b/pr-summary/src/config.ts index fe595cb..f71efcd 100644 --- a/pr-summary/src/config.ts +++ b/pr-summary/src/config.ts @@ -17,7 +17,7 @@ if (process.env.NODE_ENV === "development") { // eslint-disable-next-line @typescript-eslint/no-for-in-array, no-restricted-syntax, guard-for-in for (const key in actionYaml.inputs) { const envKey = `INPUT_${key.toUpperCase()}` - const envValue = actionYaml.inputs[key].default + const envValue = actionYaml.inputs[key]?.default if (envValue && !Object.keys(process.env).includes(envKey)) { process.env[envKey] = envValue } diff --git a/pr-summary/src/main.ts b/pr-summary/src/main.ts index efa92d1..13d9fa1 100644 --- a/pr-summary/src/main.ts +++ b/pr-summary/src/main.ts @@ -40,8 +40,8 @@ export async function run(config: Config): Promise { const regex = new RegExp(`^${markerStart}\\s+([\\s\\S]*?)${markerEnd}$`, "g") comments.forEach(comment => { ;[...(comment.body?.matchAll(regex) ?? [])].forEach(match => { - const commentBase = match.groups!.base - const commentHead = match.groups!.head + const commentBase = match.groups!.base! + const commentHead = match.groups!.head! if (commentHead) base = commentHead !== head ? commentHead : commentBase }) }) diff --git a/pr-summary/tsconfig.json b/pr-summary/tsconfig.json index 464cb8b..87ed8f5 100644 --- a/pr-summary/tsconfig.json +++ b/pr-summary/tsconfig.json @@ -1,19 +1,7 @@ { "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../tsconfig.json", "compilerOptions": { - "target": "ES2022", - "module": "NodeNext", - "rootDir": "./src", - "moduleResolution": "NodeNext", - "baseUrl": "./", - "sourceMap": true, - "outDir": "./dist", - "noImplicitAny": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true, - "newLine": "lf" - }, - "exclude": ["./dist", "./node_modules"] + "rootDir": "./src" + } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8443ea1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "compilerOptions": { + "composite": true, + "target": "esnext", + "module": "nodenext", + "moduleResolution": "nodenext", + "baseUrl": "./", + "sourceMap": true, + "outDir": "./dist", + "noImplicitAny": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "noUncheckedIndexedAccess": true, + "strict": true, + "skipLibCheck": true, + "newLine": "lf", + "erasableSyntaxOnly": true, + "rewriteRelativeImportExtensions": true + }, + "exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"] +}