From 6d572c1a47a36930c55d3f9ca3880b13e9acbaec Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 May 2017 20:02:41 -0400 Subject: [PATCH] feat(gitlab: events): add ability to ignore branch(es) & user(s) via conf Closes #11. Closes #12 --- lib/Discord/Commands/Conf.js | 5 ++++- lib/Models/ChannelConfig.js | 11 +++++++++-- lib/Web.js | 12 ++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/Discord/Commands/Conf.js b/lib/Discord/Commands/Conf.js index cf96039..2903e9e 100644 --- a/lib/Discord/Commands/Conf.js +++ b/lib/Discord/Commands/Conf.js @@ -48,7 +48,9 @@ class ConfCommand extends Command { .addField('Repos (repos)', channelConf.repos[0] ? channelConf.repos.map(e => `\`${e}\``).join(', ') : 'None', true) .addField('Repo (repo)', channelConf.repo ? `\`${channelConf.repo}\u200B\`` : 'None', true) .addField('Use Embed (embed)', channelConf.embed ? `Yes` : 'No', true) - .addField('Disabled Events (disabledEvents)', channelConf.disabledEvents && channelConf.disabledEvents.length ? channelConf.disabledEvents.map(e => `\`${e}\``).join(', ') : 'None', true); + .addField('Disabled Events (disabledEvents)', channelConf.disabledEvents && channelConf.disabledEvents.length ? channelConf.disabledEvents.map(e => `\`${e}\``).join(', ') : 'None', true) + .addField('Ignored Users (ignoredUsers)', channelConf.ignoredUsers && !!channelConf.ignoredUsers.length ? channelConf.ignoredUsers.map(e => `\`${e}\``).join(', ') : 'None', true) + .addField('Ignored Branches (ignoredBranches)', channelConf.ignoredBranches && !!channelConf.ignoredBranches.length ? channelConf.ignoredBranches.map(e => `\`${e}\``).join(', ') : 'None', true); return msg.channel.send({ embed }); } } @@ -74,6 +76,7 @@ class ConfCommand extends Command { const validKeys = ((args.includes('--global') || args.includes('-g')) ? ServerConfig : ChannelConfig).validKeys; let value = args.filter(e => !e.includes('-g')).slice(2, 20).join(' '); if (key !== 'prefix' && value.includes(', ')) value = value.split(', '); + if (!Array.isArray(value) && (key === 'ignoredUsers' || key === 'ignoredBranches' || key === 'disabledEvents')) value = value ? [value] : []; const embedData = { author: conf === channelConf ? `${guild.name} #${channel.name}` : guild.name, confName: conf === channelConf ? 'channel' : 'server', diff --git a/lib/Models/ChannelConfig.js b/lib/Models/ChannelConfig.js index 79d2f26..5a5bf97 100644 --- a/lib/Models/ChannelConfig.js +++ b/lib/Models/ChannelConfig.js @@ -29,6 +29,8 @@ let channelConfigSchema = Schema({ 'pipeline', ], }, + ignoredUsers: Array, + ignoredBranches: Array, }); let channelConfig = mongoose.model('ChannelConfig', channelConfigSchema); @@ -86,8 +88,10 @@ class ChannelConfig { this.validKeys = [ 'repos', 'repo', - 'disabledEvents', 'embed', + 'disabledEvents', + 'ignoredUsers', + 'ignoredBranches', ]; this.setupEvents = false; } @@ -189,7 +193,10 @@ class ChannelConfig { channelName: channel.name, repos: [], prefix: `GL! `, - disabledEvents: [], + disabledEvents: [ + 'merge_request/update', + 'pipeline', + ], }; return channelConfig.create(conf).then(() => { this._data.set(conf.channelID, new ChannelConfigItem(this, conf)); diff --git a/lib/Web.js b/lib/Web.js index a24e74e..41122c1 100644 --- a/lib/Web.js +++ b/lib/Web.js @@ -57,9 +57,17 @@ app.post('/', (req, res) => { channels.forEach(conf => { const wantsEmbed = !!conf.embed; - const { channelID, disabledEvents } = conf; + const { channelID, disabledEvents, ignoredUsers, ignoredBranches } = conf; const channel = bot.channels.get(channelID); - if (!channel || disabledEvents.includes(event) || disabledEvents.includes(`${event}${actionText}`)) return; + const actor = { + name: data.user ? data.user.name : data.user_name, + id: data.user ? data.user.id : data.user_id, + }; + const branch = data.ref && data.ref ? data.ref.split('/')[2] : data.object_attributes.ref; + if (!channel) return; + if (disabledEvents.includes(event) || disabledEvents.includes(`${event}${actionText}`)) return; + if (ignoredUsers && (ignoredUsers.includes(actor.name) || ignoredUsers.includes(actor.id))) return; + if (ignoredBranches && branch && ignoredBranches.includes(branch)) return; if (wantsEmbed) { channel.send({ embed: eventResponse.embed }).catch(err => handleError(err, channel));