def ttest_at_team(self): # FIXME mention = "test_team" team_name = "测试team" description = "测试" team = Team.add(mention, team_name, description) ok_(team.uid == mention) content = "@test_team code" users = get_mentions_from_text(content) # ok_(len(users) == 0) team_id = team.id user_id = "chengeng" identity = 2 rl = TeamUserRelationship.create(team_id=team_id, user_id=user_id, identity=identity) ok_(rl.user_id == user_id) users = get_mentions_from_text(content) ok_(users[0] == user_id) rl.delete() users = get_mentions_from_text(content) ok_(len(users) == 0) team.delete() users = get_mentions_from_text(content) ok_(users[0] == mention)
def test_at_team(self): mention = "test_team" team_name = "测试team" description = "测试" team = Team.add(mention, team_name, description) ok_(team.uid == mention) content = "@test_team code" users = get_mentions_from_text(content) ok_(len(users) == 0) team_id = team.id user_id = "chengeng" identity = 2 rl = TeamUserRelationship.create(team_id=team_id, user_id=user_id, identity=identity) ok_(rl.user_id == user_id) users = get_mentions_from_text(content) ok_(users[0] == user_id) rl.delete() users = get_mentions_from_text(content) ok_(len(users) == 0) team.delete() users = get_mentions_from_text(content) ok_(users[0] == mention)
def add_codereview(self, request): content = request.data.get('content') from_sha = request.data.get('from_sha') to_sha = request.data.get('to_sha') old_path = request.data.get('old_path') new_path = request.data.get('new_path') from_oid = request.data.get('from_oid') to_oid = request.data.get('to_oid') new_no = request.data.get('new_no') old_no = request.data.get('old_no', '') for t in ('content', 'from_sha', 'old_path', 'new_path', 'from_oid', 'to_oid', 'new_no'): val = locals()[t] if not val.strip(): raise MissingFieldError(t) old_no = int(old_no) if old_no.isdigit() else LINECOMMENT_INDEX_EMPTY new_no = int(new_no) if new_no.isdigit() else LINECOMMENT_INDEX_EMPTY author = request.data.get('username') if not author: author = request.user.name self.ticket.add_codereview(from_sha, to_sha, old_path, new_path, from_oid, to_oid, old_no, new_no, author, content) at_users = get_mentions_from_text(content) for u in at_users: User(u).add_invited_pull_request(self.ticket.id) return {'ok': True}
def comment(self, request): if request.method == 'POST': content = request.get_form_var('content').decode('utf-8') if not content.strip(): return {'error': 'Content is empty!'} user = request.user current_user = request.user author = user.name comment = self.ticket.add_comment(content, author) ticket = self.ticket pullreq = self.pullreq project = self.project html = st('/pull/ticket_comment.html', **locals()) if request.get_form_var('comment_and_close'): close_pull(ticket, pullreq, user, content, comment, request) return dict(r=0, reload=1, redirect_to=self.url) elif request.get_form_var('comment_and_reopen'): if not pullreq.is_temp_pull(): ticket.open(author) return dict(r=0, reload=1, redirect_to=self.url) else: at_users = get_mentions_from_text(content) for u in at_users: User(u).add_invited_pull_request(ticket.id) return dict(r=0, html=html) return request.redirect(self.url)
def get_pr_notify_toaddr_receivers(author, content, pullreq, ticket): noti_receivers = [] at_users = get_mentions_from_text(content) noti_receivers.extend(at_users + [ticket.author, pullreq.to_proj.owner_id]) if author in noti_receivers: noti_receivers.remove(author) return noti_receivers
def noti_receivers(self): participants = self._ticket.participants mentions = get_mentions_from_text(self._content) extra = [self._ticket.author, self._pullreq.to_proj.owner_id] receivers = set(participants + mentions + extra) receivers.discard(self._sender) return Mute.filter('ticket', self._target.name, self._pullreq.ticket_id, receivers)
def noti_receivers(self): comment = self._comment comments = comment.gets_by_proj_and_ref(self._proj.id, comment.ref) co_authors = {c.author for c in comments} mentions = set(get_mentions_from_text(comment.content)) extra_receivers = set( [r for r in (self._proj.owner_id, self._commit_author) if r]) rs = (co_authors | mentions | extra_receivers) rs.discard(comment.author) return rs
def noti_receivers(self): comment = self._comment comments = comment.gets_by_proj_and_ref( self._proj.id, comment.ref) co_authors = {c.author for c in comments} mentions = set(get_mentions_from_text(comment.content)) extra_receivers = set([r for r in (self._proj.owner_id, self._commit_author) if r]) rs = (co_authors | mentions | extra_receivers) rs.discard(comment.author) return rs
def get_pr_notify_receivers(author, content, pullreq, ticket, channel="notify", extra_receivers=[]): noti_receivers = ticket.participants at_users = get_mentions_from_text(content) noti_receivers.extend( at_users + [ticket.author, pullreq.to_proj.owner_id] + extra_receivers) noti_receivers = list(set(noti_receivers)) # not to notify author himself if author in noti_receivers: noti_receivers.remove(author) return noti_receivers
def noti_receivers(self): issue = self._issue target = self._target mentions = get_mentions_from_text(self._content) participants = [p.user_id for p in issue.participants if p] if issue.target_type == 'project': extra_receivers = [issue.creator_id, target.owner_id] else: extra_receivers = self._target.user_ids extra_receivers.append(issue.creator_id) rs = set(mentions + participants + extra_receivers) rs.discard(self._sender) return rs
def get_issue_notify_receivers(author, content, target, issue, channel='notify'): noti_receivers = [] at_users = get_mentions_from_text(content) participants = [p.user_id for p in issue.participants if p] if issue.target_type == "project": noti_receivers.extend( at_users + [issue.creator_id, target.owner_id] + participants) elif issue.target_type == "team": noti_receivers.extend( at_users + [issue.creator_id] + participants + target.user_ids) noti_receivers = list(set(noti_receivers)) # not to notify author himself if author in noti_receivers: noti_receivers.remove(author) return noti_receivers
def review_comment(self, request): ''' pull linecomment ''' user = request.user current_user = request.user if request.method == 'POST' and user: project = CodeDoubanProject.get_by_name(self.proj_name) project_id = project.id from_sha = request.get_form_var('from_sha') assert from_sha, "comment from_sha cannot be empty" old_path = request.get_form_var('old_path') assert old_path, "comment old_path cannot be empty" new_path = request.get_form_var('new_path') # position = request.get_form_var('position') # assert position, "comment position cannot be empty" from_oid = request.get_form_var('from_oid') assert from_oid, "comment from_oid cannot be empty" to_oid = request.get_form_var('to_oid') assert to_oid, "comment to_oid cannot be empty" old = request.get_form_var('old_no') old = int(old) if old.isdigit() else LINECOMMENT_INDEX_EMPTY new = request.get_form_var('new_no') new = int(new) if new.isdigit() else LINECOMMENT_INDEX_EMPTY content = request.get_form_var('content', '').decode('utf-8') # commit_author = request.get_form_var('commit_author') if not content.strip(): return {'error': 'Content is empty!'} author = user.name ticket = self.ticket linecomment = ticket.add_codereview(from_sha, '', old_path, new_path, from_oid, to_oid, old, new, author, content) pullreq = self.pullreq codereviews = [linecomment] at_users = get_mentions_from_text(content) for u in at_users: User(u).add_invited_pull_request(ticket.id) # TODO: 把dispatch 从model移到这里 # r.html_with_diff used by codelive return dict( r=0, html=st('/pull/ticket_linecomment.html', **locals()), html_with_diff=st('/pull/render_ticket_codereview.html', **locals()))
def review_comment(self, request): ''' pull linecomment ''' user = request.user current_user = request.user if request.method == 'POST' and user: project = CodeDoubanProject.get_by_name(self.proj_name) project_id = project.id from_sha = request.get_form_var('from_sha') assert from_sha, "comment from_sha cannot be empty" old_path = request.get_form_var('old_path') assert old_path, "comment old_path cannot be empty" new_path = request.get_form_var('new_path') # position = request.get_form_var('position') # assert position, "comment position cannot be empty" from_oid = request.get_form_var('from_oid') assert from_oid, "comment from_oid cannot be empty" to_oid = request.get_form_var('to_oid') assert to_oid, "comment to_oid cannot be empty" old = request.get_form_var('old_no') old = int(old) if old.isdigit() else LINECOMMENT_INDEX_EMPTY new = request.get_form_var('new_no') new = int(new) if new.isdigit() else LINECOMMENT_INDEX_EMPTY content = request.get_form_var('content', '').decode('utf-8') # commit_author = request.get_form_var('commit_author') if not content.strip(): return {'error': 'Content is empty!'} author = user.name ticket = self.ticket linecomment = ticket.add_codereview(from_sha, '', old_path, new_path, from_oid, to_oid, old, new, author, content) pullreq = self.pullreq codereviews = [linecomment] at_users = get_mentions_from_text(content) for u in at_users: User(u).add_invited_pull_request(ticket.id) # TODO: 把dispatch 从model移到这里 # r.html_with_diff used by codelive return dict(r=0, html=st('/pull/ticket_linecomment.html', **locals()), html_with_diff=st( '/pull/render_ticket_codereview.html', **locals()))
def add_pull(ticket, pullreq, user): from dispatches import dispatch from vilya.libs.text import get_mentions_from_text from vilya.libs.signals import pullrequest_signal from vilya.models.user import get_author_by_email from vilya.models.user import User from vilya.models.trello.core import process_trello_notify reporter = user.username commits = pullreq.commits # TODO: refactory is! auto number how to? shas = [p.sha for p in commits] ticket_len = Ticket.get_count_by_proj_id(ticket.project_id) if ticket_len == 0: # 检查是否创建过新PR,若未创建过则以旧PR号为基准累加 max_ticket_id = PullRequest.get_max_ticket_id(ticket.project_id) if max_ticket_id >= 0: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author, max_ticket_id + 1) else: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author) else: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author) pullreq = pullreq.insert(ticket.ticket_number) if shas: ticket.add_commits(','.join(shas), reporter) noti_receivers = [committer.name for committer in CodeDoubanProject.get_committers_by_project(pullreq.to_proj.id)] # noqa noti_receivers = noti_receivers + [pullreq.to_proj.owner.name] get_commit_author = lambda u: get_author_by_email(u.email.encode('utf-8')) commit_authors = {get_commit_author(c.author) for c in commits} commit_authors = {a for a in commit_authors if a} noti_receivers.extend(commit_authors) # diffs, author_by_file, authors = pullreq.get_diffs(with_blame=True) # FIXME: diffs没用到? # diffs = pullreq.get_diffs() # blame代码变更的原作者, 也加到at users at_users = get_mentions_from_text(ticket.description) # at_users.extend(authors) at_users.append(pullreq.to_proj.owner_id) at_users = set(at_users) # FIXME: invited_users is used on page /hub/my_pull_requests/ invited_users = [User(u).add_invited_pull_request(ticket.id) for u in at_users] ProjectOwnLRUCounter(user.username).use(pullreq.to_proj.id) ProjectWatchLRUCounter(user.username).use(pullreq.to_proj.id) # TODO: 重构Feed之后取消这个信号的发送 pullrequest_signal.send(user.username, extra_receivers=noti_receivers, pullreq=pullreq, comment=ticket.description, ticket_id=ticket.ticket_id, ticket=ticket, status="unmerge", new_version=True) dispatch('pullreq', data=dict(sender=user.username, content=ticket.description, ticket=ticket, status='unmerge', new_version=True, extra_receivers=noti_receivers)) dispatch('pr_actions', data=dict(type='pr_opened', hooks=pullreq.to_proj.hooks, author=user, title=ticket.title, body=ticket.description, ticket=ticket, pullreq=pullreq)) # FIXME: move to dispatch process_trello_notify(user, ticket) return pullreq
def noti_receivers(self): receivers = set() receivers.add(self._recommend.to_user) mentions = set(get_mentions_from_text(self._recommend.content)) return receivers | mentions
def add_pull(ticket, pullreq, user): from dispatches import dispatch from vilya.libs.text import get_mentions_from_text from vilya.libs.signals import pullrequest_signal from vilya.models.user import get_author_by_email from vilya.models.user import User from vilya.models.trello.core import process_trello_notify reporter = user.username commits = pullreq.commits # TODO: refactory is! auto number how to? shas = [p.sha for p in commits] ticket_len = Ticket.get_count_by_proj_id(ticket.project_id) if ticket_len == 0: # 检查是否创建过新PR,若未创建过则以旧PR号为基准累加 max_ticket_id = PullRequest.get_max_ticket_id(ticket.project_id) if max_ticket_id >= 0: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author, max_ticket_id + 1) else: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author) else: ticket = Ticket.add(ticket.project_id, ticket.title, ticket.description, ticket.author) pullreq = pullreq.insert(ticket.ticket_number) if shas: ticket.add_commits(','.join(shas), reporter) noti_receivers = [ committer.name for committer in CodeDoubanProject.get_committers_by_project(pullreq.to_proj.id) ] # noqa noti_receivers = noti_receivers + [pullreq.to_proj.owner.name] get_commit_author = lambda u: get_author_by_email(u.email.encode('utf-8')) commit_authors = {get_commit_author(c.author) for c in commits} commit_authors = {a for a in commit_authors if a} noti_receivers.extend(commit_authors) # diffs, author_by_file, authors = pullreq.get_diffs(with_blame=True) # FIXME: diffs没用到? # diffs = pullreq.get_diffs() # blame代码变更的原作者, 也加到at users at_users = get_mentions_from_text(ticket.description) # at_users.extend(authors) at_users.append(pullreq.to_proj.owner_id) at_users = set(at_users) # FIXME: invited_users is used on page /hub/my_pull_requests/ invited_users = [ User(u).add_invited_pull_request(ticket.id) for u in at_users ] ProjectOwnLRUCounter(user.username).use(pullreq.to_proj.id) ProjectWatchLRUCounter(user.username).use(pullreq.to_proj.id) # TODO: 重构Feed之后取消这个信号的发送 pullrequest_signal.send(user.username, extra_receivers=noti_receivers, pullreq=pullreq, comment=ticket.description, ticket_id=ticket.ticket_id, ticket=ticket, status="unmerge", new_version=True) dispatch('pullreq', data=dict(sender=user.username, content=ticket.description, ticket=ticket, status='unmerge', new_version=True, extra_receivers=noti_receivers)) dispatch('pr_actions', data=dict(type='pr_opened', hooks=pullreq.to_proj.hooks, author=user, title=ticket.title, body=ticket.description, ticket=ticket, pullreq=pullreq)) # FIXME: move to dispatch process_trello_notify(user, ticket) return pullreq