mirror of
https://github.com/github/codeql-action.git
synced 2025-12-30 19:20:08 +08:00
136 lines
4.0 KiB
JavaScript
136 lines
4.0 KiB
JavaScript
import { get, optionsToCacheKey, set } from "./cache.js";
|
|
import { getAppAuthentication } from "./get-app-authentication.js";
|
|
import { toTokenAuthentication } from "./to-token-authentication.js";
|
|
async function getInstallationAuthentication(state, options, customRequest) {
|
|
const installationId = Number(options.installationId || state.installationId);
|
|
if (!installationId) {
|
|
throw new Error(
|
|
"[@octokit/auth-app] installationId option is required for installation authentication."
|
|
);
|
|
}
|
|
if (options.factory) {
|
|
const { type, factory, oauthApp, ...factoryAuthOptions } = {
|
|
...state,
|
|
...options
|
|
};
|
|
return factory(factoryAuthOptions);
|
|
}
|
|
const request = customRequest || state.request;
|
|
return getInstallationAuthenticationConcurrently(
|
|
state,
|
|
{ ...options, installationId },
|
|
request
|
|
);
|
|
}
|
|
const pendingPromises = /* @__PURE__ */ new Map();
|
|
function getInstallationAuthenticationConcurrently(state, options, request) {
|
|
const cacheKey = optionsToCacheKey(options);
|
|
if (pendingPromises.has(cacheKey)) {
|
|
return pendingPromises.get(cacheKey);
|
|
}
|
|
const promise = getInstallationAuthenticationImpl(
|
|
state,
|
|
options,
|
|
request
|
|
).finally(() => pendingPromises.delete(cacheKey));
|
|
pendingPromises.set(cacheKey, promise);
|
|
return promise;
|
|
}
|
|
async function getInstallationAuthenticationImpl(state, options, request) {
|
|
if (!options.refresh) {
|
|
const result = await get(state.cache, options);
|
|
if (result) {
|
|
const {
|
|
token: token2,
|
|
createdAt: createdAt2,
|
|
expiresAt: expiresAt2,
|
|
permissions: permissions2,
|
|
repositoryIds: repositoryIds2,
|
|
repositoryNames: repositoryNames2,
|
|
singleFileName: singleFileName2,
|
|
repositorySelection: repositorySelection2
|
|
} = result;
|
|
return toTokenAuthentication({
|
|
installationId: options.installationId,
|
|
token: token2,
|
|
createdAt: createdAt2,
|
|
expiresAt: expiresAt2,
|
|
permissions: permissions2,
|
|
repositorySelection: repositorySelection2,
|
|
repositoryIds: repositoryIds2,
|
|
repositoryNames: repositoryNames2,
|
|
singleFileName: singleFileName2
|
|
});
|
|
}
|
|
}
|
|
const appAuthentication = await getAppAuthentication(state);
|
|
const payload = {
|
|
installation_id: options.installationId,
|
|
mediaType: {
|
|
previews: ["machine-man"]
|
|
},
|
|
headers: {
|
|
authorization: `bearer ${appAuthentication.token}`
|
|
}
|
|
};
|
|
if (options.repositoryIds) {
|
|
Object.assign(payload, { repository_ids: options.repositoryIds });
|
|
}
|
|
if (options.repositoryNames) {
|
|
Object.assign(payload, {
|
|
repositories: options.repositoryNames
|
|
});
|
|
}
|
|
if (options.permissions) {
|
|
Object.assign(payload, { permissions: options.permissions });
|
|
}
|
|
const {
|
|
data: {
|
|
token,
|
|
expires_at: expiresAt,
|
|
repositories,
|
|
permissions: permissionsOptional,
|
|
repository_selection: repositorySelectionOptional,
|
|
single_file: singleFileName
|
|
}
|
|
} = await request(
|
|
"POST /app/installations/{installation_id}/access_tokens",
|
|
payload
|
|
);
|
|
const permissions = permissionsOptional || {};
|
|
const repositorySelection = repositorySelectionOptional || "all";
|
|
const repositoryIds = repositories ? repositories.map((r) => r.id) : void 0;
|
|
const repositoryNames = repositories ? repositories.map((repo) => repo.name) : void 0;
|
|
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
const cacheOptions = {
|
|
token,
|
|
createdAt,
|
|
expiresAt,
|
|
repositorySelection,
|
|
permissions,
|
|
repositoryIds,
|
|
repositoryNames
|
|
};
|
|
if (singleFileName) {
|
|
Object.assign(payload, { singleFileName });
|
|
}
|
|
await set(state.cache, options, cacheOptions);
|
|
const cacheData = {
|
|
installationId: options.installationId,
|
|
token,
|
|
createdAt,
|
|
expiresAt,
|
|
repositorySelection,
|
|
permissions,
|
|
repositoryIds,
|
|
repositoryNames
|
|
};
|
|
if (singleFileName) {
|
|
Object.assign(cacheData, { singleFileName });
|
|
}
|
|
return toTokenAuthentication(cacheData);
|
|
}
|
|
export {
|
|
getInstallationAuthentication
|
|
};
|