diff --git a/dist/index.js b/dist/index.js index 8258ff1..84fd382 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13755,7 +13755,14 @@ 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`); - BASE_SHA = execSync(`git rev-parse origin/${mainBranchName}~1`, { encoding: 'utf-8' }); + let getBaseShaCommand = `git rev-parse origin/${mainBranchName}`; + const commitCountOutput = execSync(`git rev-list --count origin/${mainBranchName}`, { encoding: 'utf-8' }); + const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10); + if (commitCount > 1) { + getBaseShaCommand = `${getBaseShaCommand}~1`; + } + + BASE_SHA = execSync(getBaseShaCommand, { encoding: 'utf-8' }); core.setOutput('noPreviousBuild', 'true'); } } else { @@ -13763,9 +13770,8 @@ let BASE_SHA; process.stdout.write(`Found the last successful workflow run on 'origin/${mainBranchName}'\n`); process.stdout.write(`Commit: ${BASE_SHA}\n`); } - } - const stripNewLineEndings = sha => sha.replace('\n', ''); + } core.setOutput('base', stripNewLineEndings(BASE_SHA)); core.setOutput('head', stripNewLineEndings(HEAD_SHA)); })(); @@ -13843,6 +13849,13 @@ async function commitExists(commitSha) { } } +/** + * Strips LF line endings from given string + * @param {string} string + */ +function stripNewLineEndings(string) { + return string.replace('\n', ''); +} })(); module.exports = __webpack_exports__; diff --git a/find-successful-workflow.js b/find-successful-workflow.js index bc1181f..1d04418 100644 --- a/find-successful-workflow.js +++ b/find-successful-workflow.js @@ -47,7 +47,14 @@ 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`); - BASE_SHA = execSync(`git rev-parse origin/${mainBranchName}~1`, { encoding: 'utf-8' }); + let getBaseShaCommand = `git rev-parse origin/${mainBranchName}`; + const commitCountOutput = execSync(`git rev-list --count origin/${mainBranchName}`, { encoding: 'utf-8' }); + const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10); + if (commitCount > 1) { + getBaseShaCommand = `${getBaseShaCommand}~1`; + } + + BASE_SHA = execSync(getBaseShaCommand, { encoding: 'utf-8' }); core.setOutput('noPreviousBuild', 'true'); } } else { @@ -55,9 +62,8 @@ let BASE_SHA; process.stdout.write(`Found the last successful workflow run on 'origin/${mainBranchName}'\n`); process.stdout.write(`Commit: ${BASE_SHA}\n`); } - } - const stripNewLineEndings = sha => sha.replace('\n', ''); + } core.setOutput('base', stripNewLineEndings(BASE_SHA)); core.setOutput('head', stripNewLineEndings(HEAD_SHA)); })(); @@ -134,3 +140,11 @@ async function commitExists(commitSha) { return false; } } + +/** + * Strips LF line endings from given string + * @param {string} string + */ +function stripNewLineEndings(string) { + return string.replace('\n', ''); +} \ No newline at end of file