Skip to content

Commit

Permalink
LabHub: Make noisy commands private to org members
Browse files Browse the repository at this point in the history
Implemented a command_filter which is dependent
on LabHub and makes desired commands private
to members of the organization.
  • Loading branch information
nvzard committed Aug 13, 2018
1 parent 2780589 commit ce9cf18
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
4 changes: 4 additions & 0 deletions plugins/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

MAX_MSG_LEN = 1000
MAX_LINES = 20

PRIVATE_CMDS = ['assign_cmd', 'create_issue_cmd', 'invite_cmd', 'mark_cmd',
'pr_stats', 'unassign_cmd', 'pitchfork', 'the_rules', 'wa',
'answer', 'lmgtfy', 'ghetto', 'explain', 'nevermind']
44 changes: 17 additions & 27 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,14 @@
import github3
from IGitt.GitHub.GitHub import GitHub, GitHubToken
from IGitt.GitLab.GitLab import GitLab, GitLabPrivateToken
from errbot import BotPlugin, re_botcmd
from errbot import BotPlugin, cmdfilter, re_botcmd
from errbot.templating import tenv

from functools import wraps
from plugins import constants
from utils.backends import message_link
from utils.mixin import DefaultConfigMixin


def members_only(func):
@wraps(func)
def wrapper(*args, **kwargs):
# plugin instance
instance = args[0]
msg = args[1]
user = msg.frm.nick
ret = func(*args, **kwargs)

for team in instance.team_mapping().values():
if team.is_member(user):
yield from ret
return
yield ('You need to be a member of this organization'
' to use this command.')

return wrapper


class LabHub(DefaultConfigMixin, BotPlugin):
"""GitHub and GitLab utilities""" # Ignore QuotesBear

Expand Down Expand Up @@ -108,6 +89,21 @@ def team_mapping(self):
'maintainers': self.TEAMS[self.GH_ORG_NAME + ' maintainers'],
}

@cmdfilter
def members_only(self, msg, cmd, args, dry_run):
user = msg.frm.nick
commands = constants.PRIVATE_CMDS

if cmd in commands:
for team in self.team_mapping().values():
if team.is_member(user):
return msg, cmd, args
self.send(msg.frm, 'You need to be a member of this organization '
'to use this command.')
return None, None, None
else:
return msg, cmd, args

def is_team_member(self, user, team):
teams = self.team_mapping()
return teams[team].is_member(user)
Expand All @@ -119,7 +115,6 @@ def is_room_member(invitee, msg):
# Ignore LineLengthBear, PycodestyleBear
@re_botcmd(pattern=r'^(?:(?:welcome)|(?:inv)|(?:invite))\s+@?([\w-]+)(?:\s+(?:to)\s+(\w+))?$',
re_cmd_name_help='invite ([@]<username> [to <team>]|me)')
@members_only
def invite_cmd(self, msg, match):
"""
Invite given user to given team. By default it invites to
Expand Down Expand Up @@ -198,7 +193,6 @@ def callback_message(self, msg):
@re_botcmd(pattern=r'(?:new|file) issue ([\w\-\.]+?)(?: |\n)(.+?)(?:$|\n((?:.|\n)*))', # Ignore LineLengthBear, PyCodeStyleBear
re_cmd_name_help='new issue repo-name title\n[description]',
flags=re.IGNORECASE)
@members_only
def create_issue_cmd(self, msg, match):
"""Create issues on GitHub and GitLab repositories.""" # Ignore QuotesBear, LineLengthBear, PyCodeStyleBear
user = msg.frm.nick
Expand Down Expand Up @@ -233,7 +227,6 @@ def is_newcomer_issue(iss):
@re_botcmd(pattern=r'^unassign\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/issues/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear
re_cmd_name_help='unassign <complete-issue-URL>',
flags=re.IGNORECASE)
@members_only
def unassign_cmd(self, msg, match):
"""Unassign from an issue.""" # Ignore QuotesBear
org = match.group(2)
Expand Down Expand Up @@ -262,7 +255,6 @@ def unassign_cmd(self, msg, match):
@re_botcmd(pattern=r'mark\s+(wip|pending(?:(?:-|\s+)review)?\b)\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/(pull|merge_requests)/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear
re_cmd_name_help='mark (wip|pending) <complete-PR-URL>',
flags=re.IGNORECASE)
@members_only
def mark_cmd(self, msg, match):
"""Mark a given PR/MR with status labels.""" # Ignore QuotesBear
state, host, org, repo_name, xr, number = match.groups()
Expand Down Expand Up @@ -318,7 +310,6 @@ def mark_cmd(self, msg, match):
@re_botcmd(pattern=r'^assign\s+https://(github|gitlab)\.com/([^/]+)/([^/]+/)+issues/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear
re_cmd_name_help='assign <complete-issue-URL>',
flags=re.IGNORECASE)
@members_only
def assign_cmd(self, msg, match):
"""Assign to GitLab and GitHub issues.""" # Ignore QuotesBear
org = match.group(2)
Expand Down Expand Up @@ -438,7 +429,6 @@ def community_state(pr_count: dict):

@re_botcmd(pattern=r'pr\s+stats\s+(\d+)(?:hours|hrs)',
re_cmd_name_help='pr stats <number-of-hours>(hours|hrs)')
@members_only
def pr_stats(self, msg, match):
hours = match.group(1)
pr_count = dict()
Expand Down
1 change: 1 addition & 0 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def test_alive(self):

def test_invalid_token(self):
plugins.labhub.github3.login.return_value = None
self.labhub.deactivate()
with self.assertLogs() as cm:
self.labhub.activate()
self.assertIn(
Expand Down

0 comments on commit ce9cf18

Please sign in to comment.