Add GitHub App authentication fallback

This commit is contained in:
lezi-fun committed 2026-05-17 17:16:30 +08:00
1 parent 3e6caa6730
commit 8bc1094688
7 files changed
+594 -3

No files matched your search

+5 -1
View File
@@ -68,7 +68,11 @@ async function main(): Promise<void> {
const payload = workflowCallEventName
? buildPayloadFromWorkflowCallEnv(workflowCallEventName)
: readPayloadFromGitHubEventPath();
const octokit = createGitHubClient();
const repository = payload.repository;
const octokit = await createGitHubClient({
owner: repository.owner.login,
repo: repository.name
});
const eventName = workflowCallEventName ?? process.env.GITHUB_EVENT_NAME;
if (!eventName) {
+12
View File
@@ -29,6 +29,15 @@ const envBoolean = z.preprocess((value) => {
const configSchema = z.object({
port: z.coerce.number().int().positive().default(3000),
githubToken: z.string().min(1),
githubAppId: optionalString,
githubAppPrivateKey: optionalString,
githubAppInstallationId: z.preprocess((value) => {
if (value === "" || value === undefined) {
return undefined;
}
return value;
}, z.coerce.number().int().positive().optional()),
codexModel: optionalString.default("gpt-5.4"),
codexReasoningEffort: z.preprocess((value) => {
return value === "" ? undefined : value;
@@ -46,6 +55,9 @@ const configSchema = z.object({
export const config = configSchema.parse({
port: process.env.PORT,
githubToken: process.env.GITHUB_TOKEN,
githubAppId: process.env.GITHUB_APP_ID,
githubAppPrivateKey: process.env.GITHUB_APP_PRIVATE_KEY,
githubAppInstallationId: process.env.GITHUB_APP_INSTALLATION_ID,
codexModel: process.env.CODEX_MODEL,
codexReasoningEffort: process.env.CODEX_REASONING_EFFORT,
codexBaseUrl: process.env.CODEX_BASE_URL,
+66 -1
View File
@@ -1,8 +1,73 @@
import { createAppAuth } from "@octokit/auth-app";
import { Octokit } from "@octokit/rest";
import { config } from "../config.js";
import { logger } from "../logger.js";
export async function createGitHubClient(params?: {
owner: string;
repo: string;
}): Promise<Octokit> {
if (config.githubAppId && config.githubAppPrivateKey) {
try {
const privateKey = normalizePrivateKey(config.githubAppPrivateKey);
const auth = createAppAuth({
appId: config.githubAppId,
privateKey
});
const installationId =
config.githubAppInstallationId ??
(params ? await resolveInstallationId(auth, params.owner, params.repo) : undefined);
if (!installationId) {
throw new Error("GitHub App installation id is not configured and could not be resolved from the repository.");
}
const installationAuthentication = await auth({
type: "installation",
installationId
});
return new Octokit({
auth: installationAuthentication.token
});
} catch (error) {
logger.warn(
{
error,
githubAppId: config.githubAppId,
githubAppInstallationId: config.githubAppInstallationId,
owner: params?.owner,
repo: params?.repo
},
"Failed to create GitHub App installation client; falling back to GITHUB_TOKEN."
);
}
}
export function createGitHubClient(): Octokit {
return new Octokit({
auth: config.githubToken
});
}
function normalizePrivateKey(value: string): string {
return value.includes("\\n") ? value.replace(/\\n/g, "\n") : value;
}
async function resolveInstallationId(
auth: ReturnType<typeof createAppAuth>,
owner: string,
repo: string
): Promise<number | undefined> {
const appAuthentication = await auth({ type: "app" });
const appOctokit = new Octokit({
auth: appAuthentication.token
});
const { data } = await appOctokit.request("GET /repos/{owner}/{repo}/installation", {
owner,
repo
});
return data.id;
}