Files
codeql-action/lib/init-action-post.js
Mads Navntoft 436aa0401d PoC: switch esbuild bundles to ESM with code splitting + minify
Bundles each entry point as ESM with esbuild's splitting:true so
shared transitive dependencies (Azure storage, undici, octokit, ...)
live in shared chunk files under lib/chunks/ instead of being
duplicated into every entry bundle.

A small banner is injected so bundled CommonJS deps that call
require(), __filename or __dirname at runtime still work. A
lib/package.json with { "type": "module" } is written by the build
so the bundles can keep the .js extension while being loaded as ESM
by Node; this scope avoids touching the tsc output in build/ and the
action.yml entrypoints stay unchanged.

On a measured tar.gz of the working tree this takes the repo from
~9.5 MB to ~2.6 MB compressed (lib/ alone: 8.0 MB -> 0.7 MB).
2026-05-14 14:43:23 +02:00

3 lines
8.5 KiB
JavaScript
Generated

import { createRequire as __codeqlCreateRequire } from "module";import { fileURLToPath as __codeqlFileURLToPath } from "url";import { dirname as __codeqlDirname } from "path";var require = __codeqlCreateRequire(import.meta.url);var __filename = __codeqlFileURLToPath(import.meta.url);var __dirname = __codeqlDirname(__filename);
import{m as M,o as j}from"./chunks/chunk-XFYKKQKY.js";import{a as X,c as K,d as ee,f as te}from"./chunks/chunk-WKICWMYU.js";import"./chunks/chunk-5ZRYQL45.js";import"./chunks/chunk-U2JW7LOC.js";import"./chunks/chunk-GX7WDUZJ.js";import{a as ae,b as ie,c as oe,d as ne}from"./chunks/chunk-NENGCOP5.js";import{e as Z}from"./chunks/chunk-HIJVM6IW.js";import"./chunks/chunk-2R674E4A.js";import{b as G,c as z,d as Y,e as F,f as A,h as W}from"./chunks/chunk-B34OPX2S.js";import{h as b}from"./chunks/chunk-LYJYPMC2.js";import{Da as O,J as C,La as x,M as U,Oa as m,Ra as T,Ta as g,V as E,Wb as B,Xb as H,Y as v,c as I,cc as V,da as L,db as $,ea as c,fa as S,ga as p,gc as q,ic as R,k as fe,lb as N,m as me,ob as J,pa as w,qa as f,qb as Q,ra as D,sa as y,ta as h,xa as k,ya as P,zb as _}from"./chunks/chunk-V6LGBXSF.js";var l=I(fe());var ue=I(me());import*as de from"fs";import re from"path";function be(n){let t=c(n);return{upload_failed_run_error:t.message,upload_failed_run_stack_trace:t.stack}}async function le(n,t,e){if(!e.codeQLCmd)return new f({upload_failed_run_skipped_because:"CodeQL command not found"});let a=U("GITHUB_JOB"),i=L(D("matrix"));if(v())return new f({upload_failed_run_skipped_because:"SARIF upload is disabled"});if(R(e)){if(e.languages.length!==1)return new f({upload_failed_run_skipped_because:"Unexpectedly, the configuration is not for a single language."});let r=e.languages[0],o=`/language:${r}`,s=await se(t,e,o,".",`../codeql-failed-sarif-${r}${Q.sarifExtension}`);return new w(s)}else{let r=await ae(n),o=oe(r,a,i);if(!["always","failure-only"].includes(P(o)))return new f({upload_failed_run_skipped_because:"SARIF upload is disabled"});let d=ie(r,a,i),s=ne(r,a,i),u=await se(t,e,d,s);return new w(u)}}async function se(n,t,e,a,i){let r=t.dbLocation,o=await b(t.codeQLCmd);return i===void 0&&(i="../codeql-failed-run.sarif"),r===void 0||!await n.getValue("export_diagnostics_enabled",o)?await o.diagnosticsExport(i,e,t):await o.databaseExportDiagnostics(r,i,e),{sarifFile:i,category:e,checkoutPath:a}}async function Se(n,t,e,a){let i=await le(a,e,n);if(i.isFailure())return i.value;let r=i.value;a.info(`Uploading failed SARIF file ${r.sarifFile}`);let o=await M(r.sarifFile,r.checkoutPath,r.category,e,a,J);return await j(t,o.sarifID,a,{isUnsuccessfulExecution:!0}),o?{...o.statusReport,sarifID:o.sarifID}:{}}async function we(n,t,e){let a=await le(e,t,n);if(a.isFailure())return a.value;let i=a.value;e.info(`Uploading failed SARIF file ${i.sarifFile} as artifact`);let r=await g(),o=await te(e,r.type),d=ee(y("matrix")),s=X(`sarif-artifact-${d}`);return await o.uploadArtifact(s,[re.normalize(i.sarifFile)],re.normalize("..")),{sarifID:s}}async function ye(n,t,e,a){if(process.env.CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY==="true")return{upload_failed_run_skipped_because:"Analyze Action completed successfully"};try{return q(n)?await Se(n,t,e,a):R(n)?await we(n,e,a):{upload_failed_run_skipped_because:"No analysis kind that supports failed SARIF uploads is enabled."}}catch(i){return a.debug(`Failed to upload a SARIF file for this failed CodeQL code scanning run. ${i}`),be(i)}}async function ce(n,t,e,a,i,r,o){await he(e,a,r,o);let d=await ye(a,i,r,o);if(d.upload_failed_run_skipped_because&&o.debug(`Won't upload a failed SARIF file for this CodeQL analysis because: ${d.upload_failed_run_skipped_because}.`),process.env.CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF==="true"&&!d.raw_upload_size_bytes){let s=JSON.stringify(d);throw new Error(`Expected to upload a failed SARIF file for this CodeQL code scanning run, but the result was instead ${s}.`)}if(process.env.CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF==="true"&&(ue.context.payload.pull_request?.head.repo.fork?o.info("Skipping deletion of failed SARIF because the workflow was triggered from a fork of codeql-action and doesn't have the appropriate permissions for deletion."):await _e(d,o)),a.debugMode){o.info("Debug mode is on. Uploading available database bundles and logs as Actions debugging artifacts...");let s=await e.getVersion();await n(e,a,o,s.version),await t(a)}if(O())try{de.rmSync(a.dbLocation,{recursive:!0,force:!0,maxRetries:3}),o.info(`Cleaned up database cluster directory ${a.dbLocation}.`)}catch(s){o.warning(`Failed to clean up database cluster directory ${a.dbLocation}. Details: ${s}`)}else o.debug("Skipping cleanup of database cluster directory since we are running on a GitHub-hosted runner which will be automatically cleaned up.");return d}async function he(n,t,e,a){if(t.overlayDatabaseMode!=="overlay-base"||process.env.CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY==="true"||!await e.getValue("overlay_analysis_status_save"))return;let i=y("check-run-id"),r=i!==void 0?parseInt(i,10):void 0,o=B({attemptedToBuildOverlayBaseDatabase:!0,builtOverlayBaseDatabase:!1},r!==void 0&&r>=0?r:void 0),d=await p(a);if(d===void 0){a.warning("Unable to save overlay status to the Actions cache because the available disk space could not be determined.");return}let s=await H(n,t.languages,d,o,a),u="This job attempted to run with improved incremental analysis but it did not complete successfully. One possible reason for this is disk space constraints, since improved incremental analysis can require a significant amount of disk space for some repositories.";s?a.error(`${u} This failure has been recorded in the Actions cache, so the next CodeQL analysis will run without improved incremental analysis. If you want to enable improved incremental analysis, try increasing the disk space available to the runner. If that doesn't help, contact GitHub Support for further assistance.`):a.error(`${u} The attempt to save this failure status to the Actions cache failed. The Action will attempt to run with improved incremental analysis again.`)}async function _e(n,t){let e=n.sarifID;if(e){t.startGroup("Deleting failed SARIF upload"),t.info(`In test mode, therefore deleting the failed analysis to avoid impacting tool status for the Action repository. SARIF ID to delete: ${e}.`);let a=T();try{let i=m();await E(5e3);let r=await a.request("GET /repos/:owner/:repo/code-scanning/analyses?sarif_id=:sarif_id",{owner:i.owner,repo:i.repo,sarif_id:e});if(r.data.length===1){let o=r.data[0];t.info(`Analysis ID to delete: ${o.id}.`);try{await a.request("DELETE /repos/:owner/:repo/code-scanning/analyses/:analysis_id?confirm_delete",{owner:i.owner,repo:i.repo,analysis_id:o.id}),t.info("Analysis deleted.")}catch(d){let s=S(d),u=s.includes("No analysis found for analysis ID")?`Analysis ${o.id} does not exist. It was likely already deleted.`:s;throw new Error(u)}}else throw new Error(`Expected to find exactly one analysis with sarif_id ${e}. Found ${r.data.length}.`)}catch(i){throw new Error(`Failed to delete uploaded SARIF analysis. Reason: ${S(i)}`)}finally{t.endGroup()}}else t.warning("Could not delete the uploaded SARIF analysis because a SARIF ID wasn't provided by the API when uploading the SARIF file.")}async function Fe(n){let t=_(),e,a,i;try{x();let d=await g();C(d,t);let s=m(),u=N(d,s,h(),t);if(e=await V(h(),t),e===void 0)t.warning("Debugging artifacts are unavailable since the 'init' Action failed before it could produce any.");else{let pe=await b(e.codeQLCmd);a=await ce(K,k,pe,e,s,u,t),await $()&&e.dependencyCachingEnabled!=="none"&&(i=await Z(t))}}catch(d){let s=c(d);l.setFailed(s.message);let u=await F("init-post",z(s),n,e,await p(t),t,s.message,s.stack);u!==void 0&&await A(u);return}let r=Ae(e);t.info(`CodeQL job status was ${Y(r)}.`);let o=await F("init-post","success",n,e,await p(t),t);if(o!==void 0){let d={...o,...a,job_status:r,dependency_caching_usage:i};t.info("Sending status report for init-post step."),await A(d),t.info("Status report sent for init-post step.")}}function Ae(n){let t=Ie();if(t!==void 0)return t;let e;return process.env.CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY==="true"?(l.exportVariable("CODEQL_ACTION_JOB_STATUS","JOB_STATUS_SUCCESS"),e="JOB_STATUS_SUCCESS"):n!==void 0?e="JOB_STATUS_CONFIGURATION_ERROR":e="JOB_STATUS_UNKNOWN",l.exportVariable("CODEQL_ACTION_JOB_STATUS",e),e}function Ie(){let n=process.env.CODEQL_ACTION_JOB_STATUS;if(n!==void 0)return Object.values(G).includes(n)?n:"JOB_STATUS_UNKNOWN"}async function Ce(){let n=new Date,t=_();try{await Fe(n)}catch(e){l.setFailed(`init post action failed: ${c(e).message}`),await W("init-post",n,e,t)}}Ce();