Skip to content

Commit

Permalink
feat(gitlab: events): add ability to ignore branch(es) & user(s) via …
Browse files Browse the repository at this point in the history
…conf

Closes #11. Closes #12
  • Loading branch information
dsevillamartin committed May 22, 2017
1 parent 92479b3 commit 6d572c1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/Discord/Commands/Conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
Expand All @@ -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',
Expand Down
11 changes: 9 additions & 2 deletions lib/Models/ChannelConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ let channelConfigSchema = Schema({
'pipeline',
],
},
ignoredUsers: Array,
ignoredBranches: Array,
});

let channelConfig = mongoose.model('ChannelConfig', channelConfigSchema);
Expand Down Expand Up @@ -86,8 +88,10 @@ class ChannelConfig {
this.validKeys = [
'repos',
'repo',
'disabledEvents',
'embed',
'disabledEvents',
'ignoredUsers',
'ignoredBranches',
];
this.setupEvents = false;
}
Expand Down Expand Up @@ -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));
Expand Down
12 changes: 10 additions & 2 deletions lib/Web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 6d572c1

Please sign in to comment.