codeflash-internal/js/cf-api/diff_utils.ts
Sarthak Agarwal 2f5a633b82 code-suggester package upgrade to npm registry (#1522)
### **PR Type**
- Enhancement



___

### **Description**
- Updated all import paths to use @codeflash-ai/code-suggester

- Upgraded code-suggester dependency version in package.json

- Applied type cast for octokit in getPullRequestHunks call


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>diff_utils.test.ts</strong><dd><code>Update
FileDiffContent import in tests</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/diff_utils.test.ts

- Changed FileDiffContent import path to @codeflash-ai/code-suggester


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-c0991313f3fcb63fa2a553856b85d7df03e6d99cf98d19277fe689025f89ede1">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>diff_utils.ts</strong><dd><code>Upgrade import paths
and apply octokit cast</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/diff_utils.ts

<li>Updated all import paths for code-suggester modules<br> <li> Cast
octokit argument in getPullRequestHunks call


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-f6212b48c365856b4ceb25da3ad03bd15fe171161cff046a31a92586272d4771">+4/-4</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>create-pr.ts</strong><dd><code>Fix FileDiffContent
import path in create-pr endpoint</code>&nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

js/cf-api/endpoints/create-pr.ts

- Updated FileDiffContent import to new package path


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-728a794bb81f944ae7db030f5e6ae1c1ba6888aa46e4be6846ebc5bd1ce12c2c">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>suggest-pr-changes.ts</strong><dd><code>Update
suggester and FileDiffContent import paths</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/endpoints/suggest-pr-changes.ts

- Changed suggester and FileDiffContent imports to new package


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-b4a862986fd70827b8dabcb3157972bd0d5d507cf9c12e5fbf56f24979d073f3">+2/-2</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>

<summary><strong>create-pr-from-diffcontents.ts</strong><dd><code>Update
import path in create-pr-from-diffcontents</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/github/create-pr-from-diffcontents.ts

- Updated FileDiffContent import to @codeflash-ai/code-suggester


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-b30d5e0c89beb70c0333a025726867aba5db7614911be3c46664bb59d1ab594a">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Dependencies</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>package.json</strong><dd><code>Upgrade code-suggester
dependency</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

js/cf-api/package.json

<li>Upgraded dependency: replaced code-suggester with
<br>@codeflash-ai/code-suggester v5.0.2


</details>


  </td>
<td><a
href="https://github.com/codeflash-ai/codeflash-internal/pull/1522/files#diff-53ddfb1f8a02f1231d3d15a2e694ffe1407d2cc01d3e685de5653b67fec571c7">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

___

> <details> <summary> Need help?</summary><li>Type <code>/help how to
...</code> in the comments thread for any questions about PR-Agent
usage.</li><li>Check out the <a
href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a>
for more information.</li></details>
2025-05-09 14:53:28 -04:00

60 lines
1.9 KiB
TypeScript

import { type FileDiffContent, type Hunk } from "@codeflash-ai/code-suggester/build/src/types.js"
import {
getRawSuggestionHunks,
partitionSuggestedHunksByScope,
} from "@codeflash-ai/code-suggester/build/src/utils/hunk-utils.js"
import { getPullRequestHunks } from "@codeflash-ai/code-suggester/build/src/github/review-pull-request.js"
import { type Octokit } from "@octokit/rest"
export function fileDiffsToMap(obj: Record<string, FileDiffContent>): Map<string, FileDiffContent> {
const map = new Map()
Object.keys(obj).forEach(key => {
if (
obj[key] &&
typeof obj[key] === "object" &&
typeof obj[key].oldContent === "string" &&
typeof obj[key].newContent === "string"
) {
map.set(key, {
oldContent: obj[key].oldContent,
newContent: obj[key].newContent,
} as FileDiffContent)
}
})
return map
}
export async function determineValidHunks(
octokit: Octokit,
remote: { owner: string; repo: string },
pullNumber: number,
pageSize: number,
diffContents: Map<string, FileDiffContent>,
) {
// get the hunks from the pull request
const pullRequestHunks = await getPullRequestHunks(octokit as any, remote, pullNumber, pageSize)
// get the hunks from the suggested change
const allSuggestedHunks: Map<string, Hunk[]> = getRawSuggestionHunks(diffContents)
// split hunks by commentable and uncommentable
const { validHunks, invalidHunks } = partitionSuggestedHunksByScope(
pullRequestHunks,
allSuggestedHunks,
)
return { validHunks, invalidHunks }
}
export function isDiffContentsWellFormed(
diffContents: any,
): diffContents is Record<string, FileDiffContent> {
return Object.values(diffContents).every(
content =>
content !== null &&
typeof content === "object" &&
"oldContent" in content &&
"newContent" in content &&
typeof content.oldContent === "string" &&
typeof content.newContent === "string",
)
}