mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
add verify-existing-optimizations endpoint
This commit is contained in:
parent
968026882d
commit
361be65a93
3 changed files with 80 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -160,3 +160,5 @@ cython_debug/
|
|||
#.idea/
|
||||
.aider*
|
||||
/js/common/node_modules/
|
||||
*.xml
|
||||
*.pem
|
||||
|
|
|
|||
75
js/cf-api/endpoints/verify-existing-optimizations.ts
Normal file
75
js/cf-api/endpoints/verify-existing-optimizations.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import { getInstallationOctokitByOwner } from "../github/github-utils"
|
||||
import { githubApp } from "../github/github-app"
|
||||
|
||||
|
||||
|
||||
export async function verifyExistingOptimizations(req, res) {
|
||||
const { repo_owner, repo_name, pr_number } = req.body
|
||||
|
||||
const octokit = await getInstallationOctokitByOwner(githubApp, repo_owner, repo_name)
|
||||
if (octokit instanceof Error) {
|
||||
return res.status(500).send({ error: octokit.message })
|
||||
}
|
||||
console.log(`Got installation Octokit for ${repo_owner}/${repo_name}`)
|
||||
|
||||
const pr = await octokit.rest.pulls.get({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
pull_number: pr_number,
|
||||
})
|
||||
if (pr.status !== 200) {
|
||||
return res
|
||||
.status(500)
|
||||
.send({ error: `Error getting PR ${pr_number} for ${repo_owner}/${repo_name}` })
|
||||
}
|
||||
const optimizations_dict: { [key: string]: [string] } = {}
|
||||
|
||||
if (pr.data.body.includes("This pull request contains optimizations for PR")) {
|
||||
const pr_body = pr.data.body.split("\n")
|
||||
for (const line of pr_body) {
|
||||
if (line.includes("📄")) {
|
||||
const split = line.split(" ")
|
||||
const function_name: string = split[2].replace("`", "").replace("`", "")
|
||||
const file_path: string = split[4].replace("`", "").replace("`", "")
|
||||
if (file_path in optimizations_dict) {
|
||||
optimizations_dict[file_path].push(function_name)
|
||||
} else {
|
||||
optimizations_dict[file_path] = [function_name]
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const pr_messages = await octokit.rest.issues.listComments({
|
||||
owner: repo_owner,
|
||||
repo: repo_name,
|
||||
issue_number: pr_number,
|
||||
})
|
||||
|
||||
if (pr_messages.status !== 200) {
|
||||
return res
|
||||
.status(500)
|
||||
.send({ error: `Error getting PR messages for ${repo_owner}/${repo_name}` })
|
||||
}
|
||||
|
||||
for (const message of pr_messages.data) {
|
||||
if (message.body.includes("Codeflash found optimizations for this PR")) {
|
||||
const pr_body = message.body.split("\n")
|
||||
for (const line of pr_body) {
|
||||
if (line.includes("📄")) {
|
||||
const split = line.split(" ")
|
||||
const function_name: string = split[2].replace("`", "").replace("`", "")
|
||||
const file_path: string = split[4].replace("`", "").replace("`", "")
|
||||
|
||||
if (file_path in optimizations_dict) {
|
||||
optimizations_dict[file_path].push(function_name)
|
||||
} else {
|
||||
optimizations_dict[file_path] = [function_name]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res.status(200).send(optimizations_dict)
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ import { healthcheck } from "./endpoints/healthcheck"
|
|||
import { collectEmail } from "./endpoints/collect-email"
|
||||
import { checkForValidAPIKey } from "./middlewares/check-valid-api-key"
|
||||
import { trackEndpointCalls } from "./middlewares/track-endpoint-calls"
|
||||
import { verifyExistingOptimizations } from "./endpoints/verify-existing-optimizations"
|
||||
import { getUser } from "./endpoints/cli-get-user"
|
||||
import { addAsync } from "@awaitjs/express"
|
||||
import * as Sentry from "@sentry/node"
|
||||
|
|
@ -82,6 +83,8 @@ appExpress.postAsync("/cfapi/create-pr", createPr)
|
|||
|
||||
appExpress.getAsync("/cfapi/is-github-app-installed", isGitHubAppInstalled)
|
||||
|
||||
appExpress.postAsync("/cfapi/verify-existing-optimizations", verifyExistingOptimizations)
|
||||
|
||||
// The error handler must be registered before any other error middleware and after all controllers
|
||||
Sentry.setupExpressErrorHandler(appExpress)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue