-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic groups #7
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "hubot-group-alias", | ||
"description": "Hubot page for user @mention aliases", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"author": "Michael Ball <[email protected]>", | ||
"license": "MIT", | ||
"keywords": [ | ||
|
@@ -20,6 +20,10 @@ | |
"bugs": { | ||
"url": "https://github.com/cycomachead/hubot-group-alias/issues" | ||
}, | ||
"peerDependencies": { | ||
"hubot": "2.x.x", | ||
"hubot-auth": "latest" | ||
}, | ||
"dependencies": { | ||
"coffee-script": "~1.6", | ||
"underscore": "^1.8.2" | ||
|
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 |
---|---|---|
|
@@ -20,44 +20,94 @@ | |
|
||
_ = require 'underscore' | ||
|
||
module.exports = (robot) -> | ||
config = process.env.HUBOT_GROUP_ALIAS | ||
user_prop = process.env.HUBOT_GROUP_ALIAS_NAME_PROP || '' | ||
useDynamicGroups = config == 'DYNAMIC' | ||
groupCache = {} | ||
|
||
config = process.env.HUBOT_GROUP_ALIAS | ||
buildGroupObject = () -> | ||
if !_.isEqual(groupCache, {}) | ||
return groupCache | ||
# Create a 1D list of group assignments | ||
staticGroups = config.split(';') | ||
# Create 2D list of [alias, users] | ||
staticGroups = _.map(staticGroups, (val, item, array) -> val.split('=')) | ||
# expand "user1,user2" to "@user1 @user2" | ||
staticGroups = _.map(staticGroups, (val, item, array) -> [val[0], | ||
'@' + val[1].replace(/,/g, ' @')]) | ||
# Convert 2D list to native object | ||
groupCache = _.object(staticGroups) | ||
return groupCache | ||
|
||
if !config | ||
robot.logger.warning "Configuration HUBOT_GROUP_ALIAS is not defined." | ||
return | ||
getGroups = (text) -> | ||
if useDynamicGroups | ||
groupMap = {} | ||
regex = /[:(]+(\w+)[:)]+|@(\w+)/gi | ||
matches = regex.exec(text) | ||
while matches != null | ||
# If matched, 1 group will always be `undefined` | ||
group = matches[1] || matches[2] | ||
users = robot.auth.usersWithRole(group) | ||
if !_.isEqual(users, []) | ||
groupMap[group] = users | ||
matches = regex.exec(text) | ||
for own group, members of groupMap | ||
list = _.map(members, userFromName) | ||
list = _.map(list, mentionName) | ||
groupMap[group] = listToMentions(list) | ||
return groupMap | ||
else | ||
return buildGroupObject() | ||
|
||
groups = config.split(';') | ||
userFromName = (name) -> | ||
allUsers = robot.brain.data.users | ||
for own id, user of allUsers | ||
if user.name == name | ||
return user | ||
return {} | ||
|
||
# Create 2D list of [alias, users] | ||
groups = _.map(groups, (val, item, array) -> val.split('=')) | ||
# expand "user1,user2" to "@user1 @user2" | ||
groups = _.map(groups, (val, item, array) -> [val[0], | ||
'@' + val[1].split(',').join(' @')]) | ||
mentionName = (user) -> | ||
# mention_name is for Hipchat | ||
return user[user_prop] || user.mention_name || user.name | ||
|
||
# Convert 2D list to native object | ||
groups = _.object(groups) | ||
|
||
user_prop = process.env.HUBOT_GROUP_ALIAS_NAME_PROP | ||
|
||
# Replace aliases with @mentions in the message | ||
expand = (message, user) -> | ||
filterName = user.mention_name || user[user_prop] | ||
for own alias, members of groups | ||
# Filter inviduals from their own messages. | ||
if filterName | ||
members = members.replace('@' + filterName, '') | ||
reg = new RegExp('[:(]+' + alias + '[:)]+|@' + alias, 'i') | ||
message = message.replace(reg, members) | ||
return message | ||
|
||
# Compile RegEx to match only the aliases | ||
# Note this matches (alias) :alias: and @alias | ||
aliases = _.keys(groups).join('|') | ||
listToMentions = (list) -> | ||
return '@' + list.join(' @') | ||
|
||
# Replace aliases with @mentions in the message | ||
expand = (message, groups, user) -> | ||
filterName = mentionName(user) | ||
for own alias, members of groups | ||
# Filter inviduals from their own messages. | ||
if filterName | ||
members = members.replace('@' + filterName, '').replace(/\s+/g, ' ') | ||
reg = new RegExp('[:(]+' + alias + '[:)]+|@' + alias, 'i') | ||
message = message.replace(reg, members) | ||
return message | ||
|
||
buildRegExp = () -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty parameter list is forbidden |
||
if useDynamicGroups | ||
aliases = '\\w+' | ||
else | ||
# Compile RegEx to match only the aliases | ||
# Note this matches (alias) :alias: and @alias | ||
aliases = _.keys(buildGroupObject()).join('|') | ||
# The last group is a set of stop conditions (word boundaries or end of line) | ||
atRE = '(?:@(' + aliases + ')(?:\\b[^.]|$))' | ||
emojiRE = '(?:[(:])(' + aliases + ')(?:[:)])' | ||
regex = new RegExp(atRE + '|' + emojiRE, 'i') | ||
robot.hear regex, (msg) -> | ||
msg.send expand(msg.message.text, msg.message.user) | ||
|
||
return new RegExp(atRE + '|' + emojiRE, 'i') | ||
|
||
module.exports = (robot) -> | ||
if !config | ||
robot.logger.warning "Configuration HUBOT_GROUP_ALIAS is not defined." | ||
return | ||
|
||
if useDynamicGroups && !robot.auth | ||
robot.logger.warning "Using dynamic groups requires hubot-auth to be loaded" | ||
return | ||
|
||
regex = buildRegExp() | ||
robot.hear regex, (resp) -> | ||
groups = getGroups(resp.message.text) | ||
if !_.isEqual(groups, {}) | ||
resp.send expand(resp.message.text, groups, resp.message.user) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty parameter list is forbidden