Skip to content

Commit

Permalink
Allow for job events, fix conf events command, improve url parser, sh…
Browse files Browse the repository at this point in the history
…ow full commit message if just 1 commit

Fixes #43, fixes #29, refs #52
  • Loading branch information
dsevillamartin committed Nov 28, 2019
1 parent 41884cb commit 65f2586
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 48 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Yappy Discord Bots
Copyright (c) 2019 David Sevilla Martin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 18 additions & 15 deletions lib/Discord/Commands/Conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class ConfCommand extends Command {

if (action === Actions.INVALID) {
if (!a) {
const events = conf.disabledEvents || [];
const events = conf.get('eventsList');
const embed = new this.embed().setColor('#84F139').setTitle(`#${msg.channel.name}'s disabled events`);

if (!events || !events.length) embed.setDescription('No disabled events found.');
Expand All @@ -232,7 +232,7 @@ class ConfCommand extends Command {
} else if (a.toLowerCase() === 'list') {
const embed = new this.embed().setTitle('List of available events');

EventHandler.eventsList.forEach((evt, name) => embed.addField('\u200B', `\`${name.replace('-', '/')}\``, true));
EventHandler.eventsList.forEach((evt, name) => name !== 'Unknown' && embed.addField('\u200B', `\`${name.replace('-', '/')}\``, true));

return msg.channel.send({ embed });
} else {
Expand All @@ -244,25 +244,28 @@ class ConfCommand extends Command {
}
}

let events = conf.disabledEvents;
let events = conf.get('eventsList');

if (action === Actions.DISABLE && !events.includes(key)) events.push(key);
else if (action === Actions.ENABLE) events = events.filter(e => e !== key);

return conf.set('disabledEvents', events).then(() => {
const embed = new this.embed()
.setColor('#84F139')
.setTitle(`Successfully updated #${msg.channel.name}'s disabled events`)
.setDescription([
`${action === Actions.ENABLE ? 'Enabled' : 'Disabled'} \`${key}\`.`,
'',
events.length ? 'Disabled events:\n' : 'No events are disabled.',
]);
return conf
.set('eventsList', events)
.save()
.then(() => {
const embed = new this.embed()
.setColor('#84F139')
.setTitle(`Successfully updated #${msg.channel.name}'s disabled events`)
.setDescription([
`${action === Actions.ENABLE ? 'Enabled' : 'Disabled'} \`${key}\`.`,
'',
events.length ? 'Disabled events:\n' : 'No events are disabled.',
]);

events.forEach(e => embed.addField(e, '\u200B', true));
events.forEach(e => embed.addField(e, '\u200B', true));

return msg.channel.send({ embed });
});
return msg.channel.send({ embed });
});
}

format(val, isBoolean) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Gitlab/EventHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const bot = require('../Discord/index.js');
const get = require('lodash/get');
const fs = require('fs');
const path = require('path');
const bot = require('../Discord');
const Log = require('../Util/Log');
const parser = require('../Gitlab/parser');

class Events {
constructor() {
Expand Down Expand Up @@ -81,7 +83,7 @@ class Events {
icon_url: avatar && avatar.startsWith('/') ? `https://gitlab.com${avatar}` : avatar,
};
embed.footer = {
text: data.project.path_with_namespace,
text: get(data, 'project.path_with_namespace') || parser.getRepo(get(data, 'repository.url')),
};
embed.url = embed.url || (data.object_attributes && data.object_attributes.url) || (data.project && data.project.web_url);
embed.timestamp = new Date();
Expand Down
2 changes: 1 addition & 1 deletion lib/Gitlab/Events/Unknown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Unkown extends EventResponse {
return {
color: 'danger',
title: `Repository sent unknown event: \`${eventName}${action}\``,
description: `This most likely means the developers have not gotten to styling this event.\nYou may want to disable this event if you don't want it with \`GL! conf events disable ${eventName}${action}\``,
description: `This most likely means the developers have not gotten to styling this event.\nYou may want to disable this event if you don't want it with \`GL! conf filter events disable ${eventName}${action}\``,
};
}
text(data, eventName, actionName) {
Expand Down
20 changes: 13 additions & 7 deletions lib/Gitlab/Events/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class Push extends EventResponse {
const commitCount = commits.length;
let pretext = commits
.map(commit => {
let commitMessage = commit.message.split('\n')[0].replace(UrlRegEx, RemoveUrlEmbedding);
let author = commit.author.name || data.user_username || data.user_name;
let sha = commit.id.slice(0, 7);
return `[\`${sha}\`](${commit.url}) ${commitMessage} [${author}]`;
const message = (commitCount === 1 ? commit.message : commit.message.split('\n')[0]).replace(UrlRegEx, RemoveUrlEmbedding);
const author = commit.author.name || data.user_username || data.user_name;
const sha = commit.id.slice(0, 7);

return `[\`${sha}\`](${commit.url}) ${message} [${author}]`;
})
.slice(0, 5);
const description = pretext.join('\n');
Expand All @@ -40,16 +41,21 @@ class Push extends EventResponse {
const branch = GetBranchName(data.ref);
const commits = data.commits || [];
const commitCount = commits.length;

if (!commitCount) return '';
let msg = `⚡ **${actor}** pushed ${commitCount} ${commitCount !== 1 ? 'commits' : 'commit'} to \`${branch}\``;

let commitArr = commits
.map(commit => {
let commitMessage = commit.message.replace(/\n/g, '\n ').replace(UrlRegEx, RemoveUrlEmbedding);
return ` \`${commit.id.slice(0, 7)}\` ${commitMessage} [${commit.author.name || actor}]`;
})
.slice(0, 5);
msg += `\n${commitArr.join('\n')}`;
msg += `\n<${data.project.web_url}/compare/${data.before.slice(0, 7)}...${data.after.slice(0, 7)}>`;

return [
`⚡ **${actor}** pushed ${commitCount} ${commitCount !== 1 ? 'commits' : 'commit'} to \`${branch}\``,
...commitArr,
`${data.project.web_url}/compare/${data.before.slice(0, 7)}...${data.after.slice(0, 7)}`,
].join('\n');

return msg;
}
Expand Down
32 changes: 15 additions & 17 deletions lib/Gitlab/parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const parseUrl = require('parse-github-url');

const urlRegex = /^(https?:\/\/|git@)?([\da-z\.-]+\.[a-z\.]{2,7})\/?/; // eslint-disable-line no-useless-escape

const getUrl = str => urlRegex.exec(str);
Expand All @@ -21,21 +23,17 @@ const regex = /^([^\/\s]+)\/?((?:\/?(?:\S+))*)\/([^\/]+?)(?:\.git)?$/; // eslint
module.exports = str => {
if (!str || typeof str !== 'string' || !str.length) return {};

const url = getUrl(str);
const domain = url && url[2];
const parsed = regex.exec(str.replace(urlRegex, ''));

const repo = parsed && `${parsed[1]}/${parsed[2] && `${parsed[2]}/`}${parsed[3]}`;

return parsed
? {
repo,
host: domain,
isGitlab: !domain || domain === 'gitlab.com',
repository: repo,
owner: parsed[1],
group: parsed[2],
name: parsed[3],
}
: {};
const parsed = parseUrl(str);

if (parsed.host === 'github.com') parsed.host = 'gitlab.com';

parsed.isGitlab = parsed.host === 'gitlab.com';

return parsed;
};

module.exports.getRepo = str => {
const out = module.exports(str);

return out && out.repo;
};
4 changes: 2 additions & 2 deletions lib/Util/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const isFound = (data, item) => data.includes(item) || data.includes(item.split('/')[0]);

module.exports = {
whitelist: (data = []) => item => isFound(data, item),
blacklist: (data = []) => item => !isFound(data, item),
whitelist: (data = []) => item => (item ? isFound(data, item) : true),
blacklist: (data = []) => item => (item ? !isFound(data, item) : true),
};
7 changes: 4 additions & 3 deletions lib/Web.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const addons = require('yappybots-addons');

const GetBranchName = require('./Util').GetBranchName;
const filter = require('./Util/filter');
const parser = require('./Gitlab/parser');

const Channel = require('./Models/Channel');
const ChannelRepo = require('./Models/ChannelRepo');
Expand Down Expand Up @@ -61,17 +62,17 @@ app.post('/', async (req, res) => {
.toLowerCase();
const data = req.body;

if (!event || !data || !data.project) return res.status(403).send('Invalid data. Plz use Gitlab webhooks.');
if (!event || !data || (!data.project && !data.repository)) return res.status(403).send('Invalid data. Plz use Gitlab webhooks.');

const repo = get(data, 'project.path_with_namespace');
const repo = get(data, 'project.path_with_namespace') || parser.getRepo(get(data, 'repository.url'));
const channels = (repo && (await Channel.findByRepo(repo))) || [];

const action = get(data, 'object_attributes.action');
const actionText = action ? `/${action}` : '';

Log.verbose(`GitLab | ${repo} - ${eventName}${actionText} (${channels.length} channels)`);

res.send(`${data.project.path_with_namespace} : Received ${eventName}${actionText}, emitting to ${channels.length} channels...`);
res.send(`${repo} : Received ${eventName}${actionText}, emitting to ${channels.length} channels...`);

const eventResponse = GitlabEventHandler.use(data, event);

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"mongoose": "^5.7.12",
"node-fetch": "^2.6.0",
"p-queue": "^6.2.1",
"parse-github-url": "^1.0.2",
"pretty-error": "^2.1.1",
"punycode": "^2.1.1",
"snekfetch": "^4.0.4",
Expand Down

0 comments on commit 65f2586

Please sign in to comment.