|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { Credentials } from '../authentication'; |
| 6 | +import { Logger } from '../logging'; |
| 7 | +import { AnalysisAlert, AnalysisRawResults } from './shared/analysis-result'; |
| 8 | +import { sarifParser } from '../sarif-parser'; |
| 9 | +import { extractAnalysisAlerts } from './sarif-processing'; |
| 10 | +import { CodeQLCliServer } from '../cli'; |
| 11 | +import { extractRawResults } from './bqrs-processing'; |
| 12 | +import { VariantAnalysisScannedRepositoryResult } from './shared/variant-analysis'; |
| 13 | +import { DisposableObject, DisposeHandler } from '../pure/disposable-object'; |
| 14 | +import { VariantAnalysisRepoTask } from './gh-api/variant-analysis'; |
| 15 | +import * as ghApiClient from './gh-api/gh-api-client'; |
| 16 | +import { EventEmitter } from 'vscode'; |
| 17 | + |
| 18 | +type CacheKey = `${number}/${string}`; |
| 19 | + |
| 20 | +const createCacheKey = (variantAnalysisId: number, repoTask: VariantAnalysisRepoTask): CacheKey => `${variantAnalysisId}/${repoTask.repository.full_name}`; |
| 21 | + |
| 22 | +export type ResultDownloadedEvent = { |
| 23 | + variantAnalysisId: number; |
| 24 | + repoTask: VariantAnalysisRepoTask; |
| 25 | +} |
| 26 | + |
| 27 | +export class VariantAnalysisResultsManager extends DisposableObject { |
| 28 | + private readonly cachedResults: Map<CacheKey, VariantAnalysisScannedRepositoryResult>; |
| 29 | + |
| 30 | + private readonly _onResultDownloaded = this.push(new EventEmitter<ResultDownloadedEvent>()); |
| 31 | + readonly onResultDownloaded = this._onResultDownloaded.event; |
| 32 | + |
| 33 | + private readonly _onResultLoaded = this.push(new EventEmitter<VariantAnalysisScannedRepositoryResult>()); |
| 34 | + readonly onResultLoaded = this._onResultLoaded.event; |
| 35 | + |
| 36 | + constructor( |
| 37 | + private readonly cliServer: CodeQLCliServer, |
| 38 | + private readonly storagePath: string, |
| 39 | + private readonly logger: Logger, |
| 40 | + ) { |
| 41 | + super(); |
| 42 | + this.cachedResults = new Map(); |
| 43 | + } |
| 44 | + |
| 45 | + public async download( |
| 46 | + credentials: Credentials, |
| 47 | + variantAnalysisId: number, |
| 48 | + repoTask: VariantAnalysisRepoTask, |
| 49 | + ): Promise<void> { |
| 50 | + if (!repoTask.artifact_url) { |
| 51 | + throw new Error('Missing artifact URL'); |
| 52 | + } |
| 53 | + |
| 54 | + const resultDirectory = this.getRepoStorageDirectory(variantAnalysisId, repoTask.repository.full_name); |
| 55 | + |
| 56 | + const result = await ghApiClient.getVariantAnalysisRepoResult( |
| 57 | + credentials, |
| 58 | + repoTask.artifact_url |
| 59 | + ); |
| 60 | + |
| 61 | + fs.mkdirSync(resultDirectory, { recursive: true }); |
| 62 | + await fs.writeFile(path.join(resultDirectory, 'results.zip'), JSON.stringify(result, null, 2), 'utf8'); |
| 63 | + |
| 64 | + this._onResultDownloaded.fire({ |
| 65 | + variantAnalysisId, |
| 66 | + repoTask, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public async loadResults( |
| 71 | + variantAnalysisId: number, |
| 72 | + repoTask: VariantAnalysisRepoTask |
| 73 | + ): Promise<VariantAnalysisScannedRepositoryResult> { |
| 74 | + const result = this.cachedResults.get(createCacheKey(variantAnalysisId, repoTask)); |
| 75 | + |
| 76 | + return result ?? await this.loadResultsIntoMemory(variantAnalysisId, repoTask); |
| 77 | + } |
| 78 | + |
| 79 | + private async loadResultsIntoMemory( |
| 80 | + variantAnalysisId: number, |
| 81 | + repoTask: VariantAnalysisRepoTask, |
| 82 | + ): Promise<VariantAnalysisScannedRepositoryResult> { |
| 83 | + const result = await this.loadResultsFromStorage(variantAnalysisId, repoTask); |
| 84 | + this.cachedResults.set(createCacheKey(variantAnalysisId, repoTask), result); |
| 85 | + this._onResultLoaded.fire(result); |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + private async loadResultsFromStorage( |
| 90 | + variantAnalysisId: number, |
| 91 | + repoTask: VariantAnalysisRepoTask, |
| 92 | + ): Promise<VariantAnalysisScannedRepositoryResult> { |
| 93 | + if (!(await this.isVariantAnalysisRepoDownloaded(variantAnalysisId, repoTask))) { |
| 94 | + throw new Error('Variant analysis results not downloaded'); |
| 95 | + } |
| 96 | + |
| 97 | + if (!repoTask.database_commit_sha || !repoTask.source_location_prefix) { |
| 98 | + throw new Error('Missing database commit SHA'); |
| 99 | + } |
| 100 | + |
| 101 | + const fileLinkPrefix = this.createGitHubDotcomFileLinkPrefix(repoTask.repository.full_name, repoTask.database_commit_sha); |
| 102 | + |
| 103 | + const storageDirectory = this.getRepoStorageDirectory(variantAnalysisId, repoTask.repository.full_name); |
| 104 | + |
| 105 | + const sarifPath = path.join(storageDirectory, 'results.sarif'); |
| 106 | + const bqrsPath = path.join(storageDirectory, 'results.bqrs'); |
| 107 | + if (await fs.pathExists(sarifPath)) { |
| 108 | + const interpretedResults = await this.readSarifResults(sarifPath, fileLinkPrefix); |
| 109 | + |
| 110 | + return { |
| 111 | + variantAnalysisId, |
| 112 | + repositoryId: repoTask.repository.id, |
| 113 | + interpretedResults, |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + if (await fs.pathExists(bqrsPath)) { |
| 118 | + const rawResults = await this.readBqrsResults(bqrsPath, fileLinkPrefix, repoTask.source_location_prefix); |
| 119 | + |
| 120 | + return { |
| 121 | + variantAnalysisId, |
| 122 | + repositoryId: repoTask.repository.id, |
| 123 | + rawResults, |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + throw new Error('Missing results file'); |
| 128 | + } |
| 129 | + |
| 130 | + private async isVariantAnalysisRepoDownloaded( |
| 131 | + variantAnalysisId: number, |
| 132 | + repoTask: VariantAnalysisRepoTask, |
| 133 | + ): Promise<boolean> { |
| 134 | + return await fs.pathExists(this.getRepoStorageDirectory(variantAnalysisId, repoTask.repository.full_name)); |
| 135 | + } |
| 136 | + |
| 137 | + private async readBqrsResults(filePath: string, fileLinkPrefix: string, sourceLocationPrefix: string): Promise<AnalysisRawResults> { |
| 138 | + return await extractRawResults(this.cliServer, this.logger, filePath, fileLinkPrefix, sourceLocationPrefix); |
| 139 | + } |
| 140 | + |
| 141 | + private async readSarifResults(filePath: string, fileLinkPrefix: string): Promise<AnalysisAlert[]> { |
| 142 | + const sarifLog = await sarifParser(filePath); |
| 143 | + |
| 144 | + const processedSarif = extractAnalysisAlerts(sarifLog, fileLinkPrefix); |
| 145 | + if (processedSarif.errors.length) { |
| 146 | + void this.logger.log(`Error processing SARIF file: ${os.EOL}${processedSarif.errors.join(os.EOL)}`); |
| 147 | + } |
| 148 | + |
| 149 | + return processedSarif.alerts; |
| 150 | + } |
| 151 | + |
| 152 | + private getStorageDirectory(variantAnalysisId: number): string { |
| 153 | + return path.join( |
| 154 | + this.storagePath, |
| 155 | + `${variantAnalysisId}` |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + private getRepoStorageDirectory(variantAnalysisId: number, fullName: string): string { |
| 160 | + return path.join( |
| 161 | + this.getStorageDirectory(variantAnalysisId), |
| 162 | + fullName |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + private createGitHubDotcomFileLinkPrefix(fullName: string, sha: string): string { |
| 167 | + return `https://github.com/${fullName}/blob/${sha}`; |
| 168 | + } |
| 169 | + |
| 170 | + public dispose(disposeHandler?: DisposeHandler) { |
| 171 | + super.dispose(disposeHandler); |
| 172 | + |
| 173 | + this.cachedResults.clear(); |
| 174 | + } |
| 175 | +} |
0 commit comments