def search(self, request): key_word = request.get_form_var('q') if not key_word: return self._index(request) status = request.get_form_var('status') user = request.user page = request.get_form_var('page', 1) project = self.project tickets = [] ticket_len = Ticket.get_count_by_proj_id(project.id) search_result = PullRequestSearch.search_a_phrase(key_word, project.id, size=ticket_len) if search_result and not search_result.get('error'): ticket_ids = [ id for id, in SearchEngine.decode(search_result, ['ticket_id']) ] tickets = Ticket.gets_by_projectid_and_ticketnumbers( project.id, ticket_ids) if status == "closed": tickets = [t for t in tickets if t.closed] else: tickets = [t for t in tickets if not t.closed] ticket_total_len = len(tickets) limit = TICKETS_COUNT_PER_PAGE start = TICKETS_COUNT_PER_PAGE * (int(page) - 1) tickets = tickets[start:start + limit] n_pages = (ticket_total_len - 1) / TICKETS_COUNT_PER_PAGE + 1 if status == "closed": is_closed_tab = True else: is_closed_tab = False open_tab_link = self.open_tab_link close_tab_link = self.close_tab_link return st('/pull/pulls.html', **locals())
def search(self, request): key_word = request.get_form_var('q') if not key_word: return self._index(request) status = request.get_form_var('status') user = request.user page = request.get_form_var('page', 1) project = self.project tickets = [] ticket_len = Ticket.get_count_by_proj_id(project.id) search_result = PullRequestSearch.search_a_phrase( key_word, project.id, size=ticket_len) if search_result and not search_result.get('error'): ticket_ids = [id for id, in SearchEngine.decode( search_result, ['ticket_id'])] tickets = Ticket.gets_by_projectid_and_ticketnumbers( project.id, ticket_ids) if status == "closed": tickets = [t for t in tickets if t.closed] else: tickets = [t for t in tickets if not t.closed] ticket_total_len = len(tickets) limit = TICKETS_COUNT_PER_PAGE start = TICKETS_COUNT_PER_PAGE * (int(page) - 1) tickets = tickets[start:start + limit] n_pages = (ticket_total_len - 1) / TICKETS_COUNT_PER_PAGE + 1 if status == "closed": is_closed_tab = True else: is_closed_tab = False open_tab_link = self.open_tab_link close_tab_link = self.close_tab_link return st('/pull/pulls.html', **locals())
def test_ticket_gets_by_proj_and_author(self): title = 'test title' desc = 'test desc' author = 'testuser' Ticket.add(self.proj1.id, title, desc, author) assert Ticket.gets_by_proj_and_author(self.proj1.id, author) assert not Ticket.gets_by_proj_and_author(self.proj1.id, author='anonuser')
def test_ticket_gets_by_proj_and_author(self): title = 'test title' desc = 'test desc' author = 'testuser' Ticket.add(self.proj1.id, title, desc, author) assert Ticket.gets_by_proj_and_author(self.proj1.id, author) assert not Ticket.gets_by_proj_and_author( self.proj1.id, author='anonuser')
def get_tickets(self, project, page, closed=False): tickets = Ticket.gets_by_proj( project.id, closed=closed, limit=TICKETS_COUNT_PER_PAGE, start=TICKETS_COUNT_PER_PAGE * (int(page) - 1)) if closed: tickets = sorted(tickets, key=lambda x: x.closed, reverse=True) ticket_total_len = Ticket.get_count_by_proj(project.id, closed=closed) return tickets, ticket_total_len
def get_tickets(self, project, page, closed=False): tickets = Ticket.gets_by_proj(project.id, closed=closed, limit=TICKETS_COUNT_PER_PAGE, start=TICKETS_COUNT_PER_PAGE * (int(page) - 1)) if closed: tickets = sorted(tickets, key=lambda x: x.closed, reverse=True) ticket_total_len = Ticket.get_count_by_proj(project.id, closed=closed) return tickets, ticket_total_len
def _q_index(self, request): user = request.user team = self.team page = request.get_form_var('page', 1) tickets = Ticket.gets_by_team_id( team.id, limit=TICKETS_COUNT_PER_PAGE, start=TICKETS_COUNT_PER_PAGE * (int(page) - 1)) or [] ticket_total_len = Ticket.get_count_by_team_id(team.id) or 0 is_closed_tab = False n_pages = (ticket_total_len - 1) / TICKETS_COUNT_PER_PAGE + 1 return st('/teams/team_pulls.html', **locals())
def _q_index(self, request): user = request.user team = self.team page = request.get_form_var('page', 1) tickets = Ticket.gets_by_team_id(team.id, limit=TICKETS_COUNT_PER_PAGE, start=TICKETS_COUNT_PER_PAGE * (int(page) - 1)) or [] ticket_total_len = Ticket.get_count_by_team_id(team.id) or 0 is_closed_tab = False n_pages = (ticket_total_len - 1) / TICKETS_COUNT_PER_PAGE + 1 return st('/teams/team_pulls.html', **locals())
def test_ticket_close(self): # close ticket title = 'test title' desc = 'test desc' author = 'testuser' p2_t1 = Ticket.add(self.proj2.id, title, desc, author) pullreq2 = PullRequest.open(self.proj2_fork, 'master', self.proj2, 'master') pullreq2 = pullreq2.insert(p2_t1.ticket_number) assert p2_t1.closed is None p2_t1.close('testuser') assert Ticket.get(p2_t1.id).closed is not None
def contribution_detail(self, request): req_date = request.get_form_var("date") if req_date: try: req_date = dateutil.parser.parse(req_date).astimezone(dateutil.tz.tzoffset("EST", 8 * 3600)) except ValueError as e: return "" contributions = UserContributions.get_by_date(self.name, req_date) owned = contributions.get("owned_tickets") commented = contributions.get("commented_tickets") owned_tickets = filter(None, [Ticket.get(id_) for id_ in owned]) commented_tickets = filter(None, [Ticket.get(comment[0]) for comment in commented]) return st("people_contribution_detail.html", **locals()) return ""
def test_ticket_close(self): # close ticket title = 'test title' desc = 'test desc' author = 'testuser' p2_t1 = Ticket.add(self.proj2.id, title, desc, author) pullreq2 = PullRequest.open( self.proj2_fork, 'master', self.proj2, 'master') pullreq2 = pullreq2.insert(p2_t1.ticket_number) assert p2_t1.closed is None p2_t1.close('testuser') assert Ticket.get(p2_t1.id).closed is not None
def add(cls, target_id, from_sha, to_sha, old_path, new_path, from_oid, to_oid, old_linenum, new_linenum, author, content): # TODO: dispatch 放到 view 里 from vilya.models.ticket import Ticket from vilya.libs.signals import codereview_signal from dispatches import dispatch comment = super(PullLineComment, cls).add(target_id, from_sha, to_sha, old_path, new_path, from_oid, to_oid, old_linenum, new_linenum, author, content) ticket = Ticket.get(target_id) # TODO: 重构feed之后取消signal发送 if ticket: codereview_signal.send(comment, content=content, ticket=ticket, author=author, comment=comment) dispatch('codereview', data={ 'comment': comment, 'ticket': ticket, 'sender': author, }) return comment
def _q_index(request): user = request.user if user: list_type = request.get_form_var("list_type", "invited") n_invited = user.n_open_invited n_participated = user.n_open_participated n_yours = user.n_user_open_submit_pull_requests counts = [n_invited, n_participated, n_yours, None] tab_info = [] for tab, count in zip(MY_PULL_REQUESTS_TAB_INFO, counts): tab.update(count=count) tab_info.append(tab) if list_type == "participated": tickets = user.get_participated_pull_requests() elif list_type == "yours": tickets = user.get_user_submit_pull_requests() elif list_type == "explore": from vilya.models.ticket import Ticket tickets = Ticket.gets_all_opened() ticket_total_len = len(tickets) shuffle(tickets) else: tickets = user.get_invited_pull_requests() is_closed_tab = False ticket_total_len = len(tickets) return st('my_pull_requests.html', **locals())
def get(self, request): _date_from = None _date_to = None state = request.get_form_var('state', '') author = request.get_form_var('author', '') date_from = request.get_form_var('from', '') date_to = request.get_form_var('to', '') if not state or state == "open": closed = False else: closed = True if date_from: _date_from = date_from if date_to: _date_to = date_to tickets = Ticket.gets(project_id=self.repo.id, author=author, date_from=_date_from, date_to=_date_to, closed=closed) pulls = [] for t in tickets: pull = PullRequest.get_by_proj_and_ticket(self.repo.id, t.ticket_number) pulls.append(pull.as_dict()) return pulls
def test_ticket_update_desc(self): title = 'test title' desc = 'test desc' author = 'testuser' p1_t2 = Ticket.add(self.proj1.id, title, desc, author) pullreq = PullRequest.open(self.proj1_fork, 'master', self.proj1, 'master') pullreq = pullreq.insert(p1_t2.ticket_number) new_title = 'this is new title' new_desc = 'this is new desc!' p1_t2.update(new_title, new_desc) p1_t2 = Ticket.get(p1_t2.id) assert p1_t2.title == new_title assert p1_t2.description == new_desc
def create(self, request): user = request.user if not user: raise AccessError 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') title = request.get_form_var('title', '').decode('utf-8') comment = request.get_form_var('body', '').decode('utf-8') if not all([from_ref, from_proj, to_ref, to_proj]): raise TraversalError from_proj = CodeDoubanProject.get_by_name(from_proj) to_proj = CodeDoubanProject.get_by_name(to_proj) if from_proj != to_proj: if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to create PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) ticket = Ticket(None, None, to_proj.id, title, comment, user.username, None, None) pullreq = add_pull(ticket, pullreq, user) ticket = pullreq.ticket return request.redirect( str('/%s/pull/%s/' % (to_proj.name, ticket.ticket_id)))
def main(): rs = store.execute("select id " "from codedouban_ticket") for r in rs: id, = r ticket = Ticket.get(id) # update merge pullreq = PullRequest.get_by_ticket(ticket) author = pullreq.merge_by or pullreq.to_proj.owner_id time = pullreq.merge_time ticket_id = ticket.id id = 0 if not get_node(author, TICKET_NODE_TYPE_MERGE, id, ticket_id, time) and time: print id, author, time, ticket_id store.execute("insert into ticket_nodes " "(author, type, type_id, ticket_id, created_at) " "value (%s, %s, %s, %s, %s)", (author, TICKET_NODE_TYPE_MERGE, id, ticket_id, time)) store.commit() # update close author = ticket.close_by or ticket.author time = ticket.closed ticket_id = ticket.id id = 0 if not get_node(author, TICKET_NODE_TYPE_CLOSE, id, ticket_id, time) and time: print id, author, time, ticket_id store.execute("insert into ticket_nodes " "(author, type, type_id, ticket_id, created_at) " "value (%s, %s, %s, %s, %s)", (author, TICKET_NODE_TYPE_CLOSE, id, ticket_id, time)) store.commit() print "update %s close & merge pulls" % len(rs)
def test_ticket_update_desc(self): title = 'test title' desc = 'test desc' author = 'testuser' p1_t2 = Ticket.add(self.proj1.id, title, desc, author) pullreq = PullRequest.open( self.proj1_fork, 'master', self.proj1, 'master') pullreq = pullreq.insert(p1_t2.ticket_number) new_title = 'this is new title' new_desc = 'this is new desc!' p1_t2.update(new_title, new_desc) p1_t2 = Ticket.get(p1_t2.id) assert p1_t2.title == new_title assert p1_t2.description == new_desc
def test_single_project(self): skip_test() u_to = User("admin") u_from = User("testuser") to_proj = self._prj("test", "admin") self._add(to_proj, u_to, "README.md", "hi") from_proj = self._prj("testuser/test", "testuser", to_proj.id) self._add(from_proj, u_from, "README.md", "hello") pullreq = PullRequest.open(from_proj, "master", to_proj, "master") ticket = Ticket(None, None, to_proj.id, "title", "desc", "testuser", None, None) pullreq = add_pull(ticket, pullreq, u_from) iss = ProjectIssue.add(title='title1', description='desc1', creator='owner', project=to_proj.id) IssuePRSearch.index_a_project(to_proj) res = IssueSearch.search_a_phrase('title1', to_proj.id) res = SearchEngine.decode(res, ('issue_id', )) res = [id for id, in res] assert len(res) == 1 assert res[0] == iss.id res = PullRequestSearch.search_a_phrase('title', to_proj.id) res = SearchEngine.decode(res, ('issue_id', )) res = [id for id, in res] assert len(res) == 1
def contribution_detail(self, request): req_date = request.get_form_var('date') if req_date: try: req_date = dateutil.parser.parse(req_date).astimezone( dateutil.tz.tzoffset('EST', 8 * 3600)) except ValueError as e: return "" contributions = UserContributions.get_by_date(self.name, req_date) owned = contributions.get('owned_tickets') commented = contributions.get('commented_tickets') owned_tickets = filter(None, [Ticket.get(id_) for id_ in owned]) commented_tickets = filter( None, [Ticket.get(comment[0]) for comment in commented]) return st('people_contribution_detail.html', **locals()) return ""
def async_comment_to_pr(data): ''' commit comment rewrite to pr ''' type_ = data.get('type') if type_ not in ('commit_comment', 'commit_linecomment'): return comment = data.get('comment') ref = comment.get('ref') author = comment.get('author') content = comment.get('content') proj_id = comment.get('project_id') comment_uid = comment.get('comment_uid') proj = CodeDoubanProject.get(proj_id) prs = proj.open_family_pulls anchor = comment_uid for pr in prs: if ref in pr.get_commits_shas(): content = COMMENT_TEMPLATE.format(content=content, domain=DOMAIN, proj=proj.name, sha=ref, anchor=anchor) ticket = Ticket.get_by_projectid_and_ticketnumber( pr.to_proj.id, pr.ticket_id) ticket.add_comment(content, author)
def open_pulls(self): from vilya.models.ticket import Ticket from vilya.models.pull import PullRequest pulls = [PullRequest.get_by_proj_and_ticket(self.id, t.ticket_id) for t in Ticket.gets_by_proj(self.id, limit=9999)] return pulls
def test_ticket_count(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) p1_t2 = Ticket.add(self.proj1.id, title, desc, author) pullreq = PullRequest.open(self.proj1_fork, 'master', self.proj1, 'master') pullreq = pullreq.insert(p1_t2.ticket_number) # test ticket count assert int(Ticket.get_count_by_proj(self.proj1.id)) == 2
def __init__(self, project_id, ticket_number, hl_description): self.ticket = Ticket.get_by_projectid_and_ticketnumber( project_id, ticket_number) self.ticket_project = CodeDoubanProject.get(self.ticket.project_id) self.author = User(self.ticket.author) self.ticket_url = self.ticket.url self.hl_description = hl_description if hl_description \ else self.ticket.description
def contribution_detail(self, request): req_date = request.get_form_var('date') if not req_date: return {"error": "No datetime"} try: req_date = dateutil.parser.parse(req_date).astimezone( dateutil.tz.tzoffset('EST', 8 * 3600)) except ValueError: return {"error": "Invalid date"} contributions = UserContributions.get_by_date(self.user.name, req_date) owned = contributions.get('owned_tickets') commented = contributions.get('commented_tickets') pullreqs = [Ticket.get(id_).as_dict() for id_ in owned] participated = [Ticket.get(comment[0]).as_dict() for comment in commented] return {"pull requests": pullreqs, "participated": participated}
def contribution_detail(self, request): req_date = request.get_form_var('date') if not req_date: return {"error": "No datetime"} try: req_date = dateutil.parser.parse(req_date).astimezone( dateutil.tz.tzoffset('EST', 8 * 3600)) except ValueError: return {"error": "Invalid date"} contributions = UserContributions.get_by_date(self.user.name, req_date) owned = contributions.get('owned_tickets') commented = contributions.get('commented_tickets') pullreqs = [Ticket.get(id_).as_dict() for id_ in owned] participated = [ Ticket.get(comment[0]).as_dict() for comment in commented ] return {"pull requests": pullreqs, "participated": participated}
def test_ticket_count(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) p1_t2 = Ticket.add(self.proj1.id, title, desc, author) pullreq = PullRequest.open( self.proj1_fork, 'master', self.proj1, 'master') pullreq = pullreq.insert(p1_t2.ticket_number) # test ticket count assert int(Ticket.get_count_by_proj(self.proj1.id)) == 2
def new(self, request): user = request.user if not user: raise AccessError from_proj = self.project from_ref = request.get_form_var('head_ref', from_proj.default_branch) parent_proj = from_proj.get_forked_from() to_proj = request.get_form_var('base_repo') if to_proj: to_proj = CodeDoubanProject.get_by_name(to_proj) elif parent_proj: to_proj = parent_proj else: to_proj = from_proj if not to_proj: raise TraversalError("The PR's upstream project is not existed") to_ref = request.get_form_var('base_ref', to_proj.default_branch) if from_proj != to_proj: # Allow to create PR to a different project only if user has push perm # ~~A bit weird, maybe should be separate perms # ~~If from and to projects are the same, we should be in online edit mode if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to add a PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) family = from_proj.get_fork_network() from_branches = from_proj.repo.branches to_branches = to_proj.repo.branches from_commit = pullreq.from_commit to_commit = pullreq.to_commit if not pullreq.can_pull: raise TraversalError( "The PR's head_ref or base_ref is not existed") highlighted_projects = filter(None, [from_proj, parent_proj]) commits = pullreq.commits n_commits = len(commits) n_authors = len(set(c.author.username for c in commits)) ticket_title, ticket_desc = self._choose_default_PR_title_and_description( commits) # noqa # get diff diff = pullreq.get_diff(rename_detection=True) n_files = diff.length grouped_commits = groupby(commits, lambda c: c.author_time.date()) prs = PullRequest.get_by_from_and_to(from_proj.id, from_ref, to_proj.id, to_ref) open_pullreqs = [] for pr in prs: t = Ticket.get_by_projectid_and_ticketnumber( to_proj.id, pr.ticket_id) if t and t.closed is None: open_pullreqs.append(pr) guideline_url = get_project_guidelines(to_proj) teams = Team.get_all_team_uids() return st('/pull/new.html', **locals())
def new(self, request): user = request.user if not user: raise AccessError from_proj = self.project from_ref = request.get_form_var('head_ref', from_proj.default_branch) parent_proj = from_proj.get_forked_from() to_proj = request.get_form_var('base_repo') if to_proj: to_proj = CodeDoubanProject.get_by_name(to_proj) elif parent_proj: to_proj = parent_proj else: to_proj = from_proj if not to_proj: raise TraversalError("The PR's upstream project is not existed") to_ref = request.get_form_var('base_ref', to_proj.default_branch) if from_proj != to_proj: # Allow to create PR to a different project only if user has push perm # ~~A bit weird, maybe should be separate perms # ~~If from and to projects are the same, we should be in online edit mode if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to add a PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) family = from_proj.get_fork_network() from_branches = from_proj.repo.branches to_branches = to_proj.repo.branches from_commit = pullreq.from_commit to_commit = pullreq.to_commit if not pullreq.can_pull: raise TraversalError( "The PR's head_ref or base_ref is not existed") highlighted_projects = filter(None, [from_proj, parent_proj]) commits = pullreq.commits n_commits = len(commits) n_authors = len(set(c.author.username for c in commits)) ticket_title, ticket_desc = self._choose_default_PR_title_and_description(commits) # noqa # get diff diff = pullreq.get_diff(rename_detection=True) n_files = diff.length grouped_commits = groupby(commits, lambda c: c.author_time.date()) prs = PullRequest.get_by_from_and_to( from_proj.id, from_ref, to_proj.id, to_ref) open_pullreqs = [] for pr in prs: t = Ticket.get_by_projectid_and_ticketnumber( to_proj.id, pr.ticket_id) if t and t.closed is None: open_pullreqs.append(pr) guideline_url = get_project_guidelines(to_proj) teams = Team.get_all_team_uids() return st('/pull/new.html', **locals())
def __init__(self, request, proj_name, ticket_id): self.proj_name = proj_name self.ticket_id = ticket_id self.project = CodeDoubanProject.get_by_name(self.proj_name) if not self.project: raise TraversalError() self.ticket = Ticket.get_by_projectid_and_ticketnumber( self.project.id, self.ticket_id) if not self.ticket: raise TraversalError()
def _clean_closed_tickets(self): from vilya.models.ticket import Ticket # FIXME should be async function tickets = set(self.get_invited() + self.get_participated()) for ticket_id in tickets: t = Ticket.get(ticket_id) if t and t.project and not t.closed: continue self.remove_invited(ticket_id) self.remove_participated(ticket_id)
def _index(self, request): project = CodeDoubanProject.get_by_name(self.proj_name) open_tickets = Ticket.gets_by_proj(project.id, limit=9999) pr_list = [] for t in open_tickets: pullreq = PullRequest.get_by_proj_and_ticket(project.id, t.ticket_number) if pullreq: pr_list.append(pullreq.as_dict()) return pr_list
def _index(self, request): project = CodeDoubanProject.get_by_name(self.proj_name) open_tickets = Ticket.gets_by_proj(project.id, limit=9999) pr_list = [] for t in open_tickets: pullreq = PullRequest.get_by_proj_and_ticket( project.id, t.ticket_number) if pullreq: pr_list.append(pullreq.as_dict()) return pr_list
def __init__(self, proj_name, ticket_id): self.proj_name = proj_name self.ticket_id = ticket_id self.project = CodeDoubanProject.get_by_name(proj_name) self.ticket = Ticket.get_by_projectid_and_ticketnumber( self.project.id, self.ticket_id) self.pullreq = PullRequest.get_by_proj_and_ticket( self.project.id, self.ticket_id) try: self.all_commits = self.pullreq.commits except Exception: self.all_commits = self.pullreq.get_merged_commits()
def __init__(self, project_id, ticket_id): self.project_id = project_id self.ticket_id = ticket_id self.ticket = Ticket.get_by_projectid_and_ticketnumber( self.project_id, self.ticket_id) if not self.ticket: raise NotFoundError('pull') self.pullreq = PullRequest.get_by_proj_and_ticket( self.project_id, self.ticket.ticket_number)
def __init__(self, project_id, ticket_id): self.project_id = project_id self.ticket_id = ticket_id self.ticket = Ticket.get_by_projectid_and_ticketnumber(self.project_id, self.ticket_id) if not self.ticket: raise NotFoundError('pull') self.pullreq = PullRequest.get_by_proj_and_ticket( self.project_id, self.ticket.ticket_number)
def open_network_pulls(self): from vilya.models.ticket import Ticket from vilya.models.pull import PullRequest pulls = [] projects = self.get_fork_network() for project in projects: ps = [PullRequest.get_by_proj_and_ticket(project.id, t.ticket_id) for t in Ticket.gets_by_proj(project.id, limit=9999)] pulls.extend([p for p in ps if p and p.from_proj and p.from_proj.id == self.id]) return pulls + self.open_pulls
def open_parent_pulls(self): from vilya.models.ticket import Ticket from vilya.models.pull import PullRequest pulls = [] parent = self.get_forked_from() if parent: pulls = [PullRequest.get_by_proj_and_ticket(parent.id, t.ticket_id) for t in Ticket.gets_by_proj(parent.id, limit=9999)] pulls = [p for p in pulls if p and p.from_proj and p.from_proj.id == self.id] return pulls
def get_by_repo_and_pull(cls, project_name, pull_number): project = CodeDoubanProject.get_by_name(project_name) ticket = Ticket.get_by_projectid_and_ticketnumber(project.id, pull_number) def get_id_by_repo_and_pull(proj_id, ticket_id): rs = store.execute("select id from pullreq " "where to_project=%s and ticket_id=%s", (proj_id, ticket_id)) return rs[0][0] if rs else '' id_ = get_id_by_repo_and_pull(project.id, ticket.ticket_id) return cls.get(id_)
def _get_pr_by_uid(uid): pr = {} # uid: pullrequest-project-ticketid-status if uid and uid.startswith('pullrequest'): fields = uid.split('-') if len(fields) != 4: return pr _, project_name, ticket_number, status = fields project = CodeDoubanProject.get_by_name(project_name) ticket = Ticket.get_by_projectid_and_ticketnumber(project.id, ticket_number) pr = _get_pr_by_project_and_ticket(project, ticket) return pr
def test_get_user_submitted_pull_requests(self): title = 'test title' desc = 'test desc' u = User('testu%s' % time.time()) p1_t1 = Ticket.add(self.proj1.id, title, desc, u.username) assert u.get_user_submit_pull_requests() != [] assert u.n_user_open_submit_pull_requests == 1 p1_t1.close('testuser') assert u.get_user_submit_pull_requests() == [] assert u.n_user_open_submit_pull_requests == 0 assert u.n_open_pull_requests == 0
def test_pr_stat(self): TestCase.setUp(self) _, self.proj1, _, self.proj1_fork = setup_repos( mkdtemp(), 'testproject1') _, self.proj2, _, self.proj2_fork = setup_repos( mkdtemp(), 'testproject2') pr_rs = get_all_ticket() assert len(pr_rs) == 0 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 0 assert len(pr_rs) - pr_open_count == 0 pullreq1 = PullRequest.open(self.proj1_fork, 'master', self.proj1, 'master') ticket1 = Ticket.add(self.proj1.id, 'title', 'content', 'testuser') pullreq1 = pullreq1.insert(ticket1.ticket_number) pullreq2 = PullRequest.open(self.proj2_fork, 'master', self.proj2, 'master') ticket2 = Ticket.add(self.proj2.id, 'title', 'content', 'testuser') pullreq2 = pullreq2.insert(ticket2.ticket_number) pr_rs = get_all_ticket() assert len(pr_rs) == 2 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 2 assert len(pr_rs) - pr_open_count == 0 ticket1.close("testuser") pr_rs = get_all_ticket() assert len(pr_rs) == 2 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 1 assert len(pr_rs) - pr_open_count == 1 pr_comment_count = get_ticket_comment_count() assert (pr_comment_count) == 0 ticket2.add_comment("comment1", "testuse1") ticket2.add_comment("comment2", "testuse2") pr_comment_count = get_ticket_comment_count() assert (pr_comment_count) == 2
def test_pr_stat(self): TestCase.setUp(self) _, self.proj1, _, self.proj1_fork = setup_repos( mkdtemp(), 'testproject1') _, self.proj2, _, self.proj2_fork = setup_repos( mkdtemp(), 'testproject2') pr_rs = get_all_ticket() assert len(pr_rs) == 0 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 0 assert len(pr_rs) - pr_open_count == 0 pullreq1 = PullRequest.open( self.proj1_fork, 'master', self.proj1, 'master') ticket1 = Ticket.add(self.proj1.id, 'title', 'content', 'testuser') pullreq1 = pullreq1.insert(ticket1.ticket_number) pullreq2 = PullRequest.open( self.proj2_fork, 'master', self.proj2, 'master') ticket2 = Ticket.add(self.proj2.id, 'title', 'content', 'testuser') pullreq2 = pullreq2.insert(ticket2.ticket_number) pr_rs = get_all_ticket() assert len(pr_rs) == 2 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 2 assert len(pr_rs) - pr_open_count == 0 ticket1.close("testuser") pr_rs = get_all_ticket() assert len(pr_rs) == 2 pr_open_count = len(filter(lambda x: x[1] is None, pr_rs)) assert pr_open_count == 1 assert len(pr_rs) - pr_open_count == 1 pr_comment_count = get_ticket_comment_count() assert(pr_comment_count) == 0 ticket2.add_comment("comment1", "testuse1") ticket2.add_comment("comment2", "testuse2") pr_comment_count = get_ticket_comment_count() assert(pr_comment_count) == 2
def test_get_user_submitted_pull_requests(self): title = 'test title' desc = 'test desc' u = User('testu%s' % time.time()) p1_t1 = Ticket.add(self.proj2.id, title, desc, u.username) assert u.get_user_submit_pull_requests() != [] assert u.n_user_open_submit_pull_requests == 1 p1_t1.close('testuser') assert u.get_user_submit_pull_requests() == [] assert u.n_user_open_submit_pull_requests == 0 assert u.n_open_pull_requests == 0
def test_count_ticket_rank(self): clean_up() title = 'test title' desc = 'test desc' author = 'testuser' tick1 = Ticket.add(1, title, desc, author) tick2 = Ticket.add(2, title, desc, author) created_at = datetime.now() aDay = timedelta(days=-1) yesterday = created_at + aDay node1 = TicketNode.add(TICKET_NODE_TYPE_COMMENT, 0, 'user1', tick1.id, created_at) node2 = TicketNode.add(TICKET_NODE_TYPE_COMMENT, 0, 'user1', tick1.id, created_at) node3 = TicketNode.add(TICKET_NODE_TYPE_LINECOMMENT, 0, 'user1', tick1.id, created_at) node4 = TicketNode.add(TICKET_NODE_TYPE_COMMENT, 0, 'user1', tick2.id, yesterday) node5 = TicketNode.add(TICKET_NODE_TYPE_LINECOMMENT, 0, 'user1', tick2.id, yesterday) # 验证 已经关闭的 ticket store.execute( "update codedouban_ticket set time=%s, closed=%s " "where id=%s", (yesterday, created_at, tick2.id)) store.commit() TicketRank.count_ticket_rank(True) rank_score2 = TicketRank.get_rank_by_ticket_id(tick2.id) rank_s2 = rank_score2[0][0] rank2 = 32.07 assert rank_s2 == rank2 # 验证 没有关闭的 ticket TicketRank.count_ticket_rank(False) rank_score1 = TicketRank.get_rank_by_ticket_id(tick1.id) rank_s1 = rank_score1[0][0] rank1 = 37.97 assert rank_s1 == rank1
def test_ticket(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) assert p1_t1.ticket_id == 1 assert p1_t1.title == title assert p1_t1.description == desc assert p1_t1.author == author p2_t1 = Ticket.add(self.proj2.id, title, desc, author) pullreq2 = PullRequest.open( self.proj2_fork, 'master', self.proj2, 'master') pullreq2 = pullreq2.insert(p2_t1.ticket_number) assert p2_t1.ticket_id == 1 ticket = Ticket.get_by_projectid_and_ticketnumber( self.proj1.id, p1_t1.ticket_id) assert ticket.id == p1_t1.id ticket = Ticket.get_by_projectid_and_ticketnumber( self.proj2.id, p2_t1.ticket_id) assert ticket.id == p2_t1.id
def gen_telchar_data(data): ticket_id = data.get('ticket_id') ticket = Ticket.get(ticket_id) pullreq = PullRequest.get_by_proj_and_ticket( ticket.project.id, ticket.ticket_id) fork_from = pullreq.from_proj.fork_from fork_from = pullreq.from_proj.get(fork_from).url if fork_from else None return { 'ticket_id': ticket.ticket_id, 'fork_from': fork_from, 'url': pullreq.from_proj.url, 'to_sha': pullreq.to_sha, 'from_sha': pullreq.from_sha }
def _save_merged(self, operator, from_sha, to_sha, merge_commit_sha): self.merged = "%s..%s" % (from_sha, to_sha) self.save() merged_time = datetime.now() self.merge_by = operator self.merge_time = merged_time.strftime('%Y-%m-%d %H:%M:%S') # FIXME: pullreq without ticket_id? if self.ticket_id: ticket = Ticket.get_by_projectid_and_ticketnumber( self.to_proj.id, self.ticket_id) if ticket: # add commits to ticket ticket.add_commits(merge_commit_sha, operator) TicketNode.add_merge(ticket.id, operator, merged_time)