Skip to content

Commit f0ff0bb

Browse files
authored
feat: add diff to slack message and make wording more clear and consise (#7)
* feat: add test notification * fix: add index.js * Trigger notification * feat: add diff text * chore: update diff message * chore: update diff and status message * chore: typing and make message more readable * fix: wording
1 parent 7a55be6 commit f0ff0bb

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v1
1212

1313
- name: create a deployment
14-
uses: npm/[email protected].0
14+
uses: npm/[email protected].1
1515
id: create-deployment
1616
with:
1717
type: create
@@ -20,7 +20,7 @@ jobs:
2020
environment_url: https://npmjs.com
2121
job_status: ${{job.status}}
2222
slack_token: ${{secrets.NPM_ROBOT_SLACK_TOKEN}}
23-
slack_channel: npm-ops
23+
slack_channel: npm-test-notifications
2424

2525
# add here an actual deployment
2626
- name: placeholder for actual deployment

dist/main/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12084,17 +12084,28 @@ function getEnvironment(ref) {
1208412084
return environment;
1208512085
}
1208612086
exports.getEnvironment = getEnvironment;
12087-
function postSlackNotification(slackToken, slackChannel, repo, sha, environment, status, actor) {
12087+
function postSlackNotification(slackToken, slackChannel, environment, status, context) {
12088+
var _a;
1208812089
return __awaiter(this, void 0, void 0, function* () {
1208912090
if (slackToken === '' || slackChannel === '') {
1209012091
return;
1209112092
}
12093+
const { actor, repo, sha, payload } = context;
1209212094
try {
12095+
const statusIcon = status === 'success' ? '✅' : '❌';
12096+
const afterSha = sha.slice(0, 7);
1209312097
const repoUrl = `https://github.com/${repo.owner}/${repo.repo}`;
1209412098
const deploymentUrl = `${repoUrl}/deployments?environment=${environment}#activity-log`;
1209512099
const commitUrl = `${repoUrl}/commit/${sha}`;
12100+
let commitText = `<${commitUrl}|${afterSha}>`;
12101+
const payloadForPushes = payload;
12102+
if ((payloadForPushes === null || payloadForPushes === void 0 ? void 0 : payloadForPushes.compare) !== undefined) {
12103+
const beforeSha = payloadForPushes.before.slice(0, 7);
12104+
const afterShaMessage = (_a = payloadForPushes.head_commit.message) !== null && _a !== void 0 ? _a : '';
12105+
commitText = `<${payloadForPushes.compare}|${beforeSha} ⇢ ${afterSha} ${afterShaMessage}>`;
12106+
}
1209612107
// message formatting reference - https://api.slack.com/reference/surfaces/formatting
12097-
const text = `Deployment of commit <${commitUrl}|${sha.slice(0, 6)}> for <${repoUrl}|${repo.repo}> completed with status \`${status}\` to environment <${deploymentUrl}|${environment}> by @${actor}`;
12108+
const text = `<${repoUrl}|${repo.repo}> deployment 🚀 to <${deploymentUrl}|${environment}> by @${actor} completed with ${status} ${statusIcon} - ${commitText}`;
1209812109
const slackClient = new web_api_1.WebClient(slackToken);
1209912110
const slackParams = {
1210012111
channel: slackChannel,

dist/post/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11951,17 +11951,28 @@ function getEnvironment(ref) {
1195111951
return environment;
1195211952
}
1195311953
exports.getEnvironment = getEnvironment;
11954-
function postSlackNotification(slackToken, slackChannel, repo, sha, environment, status, actor) {
11954+
function postSlackNotification(slackToken, slackChannel, environment, status, context) {
11955+
var _a;
1195511956
return __awaiter(this, void 0, void 0, function* () {
1195611957
if (slackToken === '' || slackChannel === '') {
1195711958
return;
1195811959
}
11960+
const { actor, repo, sha, payload } = context;
1195911961
try {
11962+
const statusIcon = status === 'success' ? '✅' : '❌';
11963+
const afterSha = sha.slice(0, 7);
1196011964
const repoUrl = `https://github.com/${repo.owner}/${repo.repo}`;
1196111965
const deploymentUrl = `${repoUrl}/deployments?environment=${environment}#activity-log`;
1196211966
const commitUrl = `${repoUrl}/commit/${sha}`;
11967+
let commitText = `<${commitUrl}|${afterSha}>`;
11968+
const payloadForPushes = payload;
11969+
if ((payloadForPushes === null || payloadForPushes === void 0 ? void 0 : payloadForPushes.compare) !== undefined) {
11970+
const beforeSha = payloadForPushes.before.slice(0, 7);
11971+
const afterShaMessage = (_a = payloadForPushes.head_commit.message) !== null && _a !== void 0 ? _a : '';
11972+
commitText = `<${payloadForPushes.compare}|${beforeSha} ⇢ ${afterSha} ${afterShaMessage}>`;
11973+
}
1196311974
// message formatting reference - https://api.slack.com/reference/surfaces/formatting
11964-
const text = `Deployment of commit <${commitUrl}|${sha.slice(0, 6)}> for <${repoUrl}|${repo.repo}> completed with status \`${status}\` to environment <${deploymentUrl}|${environment}> by @${actor}`;
11975+
const text = `<${repoUrl}|${repo.repo}> deployment 🚀 to <${deploymentUrl}|${environment}> by @${actor} completed with ${status} ${statusIcon} - ${commitText}`;
1196511976
const slackClient = new web_api_1.WebClient(slackToken);
1196611977
const slackParams = {
1196711978
channel: slackChannel,
@@ -12429,7 +12440,8 @@ function post() {
1242912440
console.log(`ref: ${ref}`);
1243012441
console.log(`owner: ${repo.owner}`);
1243112442
console.log(`repo: ${repo.repo}`);
12432-
console.log(`sha: ${sha}`);
12443+
console.log(`compare: ${github.context.payload.compare}`);
12444+
console.log(`new_sha: ${sha}`);
1243312445
console.log('\n');
1243412446
try {
1243512447
console.log('### post.inputs ###');
@@ -12463,7 +12475,7 @@ function post() {
1246312475
return;
1246412476
}
1246512477
// Post Slack notification
12466-
yield utils_1.postSlackNotification(slackToken, slackChannel, repo, sha, environment, status, actor);
12478+
yield utils_1.postSlackNotification(slackToken, slackChannel, environment, status, github.context);
1246712479
try {
1246812480
yield complete_1.complete(client, Number(deploymentId), status);
1246912481
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "action-deploy",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"private": true,
55
"description": "Action to manage GitHub deployments",
66
"main": "lib/main.js",

src/post.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function post (): Promise<void> {
1919
console.log(`ref: ${ref}`)
2020
console.log(`owner: ${repo.owner}`)
2121
console.log(`repo: ${repo.repo}`)
22-
console.log(`sha: ${sha}`)
22+
console.log(`compare: ${github.context.payload.compare as string}`)
23+
console.log(`new_sha: ${sha}`)
2324
console.log('\n')
2425

2526
try {
@@ -61,7 +62,7 @@ export async function post (): Promise<void> {
6162
}
6263

6364
// Post Slack notification
64-
await postSlackNotification(slackToken, slackChannel, repo, sha, environment, status, actor)
65+
await postSlackNotification(slackToken, slackChannel, environment, status, github.context)
6566

6667
try {
6768
await complete(client, Number(deploymentId), status)

src/utils.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import * as core from '@actions/core'
22
import { ChatPostMessageArguments, WebClient } from '@slack/web-api'
3+
import { Context } from '@actions/github/lib/context'
4+
import { WebhookPayload } from '@actions/github/lib/interfaces'
5+
6+
export interface WebhookPayloadForPushes extends WebhookPayload {
7+
after: string
8+
before: string
9+
compare?: string
10+
head_commit: {
11+
message?: string
12+
}
13+
}
314

415
export type DeploymentStatus =
516
| 'error'
@@ -34,21 +45,30 @@ export function getEnvironment (ref: string): string {
3445
export async function postSlackNotification (
3546
slackToken: string,
3647
slackChannel: string,
37-
repo: { owner: string, repo: string },
38-
sha: string,
3948
environment: string,
40-
status: string,
41-
actor: string): Promise<void> {
49+
status: DeploymentStatus,
50+
context: Context): Promise<void> {
4251
if (slackToken === '' || slackChannel === '') {
4352
return
4453
}
54+
const { actor, repo, sha, payload } = context
55+
4556
try {
57+
const statusIcon = status === 'success' ? '✅' : '❌'
58+
const afterSha = sha.slice(0, 7)
4659
const repoUrl = `https://github.com/${repo.owner}/${repo.repo}`
4760
const deploymentUrl = `${repoUrl}/deployments?environment=${environment}#activity-log`
4861
const commitUrl = `${repoUrl}/commit/${sha}`
62+
let commitText = `<${commitUrl}|${afterSha}>`
63+
const payloadForPushes = payload as WebhookPayloadForPushes
64+
if (payloadForPushes?.compare !== undefined) {
65+
const beforeSha = payloadForPushes.before.slice(0, 7)
66+
const afterShaMessage = payloadForPushes.head_commit.message ?? ''
67+
commitText = `<${payloadForPushes.compare}|${beforeSha}${afterSha} ${afterShaMessage}>`
68+
}
4969

5070
// message formatting reference - https://api.slack.com/reference/surfaces/formatting
51-
const text = `Deployment of commit <${commitUrl}|${sha.slice(0, 6)}> for <${repoUrl}|${repo.repo}> completed with status \`${status}\` to environment <${deploymentUrl}|${environment}> by @${actor}`
71+
const text = `<${repoUrl}|${repo.repo}> deployment 🚀 to <${deploymentUrl}|${environment}> by @${actor} completed with ${status} ${statusIcon} - ${commitText}`
5272
const slackClient = new WebClient(slackToken)
5373
const slackParams: ChatPostMessageArguments = {
5474
channel: slackChannel,

0 commit comments

Comments
 (0)