Skip to content

Commit

Permalink
Merge pull request #41124 from rayane-djouah/improve-workflow-failure…
Browse files Browse the repository at this point in the history
…-notifier
  • Loading branch information
blimpich authored Apr 30, 2024
2 parents 4d9e102 + aa13921 commit 942c851
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
44 changes: 34 additions & 10 deletions .github/workflows/failureNotifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,49 @@ 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 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
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;
Expand All @@ -50,14 +80,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 previousJob = previousRunJobs.jobs.find(job => job.name === jobName);
if (previousJob?.conclusion === 'success') {
const annotations = await github.rest.checks.listAnnotations({
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
16 changes: 15 additions & 1 deletion workflow_tests/mocks/failureNotifierMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ 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,
Expand Down

0 comments on commit 942c851

Please sign in to comment.