Extract installation auth helper

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Parker Brown
2026-03-20 23:07:08 -07:00
parent a2a14fd880
commit 8b90615b3f
2 changed files with 51 additions and 64 deletions
+22 -29
View File
@@ -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
+29 -35
View File
@@ -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);
}