def test_gets_by_target_and_ref(self): # commit self.clear_comments(CommitLineComment, TARGET_ID, FROM_SHA) c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) c2 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) c3 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 3 self.clear_comments(PullLineComment, TARGET_ID, FROM_SHA) # pull PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 2
def _q_lookup(self, request, comment_id): comment = PullLineComment.get(comment_id) if not comment: raise TraversalError("Unable to find comment %s" % comment_id) else: self.code_review = comment return self
def migrate_pull_linecomments(): print '## run migrate_pull_linecomments' rs = store.execute("select id " "from codedouban_ticket_codereview") for r in rs: id, = r c = TicketCodereview.get(id) print '>>>', c.id, c.ticket_id, c.from_ref, c.content PATH = c.path if c.path else '' new_c = PullLineComment.add_raw(c.ticket_id, c.from_ref, EMPTY_TO_SHA, PATH, c.new_path if c.new_path else PATH, EMPTY_OIDS, EMPTY_OIDS, c.old, c.new, c.author, c.content, c.position, c.time, c.time) tn = TicketNode.add_codereview(new_c) print '>>> INSERT TicketNode', tn.id, tn.type_id, tn.author, tn.ticket_id print "migrate_pull_linecomments: %s records" % len(rs)
def files_tab(self, request): # call by ajax user = request.user current_user = request.user ticket = self.ticket pullreq = self.pullreq project = self.project has_proj_perm = project.has_push_perm(user.name) if user else False show_merge_guide = ( has_proj_perm or user.username == ticket.author) \ if user and not ticket.closed else False # get line comments # commits = self.all_commits # last_commit_ref = commits[0].sha if commits else '' # linecomments = PullLineComment.gets_by_target_and_ref( # self.ticket.id, last_commit_ref) linecomments = PullLineComment.gets_by_target(self.ticket.id) # get diff(patches) # FIXME: ignore_space,这是被ajax请求的,js那边没这逻辑。应该在前端加一个按钮 whitespace = request.get_form_var('w', '0') if whitespace.isdigit() and int(whitespace) == 1: ignore = True else: ignore = False try: diff = pullreq.get_diff(ignore_space=ignore, rename_detection=True, linecomments=linecomments) except: # FIXME: diff 的默认值不该为 [],其他很多地方也是 diff = [] return st('/pull/diffs_pane.html', **locals())
def _q_lookup(self, request, comment_id): comment = PullLineComment.get(comment_id) if not comment: raise TraversalError( "Unable to find comment %s" % comment_id) else: self.code_review = comment return self
def test_ticket_code_review(self): title = 'test title' desc = 'test desc' author = 'testuser' p1_t1 = Ticket.add(self.proj1.id, title, desc, author) pullreq1 = PullRequest.open(self.proj1_fork, 'master', self.proj1, 'master') pullreq1 = pullreq1.insert(p1_t1.ticket_number) # ticket code review path = '/README.md' #position = 10 from_sha = '454418c61cd7ef1a65818121746b45064a5af5d6' oid = '454418c61cd7ef1a65818121746b45064a5af5d6' codereview = p1_t1.add_codereview(from_sha, '', path, path, oid, oid, LINECOMMENT_INDEX_EMPTY, LINECOMMENT_INDEX_EMPTY, author, 'comment content') assert codereview.ticket_id == p1_t1.id assert codereview.path == path assert codereview.from_sha == from_sha # test update content assert codereview.content == 'comment content' codereview.update('content updated') codereview = PullLineComment.get(codereview.id) assert codereview.content == 'content updated' codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 1 p1_t1.add_codereview(from_sha, '', path, path, oid, oid, LINECOMMENT_INDEX_EMPTY, LINECOMMENT_INDEX_EMPTY, author, 'another comment content') codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 2 # test delete comment codereview.delete() codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 1
def add_codereview(self, from_sha, to_sha, old_path, new_path, from_oid, to_oid, old_linenum, new_linenum, author, content): new_path = new_path or old_path self.add_participant(author) codereview = PullLineComment.add(self.id, from_sha, to_sha, old_path, new_path, from_oid, to_oid, old_linenum, new_linenum, author, content) TicketNode.add_codereview(codereview) return codereview
def test_ticket_code_review(self): title = 'test title' desc = 'test desc' author = 'testuser' p1_t1 = Ticket.add(self.proj1.id, title, desc, author) pullreq1 = PullRequest.open( self.proj1_fork, 'master', self.proj1, 'master') pullreq1 = pullreq1.insert(p1_t1.ticket_number) # ticket code review path = '/README.md' #position = 10 from_sha = '454418c61cd7ef1a65818121746b45064a5af5d6' oid = '454418c61cd7ef1a65818121746b45064a5af5d6' codereview = p1_t1.add_codereview( from_sha, '', path, path, oid, oid, LINECOMMENT_INDEX_EMPTY, LINECOMMENT_INDEX_EMPTY, author, 'comment content') assert codereview.ticket_id == p1_t1.id assert codereview.path == path assert codereview.from_sha == from_sha #test update content assert codereview.content == 'comment content' codereview.update('content updated') codereview = PullLineComment.get(codereview.id) assert codereview.content == 'content updated' codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 1 p1_t1.add_codereview( from_sha, '', path, path, oid, oid, LINECOMMENT_INDEX_EMPTY, LINECOMMENT_INDEX_EMPTY, author, 'another comment content') codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 2 #test delete comment codereview.delete() codereviews = PullLineComment.gets_by_target_and_ref( p1_t1.id, from_sha) assert len(codereviews) == 1
def test_delete_comment(self): # commit c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 1 c1.delete() cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 0 # pull c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 1 c2.delete() cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 0
def edit(self, request): user = request.user project = CodeDoubanProject.get_by_name(self.proj_name) content = request.get_form_var( 'pull_request_review_comment', '').decode('utf-8') if self.code_review.author == user.name: self.code_review.update(content) linecomment = PullLineComment.get(self.code_review.id) pullreq = True return dict( r=0, html=st('/pull/ticket_linecomment.html', **locals())) return dict(r=1)
def edit(self, request): user = request.user project = CodeDoubanProject.get_by_name(self.proj_name) content = request.get_form_var('pull_request_review_comment', '').decode('utf-8') if self.code_review.author == user.name: self.code_review.update(content) linecomment = PullLineComment.get(self.code_review.id) pullreq = True return dict(r=0, html=st('/pull/ticket_linecomment.html', **locals())) return dict(r=1)
def test_delete_comment(self): # commit self.clear_comments(CommitLineComment, TARGET_ID, FROM_SHA) c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 1 c1.delete() cs = CommitLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 0 # pull self.clear_comments(PullLineComment, TARGET_ID, FROM_SHA) c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 1 c2.delete() cs = PullLineComment.gets_by_target_and_ref(TARGET_ID, FROM_SHA) assert len(cs) == 0
def get_codereviews_group(self): # FIXME: refactor this # group = { linecomment_id: ... } group = {} codereviews = PullLineComment.gets_by_target(self.id) comparer = lambda x: (x.old_path + (str(x.oids) if x.has_oids else x.from_sha) + str(x.linenum if x.has_linenum else x.position)) codereviews.sort(key=comparer) for k, v in groupby(codereviews, key=comparer): crs = list(v) codereview = crs[0] group[codereview.id] = crs # 相当于缩点了 return group
def test_update_comment(self): # commit c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) assert c1.content == CONTENT1 c1.update(CONTENT2) c1 = CommitLineComment.get(c1.id) assert c1.content == CONTENT2 # pull c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT2) assert c2.content == CONTENT2 c2.update(CONTENT_ZH) c2 = CommitLineComment.get(c2.id) assert c2.content == CONTENT_ZH
def test_add_comment(self): # commit c1 = CommitLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT1) assert c1.target_id == TARGET_ID assert c1.target_type == LINECOMMENT_TYPE_COMMIT assert c1.from_sha == FROM_SHA assert c1.to_sha == TO_SHA assert c1.old_path == OLD_PATH assert c1.new_path == NEW_PATH assert c1.from_oid == FROM_OID assert c1.to_oid == TO_OID assert c1.old_linenum == 20 assert c1.new_linenum == 30 assert c1.linenum == (20, 30) assert c1.author == AUTHOR assert c1.content == CONTENT1 assert c1.position is None assert c1.paths # pull c2 = PullLineComment.add(TARGET_ID, FROM_SHA, TO_SHA, OLD_PATH, NEW_PATH, FROM_OID, TO_OID, 20, 30, AUTHOR, CONTENT2) assert c2.target_id == TARGET_ID assert c2.target_type == LINECOMMENT_TYPE_PULL assert c2.from_sha == FROM_SHA assert c2.to_sha == TO_SHA assert c2.old_path == OLD_PATH assert c2.new_path == NEW_PATH assert c2.from_oid == FROM_OID assert c2.to_oid == TO_OID assert c2.old_linenum == 20 assert c2.new_linenum == 30 assert c2.linenum == (20, 30) assert c2.author == AUTHOR assert c2.content == CONTENT2 assert c2.position is None assert c2.paths
def _toggle_whitespace(self, request, project, paths, ignore_space, **kwargs): # For pull/new from_proj = request.get_form_var('from_proj') from_ref = request.get_form_var('from_ref') to_ref = request.get_form_var('to_ref') to_proj = request.get_form_var('to_proj') sha1 = kwargs.get('sha1') context_lines = kwargs.get('context_lines') ref = sha1 if ref is None: ref = project.default_branch if hasattr(self, 'ticket'): pullreq = PullRequest.get_by_proj_and_ticket( project.id, self.ticket.ticket_number) linecomments = PullLineComment.gets_by_target(self.ticket.id) else: from_proj = CodeDoubanProject.get_by_name(from_proj) parent_proj = from_proj.get_forked_from() if to_proj: to_proj = CodeDoubanProject.get_by_name(to_proj) elif parent_proj: to_proj = parent_proj else: to_proj = from_proj pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) linecomments = [] kw = { 'paths': paths, 'rename_detection': True, 'linecomments': linecomments } if context_lines is not None: kw.update({'context_lines': context_lines}) diff = pullreq.get_diff(**kw) return diff
def codereview(self): if self.type == TICKET_NODE_TYPE_LINECOMMENT: return PullLineComment.get(self.type_id)
def get_codereviews(self): codereviews = PullLineComment.gets_by_target(self.id) return codereviews