mirror of
https://github.com/OPSnet/GithubBot.git
synced 2026-01-16 18:04:19 -05:00
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
This commit is contained in:
42
server.js
42
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();
|
||||
start();
|
||||
|
||||
Reference in New Issue
Block a user