Skip to content

Commit 4fcebfa

Browse files
committed
Use one for loop to process all runs
1 parent f173ce3 commit 4fcebfa

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ jobs:
100100
cancel_others: 'true'
101101
concurrent_skipping: 'outdated_runs'
102102
skip_after_successful_duplicate: 'true'
103+
# Test 'do_not_skip' with 'pull_request'
103104
do_not_skip: '["pull_request", "workflow_dispatch", "schedule"]'
104105

105106
- name: Simulate task

dist/index.js

Lines changed: 21 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -450,46 +450,51 @@ async function main(): Promise<void> {
450450
const octokit = github.getOctokit(token)
451451

452452
// Get and parse the current workflow run.
453-
const {data: workflowRun} = await octokit.rest.actions.getWorkflowRun({
453+
const {data: apiCurrentRun} = await octokit.rest.actions.getWorkflowRun({
454454
...repo,
455455
run_id: github.context.runId
456456
})
457-
const treeHash = workflowRun.head_commit?.tree_id
458-
if (!treeHash) {
457+
const currentTreeHash = apiCurrentRun.head_commit?.tree_id
458+
if (!currentTreeHash) {
459459
exitFail(`
460-
Could not find the tree hash of run ${workflowRun.id} (Workflow ID: ${workflowRun.workflow_id},
461-
Name: ${workflowRun.name}, Head Branch: ${workflowRun.head_branch}, Head SHA: ${workflowRun.head_sha}).
460+
Could not find the tree hash of run ${apiCurrentRun.id} (Workflow ID: ${apiCurrentRun.workflow_id},
461+
Name: ${apiCurrentRun.name}, Head Branch: ${apiCurrentRun.head_branch}, Head SHA: ${apiCurrentRun.head_sha}).
462462
This might be a run associated with a headless or removed commit.
463463
`)
464464
}
465-
const currentRun = mapWorkflowRun(workflowRun, treeHash)
465+
const currentRun = mapWorkflowRun(apiCurrentRun, currentTreeHash)
466466

467467
// Fetch list of runs for current workflow.
468468
const {
469-
data: {workflow_runs: workflowRuns}
469+
data: {workflow_runs: apiAllRuns}
470470
} = await octokit.rest.actions.listWorkflowRuns({
471471
...repo,
472472
workflow_id: currentRun.workflowId,
473473
per_page: 100
474474
})
475475

476+
// List with all workflow runs.
477+
const allRuns = []
478+
// List with older workflow runs only (used to prevent some nasty race conditions and edge cases).
479+
const olderRuns = []
480+
476481
// Check and map all runs.
477-
const allRuns = workflowRuns.reduce((result: WorkflowRun[], run) => {
482+
for (const run of apiAllRuns) {
478483
// Filter out current run and runs that lack 'head_commit' (most likely runs associated with a headless or removed commit).
479484
// See https://github.com/fkirc/skip-duplicate-actions/pull/178.
480485
if (run.id !== currentRun.id && run.head_commit) {
481-
result.push(mapWorkflowRun(run, run.head_commit.tree_id))
486+
const mappedRun = mapWorkflowRun(run, run.head_commit.tree_id)
487+
// Add to list of all runs.
488+
allRuns.push(mappedRun)
489+
// Check if run can be added to list of older runs.
490+
if (
491+
new Date(mappedRun.createdAt).getTime() <
492+
new Date(currentRun.createdAt).getTime()
493+
) {
494+
olderRuns.push(mappedRun)
495+
}
482496
}
483-
return result
484-
}, [])
485-
486-
// List with older workflow runs only (used to prevent some nasty race conditions and edge cases).
487-
const olderRuns = allRuns.filter(run => {
488-
return (
489-
new Date(run.createdAt).getTime() <
490-
new Date(currentRun.createdAt).getTime()
491-
)
492-
})
497+
}
493498

494499
const skipDuplicateActions = new SkipDuplicateActions(inputs, {
495500
repo,

0 commit comments

Comments
 (0)