Skip to content

Commit

Permalink
Merge pull request #6 from midleman/mi/fix-failed
Browse files Browse the repository at this point in the history
fix: failed test notification
  • Loading branch information
midleman authored Jan 28, 2025
2 parents e5a5a2d + 893812d commit f5fda98
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
34 changes: 30 additions & 4 deletions .github/workflows/test-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,44 @@ jobs:
junit_path: 'test-results/junit.xml'
report_url: 'http://www.google.com' # replace with link to hosted report url

slack-notify:
slack-notify-always:
if: always()
needs: [job-1, job-2, e2e-test-1, e2e-test-2]
runs-on: ubuntu-latest
steps:
- name: Send Slack Notification
uses: midleman/slack-workflow-status@v2.0.2 # for testing changes, use branch instead of version #
uses: midleman/slack-workflow-status@master # i'm using branch instead of version # for testing purposes. should use version #
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
slack_token: ${{ secrets.SLACK_TOKEN }}
notify_on: "always"
channel: "U07KNKY6S5C"
comment_junit_failures: "true"
comment_junit_flakes: "true"
# comment_junit_failures: "true"
# comment_junit_flakes: "true"

# slack-notify-never:
# if: always()
# needs: [job-1, job-2, e2e-test-1, e2e-test-2]
# runs-on: ubuntu-latest
# steps:
# - name: Send Slack Notification
# uses: midleman/slack-workflow-status@mi/fix-failed # for testing changes, use branch instead of version #
# with:
# repo_token: ${{ secrets.GITHUB_TOKEN }}
# slack_token: ${{ secrets.SLACK_TOKEN }}
# notify_on: "never"
# channel: "U07KNKY6S5C"

# slack-notify-failure:
# if: always()
# needs: [job-1, job-2, e2e-test-1, e2e-test-2]
# runs-on: ubuntu-latest
# steps:
# - name: Send Slack Notification
# uses: midleman/slack-workflow-status@mi/fix-failed # for testing changes, use branch instead of version #
# with:
# repo_token: ${{ secrets.GITHUB_TOKEN }}
# slack_token: ${{ secrets.SLACK_TOKEN }}
# notify_on: "failure"
# channel: "U07KNKY6S5C"

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
# sends the workflow summary slack message. and depending on configuration, it can
# also comment in a thread with the playwright test results and report hyperlink.
- name: Post Workflow Status to Slack
uses: midleman/slack-workflow-status@v2.1.0
uses: midleman/slack-workflow-status@v2.2.0
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
slack_token: ${{ secrets.SLACK_TOKEN }}
Expand Down
63 changes: 58 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26803,6 +26803,29 @@ exports.downloadArtifact = downloadArtifact;

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -26817,6 +26840,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.fetchWorkflowArtifacts = void 0;
const core = __importStar(__nccwpck_require__(7484));
const github_1 = __nccwpck_require__(3228);
const parseJunitReports_1 = __nccwpck_require__(1967);
const downloadArtifact_1 = __nccwpck_require__(9209);
Expand All @@ -26833,7 +26857,7 @@ const extract_zip_1 = __importDefault(__nccwpck_require__(1683));
* @param jobsToFetch - max number of jobs to fetch
* @returns Workflow run data and processed artifacts: flakes, failures, report URLs
*/
function fetchWorkflowArtifacts(githubToken, jobsToFetch = 30) {
function fetchWorkflowArtifacts(githubToken, notifyOn, jobsToFetch = 30) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = (0, github_1.getOctokit)(githubToken);
// Fetch workflow run data
Expand All @@ -26854,10 +26878,9 @@ function fetchWorkflowArtifacts(githubToken, jobsToFetch = 30) {
// Check if there are any job failures
const hasFailures = completedJobs.some((job) => !['success', 'skipped'].includes(job.conclusion));
// Decide whether to send a notification
const notifyOn = process.env.NOTIFY_ON || 'always';
const shouldNotify = notifyOn === 'always' || (notifyOn.includes('fail') && hasFailures);
if (!shouldNotify) {
console.info('No notification sent: All jobs passed and "notify_on" is set to "fail-only".');
core.info('No notification sent: All jobs passed and "notify_on" is set to "fail-only".');
return {
workflowRun,
jobs: { failedTests: {}, flakyTests: {}, reportUrls: {} }
Expand Down Expand Up @@ -27085,7 +27108,7 @@ function main() {
core.setSecret(githubToken);
core.setSecret(slackToken);
// Fetch workflow run data and job information
const { workflowRun, jobs } = yield (0, fetchArtifacts_1.fetchWorkflowArtifacts)(githubToken);
const { workflowRun, jobs } = yield (0, fetchArtifacts_1.fetchWorkflowArtifacts)(githubToken, notifyOn);
const { completedJobs, shouldNotify } = yield (0, analyzeJobs_1.analyzeJobs)({
githubToken,
workflowRun,
Expand Down Expand Up @@ -27320,6 +27343,29 @@ exports.sendSlackMessage = sendSlackMessage;

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -27331,6 +27377,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.analyzeJobs = void 0;
const core = __importStar(__nccwpck_require__(7484));
const github_1 = __nccwpck_require__(3228);
function analyzeJobs({ githubToken, workflowRun, notifyOn, jobsToFetch }) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -27343,7 +27390,13 @@ function analyzeJobs({ githubToken, workflowRun, notifyOn, jobsToFetch }) {
});
const completedJobs = jobsResponse.jobs.filter((job) => job.status === 'completed');
const hasFailures = completedJobs.some((job) => !['success', 'skipped'].includes(job.conclusion));
const shouldNotify = notifyOn === 'always' || (notifyOn === 'fail-only' && hasFailures);
const shouldNotify = notifyOn === 'always' || (notifyOn.includes('fail') && hasFailures);
if (shouldNotify) {
const reason = notifyOn === 'always'
? '"notifyOn" is set to "always"'
: 'At least one job failed';
core.info(`Sending notification: ${reason}`);
}
return { completedJobs, shouldNotify };
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/github/fetchArtifacts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import { context, getOctokit } from '@actions/github'
import { parseJUnitReports } from './parseJunitReports'
import { downloadArtifact } from './downloadArtifact'
Expand All @@ -17,6 +18,7 @@ import extract from 'extract-zip'
*/
export async function fetchWorkflowArtifacts(
githubToken: string,
notifyOn: string,
jobsToFetch = 30
): Promise<{
workflowRun: any // eslint-disable-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -54,12 +56,11 @@ export async function fetchWorkflowArtifacts(
)

// Decide whether to send a notification
const notifyOn = process.env.NOTIFY_ON || 'always'
const shouldNotify =
notifyOn === 'always' || (notifyOn.includes('fail') && hasFailures)

if (!shouldNotify) {
console.info(
core.info(
'No notification sent: All jobs passed and "notify_on" is set to "fail-only".'
)
return {
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ async function main(): Promise<void> {
core.setSecret(slackToken)

// Fetch workflow run data and job information
const { workflowRun, jobs } = await fetchWorkflowArtifacts(githubToken)
const { workflowRun, jobs } = await fetchWorkflowArtifacts(
githubToken,
notifyOn
)

const { completedJobs, shouldNotify } = await analyzeJobs({
githubToken,
Expand Down
11 changes: 10 additions & 1 deletion src/utils/analyzeJobs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import { getOctokit } from '@actions/github'

export async function analyzeJobs({
Expand Down Expand Up @@ -34,7 +35,15 @@ export async function analyzeJobs({
)

const shouldNotify =
notifyOn === 'always' || (notifyOn === 'fail-only' && hasFailures)
notifyOn === 'always' || (notifyOn.includes('fail') && hasFailures)

if (shouldNotify) {
const reason =
notifyOn === 'always'
? '"notifyOn" is set to "always"'
: 'At least one job failed'
core.info(`Sending notification: ${reason}`)
}

return { completedJobs, shouldNotify }
}

0 comments on commit f5fda98

Please sign in to comment.