From 4531ac84269f0a31c14f200d73d72b8fb09b3711 Mon Sep 17 00:00:00 2001 From: itismadness Date: Thu, 8 Nov 2018 21:56:50 +0530 Subject: [PATCH] Improvements for IRC message for new commits and other cleanup - Only print out first 50 characters of first line of commit - Don't print out "Github event" message for unknown message types - Shorten GitHub commit url to only include first 7 characters of git hash - Some code style cleanup --- server.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/server.js b/server.js index c97665b..47f5508 100644 --- a/server.js +++ b/server.js @@ -14,60 +14,58 @@ client.addListener('registered', () => { } }); -let messages = [] -function handle_issue(body) { - // Format: - // Gazelle - itismadness opened issue #59 - Test Issue... | https://github.com/OPSnet/Gazelle/issues/59 +function handle_issue(body, messages) { let message = `${body.repository.name} - ${body.sender.login} ${body.action} issue #${body.issue.number}`; message += ` - ${body.issue.title} | ${body.issue.html_url}`; - + // TODO: handle comments messages.push(message); } -function handle_commits(body) { +function handle_commits(body, messages) { let ref = body.ref.split('/'); - let branch = ref[ref.length-1]; + let branch = ref[ref.length - 1]; if (branch !== body.repository.master_branch) { return; } for (let commit of body.commits) { let id = commit.id.substr(0, 7); let message = `${body.repository.name} - ${commit.author.name} just pushed commit ${id} to ${branch}`; - message += ` - ${commit.message} | ${commit.url}`; + message += ` - ${commit.message.split(/\r?\n/)[0].substr(0, 50).trim()} | ${commit.url.replace(commit.id, id)}`; messages.push(message); } } -fastify.get('/', async (req, reply) => { - return { hello: 'world' } +fastify.get('/', async () => { + return { hello: 'world' }; }); -fastify.post('/', async (req, reply) => { +fastify.post('/', async (req) => { let body = req.body; - + let event = req.headers['x-github-event']; - messages = []; + let messages = []; if (event === 'issues') { - handle_issue(body); + handle_issue(body, messages); } else if (event === 'push') { - handle_commits(body); + handle_commits(body, messages); } else { - console.log(JSON.stringify(body, null, 2)); - console.log(req.headers); - let message = `New Github Event ${event} by ${body.sender.login}`; - messages.push(message); + // View these events within GitHub to see headers and body + // console.log(JSON.stringify(body, null, 2)); + // console.log(req.headers); + // let message = `New Github Event ${event} by ${body.sender.login}`; + // messages.push(message); } - for (channel of settings.channels) { + for (let channel of settings.channels) { for (let message of messages) { client.say(channel, message); } } - messages = []; + return { message: 'posted' }; }); @@ -81,4 +79,4 @@ const start = async () => { }); }; -start(); \ No newline at end of file +start();