generated from actions/container-toolkit-action
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
suguru-yazawa
committed
Aug 27, 2019
1 parent
a20e6fd
commit 276927a
Showing
5 changed files
with
129 additions
and
10 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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
package-lock.json | ||
node_modules | ||
node_modules | ||
docker-compose.yml | ||
.node_repl_history | ||
.config/ | ||
.npm/ |
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
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
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,84 @@ | ||
import { Status } from './utils'; | ||
import * as github from '@actions/github'; | ||
import { SectionBlock, MessageAttachment, MrkdwnElement } from '@slack/types'; | ||
import { | ||
IncomingWebhook, IncomingWebhookDefaultArguments, | ||
IncomingWebhookSendArguments, IncomingWebhookResult | ||
} from '@slack/webhook'; | ||
|
||
const SLACK_WEBHOOK: string = process.env.SLACK_WEBHOOK || ''; | ||
if (SLACK_WEBHOOK === '') { | ||
throw new Error('ERROR: Missing "SLACK_WEBHOOK"\nPlease configure "SLACK_WEBHOOK" as environment variable'); | ||
} | ||
|
||
export class Slack { | ||
client: IncomingWebhook; | ||
readonly color: string[] = ['danger', 'good']; | ||
|
||
constructor( | ||
icon_emoji: string, | ||
username: string, | ||
channel: string | ||
) { | ||
const params: IncomingWebhookDefaultArguments = { | ||
username, | ||
icon_emoji, | ||
channel | ||
}; | ||
|
||
this.client = new IncomingWebhook(SLACK_WEBHOOK, params); | ||
} | ||
|
||
/** | ||
* Get slack blocks UI | ||
*/ | ||
protected get blocks(): SectionBlock { | ||
const { sha, eventName, workflow, ref, action } = github.context; | ||
const { owner, repo } = github.context.repo; | ||
|
||
const blocks: SectionBlock = { | ||
type: 'section', | ||
fields: [ | ||
{ type: 'mrkdwn', text: `*repo*\n${repo}` }, | ||
{ type: 'mrkdwn', text: `*sha*\n${sha}` }, | ||
{ type: 'mrkdwn', text: `*eventName*\n${eventName}` }, | ||
{ type: 'mrkdwn', text: `*workflow*\n${workflow}` }, | ||
{ type: 'mrkdwn', text: `*ref*\n${ref}` }, | ||
{ type: 'mrkdwn', text: `*action*\n${action}` } | ||
] | ||
} | ||
|
||
return blocks; | ||
} | ||
|
||
/** | ||
* Generate slack payload | ||
*/ | ||
protected generatePayload(type: Status, text: MrkdwnElement): IncomingWebhookSendArguments { | ||
const blocks: SectionBlock = { ...this.blocks, text }; | ||
const attachments: MessageAttachment = { | ||
color: this.color[type], | ||
blocks: [blocks] | ||
} | ||
const payload: IncomingWebhookSendArguments = { | ||
attachments: [attachments] | ||
} | ||
|
||
return payload; | ||
} | ||
|
||
/** | ||
* Notify information about github actions to Slack | ||
*/ | ||
public async notify(type: Status, text: string): Promise<IncomingWebhookResult> { | ||
const slack_text: MrkdwnElement = { type: 'mrkdwn', text }; | ||
let payload: IncomingWebhookSendArguments = this.generatePayload(type, slack_text); | ||
|
||
try { | ||
const result = await this.client.send(payload); | ||
return result; | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
} |
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,16 @@ | ||
export enum Status { | ||
Failure, | ||
Success | ||
} | ||
|
||
export function getStatus(type: string): Status { | ||
const lowercase_type: string = type.toLowerCase(); | ||
|
||
if (lowercase_type.includes('success')) { | ||
return Status.Success; | ||
} else if (lowercase_type.includes('fail')) { | ||
return Status.Failure; | ||
} else { | ||
throw new Error('Allow words that contain "success" or "fail"'); | ||
} | ||
} |