-
Notifications
You must be signed in to change notification settings - Fork 376
Basic support for overlay PR analysis #2945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9022c73
Add AugmentationProperties.overlayDatabaseMode
cklin ee8a8c4
config-utils: populate getOverlayDatabaseMode()
cklin a336faa
databaseInitCluster: use overlayDatabaseMode from config
cklin 60a2a7d
Add isAnalyzingPullRequest()
cklin da758dc
Add Feature.OverlayAnalysis
cklin 93e8729
getOverlayDatabaseMode: use Feature.OverlayAnalysis
cklin b442537
Limit OverlayAnalysis to internal repos
cklin d42ce71
Add AugmentationProperties.useOverlayDatabaseCaching
cklin 6ca06f4
Upload overlay-base database to actions cache
cklin b95402d
Extract checkOverlayBaseDatabase()
cklin 2fc04c8
Download overlay-base database from actions cache
cklin 42835b3
Override cleanup-level for overlay-base database
cklin 6a51e63
Add "overlay" to SARIF incrementalMode run property
cklin 8c5122e
Add getPullRequestBranches() tests
cklin 95a1b7e
Add getOverlayDatabaseMode() tests
cklin ec836d6
build: refresh js files
cklin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Upload overlay-base database to actions cache
- Loading branch information
commit 6ca06f41c4d79aaf9fbff552e26cdac3f12a80e7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
import { getTemporaryDirectory } from "./actions-util"; | ||
import * as actionsCache from "@actions/cache"; | ||
|
||
import { getRequiredInput, getTemporaryDirectory } from "./actions-util"; | ||
import { type CodeQL } from "./codeql"; | ||
import { type Config } from "./config-utils"; | ||
import { getFileOidsUnderPath } from "./git-utils"; | ||
import { getCommitOid, getFileOidsUnderPath } from "./git-utils"; | ||
import { Logger } from "./logging"; | ||
import { isInTestMode, withTimeout } from "./util"; | ||
|
||
export enum OverlayDatabaseMode { | ||
Overlay = "overlay", | ||
|
@@ -122,3 +126,111 @@ function computeChangedFiles( | |
} | ||
return changes; | ||
} | ||
|
||
// Constants for database caching | ||
const CACHE_VERSION = 1; | ||
const CACHE_PREFIX = "codeql-overlay-base-database"; | ||
const MAX_CACHE_OPERATION_MS = 120_000; // Two minutes | ||
|
||
/** | ||
* Uploads the overlay-base database to the GitHub Actions cache. If conditions | ||
* for uploading are not met, the function does nothing and returns false. | ||
* | ||
* This function uses the `checkout_path` input to determine the repository path | ||
* and works only when called from `analyze` or `upload-sarif`. | ||
* | ||
* @param codeql The CodeQL instance | ||
* @param config The configuration object | ||
* @param logger The logger instance | ||
* @returns A promise that resolves to true if the upload was performed and | ||
* successfully completed, or false otherwise | ||
*/ | ||
export async function uploadOverlayBaseDatabaseToCache( | ||
codeql: CodeQL, | ||
config: Config, | ||
logger: Logger, | ||
): Promise<boolean> { | ||
const overlayDatabaseMode = config.augmentationProperties.overlayDatabaseMode; | ||
if (overlayDatabaseMode !== OverlayDatabaseMode.OverlayBase) { | ||
logger.debug( | ||
`Overlay database mode is ${overlayDatabaseMode}. ` + | ||
"Skip uploading overlay-base database to cache.", | ||
); | ||
return false; | ||
} | ||
if (!config.augmentationProperties.useOverlayDatabaseCaching) { | ||
logger.debug( | ||
"Overlay database caching is disabled. " + | ||
"Skip uploading overlay-base database to cache.", | ||
); | ||
return false; | ||
} | ||
if (isInTestMode()) { | ||
logger.debug( | ||
"In test mode. Skip uploading overlay-base database to cache.", | ||
); | ||
return false; | ||
} | ||
|
||
// An overlay-base database should contain the base database OIDs file. | ||
// Verifying that the file exists serves as a sanity check. | ||
const baseDatabaseOidsFilePath = getBaseDatabaseOidsFilePath(config); | ||
if (!fs.existsSync(baseDatabaseOidsFilePath)) { | ||
logger.warning( | ||
"Cannot upload overlay-base database to cache: " + | ||
`${baseDatabaseOidsFilePath} does not exist`, | ||
); | ||
return false; | ||
} | ||
|
||
const dbLocation = config.dbLocation; | ||
const codeQlVersion = (await codeql.getVersion()).version; | ||
const checkoutPath = getRequiredInput("checkout_path"); | ||
const cacheKey = await generateCacheKey(config, codeQlVersion, checkoutPath); | ||
logger.info( | ||
`Uploading overlay-base database to Actions cache with key ${cacheKey}`, | ||
); | ||
|
||
try { | ||
const cacheId = await withTimeout( | ||
MAX_CACHE_OPERATION_MS, | ||
actionsCache.saveCache([dbLocation], cacheKey), | ||
() => {}, | ||
); | ||
if (cacheId === undefined) { | ||
logger.warning("Timed out while uploading overlay-base database"); | ||
return false; | ||
} | ||
} catch (error) { | ||
logger.warning( | ||
"Failed to upload overlay-base database to cache: " + | ||
`${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
return false; | ||
} | ||
logger.info(`Successfully uploaded overlay-base database from ${dbLocation}`); | ||
return true; | ||
} | ||
|
||
async function generateCacheKey( | ||
config: Config, | ||
codeQlVersion: string, | ||
checkoutPath: string, | ||
): Promise<string> { | ||
const sha = await getCommitOid(checkoutPath); | ||
return `${getCacheRestoreKey(config, codeQlVersion)}${sha}`; | ||
} | ||
|
||
function getCacheRestoreKey(config: Config, codeQlVersion: string): string { | ||
// The restore key (prefix) specifies which cached overlay-base databases are | ||
// compatible with the current analysis: the cached database must have the | ||
// same cache version and the same CodeQL bundle version. | ||
// | ||
// Actions cache supports using multiple restore keys to indicate preference. | ||
// Technically we prefer a cached overlay-base database with the same SHA as | ||
// we are analyzing. However, since overlay-base databases are built from the | ||
// default branch and used in PR analysis, it is exceedingly unlikely that | ||
// the commit SHA will ever be the same, so we can just leave it out. | ||
const languages = [...config.languages].sort().join("_"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Henry has an open PR to open up languages more, which might affect this in the future. Not necessarily something we need to consider here, but may need to consider there. |
||
return `${CACHE_PREFIX}-${CACHE_VERSION}-${languages}-${codeQlVersion}-`; | ||
mbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The guard logic for overlay database caching (checking mode, caching flag, and test mode) is duplicated in both upload and download functions. Consider extracting this into a shared helper to reduce duplication and simplify future maintenance.
Copilot uses AI. Check for mistakes.