From 37999e80191c469f12df50a82f2c3d0900f32b3d Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 27 Apr 2024 00:55:13 +0100 Subject: [PATCH 1/6] Improve workflow failiure notifier - Add logic to fetch and process previous workflow run and jobs --- .github/workflows/failureNotifier.yml | 47 +++++++++++++++++++++------ 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/.github/workflows/failureNotifier.yml b/.github/workflows/failureNotifier.yml index 9eb5bc6eb409..7db1490523f4 100644 --- a/.github/workflows/failureNotifier.yml +++ b/.github/workflows/failureNotifier.yml @@ -27,19 +27,52 @@ jobs: }); return jobsData.data; + - name: Fetch Previous Workflow Run + id: previous-workflow-run + uses: actions/github-script@v7 + with: + script: | + const runId = "${{ github.event.workflow_run.id }}"; + const allRuns = await github.rest.actions.listWorkflowRuns({ + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: preDeploy.yml, + }); + const run = allRuns.data.workflow_runs.find(run => run.id === runId); + const run_number = run.run_number; + const previousRun = allRuns.data.workflow_runs.find(run => run.run_number === run_number - 1); + if (previousRun.actor.login === 'OSBotify') { + return allRuns.data.workflow_runs.find(run.run_number === run_number - 2); + } + return previousRun; + + - name: Fetch Previous Workflow Run Jobs + id: previous-workflow-jobs + uses: actions/github-script@v7 + with: + script: | + const previousRun = ${{ steps.previous-workflow-run.outputs.result }}; + const runId = previousRun.id; + const jobsData = await github.rest.actions.listJobsForWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: runId, + }); + return jobsData.data; + - name: Process Each Failed Job uses: actions/github-script@v7 with: script: | const jobs = ${{ steps.fetch-workflow-jobs.outputs.result }}; - + const previousRun = ${{ steps.previous-workflow-run.outputs.result }}; + const previousRunJobs = ${{ steps.previous-workflow-jobs.outputs.result }}; const headCommit = "${{ github.event.workflow_run.head_commit.id }}"; const prData = await github.rest.repos.listPullRequestsAssociatedWithCommit({ owner: context.repo.owner, repo: context.repo.repo, commit_sha: headCommit, }); - const pr = prData.data[0]; const prLink = pr.html_url; const prAuthor = pr.user.login; @@ -50,14 +83,8 @@ jobs: if (jobs.jobs[i].conclusion == 'failure') { const jobName = jobs.jobs[i].name; const jobLink = jobs.jobs[i].html_url; - const issues = await github.rest.issues.listForRepo({ - owner: context.repo.owner, - repo: context.repo.repo, - labels: failureLabel, - state: 'open' - }); - const existingIssue = issues.data.find(issue => issue.title.includes(jobName)); - if (!existingIssue) { + const previousJobSucceeded = previousRunJobs.jobs.find(job => job.name === jobName && job.conclusion === 'success'); + if (previousJobSucceeded) { const annotations = await github.rest.checks.listAnnotations({ owner: context.repo.owner, repo: context.repo.repo, From 23c48b26e7e4bfcd041bc75bbab755d25e05f65e Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:12:50 +0100 Subject: [PATCH 2/6] fixes --- .github/workflows/failureNotifier.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/failureNotifier.yml b/.github/workflows/failureNotifier.yml index 7db1490523f4..ab120943c863 100644 --- a/.github/workflows/failureNotifier.yml +++ b/.github/workflows/failureNotifier.yml @@ -32,7 +32,7 @@ jobs: uses: actions/github-script@v7 with: script: | - const runId = "${{ github.event.workflow_run.id }}"; + const runId = ${{ github.event.workflow_run.id }}; const allRuns = await github.rest.actions.listWorkflowRuns({ owner: context.repo.owner, repo: context.repo.repo, @@ -83,7 +83,8 @@ jobs: if (jobs.jobs[i].conclusion == 'failure') { const jobName = jobs.jobs[i].name; const jobLink = jobs.jobs[i].html_url; - const previousJobSucceeded = previousRunJobs.jobs.find(job => job.name === jobName && job.conclusion === 'success'); + const previousJob = previousRunJobs.jobs.find(job => job.name === jobName); + previousJobSucceeded = previousJob.conclusion === 'success'; if (previousJobSucceeded) { const annotations = await github.rest.checks.listAnnotations({ owner: context.repo.owner, From 7e4c70dff9648d34841583bed8b5a7985648aa1e Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:15:26 +0100 Subject: [PATCH 3/6] fixes --- .github/workflows/failureNotifier.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/failureNotifier.yml b/.github/workflows/failureNotifier.yml index ab120943c863..d10636041ac2 100644 --- a/.github/workflows/failureNotifier.yml +++ b/.github/workflows/failureNotifier.yml @@ -36,13 +36,13 @@ jobs: const allRuns = await github.rest.actions.listWorkflowRuns({ owner: context.repo.owner, repo: context.repo.repo, - workflow_id: preDeploy.yml, + workflow_id: 'preDeploy.yml', }); const run = allRuns.data.workflow_runs.find(run => run.id === runId); const run_number = run.run_number; const previousRun = allRuns.data.workflow_runs.find(run => run.run_number === run_number - 1); if (previousRun.actor.login === 'OSBotify') { - return allRuns.data.workflow_runs.find(run.run_number === run_number - 2); + return allRuns.data.workflow_runs.find(run => run.run_number === run_number - 2); } return previousRun; From c1eeff7c8bb74d077f0a3dab15df487b070d68b1 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:41:49 +0100 Subject: [PATCH 4/6] fix mocks --- workflow_tests/mocks/failureNotifierMocks.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/workflow_tests/mocks/failureNotifierMocks.ts b/workflow_tests/mocks/failureNotifierMocks.ts index cbea6fce95ae..72a79d9ff17b 100644 --- a/workflow_tests/mocks/failureNotifierMocks.ts +++ b/workflow_tests/mocks/failureNotifierMocks.ts @@ -5,8 +5,16 @@ import {createMockStep} from '../utils/utils'; // notifyfailure const FAILURENOTIFIER__NOTIFYFAILURE__FETCH_WORKFLOW_RUN_JOBS__STEP_MOCK = createMockStep('Fetch Workflow Run Jobs', 'Fetch Workflow Run Jobs', 'NOTIFYFAILURE', [], []); +const FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN__STEP_MOCK = createMockStep('Fetch Previous Workflow Run', 'Fetch Previous Workflow Run', 'NOTIFYFAILURE', [], []); +const FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN_JOBS__STEP_MOCK = createMockStep('Fetch Previous Workflow Run Jobs', 'Fetch Previous Workflow Run Jobs', 'NOTIFYFAILURE', [], []); const FAILURENOTIFIER__NOTIFYFAILURE__PROCESS_EACH_FAILED_JOB__STEP_MOCK = createMockStep('Process Each Failed Job', 'Process Each Failed Job', 'NOTIFYFAILURE', [], []); -const FAILURENOTIFIER__NOTIFYFAILURE__STEP_MOCKS = [FAILURENOTIFIER__NOTIFYFAILURE__FETCH_WORKFLOW_RUN_JOBS__STEP_MOCK, FAILURENOTIFIER__NOTIFYFAILURE__PROCESS_EACH_FAILED_JOB__STEP_MOCK]; + +const FAILURENOTIFIER__NOTIFYFAILURE__STEP_MOCKS = [ + FAILURENOTIFIER__NOTIFYFAILURE__FETCH_WORKFLOW_RUN_JOBS__STEP_MOCK, + FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN__STEP_MOCK, + FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN_JOBS__STEP_MOCK, + FAILURENOTIFIER__NOTIFYFAILURE__PROCESS_EACH_FAILED_JOB__STEP_MOCK, +]; export default { FAILURENOTIFIER__NOTIFYFAILURE__STEP_MOCKS, From 47adcd982e1fa62f405f399a25cb0fd37d6c96e1 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:50:41 +0100 Subject: [PATCH 5/6] prettier --- workflow_tests/mocks/failureNotifierMocks.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/workflow_tests/mocks/failureNotifierMocks.ts b/workflow_tests/mocks/failureNotifierMocks.ts index 72a79d9ff17b..ca4009aef952 100644 --- a/workflow_tests/mocks/failureNotifierMocks.ts +++ b/workflow_tests/mocks/failureNotifierMocks.ts @@ -6,7 +6,13 @@ import {createMockStep} from '../utils/utils'; // notifyfailure const FAILURENOTIFIER__NOTIFYFAILURE__FETCH_WORKFLOW_RUN_JOBS__STEP_MOCK = createMockStep('Fetch Workflow Run Jobs', 'Fetch Workflow Run Jobs', 'NOTIFYFAILURE', [], []); const FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN__STEP_MOCK = createMockStep('Fetch Previous Workflow Run', 'Fetch Previous Workflow Run', 'NOTIFYFAILURE', [], []); -const FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN_JOBS__STEP_MOCK = createMockStep('Fetch Previous Workflow Run Jobs', 'Fetch Previous Workflow Run Jobs', 'NOTIFYFAILURE', [], []); +const FAILURENOTIFIER__NOTIFYFAILURE_FETCH_PREVIOUS_WORKFLOW_RUN_JOBS__STEP_MOCK = createMockStep( + 'Fetch Previous Workflow Run Jobs', + 'Fetch Previous Workflow Run Jobs', + 'NOTIFYFAILURE', + [], + [], +); const FAILURENOTIFIER__NOTIFYFAILURE__PROCESS_EACH_FAILED_JOB__STEP_MOCK = createMockStep('Process Each Failed Job', 'Process Each Failed Job', 'NOTIFYFAILURE', [], []); const FAILURENOTIFIER__NOTIFYFAILURE__STEP_MOCKS = [ From aa13921f6fe8179618588582c3ad7e121ff04cb6 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Mon, 29 Apr 2024 19:51:07 +0100 Subject: [PATCH 6/6] adress review comments --- .github/workflows/failureNotifier.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/failureNotifier.yml b/.github/workflows/failureNotifier.yml index d10636041ac2..39dfbe8e84a7 100644 --- a/.github/workflows/failureNotifier.yml +++ b/.github/workflows/failureNotifier.yml @@ -38,12 +38,9 @@ jobs: repo: context.repo.repo, workflow_id: 'preDeploy.yml', }); - const run = allRuns.data.workflow_runs.find(run => run.id === runId); - const run_number = run.run_number; - const previousRun = allRuns.data.workflow_runs.find(run => run.run_number === run_number - 1); - if (previousRun.actor.login === 'OSBotify') { - return allRuns.data.workflow_runs.find(run => run.run_number === run_number - 2); - } + const filteredRuns = allRuns.data.workflow_runs.filter(run => run.actor.login !== 'OSBotify' && run.status !== 'cancelled'); + const currentIndex = filteredRuns.findIndex(run => run.id === runId); + const previousRun = filteredRuns[currentIndex + 1]; return previousRun; - name: Fetch Previous Workflow Run Jobs @@ -84,8 +81,7 @@ jobs: const jobName = jobs.jobs[i].name; const jobLink = jobs.jobs[i].html_url; const previousJob = previousRunJobs.jobs.find(job => job.name === jobName); - previousJobSucceeded = previousJob.conclusion === 'success'; - if (previousJobSucceeded) { + if (previousJob?.conclusion === 'success') { const annotations = await github.rest.checks.listAnnotations({ owner: context.repo.owner, repo: context.repo.repo,