From 31350f9f2d3f2443f66fe6f5f08905921044f623 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Sat, 13 Nov 2021 16:05:42 +0700 Subject: [PATCH 1/8] :memo: github token --- Readme.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 4e7c800..89182f8 100644 --- a/Readme.md +++ b/Readme.md @@ -1,11 +1,22 @@ # Discord gcloud build bot - - - - -To deploy to cloud function +## Generate Github token +Setting > Developer settings > Personal access tokens + +selects following scopes + +- [x] repo + - [x] repo:status + - [x] repo_deployment + - [x] public_repo + - [x] repo:invite + - [x] security_events +- [ ] user + - [x] read:user + - [x] user:email + +## To deploy to cloud function From 8e7b1a22f4d18fc90598cfa44ab692aa70446f96 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Sat, 13 Nov 2021 16:10:59 +0700 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9D=20deploy=20node=20runtime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 89182f8..cb8db51 100644 --- a/Readme.md +++ b/Readme.md @@ -32,7 +32,7 @@ and run following command, ``` -gcloud functions deploy subscribeDiscord --trigger-topic cloud-builds --runtime nodejs10 --env-vars-file .env.yaml +gcloud functions deploy subscribeDiscord --trigger-topic cloud-builds --runtime nodejs14 --env-vars-file .env.yaml ``` From fcf7a6ac7413422111998a38b78e6b575a29b699 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:50:52 +0700 Subject: [PATCH 3/8] =?UTF-8?q?=E2=9C=A8=20author?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 122 ---------------------------------------------- package.json | 3 +- src/discord.ts | 3 ++ src/github.ts | 10 +++- src/index.ts | 89 +++++++++++++++++++++------------ src/interfaces.ts | 6 +++ 6 files changed, 76 insertions(+), 157 deletions(-) delete mode 100644 index.js diff --git a/index.js b/index.js deleted file mode 100644 index 5d33fce..0000000 --- a/index.js +++ /dev/null @@ -1,122 +0,0 @@ -const axios = require("axios"); - -const GREY = 9545382; -// const BLUE = 39393 -const RED = 16333359; -const GREEN = 53606; -const YELLOW = 16302848; - -const INFO_IMG = "https://img.icons8.com/color/2x/info.png"; -const ERROR_IMG = "https://img.icons8.com/flat_round/2x/stop.png"; -const WARNING_IMG = "https://img.icons8.com/flat_round/2x/pause.png"; -const SUCCESS_IMG = "https://img.icons8.com/flat_round/2x/checkmark.png"; - -const addFeild = (inline = false) => - (name) => (value) => ({ inline, name, value }); - -module.exports.subscribeDiscord = function (event) { - const build = eventToBuild(event.data); - const status = [ - "WORKING", - "SUCCESS", - "FAILURE", - "INTERNAL_ERROR", - "TIMEOUT", - "CANCELLED", - ]; - - if (!status.includes(build.status)) return; - - const buildStatus = build.status; - const substitutions = build.substitutions; - const logUrl = build.logUrl; - const branch = substitutions.BRANCH_NAME; - const repo = substitutions.REPO_NAME; - const commit = substitutions.COMMIT_SHA; - const owner = process.env.OWNER_NAME; - const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; - - const inlineField = addFeild(true); - - fetchGitMessage({ commit, repo, owner }).then((result) => { - const thumbnail = getThumbnail(buildStatus); - const color = getColor(buildStatus); - const title = `${repo}(${branch}) is ${capitalized(buildStatus)}` - axios.post(DISCORD_WEBHOOK_URL, { - embeds: [ - { - title, - color, - thumbnail, - fields: [ - inlineField("Repo")(repo), - inlineField("Branch")(branch), - inlineField("Status")(buildStatus), - addFeild()("See log")(logUrl), - addFeild()("Commit")(getMessage(result)), - ], - }, - ], - }) - .catch((e) => { - console.error(e.response.message); - }); - }); -}; - -const eventToBuild = (data) => { - return JSON.parse(Buffer.from(data, "base64").toString()); -}; - -const getColor = ( - statusIndex, -) => { - const colorSet = { SUCCESS: GREEN, WORKING: GREY, CANCELLED: YELLOW }; - return colorSet[statusIndex] || RED; -}; - -const getThumbnail = (status) => { - const imgSet = { - SUCCESS: SUCCESS_IMG, - WORKING: INFO_IMG, - CANCELLED: WARNING_IMG, - }; - return { - url: imgSet[status] || ERROR_IMG, - width: 64, - height: 64 - }; -}; - -const getMessage = (result) => { - try { - return result.data.data.repository.object.message; - } catch (error) { - return error; - } -}; - -const fetchGitMessage = ({ commit, repo, owner }) => - axios.post( - "https://api.github.com/graphql", - { - query: `{ - repository(owner:"${owner}",name:"${repo}") { - object(oid: "${commit}") { - ... on Commit { - message - } - } - } - }`, - }, - { - headers: { - Authorization: `bearer ${process.env.GITHUB_API_TOKEN}`, - }, - }, - ); - -const capitalized = (textString) => { - return `${textString[0].toUpperCase()}${textString.toLowerCase().slice(1)}` -} \ No newline at end of file diff --git a/package.json b/package.json index 613da74..5bc81c4 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,9 @@ "name": "nimble-build-notification", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "dist/index.js", "scripts": { + "build": "tsc", "deploy": "gcloud functions deploy subscribeDiscord --env-vars-file .env.yaml" }, "author": "", diff --git a/src/discord.ts b/src/discord.ts index dc7c710..0de4ba2 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -49,3 +49,6 @@ export const getTitle = ({ branch: string; status: string; }): string => `${repo}(${branch}) is ${capitalized(status)}`; + +export const getUrl = (message: string, url: string): string => + `[${message}](${url})`; diff --git a/src/github.ts b/src/github.ts index f7fb03b..32809c0 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,9 +1,11 @@ import axios, { AxiosPromise, AxiosResponse } from 'axios'; import { CommitResponse } from './interfaces'; -export const getMessage = (result: AxiosResponse): string => { +type Repo = CommitResponse['data']['repository']['object']; + +export const getRepository = (result: AxiosResponse): Repo => { try { - return result.data.data.repository.object.message; + return result.data.data.repository.object; } catch (error) { return error; } @@ -24,6 +26,10 @@ export const fetchGitMessage = ({ object(oid: "${commit}") { ... on Commit { message + author { + avatarUrl + name + } } } } diff --git a/src/index.ts b/src/index.ts index db47b2b..fbc053d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,37 @@ import axios from 'axios'; import * as discord from './discord'; -import { fetchGitMessage, getMessage } from './github'; +import { fetchGitMessage, getRepository } from './github'; import { CloudFunction, CloudFunctionEvent } from './interfaces'; -const addFeild = +interface AddFieldReturn { + inline: boolean; + name: string; + value: string; +} + +const addField = (inline = false) => - (name: string) => - (value: string) => ({ + (nameOrValue: string, value?: string) => ({ inline, - name, - value, + name: value ? nameOrValue : '', + value: value || nameOrValue, }); const eventToBuild = (data: string): CloudFunctionEvent => { return JSON.parse(Buffer.from(data, 'base64').toString()); }; -export const subscribeDiscord: CloudFunction = (event): void => { +const sendDiscordMessage = async (embeds: { fields: AddFieldReturn[] }[]) => { + try { + await axios.post(process.env.DISCORD_WEBHOOK_URL, { + embeds, + }); + } catch (error) { + console.error(error); + } +}; + +export const subscribeDiscord: CloudFunction = async event => { const build = eventToBuild(event.data); const status = [ 'WORKING', @@ -36,33 +51,43 @@ export const subscribeDiscord: CloudFunction = (event): void => { const repo = substitutions.REPO_NAME; const commit = substitutions.COMMIT_SHA; const owner = process.env.OWNER_NAME; - const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; - const inlineField = addFeild(true); + const inlineField = addField(true); + const blockField = addField(); + + const log = discord.getUrl('See log', logUrl); + + try { + const result = await fetchGitMessage({ commit, repo, owner }); + const { author: githubAuthor, message } = getRepository(result); + + const fields = [ + blockField(message), + inlineField('Repo', repo), + inlineField('Branch', branch), + inlineField('Status', buildStatus), + inlineField('Log', log), + ]; - fetchGitMessage({ commit, repo, owner }).then(result => { - const thumbnail = discord.getThumbnail(buildStatus); const color = discord.getColor(buildStatus); const title = discord.getTitle({ repo, branch, status: buildStatus }); - axios - .post(DISCORD_WEBHOOK_URL, { - embeds: [ - { - title, - color, - thumbnail, - fields: [ - inlineField('Repo')(repo), - inlineField('Branch')(branch), - inlineField('Status')(buildStatus), - addFeild()('See log')(logUrl), - addFeild()('Commit')(getMessage(result)), - ], - }, - ], - }) - .catch(e => { - console.error(e.response.message); - }); - }); + const thumbnail = discord.getThumbnail(buildStatus); + const author = { + name: githubAuthor.name, + icon_url: githubAuthor.avatarUrl, + }; + const embeds = [ + { + title, + color, + thumbnail, + author, + fields, + }, + ]; + + await sendDiscordMessage(embeds); + } catch (error) { + console.error(error); + } }; diff --git a/src/interfaces.ts b/src/interfaces.ts index 39d76a8..07ad606 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -78,6 +78,12 @@ export interface CommitResponse { }; } +export interface Author { + name: string; + avatarUrl: string; +} + export interface Commit { message: string; + author: Author; } From ce3b7bd508941e22ce71d18dbac78d4f4e874be1 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:51:14 +0700 Subject: [PATCH 4/8] =?UTF-8?q?=E2=9E=95=20momentjs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 5 +++++ package.json | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index dd01cd8..35d1cf2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -807,6 +807,11 @@ "brace-expansion": "^1.1.7" } }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index 5bc81c4..b416014 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "author": "", "license": "ISC", "dependencies": { - "axios": "^0.21.2" + "axios": "^0.21.2", + "moment": "^2.29.1" }, "devDependencies": { "@types/node": "^14.0.14", From b2e4e64786ac25486489c486c09ab114246447f7 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Mon, 15 Nov 2021 13:56:43 +0700 Subject: [PATCH 5/8] =?UTF-8?q?=E2=9C=A8=20commited=20date?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/github.ts | 1 + src/index.ts | 24 ++++++++++++++++++------ src/interfaces.ts | 3 ++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/github.ts b/src/github.ts index 32809c0..a596b99 100644 --- a/src/github.ts +++ b/src/github.ts @@ -26,6 +26,7 @@ export const fetchGitMessage = ({ object(oid: "${commit}") { ... on Commit { message + committedDate author { avatarUrl name diff --git a/src/index.ts b/src/index.ts index fbc053d..1fcdfbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ import axios from 'axios'; +import moment = require('moment'); import * as discord from './discord'; import { fetchGitMessage, getRepository } from './github'; import { CloudFunction, CloudFunctionEvent } from './interfaces'; +moment.locale('th'); + interface AddFieldReturn { - inline: boolean; + inline?: boolean; name: string; value: string; } @@ -13,7 +16,7 @@ const addField = (inline = false) => (nameOrValue: string, value?: string) => ({ inline, - name: value ? nameOrValue : '', + name: value ? nameOrValue : undefined, value: value || nameOrValue, }); @@ -44,8 +47,8 @@ export const subscribeDiscord: CloudFunction = async event => { if (!status.includes(build.status)) return; + const { substitutions, finishTime } = build; const buildStatus = build.status; - const substitutions = build.substitutions; const logUrl = build.logUrl; const branch = substitutions.BRANCH_NAME; const repo = substitutions.REPO_NAME; @@ -53,16 +56,24 @@ export const subscribeDiscord: CloudFunction = async event => { const owner = process.env.OWNER_NAME; const inlineField = addField(true); - const blockField = addField(); const log = discord.getUrl('See log', logUrl); try { const result = await fetchGitMessage({ commit, repo, owner }); - const { author: githubAuthor, message } = getRepository(result); + const { + author: githubAuthor, + message: description, + committedDate, + } = getRepository(result); + + const mDate = moment(committedDate); + const committedAt = mDate.calendar(); + const buildTime = finishTime ? moment(finishTime).fromNow(true) : 'N/A'; const fields = [ - blockField(message), + inlineField('Committed at', committedAt), + inlineField('Build time', buildTime), inlineField('Repo', repo), inlineField('Branch', branch), inlineField('Status', buildStatus), @@ -79,6 +90,7 @@ export const subscribeDiscord: CloudFunction = async event => { const embeds = [ { title, + description, color, thumbnail, author, diff --git a/src/interfaces.ts b/src/interfaces.ts index 07ad606..ea2cc94 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,4 +1,4 @@ -enum CloudFunctionStatus { +export enum CloudFunctionStatus { STATUS_UNKNOWN = 'STATUS_UNKNOWN', QUEUED = 'QUEUED', WORKING = 'WORKING', @@ -86,4 +86,5 @@ export interface Author { export interface Commit { message: string; author: Author; + committedDate: string; } From ca9e512510e095d956115a0ad42d649a224c415c Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Mon, 15 Nov 2021 17:08:26 +0700 Subject: [PATCH 6/8] =?UTF-8?q?=E2=9E=95=20moment-timezone=20+=20more=20in?= =?UTF-8?q?fos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 8 ++++++++ package.json | 3 ++- src/index.ts | 11 +++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35d1cf2..ebd47f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -812,6 +812,14 @@ "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" }, + "moment-timezone": { + "version": "0.5.34", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.34.tgz", + "integrity": "sha512-3zAEHh2hKUs3EXLESx/wsgw6IQdusOT8Bxm3D9UrHPQR7zlMmzwybC8zHEM1tQ4LJwP7fcxrWr8tuBg05fFCbg==", + "requires": { + "moment": ">= 2.9.0" + } + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index b416014..64d9396 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "license": "ISC", "dependencies": { "axios": "^0.21.2", - "moment": "^2.29.1" + "moment": "^2.29.1", + "moment-timezone": "^0.5.34" }, "devDependencies": { "@types/node": "^14.0.14", diff --git a/src/index.ts b/src/index.ts index 1fcdfbd..d7ce688 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import axios from 'axios'; -import moment = require('moment'); +import * as moment from 'moment-timezone'; import * as discord from './discord'; import { fetchGitMessage, getRepository } from './github'; import { CloudFunction, CloudFunctionEvent } from './interfaces'; @@ -47,7 +47,8 @@ export const subscribeDiscord: CloudFunction = async event => { if (!status.includes(build.status)) return; - const { substitutions, finishTime } = build; + const { substitutions, finishTime, startTime } = build; + const buildId = build.id.split('-')[0]; const buildStatus = build.status; const logUrl = build.logUrl; const branch = substitutions.BRANCH_NAME; @@ -67,13 +68,15 @@ export const subscribeDiscord: CloudFunction = async event => { committedDate, } = getRepository(result); + const BKK_TIMEZONE = 'Asia/Bangkok'; const mDate = moment(committedDate); - const committedAt = mDate.calendar(); - const buildTime = finishTime ? moment(finishTime).fromNow(true) : 'N/A'; + const committedAt = mDate.tz(BKK_TIMEZONE).calendar(); + const buildTime = finishTime ? moment(startTime).fromNow(true) : '-'; const fields = [ inlineField('Committed at', committedAt), inlineField('Build time', buildTime), + inlineField('Build ID', buildId), inlineField('Repo', repo), inlineField('Branch', branch), inlineField('Status', buildStatus), From 9526ad26151ef4a4cacc09020c67f767873f47b4 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Wed, 17 Nov 2021 17:47:33 +0700 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=90=9B=20support=20tag=20build?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/discord.ts | 6 +++--- src/index.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/discord.ts b/src/discord.ts index 0de4ba2..6e785ec 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -42,13 +42,13 @@ const capitalized = (textString: string) => { export const getTitle = ({ repo, - branch, + target, status, }: { repo: string; - branch: string; + target: string; status: string; -}): string => `${repo}(${branch}) is ${capitalized(status)}`; +}): string => `${repo}(${target}) is ${capitalized(status)}`; export const getUrl = (message: string, url: string): string => `[${message}](${url})`; diff --git a/src/index.ts b/src/index.ts index d7ce688..85786ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,6 +52,7 @@ export const subscribeDiscord: CloudFunction = async event => { const buildStatus = build.status; const logUrl = build.logUrl; const branch = substitutions.BRANCH_NAME; + const tag = substitutions.TAG_NAME; const repo = substitutions.REPO_NAME; const commit = substitutions.COMMIT_SHA; const owner = process.env.OWNER_NAME; @@ -72,19 +73,22 @@ export const subscribeDiscord: CloudFunction = async event => { const mDate = moment(committedDate); const committedAt = mDate.tz(BKK_TIMEZONE).calendar(); const buildTime = finishTime ? moment(startTime).fromNow(true) : '-'; + const inlineBranch = inlineField('Branch', branch); + const inlineTag = inlineField('Tag', tag); + const target = branch || tag; const fields = [ inlineField('Committed at', committedAt), inlineField('Build time', buildTime), inlineField('Build ID', buildId), inlineField('Repo', repo), - inlineField('Branch', branch), + branch ? inlineBranch : inlineTag, inlineField('Status', buildStatus), inlineField('Log', log), ]; const color = discord.getColor(buildStatus); - const title = discord.getTitle({ repo, branch, status: buildStatus }); + const title = discord.getTitle({ repo, target, status: buildStatus }); const thumbnail = discord.getThumbnail(buildStatus); const author = { name: githubAuthor.name, From c273b13f74bf8497a617999b689dd05d4c096d06 Mon Sep 17 00:00:00 2001 From: Itchakhun <16811182+itchakhun@users.noreply.github.com> Date: Tue, 30 Nov 2021 11:58:52 +0700 Subject: [PATCH 8/8] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20handle=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/github.ts | 22 +++++++++++++++------- src/index.ts | 13 +++++++++---- src/interfaces.ts | 8 ++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/github.ts b/src/github.ts index a596b99..a874053 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,8 +1,14 @@ import axios, { AxiosPromise, AxiosResponse } from 'axios'; -import { CommitResponse } from './interfaces'; +import { CommitResponse, ErrorResponse } from './interfaces'; type Repo = CommitResponse['data']['repository']['object']; +export const isError = ( + payload: AxiosResponse +): payload is AxiosResponse => { + return 'errors' in payload.data; +}; + export const getRepository = (result: AxiosResponse): Repo => { try { return result.data.data.repository.object; @@ -17,11 +23,8 @@ export const fetchGitMessage = ({ owner, }: { [k: string]: string; -}): AxiosPromise => { - return axios.post( - 'https://api.github.com/graphql', - { - query: `{ +}): AxiosPromise => { + const query = `{ repository(owner:"${owner}",name:"${repo}") { object(oid: "${commit}") { ... on Commit { @@ -34,7 +37,12 @@ export const fetchGitMessage = ({ } } } - }`, + }`; + console.log({ query }); + return axios.post( + 'https://api.github.com/graphql', + { + query, }, { headers: { diff --git a/src/index.ts b/src/index.ts index 85786ad..a48f017 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,12 @@ -import axios from 'axios'; +import axios, { AxiosResponse } from 'axios'; import * as moment from 'moment-timezone'; import * as discord from './discord'; -import { fetchGitMessage, getRepository } from './github'; -import { CloudFunction, CloudFunctionEvent } from './interfaces'; +import { fetchGitMessage, getRepository, isError } from './github'; +import { + CloudFunction, + CloudFunctionEvent, + CommitResponse, +} from './interfaces'; moment.locale('th'); @@ -63,11 +67,12 @@ export const subscribeDiscord: CloudFunction = async event => { try { const result = await fetchGitMessage({ commit, repo, owner }); + if (isError(result)) throw new Error(result.data.errors[0].message); const { author: githubAuthor, message: description, committedDate, - } = getRepository(result); + } = getRepository(result as AxiosResponse); const BKK_TIMEZONE = 'Asia/Bangkok'; const mDate = moment(committedDate); diff --git a/src/interfaces.ts b/src/interfaces.ts index ea2cc94..1c82512 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -78,6 +78,14 @@ export interface CommitResponse { }; } +interface Error { + message: string; +} + +export interface ErrorResponse { + errors: Error[]; +} + export interface Author { name: string; avatarUrl: string;