|
1 | 1 | import * as fs from "fs";
|
2 | 2 | import * as path from "path";
|
3 | 3 |
|
| 4 | +import * as actionsCache from "@actions/cache"; |
| 5 | + |
4 | 6 | import { getTemporaryDirectory } from "./actions-util";
|
| 7 | +import { type CodeQL } from "./codeql"; |
5 | 8 | import { type Config } from "./config-utils";
|
6 | 9 | import { getFileOidsUnderPath } from "./git-utils";
|
7 | 10 | import { Logger } from "./logging";
|
| 11 | +import { isInTestMode, withTimeout } from "./util"; |
8 | 12 |
|
9 | 13 | export enum OverlayDatabaseMode {
|
10 | 14 | Overlay = "overlay",
|
@@ -122,3 +126,103 @@ function computeChangedFiles(
|
122 | 126 | }
|
123 | 127 | return changes;
|
124 | 128 | }
|
| 129 | + |
| 130 | +// Constants for database caching |
| 131 | +const CACHE_VERSION = 1; |
| 132 | +const CACHE_PREFIX = "codeql-overlay-base-database"; |
| 133 | +const MAX_CACHE_OPERATION_MS = 120_000; // Two minutes |
| 134 | + |
| 135 | +/** |
| 136 | + * Uploads the overlay-base database to the GitHub Actions cache. If conditions |
| 137 | + * for uploading are not met, the function does nothing and returns false. |
| 138 | + * |
| 139 | + * @param codeql The CodeQL instance |
| 140 | + * @param config The configuration object |
| 141 | + * @param logger The logger instance |
| 142 | + * @returns A promise that resolves to true if the upload was performed and |
| 143 | + * successfully completed, or false otherwise |
| 144 | + */ |
| 145 | +export async function uploadOverlayBaseDatabaseToCache( |
| 146 | + codeql: CodeQL, |
| 147 | + config: Config, |
| 148 | + logger: Logger, |
| 149 | +): Promise<boolean> { |
| 150 | + const overlayDatabaseMode = config.augmentationProperties.overlayDatabaseMode; |
| 151 | + if (overlayDatabaseMode !== OverlayDatabaseMode.OverlayBase) { |
| 152 | + logger.debug( |
| 153 | + `Overlay database mode is ${overlayDatabaseMode}. ` + |
| 154 | + "Skip uploading overlay-base database to cache.", |
| 155 | + ); |
| 156 | + return false; |
| 157 | + } |
| 158 | + if (!config.augmentationProperties.useOverlayDatabaseCaching) { |
| 159 | + logger.debug( |
| 160 | + "Overlay database caching is disabled. " + |
| 161 | + "Skip uploading overlay-base database to cache.", |
| 162 | + ); |
| 163 | + return false; |
| 164 | + } |
| 165 | + if (isInTestMode()) { |
| 166 | + logger.debug( |
| 167 | + "In test mode. Skip uploading overlay-base database to cache.", |
| 168 | + ); |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + // An overlay-base database should contain the base database OIDs file. |
| 173 | + // Verifying that the file exists serves as a sanity check. |
| 174 | + const baseDatabaseOidsFilePath = getBaseDatabaseOidsFilePath(config); |
| 175 | + if (!fs.existsSync(baseDatabaseOidsFilePath)) { |
| 176 | + logger.warning( |
| 177 | + "Cannot upload overlay-base database to cache: " + |
| 178 | + `${baseDatabaseOidsFilePath} does not exist`, |
| 179 | + ); |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + const dbLocation = config.dbLocation; |
| 184 | + const codeQlVersion = (await codeql.getVersion()).version; |
| 185 | + const cacheKey = generateCacheKey(config, codeQlVersion); |
| 186 | + logger.info( |
| 187 | + `Uploading overlay-base database to Actions cache with key ${cacheKey}`, |
| 188 | + ); |
| 189 | + |
| 190 | + try { |
| 191 | + const cacheId = await withTimeout( |
| 192 | + MAX_CACHE_OPERATION_MS, |
| 193 | + actionsCache.saveCache([dbLocation], cacheKey), |
| 194 | + () => {}, |
| 195 | + ); |
| 196 | + if (cacheId === undefined) { |
| 197 | + logger.warning("Timed out while uploading overlay-base database"); |
| 198 | + return false; |
| 199 | + } |
| 200 | + } catch (error) { |
| 201 | + logger.warning( |
| 202 | + "Failed to upload overlay-base database to cache: " + |
| 203 | + `${error instanceof Error ? error.message : String(error)}`, |
| 204 | + ); |
| 205 | + return false; |
| 206 | + } |
| 207 | + logger.info(`Successfully uploaded overlay-base database from ${dbLocation}`); |
| 208 | + return true; |
| 209 | +} |
| 210 | + |
| 211 | +function generateCacheKey(config: Config, codeQlVersion: string): string { |
| 212 | + const sha = process.env.GITHUB_SHA || "unknown"; |
| 213 | + return `${getCacheRestoreKey(config, codeQlVersion)}${sha}`; |
| 214 | +} |
| 215 | + |
| 216 | +function getCacheRestoreKey(config: Config, codeQlVersion: string): string { |
| 217 | + // The restore key (prefix) specifies which cached overlay-base databases are |
| 218 | + // compatible with the current analysis: the cached database must have the |
| 219 | + // same cache version and the same CodeQL bundle version. |
| 220 | + // |
| 221 | + // Actions cache supports using multiple restore keys to indicate preference. |
| 222 | + // Technically we prefer a cached overlay-base database with the same SHA as |
| 223 | + // we are analyzing. However, since overlay-base databases are built from the |
| 224 | + // default branch and used in PR analysis, it is exceedingly unlikely that |
| 225 | + // the commit SHA will ever be the same, so we can just leave it out. |
| 226 | + const languages = [...config.languages].sort().join("_"); |
| 227 | + return `${CACHE_PREFIX}-${CACHE_VERSION}-${languages}-${codeQlVersion}-`; |
| 228 | +} |
0 commit comments