Skip to content

Commit

Permalink
update: add features
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru-yazawa committed Aug 27, 2019
1 parent a20e6fd commit 276927a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .gitignore
@@ -1,2 +1,6 @@
package-lock.json
node_modules
node_modules
docker-compose.yml
.node_repl_history
.config/
.npm/
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -26,10 +26,11 @@
"homepage": "https://github.com/actions/container-toolkit-template#readme",
"dependencies": {
"@actions/core": "^1.0.0",
"@actions/io": "^1.0.0",
"@actions/exec": "^1.0.0",
"@actions/github": "^1.0.0",
"@actions/tool-cache": "^1.0.0"
"@actions/io": "^1.0.0",
"@actions/tool-cache": "^1.0.0",
"@slack/webhook": "^5.0.1"
},
"devDependencies": {
"@types/jest": "^24.0.13",
Expand Down
28 changes: 21 additions & 7 deletions src/main.ts
@@ -1,14 +1,28 @@
const core = require('@actions/core');
const github = require('@actions/github');
import * as core from '@actions/core';
import { Slack } from './slack';
import { getStatus } from './utils';

async function run() {
try {
const myInput = core.getInput('myInput');
core.debug(`Hello ${myInput} from inside a container`);
const type: string = core.getInput('type', { required: true });
const text: string = core.getInput('text') || '*The Result of Github Actions*';
const channel: string = core.getInput('channel') || '#general';
const icon_emoji: string = core.getInput('icon_emoji') || 'github';
const username: string = core.getInput('username') || 'Github Actions';

core.debug(`Input variables:\n`);
core.debug(`\ttype: ${type}`);
core.debug(`\ttext: ${text}`);
core.debug(`\tchannel: ${channel}`);
core.debug(`\ticon_emoji: ${icon_emoji}`);
core.debug(`\tusername: ${username}`);

const slack = new Slack(icon_emoji, username, channel);
const status = getStatus(type);
const result = await slack.notify(status, text);

core.debug(`Response from Slack: ${result}`);

// Get github context data
const context = github.context;
console.log(`We can even get context data, like the repo: ${context.repo.repo}`)
} catch (error) {
core.setFailed(error.message);
}
Expand Down
84 changes: 84 additions & 0 deletions src/slack.ts
@@ -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;
}
}
}
16 changes: 16 additions & 0 deletions src/utils.ts
@@ -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"');
}
}

0 comments on commit 276927a

Please sign in to comment.