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') diff --git a/src/lib/runs.js b/src/lib/runs.js index 6afc84a..b508421 100644 --- a/src/lib/runs.js +++ b/src/lib/runs.js @@ -7,11 +7,12 @@ import { inspect } from 'util' import core from '@actions/core' import github from '@actions/github' -export default async function ({ octokit, workflow_id, run_id, before }) { +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, - workflow_id + workflow_id, + branch }) // find any instances of the same workflow