Skip to content

Commit 7f62ef8

Browse files
refactor: make actions/core mockable
1 parent 055da60 commit 7f62ef8

File tree

2 files changed

+144
-94
lines changed

2 files changed

+144
-94
lines changed

dist/main/index.js

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32280,16 +32280,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3228032280
return (mod && mod.__esModule) ? mod : { "default": mod };
3228132281
};
3228232282
Object.defineProperty(exports, "__esModule", ({ value: true }));
32283+
exports.runRegisterSource = runRegisterSource;
3228332284
const core = __importStar(__nccwpck_require__(2186));
3228432285
const github = __importStar(__nccwpck_require__(5438));
3228532286
const axios_1 = __importDefault(__nccwpck_require__(8757));
3228632287
const fs = __importStar(__nccwpck_require__(7147));
3228732288
const path = __importStar(__nccwpck_require__(1017));
3228832289
const os = __importStar(__nccwpck_require__(2037));
32289-
function loadTokenFromFile() {
32290+
function loadTokenFromFile(coreAdapter) {
3229032291
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
3229132292
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
32292-
core.debug(`Using token file path: ${tokenFilePath}`);
32293+
coreAdapter.debug(`Using token file path: ${tokenFilePath}`);
3229332294
if (!fs.existsSync(tokenFilePath)) {
3229432295
throw new Error(`Token file does not exist at ${tokenFilePath}`);
3229532296
}
@@ -32298,49 +32299,49 @@ function loadTokenFromFile() {
3229832299
if (!token) {
3229932300
throw new Error('Token not found in token file');
3230032301
}
32301-
core.debug(`Token: ${token}`);
32302+
coreAdapter.debug(`Token: ${token}`);
3230232303
return { token, tokenFilePath };
3230332304
}
32304-
function loadBuildingBlockRunFromBase64(encodedRun) {
32305-
core.debug('Using buildingBlockRun from GitHub event payload');
32305+
function loadBuildingBlockRunFromBase64(encodedRun, coreAdapter) {
32306+
coreAdapter.debug('Using buildingBlockRun from GitHub event payload');
3230632307
if (!encodedRun) {
3230732308
throw new Error('Neither buildingBlockRunUrl input nor buildingBlockRun payload provided');
3230832309
}
3230932310
const decodedBuildingBlockRun = Buffer.from(encodedRun, 'base64').toString('utf-8');
3231032311
const buildingBlockRunJson = JSON.parse(decodedBuildingBlockRun);
32311-
core.debug(`Decoded Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
32312+
coreAdapter.debug(`Decoded Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
3231232313
return buildingBlockRunJson;
3231332314
}
32314-
async function loadBuildingBlockRunFromUrl(url, token) {
32315-
core.debug('Using buildingBlockRunUrl input');
32315+
async function loadBuildingBlockRunFromUrl(url, token, coreAdapter) {
32316+
coreAdapter.debug('Using buildingBlockRunUrl input');
3231632317
const headers = {
3231732318
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
3231832319
'Authorization': `Bearer ${token}`
3231932320
};
3232032321
try {
3232132322
const response = await axios_1.default.get(url, { headers });
3232232323
const buildingBlockRunJson = response.data;
32323-
core.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
32324+
coreAdapter.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
3232432325
return buildingBlockRunJson;
3232532326
}
3232632327
catch (fetchError) {
3232732328
if (axios_1.default.isAxiosError(fetchError)) {
3232832329
if (fetchError.response) {
32329-
core.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
32330-
core.error(`Status code: ${fetchError.response.status}`);
32330+
coreAdapter.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
32331+
coreAdapter.error(`Status code: ${fetchError.response.status}`);
3233132332
}
3233232333
else {
32333-
core.error(`Fetch error message: ${fetchError.message}`);
32334+
coreAdapter.error(`Fetch error message: ${fetchError.message}`);
3233432335
}
3233532336
}
3233632337
else {
32337-
core.error(`Unexpected error during fetch: ${fetchError}`);
32338+
coreAdapter.error(`Unexpected error during fetch: ${fetchError}`);
3233832339
}
3233932340
throw fetchError;
3234032341
}
3234132342
}
32342-
function extractInputs(buildingBlockRun) {
32343-
core.debug('Extracting inputs from building block run');
32343+
function extractInputs(buildingBlockRun, coreAdapter) {
32344+
coreAdapter.debug('Extracting inputs from building block run');
3234432345
const inputs = buildingBlockRun.spec.buildingBlock.spec.inputs;
3234532346
const extractedInputs = {};
3234632347
inputs.forEach((input) => {
@@ -32349,10 +32350,10 @@ function extractInputs(buildingBlockRun) {
3234932350
extractedInputs[input.key] = value;
3235032351
}
3235132352
});
32352-
core.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
32353+
coreAdapter.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
3235332354
// Write each extracted input to GITHUB_OUTPUT
3235432355
for (const [key, value] of Object.entries(extractedInputs)) {
32355-
core.setOutput(key, value);
32356+
coreAdapter.setOutput(key, value);
3235632357
}
3235732358
return extractedInputs;
3235832359
}
@@ -32363,85 +32364,93 @@ function buildRequestHeaders(token) {
3236332364
'Authorization': `Bearer ${token}`
3236432365
};
3236532366
}
32366-
function buildRequestPayload(steps) {
32367+
function buildRequestPayload(steps, githubContext) {
3236732368
return {
3236832369
source: {
3236932370
id: 'github',
32370-
externalRunId: github.context.runId,
32371-
externalRunUrl: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
32371+
externalRunId: githubContext.context.runId,
32372+
externalRunUrl: `https://github.com/${githubContext.context.repo.owner}/${githubContext.context.repo.repo}/actions/runs/${githubContext.context.runId}`
3237232373
},
3237332374
steps: steps
3237432375
};
3237532376
}
32376-
async function registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath) {
32377-
core.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
32378-
core.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
32377+
async function registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath, coreAdapter) {
32378+
coreAdapter.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
32379+
coreAdapter.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
3237932380
try {
3238032381
const response = await axios_1.default.post(`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`, requestPayload, {
3238132382
headers: requestHeaders
3238232383
});
32383-
core.setOutput('response', response.data);
32384-
core.setOutput('token_file', tokenFilePath);
32384+
coreAdapter.setOutput('response', response.data);
32385+
coreAdapter.setOutput('token_file', tokenFilePath);
3238532386
}
3238632387
catch (registerError) {
3238732388
if (axios_1.default.isAxiosError(registerError)) {
3238832389
if (registerError.response) {
32389-
core.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
32390-
core.error(`Status code: ${registerError.response.status}`);
32390+
coreAdapter.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
32391+
coreAdapter.error(`Status code: ${registerError.response.status}`);
3239132392
}
3239232393
else {
32393-
core.error(`Register source error message: ${registerError.message}`);
32394+
coreAdapter.error(`Register source error message: ${registerError.message}`);
3239432395
}
3239532396
}
3239632397
else {
32397-
core.error(`Unexpected error: ${registerError}`);
32398+
coreAdapter.error(`Unexpected error: ${registerError}`);
3239832399
}
3239932400
throw registerError;
3240032401
}
3240132402
}
32402-
async function run() {
32403+
async function runRegisterSource(coreAdapter = core, githubContext = github) {
3240332404
try {
32404-
const stepsInput = core.getInput('steps');
32405-
const buildingBlockRunUrl = core.getInput('buildingBlockRunUrl');
32406-
core.debug(`Steps Input: ${stepsInput}`);
32407-
core.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
32405+
const stepsInput = coreAdapter.getInput('steps');
32406+
const buildingBlockRunUrl = coreAdapter.getInput('buildingBlockRunUrl');
32407+
coreAdapter.debug(`Steps Input: ${stepsInput}`);
32408+
coreAdapter.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
3240832409
// Load token
32409-
const { token, tokenFilePath } = loadTokenFromFile();
32410+
const { token, tokenFilePath } = loadTokenFromFile(coreAdapter);
3241032411
// Load building block run
3241132412
let buildingBlockRunJson;
3241232413
if (buildingBlockRunUrl) {
32413-
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token);
32414+
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token, coreAdapter);
3241432415
}
3241532416
else {
32416-
const buildingBlockRun = github.context.payload.inputs.buildingBlockRun;
32417-
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun);
32417+
const buildingBlockRun = githubContext.context.payload.inputs.buildingBlockRun;
32418+
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun, coreAdapter);
3241832419
}
3241932420
// Extract common data from buildingBlockRunJson
3242032421
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
3242132422
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
32422-
core.debug(`Base URL: ${baseUrl}`);
32423-
core.debug(`BB Run UUID: ${bbRunUuid}`);
32423+
coreAdapter.debug(`Base URL: ${baseUrl}`);
32424+
coreAdapter.debug(`BB Run UUID: ${bbRunUuid}`);
3242432425
// Extract inputs and write to outputs
32425-
extractInputs(buildingBlockRunJson);
32426+
extractInputs(buildingBlockRunJson, coreAdapter);
3242632427
// Parse the JSON steps input
3242732428
const steps = JSON.parse(stepsInput);
32428-
core.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
32429+
coreAdapter.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
3242932430
// Prepare the request payload and headers
32430-
const requestPayload = buildRequestPayload(steps);
32431+
const requestPayload = buildRequestPayload(steps, githubContext);
3243132432
const requestHeaders = buildRequestHeaders(token);
3243232433
// Register the source
32433-
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath);
32434+
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
3243432435
}
3243532436
catch (error) {
3243632437
if (error instanceof Error) {
32437-
core.setFailed(error.message);
32438+
coreAdapter.setFailed(error.message);
32439+
throw error;
3243832440
}
3243932441
else {
32440-
core.setFailed('An unknown error occurred');
32442+
coreAdapter.setFailed('An unknown error occurred');
32443+
throw error;
3244132444
}
3244232445
}
3244332446
}
32444-
run();
32447+
async function run() {
32448+
await runRegisterSource(core, github);
32449+
}
32450+
// Only run if this file is executed directly (not imported)
32451+
if (require.main === require.cache[eval('__filename')]) {
32452+
run();
32453+
}
3244532454

3244632455

3244732456
/***/ }),

0 commit comments

Comments
 (0)