Skip to content

Commit

Permalink
feat(spinbot): assign issue functionality (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander committed Jan 3, 2019
1 parent 842384e commit 6b821e6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
56 changes: 56 additions & 0 deletions spinbot/event/issue_comment_assign_handler.py
@@ -0,0 +1,56 @@
from gh import AddLabel, RemoveLabel
from .handler import Handler
from .command import GetCommands
from .issue_event import GetIssue, GetRepo

class IssueCommentAssignHandler(Handler):
def __init__(self):
super().__init__()

def handles(self, event):
return (event.type == 'IssueCommentEvent'
and (event.payload.get('action') == 'created'
or event.payload.get('action') == 'edited'))

def handle(self, g, event):
# avoid fetching until needed
issue = None
def get_issue():
nonlocal issue
if issue is None:
issue = GetIssue(g, event)
if issue is None:
raise RuntimeError('No issue found for {}'.format(event))
return issue

def get_user(command, issue):
if len(command) == 1:
issue.create_comment('You must specify someone to (un)assign, e.g. `@spinnakerbot (un)assign-issue user`')
return

user = command[1]
if user[0] == '@':
user = user[1:]

return user

for command in GetCommands(event.payload.get('comment', {}).get('body')):
if command[0] == 'assign-issue':
issue = get_issue()
user = get_user(command, issue)
repo = GetRepo(g, event)
if not repo.has_in_assignees(user):
issue.create_comment('User @{} cannot be assigned in this repo ({}). Are they in the correct org/team?'.format(user, repo.full_name))
return
issue.add_to_assignees(user)

if command[0] == 'unassign-issue':
issue = get_issue()
user = get_user(command, issue)
has_assignee = len(list(filter(lambda a: a.login == user, issue.assignees))) > 0
if not has_assignee:
issue.create_comment('User @{} is not assigned to this issue yet.'.format(user))
return
issue.remove_from_assignees(user)

IssueCommentAssignHandler()
9 changes: 9 additions & 0 deletions spinbot/event/issue_event.py
Expand Up @@ -10,3 +10,12 @@ def GetIssue(g, event):
return None

return g.get_issue(repo, number)

def GetRepo(g, event):
url = event.payload.get('issue', {}).get('url')
if not url:
return None

repo = '/'.join(url.split('/')[-4:-2])
return g.get_repo(repo)

4 changes: 4 additions & 0 deletions spinbot/gh/client.py
Expand Up @@ -68,6 +68,10 @@ def get_label(self, repo, name, create=True):

return label

def get_repo(self, r):
print(r)
return self.g.get_repo(r)

def repos(self):
for r in self._repos:
yield self.g.get_repo(r)
Expand Down
1 change: 1 addition & 0 deletions spinbot/manifests/deploy.yml
Expand Up @@ -80,6 +80,7 @@ data:
event:
handlers:
- name: label_issue_comment_event_handler
- name: issue_comment_assign_handler
- name: log_event_handler
config:
payload: false
Expand Down

0 comments on commit 6b821e6

Please sign in to comment.