From 6eb2e15d1b510ff00260e7d1db952dc45ac805fd Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Thu, 29 Aug 2024 12:06:03 +0300 Subject: [PATCH] feat: BASE to undefined if HEAD~1 does not exist --- dist/index.js | 18 ++++++++++++++---- find-successful-workflow.ts | 32 +++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/dist/index.js b/dist/index.js index 01d3178..a0119ff 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37872,6 +37872,7 @@ const defaultWorkingDirectory = '.'; const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin); let BASE_SHA; (() => __awaiter(void 0, void 0, void 0, function* () { + var _a; if (workingDirectory !== defaultWorkingDirectory) { if ((0, fs_1.existsSync)(workingDirectory)) { process.chdir(workingDirectory); @@ -37923,13 +37924,22 @@ let BASE_SHA; process.stdout.write('\n'); process.stdout.write(`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`); process.stdout.write('\n'); - const commitCountOutput = (0, child_process_1.spawnSync)('git', ['rev-list', '--count', `origin/${mainBranchName}`], { encoding: 'utf-8' }).stdout; - const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10); - const LAST_COMMIT_CMD = `origin/${mainBranchName}${commitCount > 1 ? '~1' : ''}`; + // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash + const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; const baseRes = (0, child_process_1.spawnSync)('git', ['rev-parse', LAST_COMMIT_CMD], { encoding: 'utf-8', }); - BASE_SHA = baseRes.stdout; + if (baseRes.status !== 0 || !baseRes.stdout) { + process.stdout.write(`HEAD~1 does not exist. BASE_SHA is set to undefined.\n`); + const emptyTreeRes = (0, child_process_1.spawnSync)('git', ['hash-object', '-t', 'tree', '/dev/null'], { + encoding: 'utf-8', + }); + BASE_SHA = + (_a = emptyTreeRes.stdout) !== null && _a !== void 0 ? _a : `4b825dc642cb6eb9a060e54bf8d69288fbee4904`; + } + else { + BASE_SHA = baseRes.stdout; + } } core.setOutput('noPreviousBuild', 'true'); } diff --git a/find-successful-workflow.ts b/find-successful-workflow.ts index c2c1324..b144a1d 100644 --- a/find-successful-workflow.ts +++ b/find-successful-workflow.ts @@ -94,23 +94,29 @@ let BASE_SHA: string; ); process.stdout.write('\n'); - const commitCountOutput = spawnSync( - 'git', - ['rev-list', '--count', `origin/${mainBranchName}`], - { encoding: 'utf-8' }, - ).stdout; - const commitCount = parseInt( - stripNewLineEndings(commitCountOutput), - 10, - ); + // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash + const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; - const LAST_COMMIT_CMD = `origin/${mainBranchName}${ - commitCount > 1 ? '~1' : '' - }`; const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], { encoding: 'utf-8', }); - BASE_SHA = baseRes.stdout; + + if (baseRes.status !== 0 || !baseRes.stdout) { + process.stdout.write( + `HEAD~1 does not exist. BASE_SHA is set to undefined.\n`, + ); + const emptyTreeRes = spawnSync( + 'git', + ['hash-object', '-t', 'tree', '/dev/null'], + { + encoding: 'utf-8', + }, + ); + BASE_SHA = + emptyTreeRes.stdout ?? `4b825dc642cb6eb9a060e54bf8d69288fbee4904`; + } else { + BASE_SHA = baseRes.stdout; + } } core.setOutput('noPreviousBuild', 'true'); }