Integrate Slack notifications

This commit is contained in:
MaksimZhukov 2021-05-25 21:00:46 +03:00
parent ead7541b7c
commit 4e15cde332
6 changed files with 46 additions and 21 deletions

View File

@ -31,21 +31,4 @@ jobs:
uses: ./ uses: ./
with: with:
source-tag: ${{ env.TAG_NAME }} source-tag: ${{ env.TAG_NAME }}
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}
- name: Send slack message
if: failure()
run: |
curl `
-X POST `
-H 'Content-type: application/json' `
--data '{\"text\":\"Failed to update a major tag for the ${{ github.repository }} action\"}' `
${{ secrets.SLACK_WEBHOOK }}
- name: Send slack message
if: success()
run: |
curl `
-X POST `
-H 'Content-type: application/json' `
--data '{\"text\":\"The ${{ steps.update-major-tag.outputs.major-tag }} tag has been successfully updated for the ${{ github.repository }} action to include changes from the ${{ env.TAG_NAME }}\"}' `
${{ secrets.SLACK_WEBHOOK }}

View File

@ -4,6 +4,8 @@ inputs:
source-tag: source-tag:
description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3' description: 'Tag name that the major tag will point to. Examples: v1.2.3, 1.2.3'
required: true required: true
slack-webhook:
description: 'Slack Webhook URL to post a message'
token: token:
description: 'Token to get an authenticated Octokit' description: 'Token to get an authenticated Octokit'
default: ${{ github.token }} default: ${{ github.token }}

20
dist/index.js vendored
View File

@ -26,9 +26,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.updateTag = exports.validateIfReleaseIsPublished = void 0; exports.postMessageToSlack = exports.updateTag = exports.validateIfReleaseIsPublished = void 0;
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438); const github_1 = __nccwpck_require__(5438);
const http_client_1 = __nccwpck_require__(9925);
async function findTag(tag, octokitClient) { async function findTag(tag, octokitClient) {
try { try {
const { data: foundTag } = await octokitClient.git.getRef({ const { data: foundTag } = await octokitClient.git.getRef({
@ -96,6 +97,12 @@ async function updateTag(sourceTag, targetTag, octokitClient) {
} }
} }
exports.updateTag = updateTag; exports.updateTag = updateTag;
async function postMessageToSlack(slackWebhook, message) {
const jsonData = { text: message };
const http = new http_client_1.HttpClient();
await http.postJson(slackWebhook, jsonData);
}
exports.postMessageToSlack = postMessageToSlack;
/***/ }), /***/ }),
@ -127,6 +134,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438)); const github = __importStar(__nccwpck_require__(5438));
const github_1 = __nccwpck_require__(5438);
const api_utils_1 = __nccwpck_require__(2430); const api_utils_1 = __nccwpck_require__(2430);
const version_utils_1 = __nccwpck_require__(1534); const version_utils_1 = __nccwpck_require__(1534);
async function run() { async function run() {
@ -140,12 +148,22 @@ async function run() {
await api_utils_1.updateTag(sourceTagName, majorTag, octokitClient); await api_utils_1.updateTag(sourceTagName, majorTag, octokitClient);
core.setOutput('major-tag', majorTag); core.setOutput('major-tag', majorTag);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`); core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`);
const slackMessage = `The ${majorTag} tag has been successfully updated for the ${github_1.context.repo.repo} action to include changes from the ${sourceTagName}`;
await reportStatusToSlack(slackMessage);
} }
catch (error) { catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
const slackMessage = `Failed to update a major tag for the ${github_1.context.repo.repo} action`;
await reportStatusToSlack(slackMessage);
} }
} }
; ;
async function reportStatusToSlack(message) {
const slackWebhook = core.getInput('slack-webhook');
if (slackWebhook) {
await api_utils_1.postMessageToSlack(slackWebhook, message);
}
}
run(); run();

View File

@ -26,6 +26,7 @@
"@actions/github": "^4.0.0" "@actions/github": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@actions/http-client": "^1.0.11",
"@types/jest": "^26.0.23", "@types/jest": "^26.0.23",
"@types/semver": "^7.3.6", "@types/semver": "^7.3.6",
"@vercel/ncc": "^0.28.5", "@vercel/ncc": "^0.28.5",

View File

@ -1,6 +1,7 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import { context } from '@actions/github'; import { context } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils'; import { GitHub } from '@actions/github/lib/utils';
import { HttpClient } from '@actions/http-client';
interface GitRef { interface GitRef {
ref: string; ref: string;
@ -105,3 +106,9 @@ export async function updateTag(
}); });
} }
} }
export async function postMessageToSlack(slackWebhook: string, message: string): Promise<void> {
const jsonData = {text: message}
const http = new HttpClient();
await http.postJson(slackWebhook, jsonData);
}

View File

@ -1,6 +1,7 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as github from '@actions/github'; import * as github from '@actions/github';
import { updateTag, validateIfReleaseIsPublished } from './api-utils'; import { context } from '@actions/github';
import { updateTag, validateIfReleaseIsPublished, postMessageToSlack } from './api-utils';
import { validateSemverVersionFromTag, getMajorTagFromFullTag } from './version-utils'; import { validateSemverVersionFromTag, getMajorTagFromFullTag } from './version-utils';
async function run(): Promise<void> { async function run(): Promise<void> {
@ -18,9 +19,22 @@ async function run(): Promise<void> {
core.setOutput('major-tag', majorTag); core.setOutput('major-tag', majorTag);
core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`); core.info(`The '${majorTag}' major tag now points to the '${sourceTagName}' tag`);
const slackMessage = `The ${majorTag} tag has been successfully updated for the ${context.repo.repo} action to include changes from the ${sourceTagName}`;
await reportStatusToSlack(slackMessage);
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
const slackMessage = `Failed to update a major tag for the ${context.repo.repo} action`;
await reportStatusToSlack(slackMessage);
} }
}; };
async function reportStatusToSlack(message: string): Promise<void> {
const slackWebhook = core.getInput('slack-webhook');
if (slackWebhook) {
await postMessageToSlack(slackWebhook, message);
}
}
run(); run();