From 937ceaea45b6c17b0b9fbb8814a6ccf23728bb49 Mon Sep 17 00:00:00 2001 From: Mark Massoud Date: Tue, 27 Feb 2024 16:59:52 -0500 Subject: [PATCH 1/3] scope to branch --- src/lib/runs.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/runs.js b/src/lib/runs.js index 6afc84a..c0a83e0 100644 --- a/src/lib/runs.js +++ b/src/lib/runs.js @@ -8,10 +8,17 @@ import core from '@actions/core' import github from '@actions/github' export default async function ({ octokit, workflow_id, run_id, before }) { + console.log(github.context); + + const branch = github.context.ref.replace('refs/heads/', '') + console.log('branch'); + console.log(branch); + // get current run of this workflow const { data: { workflow_runs } } = await octokit.request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs', { ...github.context.repo, - workflow_id + workflow_id, + branch }) // find any instances of the same workflow From d22697423e1231b0f8a39f1a2ef652dc415780ba Mon Sep 17 00:00:00 2001 From: Mark Massoud Date: Tue, 27 Feb 2024 17:31:39 -0500 Subject: [PATCH 2/3] Update runs.js --- src/lib/runs.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/lib/runs.js b/src/lib/runs.js index c0a83e0..b508421 100644 --- a/src/lib/runs.js +++ b/src/lib/runs.js @@ -7,13 +7,7 @@ import { inspect } from 'util' import core from '@actions/core' import github from '@actions/github' -export default async function ({ octokit, workflow_id, run_id, before }) { - console.log(github.context); - - const branch = github.context.ref.replace('refs/heads/', '') - console.log('branch'); - console.log(branch); - +export default async function ({ octokit, workflow_id, run_id, branch, before }) { // get current run of this workflow const { data: { workflow_runs } } = await octokit.request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs', { ...github.context.repo, From d1141c3a7333f739cc6ad80f861494b4ca9f66c2 Mon Sep 17 00:00:00 2001 From: Mark Massoud Date: Tue, 27 Feb 2024 17:31:58 -0500 Subject: [PATCH 3/3] Update index.js --- src/lib/index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index e1ec4af..6314766 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -10,6 +10,14 @@ import runs from './runs.js' // sleep function const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) +// getting branch on pull_request event is different from push +const getBranch = (payload) => { + if (payload.pull_request) { + return payload.pull_request.head.ref + } + return payload.ref.replace('refs/heads/', '') +} + export default async function ({ token, delay, timeout }) { let timer = 0 @@ -17,7 +25,7 @@ export default async function ({ token, delay, timeout }) { const octokit = github.getOctokit(token) // extract runId - const { runId: run_id } = github.context + const { payload, runId: run_id } = github.context // get workflow id and created date from run id const { data: { workflow_id, run_started_at } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}', { @@ -30,8 +38,13 @@ export default async function ({ token, delay, timeout }) { core.info(`searching for workflow runs before ${before}`) + // branch to which to scope workflow runs + const branch = getBranch(payload) + + core.info(`searching for workflow runs for branch ${branch}`) + // get previous runs - let waiting_for = await runs({ octokit, run_id, workflow_id, before }) + let waiting_for = await runs({ octokit, workflow_id, run_id, branch, before }) if (waiting_for.length === 0) { core.info('no active run of this workflow found')