@@ -11,8 +11,7 @@ import * as actionsUtil from "./actions-util";
1111import * as analyses from "./analyses" ;
1212import * as api from "./api-client" ;
1313import { getGitHubVersion , wrapApiConfigurationError } from "./api-client" ;
14- import { getCodeQL , type CodeQL } from "./codeql" ;
15- import { getConfig } from "./config-utils" ;
14+ import { type CodeQL } from "./codeql" ;
1615import { readDiffRangesJsonFile } from "./diff-informed-analysis-utils" ;
1716import { EnvVar } from "./environment" ;
1817import { FeatureEnablement } from "./feature-flags" ;
@@ -219,15 +218,19 @@ export async function minimalInitCodeQL(
219218 return initCodeQLResult . codeql ;
220219}
221220
221+ export type CodeQLGetter = ( ) => Promise < CodeQL > ;
222+
222223// Takes a list of paths to sarif files and combines them together using the
223224// CLI `github merge-results` command when all SARIF files are produced by
224225// CodeQL. Otherwise, it will fall back to combining the files in the action.
225226// Returns the contents of the combined sarif file.
226227async function combineSarifFilesUsingCLI (
227228 sarifFiles : string [ ] ,
228229 gitHubVersion : GitHubVersion ,
229- features : FeatureEnablement ,
230+ _features : FeatureEnablement ,
230231 logger : Logger ,
232+ getCodeQL : CodeQLGetter ,
233+ tempDir : string ,
231234) : Promise < SarifFile > {
232235 logger . info ( "Combining SARIF files using the CodeQL CLI" ) ;
233236
@@ -265,18 +268,10 @@ async function combineSarifFilesUsingCLI(
265268 return combineSarifFiles ( sarifFiles , logger ) ;
266269 }
267270
268- // Initialize CodeQL, either by using the config file from the 'init' step,
269- // or by initializing it here.
270- let codeQL : CodeQL ;
271- let tempDir : string = actionsUtil . getTemporaryDirectory ( ) ;
272-
273- const config = await getConfig ( tempDir , logger ) ;
274- if ( config !== undefined ) {
275- codeQL = await getCodeQL ( config . codeQLCmd ) ;
276- tempDir = config . tempDir ;
277- } else {
278- codeQL = await minimalInitCodeQL ( logger , gitHubVersion , features ) ;
279- }
271+ // Obtain a `CodeQL` instance. For `analyze`, this is typically the instance that was used for running the queries.
272+ // For `upload-sarif`, this either initialises a new instance or returns a previously initialised one if `getCodeQL`
273+ // is called more than once.
274+ const codeQL : CodeQL = await getCodeQL ( ) ;
280275
281276 const baseTempDir = path . resolve ( tempDir , "combined-sarif" ) ;
282277 fs . mkdirSync ( baseTempDir , { recursive : true } ) ;
@@ -682,6 +677,8 @@ export interface PostProcessingResults {
682677 *
683678 * @param logger The logger to use.
684679 * @param features Information about enabled features.
680+ * @param getCodeQL A function to retrieve a `CodeQL` instance.
681+ * @param tempPath A path to a temporary directory.
685682 * @param checkoutPath The path where the repo was checked out at.
686683 * @param sarifPaths The paths of the SARIF files to post-process.
687684 * @param category The analysis category.
@@ -693,6 +690,8 @@ export interface PostProcessingResults {
693690export async function postProcessSarifFiles (
694691 logger : Logger ,
695692 features : FeatureEnablement ,
693+ getCodeQL : CodeQLGetter ,
694+ tempPath : string ,
696695 checkoutPath : string ,
697696 sarifPaths : string [ ] ,
698697 category : string | undefined ,
@@ -717,6 +716,8 @@ export async function postProcessSarifFiles(
717716 gitHubVersion ,
718717 features ,
719718 logger ,
719+ getCodeQL ,
720+ tempPath ,
720721 ) ;
721722 } else {
722723 const sarifPath = sarifPaths [ 0 ] ;
@@ -777,6 +778,8 @@ export async function writePostProcessedFiles(
777778 * to.
778779 */
779780export async function uploadFiles (
781+ tempDir : string ,
782+ codeql : CodeQL ,
780783 inputSarifPath : string ,
781784 checkoutPath : string ,
782785 category : string | undefined ,
@@ -790,6 +793,8 @@ export async function uploadFiles(
790793 ) ;
791794
792795 return uploadSpecifiedFiles (
796+ tempDir ,
797+ codeql ,
793798 sarifPaths ,
794799 checkoutPath ,
795800 category ,
@@ -803,6 +808,8 @@ export async function uploadFiles(
803808 * Uploads the given array of SARIF files.
804809 */
805810async function uploadSpecifiedFiles (
811+ tempDir : string ,
812+ codeql : CodeQL ,
806813 sarifPaths : string [ ] ,
807814 checkoutPath : string ,
808815 category : string | undefined ,
@@ -813,6 +820,8 @@ async function uploadSpecifiedFiles(
813820 const processingResults : PostProcessingResults = await postProcessSarifFiles (
814821 logger ,
815822 features ,
823+ async ( ) => codeql ,
824+ tempDir ,
816825 checkoutPath ,
817826 sarifPaths ,
818827 category ,
0 commit comments