From 850faad0cf6c02a8c0dc46eddde2363fbd6c373a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ch=C4=99ci=C5=84ski?= Date: Wed, 24 Aug 2022 09:35:37 +0200 Subject: [PATCH] Fix download old-ish artifacts (#67) * Add per_page arg * Add multiple pages * Fix * Fix --- download-artifacts/main.js | 81 ++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/download-artifacts/main.js b/download-artifacts/main.js index 3eb0c192..21fac350 100644 --- a/download-artifacts/main.js +++ b/download-artifacts/main.js @@ -56,46 +56,59 @@ async function main() { } if (!runID) { - let runs = await client.actions.listWorkflowRuns({ - owner: owner, - repo: repo, - workflow_id: workflow - }).then(workflowRunsResponse => { - return workflowRunsResponse.data.workflow_runs - .sort((a, b) => { - a_date = new Date(a.created_at) - b_date = new Date(b.created_at) - // descending order - return b_date - a_date - }) - }) - if (branch) { - runs = runs.filter(run => run.head_branch == branch) - } + for (let pageNumber = 1; pageNumber < 5 && !runID; pageNumber++) { + + let runs = await client.actions.listWorkflowRuns({ + owner: owner, + repo: repo, + workflow_id: workflow, + per_page: 100, + page: pageNumber + }).then(workflowRunsResponse => { + return workflowRunsResponse.data.workflow_runs + .sort((a, b) => { + a_date = new Date(a.created_at) + b_date = new Date(b.created_at) + // descending order + return b_date - a_date + }) + }) - for (const run of runs) { - if (commit && run.head_sha != commit) { - continue - } - if (runNumber && run.run_number != runNumber) { - continue + if (branch) { + runs = runs.filter(run => run.head_branch == branch) } - if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) { - continue - } - if (checkArtifacts) { - let artifacts = await client.actions.listWorkflowRunArtifacts({ - owner: owner, - repo: repo, - run_id: run.id, - }) - if (artifacts.data.artifacts.length == 0) { + + for (const run of runs) { + if (commit && run.head_sha != commit) { + continue + } + if (runNumber && run.run_number != runNumber) { + continue + } + if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) { continue } + if (checkArtifacts) { + let artifacts = await client.actions.listWorkflowRunArtifacts({ + owner: owner, + repo: repo, + run_id: run.id, + }) + if (artifacts.data.artifacts.length == 0) { + continue + } + } + runID = run.id + break + } + if (runID) { + break } - runID = run.id - break + } + + if (!runID) { + throw new Error("no matching workflow run found in last 500 runs") } }