This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0b3c67
commit 9631ac8
Showing
5 changed files
with
1,193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GITHUB_REPO_URL= | ||
SLACK_WEBHOOK_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.editorconfig | ||
.env.sample | ||
.eslintrc.yml | ||
.gcloudignore | ||
.git | ||
.gitignore | ||
LICENSE | ||
node_modules | ||
README.md | ||
screenshot.png | ||
|
||
#!include:.gitignore | ||
|
||
!.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require('dotenv').config(); | ||
|
||
const { IncomingWebhook } = require('@slack/webhook'); | ||
const webhook = new IncomingWebhook(process.env.SLACK_WEBHOOK_URL); | ||
|
||
const statusCodes = { | ||
CANCELLED: { | ||
color: '#face00', | ||
text: 'Build cancelled' | ||
}, | ||
FAILURE: { | ||
color: '#e52207', | ||
text: 'Build failed' | ||
}, | ||
INTERNAL_ERROR: { | ||
color: '#e52207', | ||
text: 'Internal error encountered during build' | ||
}, | ||
QUEUED: { | ||
color: '#face00', | ||
text: 'New build queued' | ||
}, | ||
SUCCESS: { | ||
color: '#98d035', | ||
text: 'Build successfully completed' | ||
}, | ||
TIMEOUT: { | ||
color: '#e52207', | ||
text: 'Build timed out' | ||
}, | ||
WORKING: { | ||
color: '#98d035', | ||
text: 'New build in progress' | ||
} | ||
}; | ||
|
||
const createSlackMessage = (build) => { | ||
let statusMessage = statusCodes[build.status].text; | ||
let branchName = build.source.repoSource.branchName; | ||
let commitSha = build.sourceProvenance.resolvedRepoSource.commitSha.substring(0,7); | ||
|
||
return { | ||
text: `${statusMessage} for *${build.projectId}*.`, | ||
blocks: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `${statusMessage} for *${build.projectId}*.` | ||
} | ||
} | ||
], | ||
attachments: [ | ||
{ | ||
blocks: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*Build Log:* <${build.logUrl}|${build.id}>` | ||
} | ||
}, | ||
{ | ||
type: 'context', | ||
elements: [ | ||
{ | ||
type: 'mrkdwn', | ||
text: `*Branch:* <${process.env.GITHUB_REPO_URL}/tree/${branchName}|${branchName}>` | ||
}, | ||
{ | ||
type: 'mrkdwn', | ||
text: `*Commit:* <${process.env.GITHUB_REPO_URL}/commit/${commitSha}|${commitSha}>` | ||
} | ||
] | ||
} | ||
], | ||
color: statusCodes[build.status].color | ||
} | ||
] | ||
}; | ||
}; | ||
|
||
module.exports.subscribe = async (event) => { | ||
const build = JSON.parse(new Buffer(event.data, 'base64').toString()); | ||
const message = createSlackMessage(build); | ||
|
||
await webhook.send(message); | ||
}; |
Oops, something went wrong.