From 8b90615b3f2bcab14a906ac53be02434c983d564 Mon Sep 17 00:00:00 2001 From: Parker Brown <17183625+parkerbxyz@users.noreply.github.com> Date: Fri, 20 Mar 2026 23:07:08 -0700 Subject: [PATCH] Extract installation auth helper Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- dist/main.cjs | 51 ++++++++++++++++++---------------------- lib/main.js | 64 +++++++++++++++++++++++---------------------------- 2 files changed, 51 insertions(+), 64 deletions(-) diff --git a/dist/main.cjs b/dist/main.cjs index d779058..1c7250f 100644 --- a/dist/main.cjs +++ b/dist/main.cjs @@ -23261,6 +23261,19 @@ function resolveInstallationTarget(enterprise, owner, repositories, core) { repositories }; } +async function createInstallationAuthResult(auth5, installation, permissions, options = {}) { + const authentication = await auth5({ + type: "installation", + installationId: installation.id, + permissions, + ...options + }); + return { + authentication, + installationId: installation.id, + appSlug: installation["app_slug"] + }; +} async function getTokenFromOwner(request2, auth5, parsedOwner, permissions) { const response = await request2("GET /users/{username}/installation", { username: parsedOwner, @@ -23268,14 +23281,7 @@ async function getTokenFromOwner(request2, auth5, parsedOwner, permissions) { hook: auth5.hook } }); - const authentication = await auth5({ - type: "installation", - installationId: response.data.id, - permissions - }); - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - return { authentication, installationId, appSlug }; + return createInstallationAuthResult(auth5, response.data, permissions); } async function getTokenFromRepository(request2, auth5, parsedOwner, parsedRepositoryNames, permissions) { const response = await request2("GET /repos/{owner}/{repo}/installation", { @@ -23285,15 +23291,9 @@ async function getTokenFromRepository(request2, auth5, parsedOwner, parsedReposi hook: auth5.hook } }); - const authentication = await auth5({ - type: "installation", - installationId: response.data.id, - repositoryNames: parsedRepositoryNames, - permissions + return createInstallationAuthResult(auth5, response.data, permissions, { + repositoryNames: parsedRepositoryNames }); - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - return { authentication, installationId, appSlug }; } async function getTokenFromEnterprise(request2, auth5, enterprise, permissions) { let response; @@ -23305,21 +23305,14 @@ async function getTokenFromEnterprise(request2, auth5, enterprise, permissions) } }); } catch (error2) { - if (error2.status === 404) { - throw new Error( - `No enterprise installation found matching the name ${enterprise}.` - ); + if (error2.status !== 404) { + throw error2; } - throw error2; + throw new Error( + `No enterprise installation found matching the name ${enterprise}.` + ); } - const authentication = await auth5({ - type: "installation", - installationId: response.data.id, - permissions - }); - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - return { authentication, installationId, appSlug }; + return createInstallationAuthResult(auth5, response.data, permissions); } // lib/request.js diff --git a/lib/main.js b/lib/main.js index f0d8db0..b05b761 100644 --- a/lib/main.js +++ b/lib/main.js @@ -154,6 +154,26 @@ function resolveInstallationTarget(enterprise, owner, repositories, core) { }; } +async function createInstallationAuthResult( + auth, + installation, + permissions, + options = {}, +) { + const authentication = await auth({ + type: "installation", + installationId: installation.id, + permissions, + ...options, + }); + + return { + authentication, + installationId: installation.id, + appSlug: installation["app_slug"], + }; +} + async function getTokenFromOwner(request, auth, parsedOwner, permissions) { // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app // This endpoint works for both users and organizations @@ -165,16 +185,7 @@ async function getTokenFromOwner(request, auth, parsedOwner, permissions) { }); // Get token for all repositories of the given installation - const authentication = await auth({ - type: "installation", - installationId: response.data.id, - permissions, - }); - - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - - return { authentication, installationId, appSlug }; + return createInstallationAuthResult(auth, response.data, permissions); } async function getTokenFromRepository( @@ -194,17 +205,9 @@ async function getTokenFromRepository( }); // Get token for given repositories - const authentication = await auth({ - type: "installation", - installationId: response.data.id, + return createInstallationAuthResult(auth, response.data, permissions, { repositoryNames: parsedRepositoryNames, - permissions, }); - - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - - return { authentication, installationId, appSlug }; } async function getTokenFromEnterprise(request, auth, enterprise, permissions) { @@ -217,25 +220,16 @@ async function getTokenFromEnterprise(request, auth, enterprise, permissions) { }, }); } catch (error) { - /* c8 ignore next 8 */ - if (error.status === 404) { - throw new Error( - `No enterprise installation found matching the name ${enterprise}.` - ); + /* c8 ignore next 3 */ + if (error.status !== 404) { + throw error; } - throw error; + throw new Error( + `No enterprise installation found matching the name ${enterprise}.` + ); } // Get token for the enterprise installation - const authentication = await auth({ - type: "installation", - installationId: response.data.id, - permissions, - }); - - const installationId = response.data.id; - const appSlug = response.data["app_slug"]; - - return { authentication, installationId, appSlug }; + return createInstallationAuthResult(auth, response.data, permissions); }