def test_create_with_mention(self, backend): self.log_user() commit_id = backend.repo.get_commit('300').raw_id text = u'@test_regular check CommentOnCommit' params = {'text': text, 'csrf_token': self.csrf_token} self.app.post(url(controller='changeset', action='comment', repo_name=backend.repo_name, revision=commit_id), params=params) response = self.app.get( url(controller='changeset', action='index', repo_name=backend.repo_name, revision=commit_id)) # test DB assert ChangesetComment.query().count() == 1 assert_comment_links(response, ChangesetComment.query().count(), 0) notification = Notification.query().one() assert len(notification.recipients) == 2 users = [x.username for x in notification.recipients] # test_regular gets notification by @mention assert sorted(users) == [u'test_admin', u'test_regular']
def test_create_inline(self): self.log_user() rev = "27cd5cce30c96924232dffcd24178a07ffeb5dfc" text = u"CommentOnRevision" f_path = "vcs/web/simplevcs/views/repository.py" line = "n1" params = {"text": text, "f_path": f_path, "line": line} response = self.app.post( url(controller="changeset", action="comment", repo_name=HG_REPO, revision=rev), params=params ) # Test response... self.assertEqual(response.status, "302 Found") response.follow() response = self.app.get(url(controller="changeset", action="index", repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain("""<div class="comments-number">0 comment(s)""" """ (%s inline)</div>""" % 1) response.mustcontain( """<div style="display:none" class="inline-comment-placeholder" """ """path="vcs/web/simplevcs/views/repository.py" """ """target_id="vcswebsimplevcsviewsrepositorypy">""" ) self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = u"/vcs_test_hg/changeset/" "27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s" % ID print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def test_create(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) self.assertTrue('''<div class="comments-number">%s ''' '''comment(s) (0 inline)</div>''' % 1 in response.body) self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = (u'/vcs_test_hg/changeset/' '27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s' % ID) print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def test_delete(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 1) comment_id = comments[0].comment_id self.app.delete(url(controller='changeset', action='delete_comment', repo_name=HG_REPO, comment_id=comment_id)) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 0) response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) response.mustcontain('''<div class="comments-number">0 comments''' ''' (0 inline)</div>''')
def test_create(self): self.log_user() rev = "27cd5cce30c96924232dffcd24178a07ffeb5dfc" text = u"CommentOnRevision" params = {"text": text} response = self.app.post( url(controller="changeset", action="comment", repo_name=HG_REPO, revision=rev), params=params ) # Test response... self.assertEqual(response.status, "302 Found") response.follow() response = self.app.get(url(controller="changeset", action="index", repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) self.assertTrue("""<div class="comments-number">%s """ """comment(s) (0 inline)</div>""" % 1 in response.body) self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = u"/vcs_test_hg/changeset/" "27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s" % ID print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def test_create(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">%s comment ''' '''(0 inline)</div>''' % 1) self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = (u'/vcs_test_hg/changeset/' '27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s' % ID) print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def test_delete(self, backend): self.log_user() commit_id = backend.repo.get_commit('300').raw_id text = u'CommentOnCommit' params = {'text': text, 'csrf_token': self.csrf_token} self.app.post(url(controller='changeset', action='comment', repo_name=backend.repo_name, revision=commit_id), params=params) comments = ChangesetComment.query().all() assert len(comments) == 1 comment_id = comments[0].comment_id self.app.post(url(controller='changeset', action='delete_comment', repo_name=backend.repo_name, comment_id=comment_id), params={ '_method': 'delete', 'csrf_token': self.csrf_token }) comments = ChangesetComment.query().all() assert len(comments) == 0 response = self.app.get( url(controller='changeset', action='index', repo_name=backend.repo_name, revision=commit_id)) assert_comment_links(response, 0, 0)
def test_delete(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 1) comment_id = comments[0].comment_id self.app.delete( url(controller='changeset', action='delete_comment', repo_name=HG_REPO, comment_id=comment_id)) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 0) response = self.app.get( url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) response.mustcontain('''<div class="comments-number">0 comments''' ''' (0 inline)</div>''')
def test_create_inline(self, backend): self.log_user() commit_id = backend.repo.get_commit('300').raw_id text = u'CommentOnCommit' f_path = 'vcs/web/simplevcs/views/repository.py' line = 'n1' params = { 'text': text, 'f_path': f_path, 'line': line, 'csrf_token': self.csrf_token } self.app.post(url(controller='changeset', action='comment', repo_name=backend.repo_name, revision=commit_id), params=params) response = self.app.get( url(controller='changeset', action='index', repo_name=backend.repo_name, revision=commit_id)) # test DB assert ChangesetComment.query().count() == 1 assert_comment_links(response, 0, ChangesetComment.query().count()) response.mustcontain( '''class="inline-comment-placeholder" ''' '''path="vcs/web/simplevcs/views/repository.py" ''' '''target_id="vcswebsimplevcsviewsrepositorypy"''') assert Notification.query().count() == 1 assert ChangesetComment.query().count() == 1 notification = Notification.query().all()[0] comment = ChangesetComment.query().first() assert notification.type_ == Notification.TYPE_CHANGESET_COMMENT assert comment.revision == commit_id sbj = 'commented on commit of {0}'.format(backend.repo_name) assert sbj in notification.subject lnk = (u'/{0}/changeset/{1}#comment-{2}'.format( backend.repo_name, commit_id, comment.comment_id)) assert lnk in notification.body assert 'on line n1' in notification.body
def get_comments(self, repo_id, revision=None, pull_request=None): """ Get's main comments based on revision or pull_request_id :param repo_id: :type repo_id: :param revision: :type revision: :param pull_request: :type pull_request: """ q = ChangesetComment.query()\ .filter(ChangesetComment.repo_id == repo_id)\ .filter(ChangesetComment.line_no == None)\ .filter(ChangesetComment.f_path == None) if revision: q = q.filter(ChangesetComment.revision == revision) elif pull_request: pull_request = self.__get_pull_request(pull_request) q = q.filter(ChangesetComment.pull_request == pull_request) else: raise Exception('Please specify revision or pull_request') q = q.order_by(ChangesetComment.created_on) return q.all()
def test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@test_regular check CommentOnRevision' params = {'text':text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">%s ''' '''comment (0 inline)</div>''' % 1) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
def test_create_with_mention(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'@test_regular check CommentOnRevision' params = {'text': text} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get( url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">%s ''' '''comment (0 inline)</div>''' % 1) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u'test_admin', u'test_regular'])
def setUp(self): for x in ChangesetComment.query().all(): self.Session.delete(x) self.Session.commit() for x in Notification.query().all(): self.Session.delete(x) self.Session.commit()
def tearDown(self): for x in ChangesetComment.query().all(): self.Session.delete(x) self.Session.commit() for x in Notification.query().all(): self.Session.delete(x) self.Session.commit()
def cleanup(self): for x in ChangesetComment.query().all(): Session().delete(x) Session().commit() for x in Notification.query().all(): Session().delete(x) Session().commit()
def setUp(self): for x in ChangesetComment.query().all(): Session().delete(x) Session().commit() for x in Notification.query().all(): Session().delete(x) Session().commit()
def delete_comment(self, repo_name, comment_id): co = ChangesetComment.get(comment_id) owner = lambda: co.author.user_id == c.rhodecode_user.user_id if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: ChangesetCommentsModel().delete(comment=co) Session.commit() return True else: raise HTTPForbidden()
def delete_comment(self, repo_name, comment_id): comment = ChangesetComment.get(comment_id) owner = (comment.author.user_id == c.rhodecode_user.user_id) is_repo_admin = h.HasRepoPermissionAny('repository.admin')(c.repo_name) if h.HasPermissionAny('hg.admin')() or is_repo_admin or owner: ChangesetCommentsModel().delete(comment=comment) Session().commit() return True else: raise HTTPForbidden()
def prepare(self, request, pylonsapp): for x in ChangesetComment.query().all(): Session().delete(x) Session().commit() for x in Notification.query().all(): Session().delete(x) Session().commit() request.addfinalizer(self.cleanup)
def test_create_inline(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' f_path = 'vcs/web/simplevcs/views/repository.py' line = 'n1' params = {'text': text, 'f_path': f_path, 'line': line} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get( url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) #test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain('''<div class="comments-number">0 comments''' ''' (%s inline)</div>''' % 1) response.mustcontain( '''<div style="display:none" class="inline-comment-placeholder" ''' '''path="vcs/web/simplevcs/views/repository.py" ''' '''target_id="vcswebsimplevcsviewsrepositorypy">''') self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = (u'/vcs_test_hg/changeset/' '27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s' % ID) print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def test_delete(self): self.log_user() rev = "27cd5cce30c96924232dffcd24178a07ffeb5dfc" text = u"CommentOnRevision" params = {"text": text} response = self.app.post( url(controller="changeset", action="comment", repo_name=HG_REPO, revision=rev), params=params ) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 1) comment_id = comments[0].comment_id self.app.delete(url(controller="changeset", action="delete_comment", repo_name=HG_REPO, comment_id=comment_id)) comments = ChangesetComment.query().all() self.assertEqual(len(comments), 0) response = self.app.get(url(controller="changeset", action="index", repo_name=HG_REPO, revision=rev)) self.assertTrue("""<div class="comments-number">0 comment(s)""" """ (0 inline)</div>""" in response.body)
def get_all_comments(self, repo_id, revision=None, pull_request=None): q = ChangesetComment.query()\ .filter(ChangesetComment.repo_id == repo_id) if revision: q = q.filter(ChangesetComment.revision == revision) elif pull_request: pull_request = self.__get_pull_request(pull_request) q = q.filter(ChangesetComment.pull_request == pull_request) else: raise Exception('Please specify commit or pull_request') q = q.order_by(ChangesetComment.created_on) return q.all()
def test_create_inline(self): self.log_user() rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' text = u'CommentOnRevision' f_path = 'vcs/web/simplevcs/views/repository.py' line = 'n1' params = {'text': text, 'f_path': f_path, 'line': line} response = self.app.post(url(controller='changeset', action='comment', repo_name=HG_REPO, revision=rev), params=params) # Test response... self.assertEqual(response.status, '302 Found') response.follow() response = self.app.get(url(controller='changeset', action='index', repo_name=HG_REPO, revision=rev)) #test DB self.assertEqual(ChangesetComment.query().count(), 1) response.mustcontain( '''<div class="comments-number">0 comments''' ''' (%s inline)</div>''' % 1 ) response.mustcontain( '''<div style="display:none" class="inline-comment-placeholder" ''' '''path="vcs/web/simplevcs/views/repository.py" ''' '''target_id="vcswebsimplevcsviewsrepositorypy">''' ) self.assertEqual(Notification.query().count(), 1) self.assertEqual(ChangesetComment.query().count(), 1) notification = Notification.query().all()[0] ID = ChangesetComment.query().first().comment_id self.assertEqual(notification.type_, Notification.TYPE_CHANGESET_COMMENT) sbj = (u'/vcs_test_hg/changeset/' '27cd5cce30c96924232dffcd24178a07ffeb5dfc#comment-%s' % ID) print "%s vs %s" % (sbj, notification.subject) self.assertTrue(sbj in notification.subject)
def delete_comment(self, repo_name, comment_id): co = ChangesetComment.get(comment_id) if co.pull_request.is_closed(): #don't allow deleting comments on closed pull request raise HTTPForbidden() owner = lambda: co.author.user_id == c.rhodecode_user.user_id if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: ChangesetCommentsModel().delete(comment=co) Session().commit() return True else: raise HTTPForbidden()
def delete_comment(self, repo_name, comment_id): co = ChangesetComment.get(comment_id) if co.pull_request.is_closed(): #don't allow deleting comments on closed pull request raise HTTPForbidden() owner = co.author.user_id == c.rhodecode_user.user_id if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: ChangesetCommentsModel().delete(comment=co) Session().commit() return True else: raise HTTPForbidden()
def test_create(self, backend): self.log_user() commit_id = backend.repo.get_commit('300').raw_id text = u'CommentOnCommit' params = {'text': text, 'csrf_token': self.csrf_token} self.app.post(url(controller='changeset', action='comment', repo_name=backend.repo_name, revision=commit_id), params=params) response = self.app.get( url(controller='changeset', action='index', repo_name=backend.repo_name, revision=commit_id)) # test DB assert ChangesetComment.query().count() == 1 assert_comment_links(response, ChangesetComment.query().count(), 0) assert Notification.query().count() == 1 assert ChangesetComment.query().count() == 1 notification = Notification.query().all()[0] comment_id = ChangesetComment.query().first().comment_id assert notification.type_ == Notification.TYPE_CHANGESET_COMMENT sbj = 'commented on commit of {0}'.format(backend.repo_name) assert sbj in notification.subject lnk = (u'/{0}/changeset/{1}#comment-{2}'.format( backend.repo_name, commit_id, comment_id)) assert lnk in notification.body
def _delete_comment(self, comment_id): comment_id = safe_int(comment_id) co = ChangesetComment.get_or_404(comment_id) if co.pull_request.is_closed(): # don't allow deleting comments on closed pull request raise HTTPForbidden() is_owner = co.author.user_id == c.rhodecode_user.user_id is_repo_admin = h.HasRepoPermissionAny('repository.admin')(c.repo_name) if h.HasPermissionAny('hg.admin')() or is_repo_admin or is_owner: old_calculated_status = co.pull_request.calculated_review_status() ChangesetCommentsModel().delete(comment=co) Session().commit() calculated_status = co.pull_request.calculated_review_status() if old_calculated_status != calculated_status: PullRequestModel()._trigger_pull_request_hook( co.pull_request, c.rhodecode_user, 'review_status_change') return True else: raise HTTPForbidden()
def get_comments(self, repo_id, revision=None, pull_request=None): """ Get's main comments based on revision or pull_request_id :param repo_id: :param revision: :param pull_request: """ q = ChangesetComment.query()\ .filter(ChangesetComment.repo_id == repo_id)\ .filter(ChangesetComment.line_no == None)\ .filter(ChangesetComment.f_path == None) if revision: q = q.filter(ChangesetComment.revision == revision) elif pull_request: pull_request = self.__get_pull_request(pull_request) q = q.filter(ChangesetComment.pull_request == pull_request) else: raise Exception('Please specify revision or pull_request') q = q.order_by(ChangesetComment.created_on) return q.all()
def _link_comments_to_version(self, pull_request_version): """ Link all unlinked comments of this pull request to the given version. :param pull_request_version: The `PullRequestVersion` to which the comments shall be linked. """ pull_request = pull_request_version.pull_request comments = ChangesetComment.query().filter( # TODO: johbo: Should we query for the repo at all here? # Pending decision on how comments of PRs are to be related # to either the source repo, the target repo or no repo at all. ChangesetComment.repo_id == pull_request.target_repo.repo_id, ChangesetComment.pull_request == pull_request, ChangesetComment.pull_request_version == None) # TODO: johbo: Find out why this breaks if it is done in a bulk # operation. for comment in comments: comment.pull_request_version_id = ( pull_request_version.pull_request_version_id) Session().add(comment)
def test_create_with_mention(self): self.log_user() rev = "27cd5cce30c96924232dffcd24178a07ffeb5dfc" text = u"@test_regular check CommentOnRevision" params = {"text": text} response = self.app.post( url(controller="changeset", action="comment", repo_name=HG_REPO, revision=rev), params=params ) # Test response... self.assertEqual(response.status, "302 Found") response.follow() response = self.app.get(url(controller="changeset", action="index", repo_name=HG_REPO, revision=rev)) # test DB self.assertEqual(ChangesetComment.query().count(), 1) self.assertTrue("""<div class="comments-number">%s """ """comment(s) (0 inline)</div>""" % 1 in response.body) self.assertEqual(Notification.query().count(), 2) users = [x.user.username for x in UserNotification.query().all()] # test_regular get's notification by @mention self.assertEqual(sorted(users), [u"test_admin", u"test_regular"])
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None): """ Creates new comment for changeset or pull request. IF status_change is not none this comment is associated with a status change of changeset or changesets associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: """ if not text: return repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no if revision: cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment.revision = revision elif pull_request: pull_request = self.__get_pull_request(pull_request) comment.pull_request = pull_request desc = pull_request.pull_request_id else: raise Exception('Please specify revision or pull_request_id') self.sa.add(comment) self.sa.flush() # make notification line = '' body = text #changeset if revision: if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) notification_type = Notification.TYPE_CHANGESET_COMMENT # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] email_kwargs = { 'status_change': status_change, } #pull request elif pull_request: _url = h.url('pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) notification_type = Notification.TYPE_PULL_REQUEST_COMMENT # get the current participants of this pull request recipients = ChangesetComment.get_users(pull_request_id= pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } # create notification objects, and emails NotificationModel().create( created_by=user, subject=subj, body=body, recipients=recipients, type_=notification_type, email_kwargs=email_kwargs ) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: email_kwargs.update({'pr_mention': True}) subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user, subject=subj, body=body, recipients=mention_recipients, type_=notification_type, email_kwargs=email_kwargs ) return comment
def get_comments(self, repo_id, revision): return ChangesetComment.query()\ .filter(ChangesetComment.repo_id == repo_id)\ .filter(ChangesetComment.revision == revision)\ .filter(ChangesetComment.line_no == None)\ .filter(ChangesetComment.f_path == None).all()
def create(self, text, repo_id, user_id, revision, f_path=None, line_no=None): """ Creates new comment for changeset :param text: :param repo_id: :param user_id: :param revision: :param f_path: :param line_no: """ if text: repo = Repository.get(repo_id) cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment = ChangesetComment() comment.repo = repo comment.user_id = user_id comment.revision = revision comment.text = text comment.f_path = f_path comment.line_no = line_no self.sa.add(comment) self.sa.flush() # make notification line = '' if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(commit_desc)s %(line)s' % \ {'commit_desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) body = text # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT ) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=mention_recipients, type_=Notification.TYPE_CHANGESET_COMMENT ) return comment
def _get_notification_data(self, repo, comment, user, comment_text, line_no=None, revision=None, pull_request=None, status_change=None, closing_pr=False): """ Get notification data :param comment_text: :param line: :returns: tuple (subj,body,recipients,notification_type,email_kwargs) """ # make notification body = comment_text # text of the comment line = '' if line_no: line = _('on line %s') % line_no #changeset if revision: notification_type = Notification.TYPE_CHANGESET_COMMENT cs = repo.scm_instance.get_changeset(revision) desc = "%s" % (cs.short_id) _url = h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re changeset: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) email_subject = '%s commented on changeset %s' % \ (user.username, h.short_id(revision)) # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system cs_author = User.get_from_cs_author(cs.author) if not cs_author: #use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] email_kwargs = { 'status_change': status_change, 'cs_comment_user': h.person(user.email), 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name, qualified=True), 'cs_comment_url': _url, 'raw_id': revision, 'message': cs.message } #pull request elif pull_request: notification_type = Notification.TYPE_PULL_REQUEST_COMMENT desc = comment.pull_request.title _url = h.url('pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ {'desc': desc, 'pr_id': comment.pull_request.pull_request_id, 'line': line}, _url) ) email_subject = '%s commented on pull request #%s' % \ (user.username, comment.pull_request.pull_request_id) # get the current participants of this pull request recipients = ChangesetComment.get_users(pull_request_id= pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_title': pull_request.title, 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'closing_pr': closing_pr, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } return subj, body, recipients, notification_type, email_kwargs, email_subject
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None, closing_pr=False, send_email=True): """ Creates new comment for changeset or pull request. IF status_change is not none this comment is associated with a status change of changeset or changesets associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: :param closing_pr: :param send_email: """ if not text: log.warning('Missing text for comment, skipping...') return repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no if revision: comment.revision = revision elif pull_request: pull_request = self.__get_pull_request(pull_request) comment.pull_request = pull_request else: raise Exception('Please specify revision or pull_request_id') Session().add(comment) Session().flush() if send_email: (subj, body, recipients, notification_type, email_kwargs, email_subject) = self._get_notification_data( repo, comment, user, comment_text=text, line_no=line_no, revision=revision, pull_request=pull_request, status_change=status_change, closing_pr=closing_pr) # create notification objects, and emails NotificationModel().create( created_by=user, subject=subj, body=body, recipients=recipients, type_=notification_type, email_kwargs=email_kwargs, email_subject=email_subject ) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: email_kwargs.update({'pr_mention': True}) subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user, subject=subj, body=body, recipients=mention_recipients, type_=notification_type, email_kwargs=email_kwargs ) return comment
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None, closing_pr=False, send_email=True, renderer=None): """ Creates new comment for commit or pull request. IF status_change is not none this comment is associated with a status change of commit or commit associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: :param closing_pr: :param send_email: """ if not text: log.warning('Missing text for comment, skipping...') return if not renderer: renderer = self._get_renderer() repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.renderer = renderer comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no #TODO (marcink): fix this and remove revision as param commit_id = revision pull_request_id = pull_request commit_obj = None pull_request_obj = None if commit_id: notification_type = EmailNotificationModel.TYPE_COMMIT_COMMENT # do a lookup, so we don't pass something bad here commit_obj = repo.scm_instance().get_commit(commit_id=commit_id) comment.revision = commit_obj.raw_id elif pull_request_id: notification_type = EmailNotificationModel.TYPE_PULL_REQUEST_COMMENT pull_request_obj = self.__get_pull_request(pull_request_id) comment.pull_request = pull_request_obj else: raise Exception('Please specify commit or pull_request_id') Session().add(comment) Session().flush() if send_email: kwargs = { 'user': user, 'renderer_type': renderer, 'repo_name': repo.repo_name, 'status_change': status_change, 'comment_body': text, 'comment_file': f_path, 'comment_line': line_no, } if commit_obj: recipients = ChangesetComment.get_users( revision=commit_obj.raw_id) # add commit author if it's in RhodeCode system cs_author = User.get_from_cs_author(commit_obj.author) if not cs_author: # use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] commit_comment_url = h.url( 'changeset_home', repo_name=repo.repo_name, revision=commit_obj.raw_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) target_repo_url = h.link_to( repo.repo_name, h.url('summary_home', repo_name=repo.repo_name, qualified=True)) # commit specifics kwargs.update({ 'commit': commit_obj, 'commit_message': commit_obj.message, 'commit_target_repo': target_repo_url, 'commit_comment_url': commit_comment_url, }) elif pull_request_obj: # get the current participants of this pull request recipients = ChangesetComment.get_users( pull_request_id=pull_request_obj.pull_request_id) # add pull request author recipients += [pull_request_obj.author] # add the reviewers to notification recipients += [x.user for x in pull_request_obj.reviewers] pr_target_repo = pull_request_obj.target_repo pr_source_repo = pull_request_obj.source_repo pr_comment_url = h.url( 'pullrequest_show', repo_name=pr_target_repo.repo_name, pull_request_id=pull_request_obj.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) # set some variables for email notification pr_target_repo_url = h.url('summary_home', repo_name=pr_target_repo.repo_name, qualified=True) pr_source_repo_url = h.url('summary_home', repo_name=pr_source_repo.repo_name, qualified=True) # pull request specifics kwargs.update({ 'pull_request': pull_request_obj, 'pr_id': pull_request_obj.pull_request_id, 'pr_target_repo': pr_target_repo, 'pr_target_repo_url': pr_target_repo_url, 'pr_source_repo': pr_source_repo, 'pr_source_repo_url': pr_source_repo_url, 'pr_comment_url': pr_comment_url, 'pr_closing': closing_pr, }) # pre-generate the subject for notification itself ( subject, _h, _e, # we don't care about those body_plaintext) = EmailNotificationModel().render_email( notification_type, **kwargs) mention_recipients = set( self._extract_mentions(text)).difference(recipients) # create notification objects, and emails NotificationModel().create( created_by=user, notification_subject=subject, notification_body=body_plaintext, notification_type=notification_type, recipients=recipients, mention_recipients=mention_recipients, email_kwargs=kwargs, ) action = ('user_commented_pull_request:{}'.format( comment.pull_request.pull_request_id) if comment.pull_request else 'user_commented_revision:{}'.format(comment.revision)) action_logger(user, action, comment.repo) return comment
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None, closing_pr=False, send_email=True): """ Creates new comment for changeset or pull request. IF status_change is not none this comment is associated with a status change of changeset or changesets associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: :param closing_pr: :param send_email: """ if not text: log.warning('Missing text for comment, skipping...') return repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no if revision: comment.revision = revision elif pull_request: pull_request = self.__get_pull_request(pull_request) comment.pull_request = pull_request else: raise Exception('Please specify revision or pull_request_id') Session().add(comment) Session().flush() if send_email: (subj, body, recipients, notification_type, email_kwargs, email_subject) = self._get_notification_data( repo, comment, user, comment_text=text, line_no=line_no, revision=revision, pull_request=pull_request, status_change=status_change, closing_pr=closing_pr) # create notification objects, and emails NotificationModel().create(created_by=user, subject=subj, body=body, recipients=recipients, type_=notification_type, email_kwargs=email_kwargs, email_subject=email_subject) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: email_kwargs.update({'pr_mention': True}) subj = _('[Mention]') + ' ' + subj NotificationModel().create(created_by=user, subject=subj, body=body, recipients=mention_recipients, type_=notification_type, email_kwargs=email_kwargs) return comment
def _get_notification_data(self, repo, comment, user, comment_text, line_no=None, revision=None, pull_request=None, status_change=None, closing_pr=False): """ Get notification data :param comment_text: :param line: :returns: tuple (subj,body,recipients,notification_type,email_kwargs) """ # make notification body = comment_text # text of the comment line = '' if line_no: line = _('on line %s') % line_no #changeset if revision: notification_type = Notification.TYPE_CHANGESET_COMMENT cs = repo.scm_instance.get_changeset(revision) desc = "%s" % (cs.short_id) _url = h.url( 'changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re changeset: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) email_subject = 'User %s commented on changeset %s' % \ (user.username, h.short_id(revision)) # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system cs_author = User.get_from_cs_author(cs.author) if not cs_author: #use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] email_kwargs = { 'status_change': status_change, 'cs_comment_user': h.person(user.email), 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name, qualified=True), 'cs_comment_url': _url, 'raw_id': revision, 'message': cs.message } #pull request elif pull_request: notification_type = Notification.TYPE_PULL_REQUEST_COMMENT desc = comment.pull_request.title _url = h.url( 'pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ {'desc': desc, 'pr_id': comment.pull_request.pull_request_id, 'line': line}, _url) ) email_subject = 'User %s commented on pull request #%s' % \ (user.username, comment.pull_request.pull_request_id) # get the current participants of this pull request recipients = ChangesetComment.get_users( pull_request_id=pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'closing_pr': closing_pr, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } return subj, body, recipients, notification_type, email_kwargs, email_subject
def create(self, text, repo_id, user_id, revision, f_path=None, line_no=None): """ Creates new comment for changeset :param text: :param repo_id: :param user_id: :param revision: :param f_path: :param line_no: """ if text: repo = Repository.get(repo_id) cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment = ChangesetComment() comment.repo = repo comment.user_id = user_id comment.revision = revision comment.text = text comment.f_path = f_path comment.line_no = line_no self.sa.add(comment) self.sa.flush() # make notification line = '' if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(commit_desc)s %(line)s' % \ {'commit_desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) body = text # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=mention_recipients, type_=Notification.TYPE_CHANGESET_COMMENT) return comment