mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 01:30:10 +08:00
79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
import { Lru } from "toad-cache";
|
|
function getCache() {
|
|
return new Lru(
|
|
// cache max. 15000 tokens, that will use less than 10mb memory
|
|
15e3,
|
|
// Cache for 1 minute less than GitHub expiry
|
|
1e3 * 60 * 59
|
|
);
|
|
}
|
|
async function get(cache, options) {
|
|
const cacheKey = optionsToCacheKey(options);
|
|
const result = await cache.get(cacheKey);
|
|
if (!result) {
|
|
return;
|
|
}
|
|
const [
|
|
token,
|
|
createdAt,
|
|
expiresAt,
|
|
repositorySelection,
|
|
permissionsString,
|
|
singleFileName
|
|
] = result.split("|");
|
|
const permissions = options.permissions || permissionsString.split(/,/).reduce((permissions2, string) => {
|
|
if (/!$/.test(string)) {
|
|
permissions2[string.slice(0, -1)] = "write";
|
|
} else {
|
|
permissions2[string] = "read";
|
|
}
|
|
return permissions2;
|
|
}, {});
|
|
return {
|
|
token,
|
|
createdAt,
|
|
expiresAt,
|
|
permissions,
|
|
repositoryIds: options.repositoryIds,
|
|
repositoryNames: options.repositoryNames,
|
|
singleFileName,
|
|
repositorySelection
|
|
};
|
|
}
|
|
async function set(cache, options, data) {
|
|
const key = optionsToCacheKey(options);
|
|
const permissionsString = options.permissions ? "" : Object.keys(data.permissions).map(
|
|
(name) => `${name}${data.permissions[name] === "write" ? "!" : ""}`
|
|
).join(",");
|
|
const value = [
|
|
data.token,
|
|
data.createdAt,
|
|
data.expiresAt,
|
|
data.repositorySelection,
|
|
permissionsString,
|
|
data.singleFileName
|
|
].join("|");
|
|
await cache.set(key, value);
|
|
}
|
|
function optionsToCacheKey({
|
|
installationId,
|
|
permissions = {},
|
|
repositoryIds = [],
|
|
repositoryNames = []
|
|
}) {
|
|
const permissionsString = Object.keys(permissions).sort().map((name) => permissions[name] === "read" ? name : `${name}!`).join(",");
|
|
const repositoryIdsString = repositoryIds.sort().join(",");
|
|
const repositoryNamesString = repositoryNames.join(",");
|
|
return [
|
|
installationId,
|
|
repositoryIdsString,
|
|
repositoryNamesString,
|
|
permissionsString
|
|
].filter(Boolean).join("|");
|
|
}
|
|
export {
|
|
get,
|
|
getCache,
|
|
set
|
|
};
|