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

@ -1,6 +1,7 @@
import * as core from '@actions/core';
import { context } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';
import { HttpClient } from '@actions/http-client';
interface GitRef {
ref: string;
@ -59,7 +60,7 @@ export async function validateIfReleaseIsPublished(
tag,
});
if (foundRelease.prerelease){
if (foundRelease.prerelease) {
throw new Error(
`The '${foundRelease.name}' release is marked as pre-release. Updating tags for pre-release is not supported`
);
@ -104,4 +105,10 @@ export async function updateTag(
sha: sourceTagSHA
});
}
}
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 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';
async function run(): Promise<void> {
@ -18,9 +19,22 @@ async function run(): Promise<void> {
core.setOutput('major-tag', majorTag);
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) {
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();