-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(discord: commands): add initorg command
Initializes all public repos in an organization. Please don't say `GL! initorg GitLab-org` kthx
- Loading branch information
1 parent
8775292
commit 8f48842
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters