From 11fd0d22e7d10c95a4dbbb84d156189e5bcab092 Mon Sep 17 00:00:00 2001 From: Rodrigo Mata Date: Fri, 17 Dec 2021 06:04:55 -0500 Subject: [PATCH] feat: ability to change last successful event (#7) --- action.yml | 5 ++++- find-successful-workflow.js | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index 4b385f5..811d5a7 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ inputs: default: "false" workflow-id: description: "The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow" + last-successful-event: + description: "The type of event to check for the last successful commit corresponding to that workflow-id, E.g. push, pull-request, release etc" + default: "push" outputs: base: @@ -28,7 +31,7 @@ runs: - name: Set base and head SHAs used for nx affected id: setSHAs shell: bash - run: node ${{ github.action_path }}/dist/index.js ${{ github.token }} ${{ inputs.main-branch-name }} ${{ inputs.error-on-no-successful-workflow }} ${{ inputs.workflow-id }} + run: node ${{ github.action_path }}/dist/index.js ${{ github.token }} ${{ inputs.main-branch-name }} ${{ inputs.error-on-no-successful-workflow }} ${{ inputs.workflow-id }} ${{ inputs.last-successful-event }} - name: Log base and head SHAs used for nx affected shell: bash diff --git a/find-successful-workflow.js b/find-successful-workflow.js index a8a9f84..ca0c057 100644 --- a/find-successful-workflow.js +++ b/find-successful-workflow.js @@ -8,6 +8,7 @@ process.env.GITHUB_TOKEN = process.argv[2]; const mainBranchName = process.argv[3]; const errorOnNoSuccessfulWorkflow = process.argv[4]; const workflowId = process.argv[5]; +const lastSuccessfulEvent = process.argv[6]; let BASE_SHA; (async () => { @@ -17,7 +18,7 @@ let BASE_SHA; BASE_SHA = execSync(`git merge-base origin/${mainBranchName} HEAD`, { encoding: 'utf-8' }); } else { try { - BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName); + BASE_SHA = await findSuccessfulCommit(workflowId, runId, owner, repo, mainBranchName, lastSuccessfulEvent); } catch (e) { core.setFailed(e.message); return; @@ -65,7 +66,7 @@ function reportFailure(branchName) { * @param {string} branch * @returns */ -async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch) { +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}`, { @@ -81,7 +82,7 @@ async function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch) { repo, branch, workflow_id, - event: 'push', + event: lastSuccessfulEvent, status: 'success' }).then(({ data: { workflow_runs } }) => workflow_runs.map(run => run.head_sha));