From eeed6da1309e0c29aafb2bfe9538467c2612980c Mon Sep 17 00:00:00 2001 From: Hiraqui Date: Wed, 6 Sep 2023 14:19:54 +0200 Subject: [PATCH] fix: support pull_request events that are already merged (#92) * fix: only merge-base on PRs that have not been merged yet * fix: build index.js --------- Co-authored-by: Martin Moro --- dist/index.js | 272 ++++++++++++++++++------------------ find-successful-workflow.js | 3 +- 2 files changed, 137 insertions(+), 138 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8258ff1..fd64d74 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13706,142 +13706,142 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"] var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { -const { Octokit } = __nccwpck_require__(1231); -const core = __nccwpck_require__(2186); -const github = __nccwpck_require__(5438); -const { execSync } = __nccwpck_require__(2081); -const { existsSync } = __nccwpck_require__(7147); - -const { runId, repo: { repo, owner }, eventName } = github.context; -process.env.GITHUB_TOKEN = process.argv[2]; -const mainBranchName = process.argv[3]; -const errorOnNoSuccessfulWorkflow = process.argv[4]; -const lastSuccessfulEvent = process.argv[5]; -const workingDirectory = process.argv[6]; -const workflowId = process.argv[7]; -const defaultWorkingDirectory = '.'; - -let BASE_SHA; -(async () => { - if (workingDirectory !== defaultWorkingDirectory) { - if (existsSync(workingDirectory)) { - process.chdir(workingDirectory); - } else { - process.stdout.write('\n'); - process.stdout.write(`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`); - } - } - - const HEAD_SHA = execSync(`git rev-parse HEAD`, { encoding: 'utf-8' }); - - if (eventName === 'pull_request') { - BASE_SHA = execSync(`git merge-base origin/${mainBranchName} HEAD`, { encoding: 'utf-8' }); - } else { - try { - BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent); - } catch (e) { - core.setFailed(e.message); - return; - } - - if (!BASE_SHA) { - if (errorOnNoSuccessfulWorkflow === 'true') { - reportFailure(mainBranchName); - return; - } else { - process.stdout.write('\n'); - process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}'\n`); - process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`); - 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' }); - core.setOutput('noPreviousBuild', 'true'); - } - } else { - process.stdout.write('\n'); - 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)); -})(); - -function reportFailure(branchName) { - core.setFailed(` - Unable to find a successful workflow run on 'origin/${branchName}' - NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error. - - Is it possible that you have no runs currently on 'origin/${branchName}'? - - If yes, then you should run the workflow without this flag first. - - If no, then you might have changed your git history and those commits no longer exist.`); -} - -/** - * Find last successful workflow run on the repo - * @param {string?} workflow_id - * @param {number} run_id - * @param {string} owner - * @param {string} repo - * @param {string} branch - * @returns - */ -async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, lastSuccessfulEvent) { - const octokit = new Octokit(); - if (!workflow_id) { - workflow_id = await octokit.request(`GET /repos/${owner}/${repo}/actions/runs/${run_id}`, { - owner, - repo, - branch, - run_id - }).then(({ data: { workflow_id } }) => workflow_id); - process.stdout.write('\n'); - process.stdout.write(`Workflow Id not provided. Using workflow '${workflow_id}'\n`); - } - // fetch all workflow runs on a given repo/branch/workflow with push and success - const shas = await octokit.request(`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`, { - owner, - repo, - // on non-push workflow runs we do not have branch property - branch: lastSuccessfulEvent !== 'push' ? undefined : branch, - workflow_id, - event: lastSuccessfulEvent, - status: 'success' - }).then(({ data: { workflow_runs } }) => workflow_runs.map(run => run.head_sha)); - - return await findExistingCommit(shas); -} - -/** - * Get first existing commit - * @param {string[]} commit_shas - * @returns {string?} - */ -async function findExistingCommit(shas) { - for (const commitSha of shas) { - if (await commitExists(commitSha)) { - return commitSha; - } - } - return undefined; -} - -/** - * Check if given commit is valid - * @param {string} commitSha - * @returns {boolean} - */ -async function commitExists(commitSha) { - try { - execSync(`git cat-file -e ${commitSha}`, { stdio: ['pipe', 'pipe', null] }); - return true; - } catch { - return false; - } -} +const { Octokit } = __nccwpck_require__(1231); +const core = __nccwpck_require__(2186); +const github = __nccwpck_require__(5438); +const { execSync } = __nccwpck_require__(2081); +const { existsSync } = __nccwpck_require__(7147); + +const { runId, repo: { repo, owner }, eventName } = github.context; +process.env.GITHUB_TOKEN = process.argv[2]; +const mainBranchName = process.argv[3]; +const errorOnNoSuccessfulWorkflow = process.argv[4]; +const lastSuccessfulEvent = process.argv[5]; +const workingDirectory = process.argv[6]; +const workflowId = process.argv[7]; +const defaultWorkingDirectory = '.'; + +let BASE_SHA; +(async () => { + if (workingDirectory !== defaultWorkingDirectory) { + if (existsSync(workingDirectory)) { + process.chdir(workingDirectory); + } else { + process.stdout.write('\n'); + process.stdout.write(`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`); + } + } + + const HEAD_SHA = execSync(`git rev-parse HEAD`, { encoding: 'utf-8' }); + + if (eventName === 'pull_request' && !github.context.payload.pull_request.merged) { + BASE_SHA = execSync(`git merge-base origin/${mainBranchName} HEAD`, { encoding: 'utf-8' }); + } else { + try { + BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent); + } catch (e) { + core.setFailed(e.message); + return; + } + + if (!BASE_SHA) { + if (errorOnNoSuccessfulWorkflow === 'true') { + reportFailure(mainBranchName); + return; + } else { + process.stdout.write('\n'); + process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}'\n`); + process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`); + 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' }); + core.setOutput('noPreviousBuild', 'true'); + } + } else { + process.stdout.write('\n'); + 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)); +})(); + +function reportFailure(branchName) { + core.setFailed(` + Unable to find a successful workflow run on 'origin/${branchName}' + NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error. + + Is it possible that you have no runs currently on 'origin/${branchName}'? + - If yes, then you should run the workflow without this flag first. + - If no, then you might have changed your git history and those commits no longer exist.`); +} + +/** + * Find last successful workflow run on the repo + * @param {string?} workflow_id + * @param {number} run_id + * @param {string} owner + * @param {string} repo + * @param {string} branch + * @returns + */ +async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, lastSuccessfulEvent) { + const octokit = new Octokit(); + if (!workflow_id) { + workflow_id = await octokit.request(`GET /repos/${owner}/${repo}/actions/runs/${run_id}`, { + owner, + repo, + branch, + run_id + }).then(({ data: { workflow_id } }) => workflow_id); + process.stdout.write('\n'); + process.stdout.write(`Workflow Id not provided. Using workflow '${workflow_id}'\n`); + } + // fetch all workflow runs on a given repo/branch/workflow with push and success + const shas = await octokit.request(`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`, { + owner, + repo, + // on non-push workflow runs we do not have branch property + branch: lastSuccessfulEvent !== 'push' ? undefined : branch, + workflow_id, + event: lastSuccessfulEvent, + status: 'success' + }).then(({ data: { workflow_runs } }) => workflow_runs.map(run => run.head_sha)); + + return await findExistingCommit(shas); +} + +/** + * Get first existing commit + * @param {string[]} commit_shas + * @returns {string?} + */ +async function findExistingCommit(shas) { + for (const commitSha of shas) { + if (await commitExists(commitSha)) { + return commitSha; + } + } + return undefined; +} + +/** + * Check if given commit is valid + * @param {string} commitSha + * @returns {boolean} + */ +async function commitExists(commitSha) { + try { + execSync(`git cat-file -e ${commitSha}`, { stdio: ['pipe', 'pipe', null] }); + return true; + } catch { + return false; + } +} })(); diff --git a/find-successful-workflow.js b/find-successful-workflow.js index 7700a5f..e4c48b0 100644 --- a/find-successful-workflow.js +++ b/find-successful-workflow.js @@ -27,8 +27,7 @@ let BASE_SHA; const headResult = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf-8' }); const HEAD_SHA = headResult.stdout; - - if (['pull_request','pull_request_target'].includes(eventName)) { + if (['pull_request','pull_request_target'].includes(eventName) && !github.context.payload.pull_request.merged) { const baseResult = spawnSync('git', ['merge-base', `origin/${mainBranchName}`, 'HEAD'], { encoding: 'utf-8' }); BASE_SHA = baseResult.stdout; } else {