Skip to content

Commit

Permalink
feat(discord: commands): add initorg command
Browse files Browse the repository at this point in the history
Initializes all public repos in an organization.
Please don't say `GL! initorg GitLab-org` kthx
  • Loading branch information
dsevillamartin committed Jul 16, 2017
1 parent 8775292 commit 8f48842
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
77 changes: 77 additions & 0 deletions lib/Discord/Commands/GitlabInitOrg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const Command = require('../Command');
const ChannelConfig = require('../../Models/ChannelConfig');
const Gitlab = require('../../Gitlab');

class GitlabInitOrgCommand extends Command {
constructor(bot) {
super(bot);

this.props.help = {
name: 'initorg',
summary: 'Initialize all repo events from an organization on the channel.',
usage: 'initorg <repo>',
examples: [
'initorg YappyBots',
'initorg Discord',
],
};

this.setConf({
permLevel: 1,
aliases: ['initializeorg'],
});
}

async run(msg, args) {
const channelid = msg.channel.id;
const org = args[0];
const organization = /^(?:https?:\/\/)?(?:github.com\/)?(\S+)$/.exec(org);

if (!organization || !organization[0]) return this.errorUsage(msg);

const orgName = organization[0];

const workingMsg = await msg.channel.send({
embed: {
color: 0xFB9738,
title: `\`${orgName}\`: ⚙ Working...`,
},
});

return Gitlab.getGroupProjects(orgName).then(data => {
const conf = ChannelConfig.FindByChannel(channelid);
const repos = data.body.filter(e => !e.private && !conf.repos.includes(e.name_with_namespace.toLowerCase())).map(e => e.name_with_namespace);

return Promise.all(repos.map(repo => ChannelConfig.AddRepoToChannel(channelid, repo))).then(() => repos);
})
.then(repos => {
const embed = this._successMessage(orgName, repos);
return workingMsg.edit({ embed });
})
.catch(err => {
let errorMessage = err && err.message ? err.message : err || null;

Log.error(err);

if (errorMessage && errorMessage !== 'Not Found') return this.commandError(msg, `Unable to get organization info for \`${orgName}\`\n${err}`);
return this.commandError(msg, `Unable to initialize! The organization \`${orgName}\` doesn't exist!`);
});
}

_successMessage(org, repos) {
return {
color: 0x84F139,
footer: {
text: this.bot.user.username,
},
title: `\`${org}\`: Successfully initialized all public repository events`,
description: [
'The repositories must all have a webhook pointing to <http://discordjsrewritetrello-datitisev.rhcloud.com/github>',
'To use embeds to have a nicer Gitlab log, say `G! conf set embed true` in this channel.',
`Initialized repos: ${repos.length ? repos.map(e => `\`${e}\``).join(', ') : 'None'}`,
].join('\n'),
};
}
}

module.exports = GitlabInitOrgCommand;
8 changes: 8 additions & 0 deletions lib/Gitlab/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ exports.Endpoints = {
MergeRequests: params => `${base}/merge_requests/${params ? '?' : ''}${Object.keys(params || {}).map(e => `${e}=${params[e]}`).join('&')}`,
};
},
groups: `${api}/projects`,
Group: (group) => {
const base = `${api}/groups/${group}`;
return {
toString: () => base,
projects: `${base}/projects`,
};
}
};
33 changes: 32 additions & 1 deletion lib/Gitlab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,45 @@ class Gitlab {
}
}

/**
* Search GitLab organizations
* @param {String} query - query
* @return {Promise}
*/
searchGroups(query) {
if (!this.tokenAvailable) {
Log.warn(`GitLab | searchGroups: returning sample issue`);
return Promise.resolve([]);
} else {
return this._request(Constants.Endpoints.Group(org), {
search: query,
});
}
}

/**
* Get GitLab organization's public projects
* @param {String} org - organization name / id
* @return {Promise}
*/
getGroupProjects(org) {
if (!this.tokenAvailable) {
Log.warn(`GitLab | getGroupProjects: returning sample issue`);
return Promise.resolve([]);
} else {
return this._request(Constants.Endpoints.Group(org).projects);
}
}

_getRepoID(ownerOrId, name) {
let repo = name && GitlabRepoParse(`${ownerOrId}/${name}`);
return (repo ? repo.repo : ownerOrId).replace(/\//g, '%2F');
}

_request(url) {
_request(url, params) {
return snekfetch
.get(url.toString())
.send(params || {})
.set('PRIVATE-TOKEN', this.token);
}
}
Expand Down

0 comments on commit 8f48842

Please sign in to comment.