mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
Refactor GitHub App installation error handling in code context hash endpoints. Introduced a dedicated assertion function to streamline error validation and improve logging for installation issues.
This commit is contained in:
parent
8d19be486e
commit
0b87fa0e12
1 changed files with 31 additions and 30 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { prisma } from "@codeflash-ai/common"
|
import { prisma } from "@codeflash-ai/common"
|
||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
|
import { Octokit } from "@octokit/core"
|
||||||
import { githubApp } from "../github/github-app.js"
|
import { githubApp } from "../github/github-app.js"
|
||||||
import { getInstallationOctokitByOwner, isUserCollaborator } from "../github/github-utils.js"
|
import { getInstallationOctokitByOwner, isUserCollaborator } from "../github/github-utils.js"
|
||||||
import { userNickname } from "../auth0-mgmt.js"
|
import { userNickname } from "../auth0-mgmt.js"
|
||||||
|
|
@ -16,6 +17,34 @@ import {
|
||||||
githubInstallationError,
|
githubInstallationError,
|
||||||
} from "../exceptions/index.js"
|
} from "../exceptions/index.js"
|
||||||
|
|
||||||
|
const GITHUB_APP_INSTALL_LINK = "https://github.com/apps/codeflash-ai/installations/select_target"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the installation octokit result and throws appropriate errors if it's an Error.
|
||||||
|
* Uses TypeScript assertion to narrow the type to Octokit on success.
|
||||||
|
*/
|
||||||
|
function assertInstallationOctokit(
|
||||||
|
octokit: Octokit | Error,
|
||||||
|
owner: string,
|
||||||
|
repo: string,
|
||||||
|
req: Request,
|
||||||
|
): asserts octokit is Octokit {
|
||||||
|
if (!(octokit instanceof Error)) return
|
||||||
|
|
||||||
|
const repoPath = `${owner}/${repo}`
|
||||||
|
const errorMessage = octokit.message
|
||||||
|
|
||||||
|
if (errorMessage.includes("not installed")) {
|
||||||
|
logger.warn("GitHub App not installed on repository", req, {
|
||||||
|
repo: repoPath,
|
||||||
|
installationLink: GITHUB_APP_INSTALL_LINK,
|
||||||
|
})
|
||||||
|
throw githubInstallationNotFound(`${repoPath}. Please install the GitHub App at: ${GITHUB_APP_INSTALL_LINK}`)
|
||||||
|
} else {
|
||||||
|
throw githubInstallationError(errorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function is_code_being_optimized_again(req: Request, res: Response) {
|
export async function is_code_being_optimized_again(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const { owner, repo, pr_number, code_contexts } = req.body
|
const { owner, repo, pr_number, code_contexts } = req.body
|
||||||
|
|
@ -29,21 +58,7 @@ export async function is_code_being_optimized_again(req: Request, res: Response)
|
||||||
throw unauthorized("")
|
throw unauthorized("")
|
||||||
}
|
}
|
||||||
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
|
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
|
||||||
if (octokit instanceof Error) {
|
assertInstallationOctokit(octokit, owner, repo, req)
|
||||||
const errorMessage = octokit.message
|
|
||||||
if (errorMessage.includes("not installed")) {
|
|
||||||
const installationLink = "https://github.com/apps/codeflash-ai/installations/select_target"
|
|
||||||
logger.warn("GitHub App not installed on repository", req, {
|
|
||||||
repo: `${owner}/${repo}`,
|
|
||||||
installationLink,
|
|
||||||
})
|
|
||||||
throw githubInstallationNotFound(
|
|
||||||
`${owner}/${repo}. Please install the GitHub App at: ${installationLink}`,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
throw githubInstallationError(errorMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check collaborator status with error handling
|
// Check collaborator status with error handling
|
||||||
try {
|
try {
|
||||||
|
|
@ -177,21 +192,7 @@ export async function add_optimized_code_context(req: Request, res: Response) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
|
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
|
||||||
if (octokit instanceof Error) {
|
assertInstallationOctokit(octokit, owner, repo, req)
|
||||||
const errorMessage = octokit.message
|
|
||||||
if (errorMessage.includes("not installed")) {
|
|
||||||
const installationLink = "https://github.com/apps/codeflash-ai/installations/select_target"
|
|
||||||
logger.warn("GitHub App not installed on repository", req, {
|
|
||||||
repo: `${owner}/${repo}`,
|
|
||||||
installationLink,
|
|
||||||
})
|
|
||||||
throw githubInstallationNotFound(
|
|
||||||
`${owner}/${repo}. Please install the GitHub App at: ${installationLink}`,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
throw githubInstallationError(errorMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check collaborator status with error handling
|
// Check collaborator status with error handling
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue