|
1 | 1 | // Used in .github/workflows/release-IPA-metrics.yml |
| 2 | +// Checks if workflow failed or if job 'Release IPA Validation Metrics' didn't run today |
| 3 | + |
| 4 | +const releaseJobName = 'Release IPA Validation Metrics'; |
| 5 | + |
2 | 6 | export default async function getShouldRunMetricsRelease({ github, context }) { |
3 | | - const response = await github.rest.actions.listWorkflowRuns({ |
| 7 | + // Get last workflow run |
| 8 | + const workflowRuns = await github.rest.actions.listWorkflowRuns({ |
4 | 9 | owner: context.repo.owner, |
5 | 10 | repo: context.repo.repo, |
6 | 11 | workflow_id: 'release-IPA-metrics.yml', |
7 | 12 | per_page: 2, |
8 | 13 | page: 1, |
9 | 14 | }); |
10 | 15 |
|
11 | | - if (!response || !response.data) { |
| 16 | + if (!workflowRuns || !workflowRuns.data) { |
12 | 17 | throw Error('listWorkFlowRuns response is empty'); |
13 | 18 | } |
14 | 19 |
|
15 | | - const { workflow_runs: runs } = response.data; |
| 20 | + const { workflow_runs: runs } = workflowRuns.data; |
| 21 | + |
| 22 | + if (!runs || runs.length === 0) { |
| 23 | + throw Error('workflowRuns is empty'); |
| 24 | + } |
| 25 | + |
| 26 | + const previousRun = runs[1]; |
| 27 | + |
| 28 | + // Check if job 'Release IPA Validation Metrics' already ran today |
| 29 | + const runJobs = await github.rest.actions.listJobsForWorkflowRun({ |
| 30 | + owner: context.repo.owner, |
| 31 | + repo: context.repo.repo, |
| 32 | + run_id: runs[1].id, |
| 33 | + per_page: 2, |
| 34 | + page: 1, |
| 35 | + }); |
16 | 36 |
|
17 | | - if (runs === undefined || runs.length === 0) { |
18 | | - throw Error('response.data.workflow_runs is empty'); |
| 37 | + if (runJobs === undefined || runJobs.length === 0) { |
| 38 | + throw Error('listJobsForWorkflowRun response is empty'); |
19 | 39 | } |
20 | 40 |
|
21 | | - console.log(runs[1]); |
| 41 | + const previousReleaseJob = runJobs.find((job) => job.name === releaseJobName); |
22 | 42 |
|
23 | | - const previousResult = runs[1].conclusion; |
| 43 | + if (!previousReleaseJob) { |
| 44 | + throw Error('Could not find previous release job with name' + releaseJobName); |
| 45 | + } |
24 | 46 |
|
25 | | - const lastRunDate = new Date(runs[1].created_at); |
| 47 | + const lastRunDate = new Date(previousReleaseJob.completed_at); |
26 | 48 | const today = new Date(); |
27 | 49 |
|
28 | | - console.log('Last run was', lastRunDate.toDateString(), 'with status', previousResult); |
29 | | - console.log('Head branch', runs[1].head_branch); |
30 | | - console.log('Run started at', runs[1].run_started_at); |
| 50 | + console.log('Last release job run was', lastRunDate.toDateString(), 'with status', previousReleaseJob.conclusion); |
31 | 51 |
|
32 | | - return previousResult === 'failure' || today.toDateString() !== lastRunDate.toDateString(); |
| 52 | + return previousRun.conclusion === 'failure' || today.toDateString() !== lastRunDate.toDateString(); |
33 | 53 | } |
0 commit comments