Merge pull request #2267 from codeflash-ai/ashraf/cf-1024-correct-error-message-when-github-app-is-not-installed

Enhance error handling for GitHub App installation checks in code con…
This commit is contained in:
mashraf-222 2026-04-28 19:38:50 +03:00 committed by GitHub
commit bf68c1a50d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
import { prisma } from "@codeflash-ai/common"
import { Request, Response } from "express"
import { Octokit } from "@octokit/core"
import { githubApp } from "../github/github-app.js"
import { getInstallationOctokitByOwner, isUserCollaborator } from "../github/github-utils.js"
import { userNickname } from "../auth0-mgmt.js"
@ -12,8 +13,38 @@ import {
validationFailure,
internalServerError,
conflict,
githubInstallationNotFound,
githubInstallationError,
} 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) {
try {
const { owner, repo, pr_number, code_contexts } = req.body
@ -27,9 +58,7 @@ export async function is_code_being_optimized_again(req: Request, res: Response)
throw unauthorized("")
}
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
if (octokit instanceof Error) {
throw internalServerError(octokit.message)
}
assertInstallationOctokit(octokit, owner, repo, req)
// Check collaborator status with error handling
try {
@ -163,9 +192,7 @@ export async function add_optimized_code_context(req: Request, res: Response) {
}
const octokit = await getInstallationOctokitByOwner(githubApp, owner, repo, userId)
if (octokit instanceof Error) {
throw internalServerError(octokit.message)
}
assertInstallationOctokit(octokit, owner, repo, req)
// Check collaborator status with error handling
try {