diff --git a/README.md b/README.md index 22a4df2..1c256e1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The goal of this action is to produce helpful messages on your Discord server ba - [x] Release - [ ] Pull Request -- [ ] Push (branches / tags) +- [x] Push (branches / tags) - [ ] Page build ## Getting Started: @@ -21,8 +21,10 @@ The webhook URL to use. This should be in a repository secret and the secret sho #### `handler` Handler defines background logic of the action. Currently avaiable handlers: - `release`: Publishes message to your discord server when release is created. + - `push`: Publishes message to your discord server when push action occurs. --- ### Example +#### Release Handler ```yaml name: Discord Webhook Messages on: @@ -39,6 +41,24 @@ jobs: webhookUrl: ${{ secrets.DISCORD_WEBHOOK }} handler: 'release' ``` +#### Push Handler +```yaml +name: Discord Webhook Messages +on: + push: + branches: + - '*' +jobs: + run: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Run Discord Webhook + uses: bythope/discord-webhook-messages@v1.0.0 + with: + webhookUrl: ${{ secrets.DISCORD_WEBHOOK }} + handler: 'push' +``` ## Contributing diff --git a/dist/index.js b/dist/index.js index b19c379..90e82c6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -36366,96 +36366,151 @@ function socketOnError() { /***/ 5523: /***/ ((module, __unused_webpack_exports, __webpack_require__) => { -const core = __webpack_require__(2186) - -class HandlerExecutor { - - constructor(input) { - this.handlers = {} - this.input = input - } - - add(key, handler) { - if (typeof this.handlers[key] === 'function') { - console.error(`Handler ${key} already exists!`) - } - if (typeof handler !== 'function') { - console.error(`Handler ${key} can not be added. handler must be a function`) - } - this.handlers[key] = handler - } - - execute(key) { - if (typeof this.handlers[key] !== 'function') { - core.setFailed(`Handler of type ${key} is not supported, available handlers: ${Object.keys(this.handlers).join(', ')}`) - return - } - const promise = this.handlers[key](this.input) - if (typeof promise.then !== 'function') { - console.error(`Handler ${key} must return promise!`) - return - } - - promise.then(result => { - core.setOutput('data', result) - }).catch(error => { - core.setFailed(`Handler ${key} failed. Reason: ${error.message}`) - console.error(error) - }) - } - -} - +const core = __webpack_require__(2186) + +class HandlerExecutor { + + constructor(input) { + this.handlers = {} + this.input = input + } + + add(key, handler) { + if (typeof this.handlers[key] === 'function') { + console.error(`Handler ${key} already exists!`) + } + if (typeof handler !== 'function') { + console.error(`Handler ${key} can not be added. handler must be a function`) + } + this.handlers[key] = handler + } + + execute(key) { + if (typeof this.handlers[key] !== 'function') { + core.setFailed(`Handler of type ${key} is not supported, available handlers: ${Object.keys(this.handlers).join(', ')}`) + return + } + const promise = this.handlers[key](this.input) + if (typeof promise.then !== 'function') { + console.error(`Handler ${key} must return promise!`) + return + } + + promise.then(result => { + core.setOutput('data', result) + }).catch(error => { + core.setFailed(`Handler ${key} failed. Reason: ${error.message}`) + console.error(error) + }) + } + +} + module.exports = HandlerExecutor /***/ }), -/***/ 285: +/***/ 5172: /***/ ((module, __unused_webpack_exports, __webpack_require__) => { -const core = __webpack_require__(2186) -const github = __webpack_require__(5438) -const { WebhookClient, MessageEmbed } = __webpack_require__(5973) -const { extractDataFromWebhookUrl } = __webpack_require__(8505) - -module.exports = ({ webhookUrl }) => { - const { payload, eventName } = github.context - - if (eventName !== 'release') { - console.warn('release handler can be executed only on "release" action triggers') - return Promise.resolve() - } - const { action, release: { body, draft, html_url, name, prerelease, published_at, tag_name, target_commitish, } } = payload - const data = { action, name, body, tag: tag_name, url: html_url, draft, prerelease, published: published_at, branch: target_commitish } - - const { id, token } = extractDataFromWebhookUrl(webhookUrl) - const client = new WebhookClient(id, token) - - let embed = createEmbed(data) - return client.send(embed).then(result => { - client.destroy() - return data - }).catch(error => { - client.destroy() - throw error - }) +const core = __webpack_require__(2186) +const github = __webpack_require__(5438) +const { WebhookClient, MessageEmbed } = __webpack_require__(5973) +const { extractDataFromWebhookUrl } = __webpack_require__(8505) + +module.exports = ({ webhookUrl }) => { + const { payload, eventName } = github.context + + if (eventName !== 'push') { + console.warn('push handler can be executed only on "push" action triggers') + return Promise.resolve() + } + + const {commits, repository: {language}, sender: {login}} = payload + const data = {commits, language, login} + + const { id, token } = extractDataFromWebhookUrl(webhookUrl) + const client = new WebhookClient(id, token) + + return client.send(createEmbed(data)).then(result => { + client.destroy() + return '' + }).catch(error => { + client.destroy() + throw error + }) +} + + +function createEmbed({ commits, language, login}) { + let embed = new MessageEmbed({ type: 'rich' }) + let description = createDescription(commits, language) + embed.setColor(0x32ecab) + embed.setTitle(login + ' pushed some changes') + embed.setDescription(description) + embed.setFooter(`Number of commits: ${commits.length}`) + embed.setTimestamp(new Date(commits[commits.length-1].timestamp)) + return embed +} + + +function createDescription(commits, language) { + let description = 'Writen in: ' + language + '\n' + + 'Following commits were added: \n' + for(let commit of commits){ + description += `\`${commit.id.substring(0, 6)}\` - ` + `**${commit.message}**` + '\n' + } + return description } +/***/ }), -function createEmbed({ action, name, body, tag, url, draft, prerelease, published, branch }) { - let embed = new MessageEmbed({ type: 'rich' }) - embed.setColor(prerelease ? 0xf66a0a : 0x28a745) - embed.setTitle(`${prerelease ? 'Pre-release' : 'Release'}: ${tag} ${name} ${draft ? '(Draft)': ''}`) - embed.setURL(url) - embed.setDescription(`${trimBody(body)}`) - embed.setFooter(`Branch: ${branch}`) - embed.setTimestamp(new Date(published)) - return embed -} +/***/ 285: +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { -function trimBody(body = '') { - if (body.length < 2000) return body - return `${body.substring(0, 2000)}...` +const core = __webpack_require__(2186) +const github = __webpack_require__(5438) +const { WebhookClient, MessageEmbed } = __webpack_require__(5973) +const { extractDataFromWebhookUrl } = __webpack_require__(8505) + +module.exports = ({ webhookUrl }) => { + const { payload, eventName } = github.context + + if (eventName !== 'release') { + console.warn('release handler can be executed only on "release" action triggers') + return Promise.resolve() + } + const { action, release: { body, draft, html_url, name, prerelease, published_at, tag_name, target_commitish, } } = payload + const data = { action, name, body, tag: tag_name, url: html_url, draft, prerelease, published: published_at, branch: target_commitish } + + const { id, token } = extractDataFromWebhookUrl(webhookUrl) + const client = new WebhookClient(id, token) + + let embed = createEmbed(data) + return client.send(embed).then(result => { + client.destroy() + return data + }).catch(error => { + client.destroy() + throw error + }) +} + + +function createEmbed({ action, name, body, tag, url, draft, prerelease, published, branch }) { + let embed = new MessageEmbed({ type: 'rich' }) + embed.setColor(prerelease ? 0xf66a0a : 0x28a745) + embed.setTitle(`${prerelease ? 'Pre-release' : 'Release'}: ${tag} ${name} ${draft ? '(Draft)': ''}`) + embed.setURL(url) + embed.setDescription(`${trimBody(body)}`) + embed.setFooter(`Branch: ${branch}`) + embed.setTimestamp(new Date(published)) + return embed +} + +function trimBody(body = '') { + if (body.length < 2000) return body + return `${body.substring(0, 2000)}...` } /***/ }), @@ -36463,27 +36518,27 @@ function trimBody(body = '') { /***/ 8505: /***/ ((__unused_webpack_module, exports) => { -const isWebhookUrl = url => { - let regExp = new RegExp(/https:\/\/discord(app|)\.com\/api\/webhooks\/\d+?\/.+/i) - return regExp.exec(url) -} - -const extractDataFromWebhookUrl = (url = "") => { - if (isWebhookUrl(url)) { - let regExp = new RegExp(/discordapp.com\/api\/webhooks\/([^\/]+)\/([^\/]+)/) - let [ _, id, token ] = url.match(regExp) - return { id, token } - } else { - return null - } -} - -const interpolate = (string, object) => { - return string -} - -exports.isWebhookUrl = isWebhookUrl -exports.extractDataFromWebhookUrl = extractDataFromWebhookUrl +const isWebhookUrl = url => { + let regExp = new RegExp(/https:\/\/discord(app|)\.com\/api\/webhooks\/\d+?\/.+/i) + return regExp.exec(url) +} + +const extractDataFromWebhookUrl = (url = "") => { + if (isWebhookUrl(url)) { + let regExp = new RegExp(/discordapp.com\/api\/webhooks\/([^\/]+)\/([^\/]+)/) + let [ _, id, token ] = url.match(regExp) + return { id, token } + } else { + return null + } +} + +const interpolate = (string, object) => { + return string +} + +exports.isWebhookUrl = isWebhookUrl +exports.extractDataFromWebhookUrl = extractDataFromWebhookUrl exports.interpolate = interpolate /***/ }), @@ -36491,25 +36546,27 @@ exports.interpolate = interpolate /***/ 4351: /***/ ((__unused_webpack_module, __unused_webpack_exports, __webpack_require__) => { -const core = __webpack_require__(2186) -const { isWebhookUrl } = __webpack_require__(8505) -const HandlerExecutor = __webpack_require__(5523) -const releaseHandler = __webpack_require__(285) - - - -const input = { - webhookUrl: core.getInput('webhookUrl'), - handler: core.getInput('handler') -} - -const handlerExecutor = new HandlerExecutor(input) -handlerExecutor.add('release', releaseHandler) - -if (!isWebhookUrl(input.webhookUrl)) { - core.setFailed('The given webhook url is not valid. Please ensure you give a full discord webhook url') -} - +const core = __webpack_require__(2186) +const { isWebhookUrl } = __webpack_require__(8505) +const HandlerExecutor = __webpack_require__(5523) +const releaseHandler = __webpack_require__(285) +const pushHandler = __webpack_require__(5172) + + + +const input = { + webhookUrl: core.getInput('webhookUrl'), + handler: core.getInput('handler') +} + +const handlerExecutor = new HandlerExecutor(input) +handlerExecutor.add('release', releaseHandler) +handlerExecutor.add('push', pushHandler) + +if (!isWebhookUrl(input.webhookUrl)) { + core.setFailed('The given webhook url is not valid. Please ensure you give a full discord webhook url') +} + handlerExecutor.execute(input.handler) /***/ }), @@ -36566,7 +36623,7 @@ module.exports = eval("require")("zlib-sync"); /***/ ((module) => { "use strict"; -module.exports = JSON.parse("{\"_from\":\"discord.js\",\"_id\":\"discord.js@12.3.1\",\"_inBundle\":false,\"_integrity\":\"sha512-mSFyV/mbvzH12UXdS4zadmeUf8IMQOo/YdunubG1wWt1xjWvtaJz/s9CGsFD2B5pTw1W/LXxxUbrQjIZ/xlUdw==\",\"_location\":\"/discord.js\",\"_phantomChildren\":{},\"_requested\":{\"type\":\"tag\",\"registry\":true,\"raw\":\"discord.js\",\"name\":\"discord.js\",\"escapedName\":\"discord.js\",\"rawSpec\":\"\",\"saveSpec\":null,\"fetchSpec\":\"latest\"},\"_requiredBy\":[\"#USER\",\"/\"],\"_resolved\":\"https://registry.npmjs.org/discord.js/-/discord.js-12.3.1.tgz\",\"_shasum\":\"8f58ac17d29b068dbfeb6c01fc7777bacd5324cf\",\"_spec\":\"discord.js\",\"_where\":\"/home/kalevski/projects/bythope/discord-webhook\",\"author\":{\"name\":\"Amish Shah\",\"email\":\"amishshah.2k@gmail.com\"},\"browser\":{\"@discordjs/opus\":false,\"https\":false,\"ws\":false,\"erlpack\":false,\"prism-media\":false,\"opusscript\":false,\"node-opus\":false,\"tweetnacl\":false,\"sodium\":false,\"worker_threads\":false,\"zlib-sync\":false,\"src/sharding/Shard.js\":false,\"src/sharding/ShardClientUtil.js\":false,\"src/sharding/ShardingManager.js\":false,\"src/client/voice/ClientVoiceManager.js\":false,\"src/client/voice/VoiceBroadcast.js\":false,\"src/client/voice/VoiceConnection.js\":false,\"src/client/voice/dispatcher/BroadcastDispatcher.js\":false,\"src/client/voice/dispatcher/StreamDispatcher.js\":false,\"src/client/voice/networking/VoiceUDPClient.js\":false,\"src/client/voice/networking/VoiceWebSocket.js\":false,\"src/client/voice/player/AudioPlayer.js\":false,\"src/client/voice/player/BasePlayer.js\":false,\"src/client/voice/player/BroadcastAudioPlayer.js\":false,\"src/client/voice/receiver/PacketHandler.js\":false,\"src/client/voice/receiver/Receiver.js\":false,\"src/client/voice/util/PlayInterface.js\":false,\"src/client/voice/util/Secretbox.js\":false,\"src/client/voice/util/Silence.js\":false,\"src/client/voice/util/VolumeInterface.js\":false},\"bugs\":{\"url\":\"https://github.com/discordjs/discord.js/issues\"},\"bundleDependencies\":false,\"commitlint\":{\"extends\":[\"@commitlint/config-angular\"],\"rules\":{\"scope-case\":[2,\"always\",\"pascal-case\"],\"type-enum\":[2,\"always\",[\"chore\",\"build\",\"ci\",\"docs\",\"feat\",\"fix\",\"perf\",\"refactor\",\"revert\",\"style\",\"test\"]]}},\"dependencies\":{\"@discordjs/collection\":\"^0.1.6\",\"@discordjs/form-data\":\"^3.0.1\",\"abort-controller\":\"^3.0.0\",\"node-fetch\":\"^2.6.0\",\"prism-media\":\"^1.2.2\",\"setimmediate\":\"^1.0.5\",\"tweetnacl\":\"^1.0.3\",\"ws\":\"^7.3.1\"},\"deprecated\":false,\"description\":\"A powerful library for interacting with the Discord API\",\"devDependencies\":{\"@commitlint/cli\":\"^9.1.2\",\"@commitlint/config-angular\":\"^9.1.1\",\"@types/node\":\"^12.12.6\",\"@types/ws\":\"^7.2.6\",\"cross-env\":\"^7.0.2\",\"discord.js-docgen\":\"github:discordjs/docgen\",\"dtslint\":\"^3.6.14\",\"eslint\":\"^7.6.0\",\"eslint-config-prettier\":\"^6.11.0\",\"eslint-plugin-import\":\"^2.22.0\",\"eslint-plugin-prettier\":\"^3.1.4\",\"husky\":\"^4.2.5\",\"jest\":\"^26.4.0\",\"json-filter-loader\":\"^1.0.0\",\"lint-staged\":\"^10.2.11\",\"prettier\":\"^2.0.5\",\"terser-webpack-plugin\":\"^4.1.0\",\"tslint\":\"^6.1.3\",\"typescript\":\"^3.9.7\",\"webpack\":\"^4.44.1\",\"webpack-cli\":\"^3.3.12\"},\"engines\":{\"node\":\">=12.0.0\"},\"exports\":{\".\":[{\"require\":\"./src/index.js\",\"import\":\"./esm/discord.mjs\"},\"./src/index.js\"],\"./esm\":\"./esm/discord.mjs\"},\"homepage\":\"https://github.com/discordjs/discord.js#readme\",\"husky\":{\"hooks\":{\"pre-commit\":\"lint-staged\",\"commit-msg\":\"commitlint -E HUSKY_GIT_PARAMS\"}},\"keywords\":[\"discord\",\"api\",\"bot\",\"client\",\"node\",\"discordapp\"],\"license\":\"Apache-2.0\",\"lint-staged\":{\"*.js\":\"eslint --fix\",\"*.ts\":\"prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf --arrow-parens avoid\"},\"main\":\"./src/index\",\"name\":\"discord.js\",\"repository\":{\"type\":\"git\",\"url\":\"git+https://github.com/discordjs/discord.js.git\"},\"runkitExampleFilename\":\"./docs/examples/ping.js\",\"scripts\":{\"build:browser\":\"webpack\",\"docs\":\"docgen --source src --custom docs/index.yml --output docs/docs.json\",\"docs:test\":\"docgen --source src --custom docs/index.yml\",\"lint\":\"eslint src\",\"lint:fix\":\"eslint src --fix\",\"lint:typings\":\"tslint typings/index.d.ts\",\"prepublishOnly\":\"npm run test && cross-env NODE_ENV=production npm run build:browser\",\"prettier\":\"prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf --arrow-parens avoid src/**/*.js typings/**/*.ts\",\"test\":\"npm run lint && npm run docs:test && npm run lint:typings\",\"test:typescript\":\"tsc\"},\"types\":\"./typings/index.d.ts\",\"unpkg\":\"./webpack/discord.min.js\",\"version\":\"12.3.1\"}"); +module.exports = JSON.parse("{\"_args\":[[\"discord.js@12.3.1\",\"D:\\\\filip\\\\programiranje\\\\Repo\\\\discord-webhook-messages\"]],\"_from\":\"discord.js@12.3.1\",\"_id\":\"discord.js@12.3.1\",\"_inBundle\":false,\"_integrity\":\"sha512-mSFyV/mbvzH12UXdS4zadmeUf8IMQOo/YdunubG1wWt1xjWvtaJz/s9CGsFD2B5pTw1W/LXxxUbrQjIZ/xlUdw==\",\"_location\":\"/discord.js\",\"_phantomChildren\":{},\"_requested\":{\"type\":\"version\",\"registry\":true,\"raw\":\"discord.js@12.3.1\",\"name\":\"discord.js\",\"escapedName\":\"discord.js\",\"rawSpec\":\"12.3.1\",\"saveSpec\":null,\"fetchSpec\":\"12.3.1\"},\"_requiredBy\":[\"/\"],\"_resolved\":\"https://registry.npmjs.org/discord.js/-/discord.js-12.3.1.tgz\",\"_spec\":\"12.3.1\",\"_where\":\"D:\\\\filip\\\\programiranje\\\\Repo\\\\discord-webhook-messages\",\"author\":{\"name\":\"Amish Shah\",\"email\":\"amishshah.2k@gmail.com\"},\"browser\":{\"@discordjs/opus\":false,\"https\":false,\"ws\":false,\"erlpack\":false,\"prism-media\":false,\"opusscript\":false,\"node-opus\":false,\"tweetnacl\":false,\"sodium\":false,\"worker_threads\":false,\"zlib-sync\":false,\"src/sharding/Shard.js\":false,\"src/sharding/ShardClientUtil.js\":false,\"src/sharding/ShardingManager.js\":false,\"src/client/voice/ClientVoiceManager.js\":false,\"src/client/voice/VoiceBroadcast.js\":false,\"src/client/voice/VoiceConnection.js\":false,\"src/client/voice/dispatcher/BroadcastDispatcher.js\":false,\"src/client/voice/dispatcher/StreamDispatcher.js\":false,\"src/client/voice/networking/VoiceUDPClient.js\":false,\"src/client/voice/networking/VoiceWebSocket.js\":false,\"src/client/voice/player/AudioPlayer.js\":false,\"src/client/voice/player/BasePlayer.js\":false,\"src/client/voice/player/BroadcastAudioPlayer.js\":false,\"src/client/voice/receiver/PacketHandler.js\":false,\"src/client/voice/receiver/Receiver.js\":false,\"src/client/voice/util/PlayInterface.js\":false,\"src/client/voice/util/Secretbox.js\":false,\"src/client/voice/util/Silence.js\":false,\"src/client/voice/util/VolumeInterface.js\":false},\"bugs\":{\"url\":\"https://github.com/discordjs/discord.js/issues\"},\"commitlint\":{\"extends\":[\"@commitlint/config-angular\"],\"rules\":{\"scope-case\":[2,\"always\",\"pascal-case\"],\"type-enum\":[2,\"always\",[\"chore\",\"build\",\"ci\",\"docs\",\"feat\",\"fix\",\"perf\",\"refactor\",\"revert\",\"style\",\"test\"]]}},\"dependencies\":{\"@discordjs/collection\":\"^0.1.6\",\"@discordjs/form-data\":\"^3.0.1\",\"abort-controller\":\"^3.0.0\",\"node-fetch\":\"^2.6.0\",\"prism-media\":\"^1.2.2\",\"setimmediate\":\"^1.0.5\",\"tweetnacl\":\"^1.0.3\",\"ws\":\"^7.3.1\"},\"description\":\"A powerful library for interacting with the Discord API\",\"devDependencies\":{\"@commitlint/cli\":\"^9.1.2\",\"@commitlint/config-angular\":\"^9.1.1\",\"@types/node\":\"^12.12.6\",\"@types/ws\":\"^7.2.6\",\"cross-env\":\"^7.0.2\",\"discord.js-docgen\":\"github:discordjs/docgen\",\"dtslint\":\"^3.6.14\",\"eslint\":\"^7.6.0\",\"eslint-config-prettier\":\"^6.11.0\",\"eslint-plugin-import\":\"^2.22.0\",\"eslint-plugin-prettier\":\"^3.1.4\",\"husky\":\"^4.2.5\",\"jest\":\"^26.4.0\",\"json-filter-loader\":\"^1.0.0\",\"lint-staged\":\"^10.2.11\",\"prettier\":\"^2.0.5\",\"terser-webpack-plugin\":\"^4.1.0\",\"tslint\":\"^6.1.3\",\"typescript\":\"^3.9.7\",\"webpack\":\"^4.44.1\",\"webpack-cli\":\"^3.3.12\"},\"engines\":{\"node\":\">=12.0.0\"},\"exports\":{\".\":[{\"require\":\"./src/index.js\",\"import\":\"./esm/discord.mjs\"},\"./src/index.js\"],\"./esm\":\"./esm/discord.mjs\"},\"homepage\":\"https://github.com/discordjs/discord.js#readme\",\"husky\":{\"hooks\":{\"pre-commit\":\"lint-staged\",\"commit-msg\":\"commitlint -E HUSKY_GIT_PARAMS\"}},\"keywords\":[\"discord\",\"api\",\"bot\",\"client\",\"node\",\"discordapp\"],\"license\":\"Apache-2.0\",\"lint-staged\":{\"*.js\":\"eslint --fix\",\"*.ts\":\"prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf --arrow-parens avoid\"},\"main\":\"./src/index\",\"name\":\"discord.js\",\"repository\":{\"type\":\"git\",\"url\":\"git+https://github.com/discordjs/discord.js.git\"},\"runkitExampleFilename\":\"./docs/examples/ping.js\",\"scripts\":{\"build:browser\":\"webpack\",\"docs\":\"docgen --source src --custom docs/index.yml --output docs/docs.json\",\"docs:test\":\"docgen --source src --custom docs/index.yml\",\"lint\":\"eslint src\",\"lint:fix\":\"eslint src --fix\",\"lint:typings\":\"tslint typings/index.d.ts\",\"prepublishOnly\":\"npm run test && cross-env NODE_ENV=production npm run build:browser\",\"prettier\":\"prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf --arrow-parens avoid src/**/*.js typings/**/*.ts\",\"test\":\"npm run lint && npm run docs:test && npm run lint:typings\",\"test:typescript\":\"tsc\"},\"types\":\"./typings/index.d.ts\",\"unpkg\":\"./webpack/discord.min.js\",\"version\":\"12.3.1\"}"); /***/ }), diff --git a/src/handlers/pushHandler.js b/src/handlers/pushHandler.js new file mode 100644 index 0000000..b3ec6ab --- /dev/null +++ b/src/handlers/pushHandler.js @@ -0,0 +1,49 @@ +const core = require('@actions/core') +const github = require('@actions/github') +const { WebhookClient, MessageEmbed } = require('discord.js') +const { extractDataFromWebhookUrl } = require('../helpers') + +module.exports = ({ webhookUrl }) => { + const { payload, eventName } = github.context + + if (eventName !== 'push') { + console.warn('push handler can be executed only on "push" action triggers') + return Promise.resolve() + } + + const {commits, repository: {language}, sender: {login}} = payload + const data = {commits, language, login} + + const { id, token } = extractDataFromWebhookUrl(webhookUrl) + const client = new WebhookClient(id, token) + + return client.send(createEmbed(data)).then(result => { + client.destroy() + return '' + }).catch(error => { + client.destroy() + throw error + }) +} + + +function createEmbed({ commits, language, login}) { + let embed = new MessageEmbed({ type: 'rich' }) + let description = createDescription(commits, language) + embed.setColor(0x32ecab) + embed.setTitle(login + ' pushed some changes') + embed.setDescription(description) + embed.setFooter(`Number of commits: ${commits.length}`) + embed.setTimestamp(new Date(commits[commits.length-1].timestamp)) + return embed +} + + +function createDescription(commits, language) { + let description = 'Writen in: ' + language + '\n' + + 'Following commits were added: \n' + for(let commit of commits){ + description += `\`${commit.id.substring(0, 6)}\` - ` + `**${commit.message}**` + '\n' + } + return description +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 945fef5..0edc1ff 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const core = require('@actions/core') const { isWebhookUrl } = require('./helpers') const HandlerExecutor = require('./HandlerExecutor') const releaseHandler = require('./handlers/releaseHandler') +const pushHandler = require('./handlers/pushHandler') @@ -12,6 +13,7 @@ const input = { const handlerExecutor = new HandlerExecutor(input) handlerExecutor.add('release', releaseHandler) +handlerExecutor.add('push', pushHandler) if (!isWebhookUrl(input.webhookUrl)) { core.setFailed('The given webhook url is not valid. Please ensure you give a full discord webhook url')