Skip to content

Commit

Permalink
Merge pull request #5 from hubot-scripts/multi-teams-followup
Browse files Browse the repository at this point in the history
Multi teams followup
  • Loading branch information
mihai committed Dec 7, 2014
2 parents 0083a7f + 895efd1 commit d7f3347
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 52 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ as a comma separated list of usernames.

## Commands

hubot team +1 # add me to the team
hubot team -1 # remove me from the team
hubot team add (me|<user>) # add me or <user> to team
hubot team remove (me|<user>) # remove me or <user> from team
hubot team (list|show) # list the people in the team
hubot team (new|empty|clear) # clear team list
hubot team count # list the current size of the team
hubot create <team_name> team # create team called <team_name>
hubot (delete|remove) <team_name> team # delete team called <team_name>
hubot list teams # list all existing teams
hubot (<team_name>) team +1 # add me to the team
hubot (<team_name>) team -1 # remove me from the team
hubot (<team_name>) team add (me|<user>) # add me or <user> to team
hubot (<team_name>) team remove (me|<user>) # remove me or <user> from team
hubot (<team_name>) team (list|show) # list the people in the team
hubot (<team_name>) team (empty|clear) # clear team list
hubot (<team_name>) team count # list the current size of the team

All commands that have the `<team_name>` in parantheses can ommit it. For example:

hubot team +1

would work just fine, adding the current user to the default team. Note: when
adding and removing users without the `<team_name>` label, those users are
included in a team that does not show up when running `hubot list teams`.


## Contributing
Expand Down
151 changes: 106 additions & 45 deletions src/team.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
# HUBOT_TEAM_ADMIN - A comma separate list of user names
#
# Commands:
# hubot create <team_name> team - add me to the team
# hubot create <team_name> team - create team called <team_name>
# hubot (delete|remove) <team_name> team - delete team called <team_name>
# hubot list teams - list all existing teams
# hubot <team_name> team +1 - add me to the team
# hubot <team_name> team -1 - remove me from the team
# hubot <team_name> team add (me|<user>) - add me or <user> to team
# hubot <team_name> team remove (me|<user>) - remove me or <user> from team
# hubot <team_name> team count - list the current size of the team
# hubot <team_name> team (list|show) - list the people in the team
# hubot <team_name> team (new|empty|clear) - clear team list
# hubot (<team_name>) team +1 - add me to the team
# hubot (<team_name>) team -1 - remove me from the team
# hubot (<team_name>) team add (me|<user>) - add me or <user> to team
# hubot (<team_name>) team remove (me|<user>) - remove me or <user> from team
# hubot (<team_name>) team count - list the current size of the team
# hubot (<team_name>) team (list|show) - list the people in the team
# hubot (<team_name>) team (empty|clear) - clear team list
#
# Author:
# mihai

config =
admin_list: process.env.HUBOT_TEAM_ADMIN
admin_list: process.env.HUBOT_TEAM_ADMIN,
default_team_label: '__default__'

module.exports = (robot) ->
robot.brain.data.teams ||= {}
Expand All @@ -39,11 +41,20 @@ module.exports = (robot) ->
return false unless teamExists(team_name, msg)
robot.brain.data.teams[team_name].length

teamLabel = (team_name) ->
label = team_name unless team_name is config.default_team_label
message = if label then "`#{label}` team" else "team"
return message

addUserToTeam = (user, team_name, msg) ->
return unless teamExists(team_name, msg)
if not team_name
robot.brain.data['teams'][config.default_team_label] ||= []
team_name = config.default_team_label
else
return unless teamExists(team_name, msg)

if user in robot.brain.data.teams[team_name]
msg.send "#{user} already in the #{team_name}"
msg.send "#{user} already in the #{teamLabel(team_name)}"
else
count = teamSize(team_name, msg)
if count > 0
Expand All @@ -53,104 +64,154 @@ module.exports = (robot) ->

robot.brain.data.teams[team_name].push(user)

message = "#{user} added to the team"
message = "#{user} added to the #{teamLabel(team_name)}"
message += countMessage if countMessage
msg.send message

addTeam = (team_name, msg) ->
if robot.brain.data.teams[team_name]
msg.send "#{team_name} already exists"
msg.send "#{teamLabel(team_name)} already exists"
else
robot.brain.data['teams'][team_name] = []
msg.send "#{team_name} team created!"

listTeams = (msg) ->
team_count = Object.keys(robot.brain.data.teams).length

if team_count > 0
message = "Team (#{team_count} total):\n"
for team of robot.brain.data.teams
message += "#{team}\n"
for user in robot.brain.data.teams[team]
message += "- #{user}\n"
msg.send message

else
msg.send "Oh noes, we gots no teams!"
msg.send "#{teamLabel(team_name)} created, add some people to it"

removeUserFromTeam = (user, team_name, msg) ->
team_name = config.default_team_label unless team_name
return unless teamExists(team_name, msg)

if user not in robot.brain.data.teams[team_name]
msg.send "#{user} already out of the team"
msg.send "#{user} already out of the #{teamLabel(team_name)}"
else
user_index = robot.brain.data.teams[team_name].indexOf(user)
robot.brain.data.teams[team_name].splice(user_index, 1)
count = teamSize(team_name, msg)
countMessage = ", " + count + " remaining" if count > 0
message = "#{user} removed from #{team_name}"
message = "#{user} removed from the #{teamLabel(team_name)}"
message += countMessage if countMessage
msg.send message

teamExists = (team_name, msg) ->
return true if team_name is config.default_team_label

if robot.brain.data.teams[team_name]
true
else
msg.send "#{team_name} does not exist, buddy."
msg.send "#{teamLabel(team_name)} does not exist"
false

##
## hubot create <team_name> team - create team called <team_name>
##
robot.respond /create (\S*) team ?.*/i, (msg) ->
team_name = msg.match[1]
addTeam(team_name, msg)

##
## hubot (delete|remove) <team_name> team - delete team called <team_name>
##
robot.respond /(delete|remove) (\S*) team ?.*/i, (msg) ->
team_name = msg.match[2]
return unless teamExists(team_name, msg)

if msg.message.user.name in admins
unless team_name is config.default_team_label
delete robot.brain.data.teams[team_name]
msg.send "Team `#{team_name}` removed"
else
msg.reply "Sorry, only admins can remove teams"

##
## hubot list teams - list all existing teams
##
robot.respond /list teams ?.*/i, (msg) ->
listTeams(msg)
team_count = Object.keys(robot.brain.data.teams).length
team_count = team_count - 1 if robot.brain.data.teams[config.default_team_label]

if team_count > 0
message = "Teams:\n"

for team of robot.brain.data.teams
continue if team is config.default_team_label
size = teamSize(team)
if size > 0
message += "`#{team}` (#{size} total)\n"
for user in robot.brain.data.teams[team]
message += "- #{user}\n"
message += "\n"
else
message += "`#{team}` (empty)\n"

else
message = "No team was created so far"

msg.send message

robot.respond /(\S*) team add (\S*) ?.*/i, (msg) ->
##
## hubot <team_name> team add (me|<user>) - add me or <user> to team
##
robot.respond /(\S*)? team add (\S*) ?.*/i, (msg) ->
team_name = msg.match[1]
user = msg.match[2]
if user.toLocaleLowerCase() == "me"
user = msg.message.user.name
addUserToTeam(user, team_name, msg)

robot.respond /(\S*) team \+1 ?.*/i, (msg) ->
##
## hubot <team_name> team +1 - add me to the team
##
robot.respond /(\S*)? team \+1 ?.*/i, (msg) ->
team_name = msg.match[1]
addUserToTeam(msg.message.user.name, team_name, msg)

robot.respond /(\S*) team remove (\S*) ?.*/i, (msg) ->
##
## hubot <team_name> team remove (me|<user>) - remove me or <user> from team
##
robot.respond /(\S*)? team remove (\S*) ?.*/i, (msg) ->
team_name = msg.match[1]
user = msg.match[2]
if user.toLocaleLowerCase() == "me"
user = msg.message.user.name
removeUserFromTeam(user, team_name, msg)

robot.respond /(\S*) team -1/i, (msg) ->
##
## hubot <team_name> team -1 - remove me from the team
##
robot.respond /(\S*)? team -1/i, (msg) ->
team_name = msg.match[1]
removeUserFromTeam(msg.message.user.name, team_name, msg)

robot.respond /(\S*) team count$/i, (msg) ->
team_name = msg.match[1]
##
## hubot <team_name> team count - list the current size of the team
##
robot.respond /(\S*)? team count$/i, (msg) ->
team_name = msg.match[1] || config.default_team_label
return unless teamExists(team_name)
msg.send "#{teamSize(team_name, msg)} people are currently in the team"

robot.respond /(\S*) team (list|show)$/i, (msg) ->
team_name = msg.match[1]
##
## hubot <team_name> team (list|show) - list the people in the team
##
robot.respond /(\S*)? team (list|show)$/i, (msg) ->
team_name = msg.match[1] || config.default_team_label
return unless teamExists(team_name)
count = teamSize(team_name, msg)
if count == 0
msg.send "There is no one in the team currently"
msg.send "There is no one in the #{teamLabel(team_name)} currently"
else
position = 0
message = "Team (#{count} total):\n"
message = "#{teamLabel(team_name)} (#{count} total):\n"
for user in robot.brain.data.teams[team_name]
position += 1
message += "#{position}. #{user}\n"
msg.send message

robot.respond /(\S*) team (new|clear|empty)$/i, (msg) ->
team_name = msg.match[1]
##
## hubot <team_name> team (empty|clear) - clear team list
##
robot.respond /(\S*)? team (clear|empty)$/i, (msg) ->
team_name = msg.match[1] || config.default_team_label
if msg.message.user.name in admins
robot.brain.data.teams[team_name] = []
msg.send "Team list cleared"
msg.send "#{teamLabel(team_name)} list cleared"
else
msg.reply "Sorry, only admins can clear the team members list"

0 comments on commit d7f3347

Please sign in to comment.