# HG changeset patch # User Marcin Kuzminski # Date 2012-06-10 00:08:10 # Node ID f29469677319e03fe41bee6db9b64745ccc5b7dd # Parent 74f2910f7ad95e647f33eb77033f170c33e7194b Added basic models for saving open pull requests - added pull-request models - added pull-requests notifications into inbox diff --git a/rhodecode/config/routing.py b/rhodecode/config/routing.py --- a/rhodecode/config/routing.py +++ b/rhodecode/config/routing.py @@ -436,9 +436,20 @@ def make_map(config): other_ref_type='(branch|book|tag)')) rmap.connect('pullrequest_home', - '/{repo_name:.*}/pull-request/new', - controller='pullrequests', action='index', - conditions=dict(function=check_repo)) + '/{repo_name:.*}/pull-request/new', controller='pullrequests', + action='index', conditions=dict(function=check_repo, + method=["GET"])) + + rmap.connect('pullrequest', + '/{repo_name:.*}/pull-request/new', controller='pullrequests', + action='create', conditions=dict(function=check_repo, + method=["POST"])) + + rmap.connect('pullrequest_show', + '/{repo_name:.*}/pull-request/{pull_request_id}', + controller='pullrequests', + action='show', conditions=dict(function=check_repo, + method=["GET"])) rmap.connect('summary_home', '/{repo_name:.*}/summary', controller='summary', conditions=dict(function=check_repo)) diff --git a/rhodecode/controllers/compare.py b/rhodecode/controllers/compare.py --- a/rhodecode/controllers/compare.py +++ b/rhodecode/controllers/compare.py @@ -120,6 +120,8 @@ class CompareController(BaseRepoControll c.statuses = c.rhodecode_db_repo.statuses([x.raw_id for x in c.cs_ranges]) + # defines that we need hidden inputs with changesets + c.as_form = request.GET.get('as_form', False) if request.environ.get('HTTP_X_PARTIAL_XHR'): return render('compare/compare_cs.html') diff --git a/rhodecode/controllers/pullrequests.py b/rhodecode/controllers/pullrequests.py --- a/rhodecode/controllers/pullrequests.py +++ b/rhodecode/controllers/pullrequests.py @@ -31,7 +31,10 @@ from pylons.i18n.translation import _ from rhodecode.lib.base import BaseRepoController, render from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator -from rhodecode.model.db import User +from rhodecode.lib import helpers as h +from rhodecode.model.db import User, PullRequest +from rhodecode.model.pull_request import PullRequestModel +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -71,20 +74,60 @@ class PullrequestsController(BaseRepoCon c.other_refs = c.org_refs c.other_repos.extend(c.org_repos) - + c.default_pull_request = org_repo.repo_name #gather forks and add to this list for fork in org_repo.forks: c.other_repos.append((fork.repo_name, '%s/%s' % ( fork.user.username, fork.repo_name)) ) #add parents of this fork also - c.other_repos.append((org_repo.parent.repo_name, '%s/%s' % ( - org_repo.parent.user.username, - org_repo.parent.repo_name)) - ) + if org_repo.parent: + c.default_pull_request = org_repo.parent.repo_name + c.other_repos.append((org_repo.parent.repo_name, '%s/%s' % ( + org_repo.parent.user.username, + org_repo.parent.repo_name)) + ) #TODO: maybe the owner should be default ? c.review_members = [] - c.available_members = [(x.user_id, x.username) for x in - User.query().filter(User.username != 'default').all()] + c.available_members = [] + for u in User.query().filter(User.username != 'default').all(): + uname = u.username + if org_repo.user == u: + uname = _('%s (owner)' % u.username) + # auto add owner to pull-request recipients + c.review_members.append([u.user_id, uname]) + c.available_members.append([u.user_id, uname]) return render('/pullrequests/pullrequest.html') + + def create(self, repo_name): + req_p = request.POST + org_repo = req_p['org_repo'] + org_ref = req_p['org_ref'] + other_repo = req_p['other_repo'] + other_ref = req_p['other_ref'] + revisions = req_p.getall('revisions') + reviewers = req_p.getall('review_members') + #TODO: wrap this into a FORM !!! + + title = req_p['pullrequest_title'] + description = req_p['pullrequest_desc'] + + try: + model = PullRequestModel() + model.create(self.rhodecode_user.user_id, org_repo, + org_ref, other_repo, other_ref, revisions, + reviewers, title, description) + Session.commit() + h.flash(_('Pull request send'), category='success') + except Exception: + raise + h.flash(_('Error occured during sending pull request'), + category='error') + log.error(traceback.format_exc()) + + return redirect(url('changelog_home', repo_name=repo_name)) + + def show(self, repo_name, pull_request_id): + c.pull_request = PullRequest.get(pull_request_id) + return render('/pullrequests/pullrequest_show.html') diff --git a/rhodecode/model/comment.py b/rhodecode/model/comment.py --- a/rhodecode/model/comment.py +++ b/rhodecode/model/comment.py @@ -118,7 +118,8 @@ class ChangesetCommentsModel(BaseModel): NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=mention_recipients, - type_=Notification.TYPE_CHANGESET_COMMENT + type_=Notification.TYPE_CHANGESET_COMMENT, + email_kwargs={'status_change': status_change} ) return comment diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -1371,9 +1371,12 @@ class ChangesetStatus(Base, BaseModel): changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id')) modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now) version = Column('version', Integer(), nullable=False, default=0) + pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True) + author = relationship('User', lazy='joined') repo = relationship('Repository') comment = relationship('ChangesetComment', lazy='joined') + pull_request = relationship('PullRequest', lazy='joined') @classmethod def get_status_lbl(cls, value): @@ -1384,6 +1387,59 @@ class ChangesetStatus(Base, BaseModel): return ChangesetStatus.get_status_lbl(self.status) +class PullRequest(Base, BaseModel): + __tablename__ = 'pull_requests' + __table_args__ = ( + {'extend_existing': True, 'mysql_engine': 'InnoDB', + 'mysql_charset': 'utf8'}, + ) + + pull_request_id = Column('pull_request_id', Integer(), nullable=False, primary_key=True) + title = Column('title', Unicode(256), nullable=True) + description = Column('description', Unicode(10240), nullable=True) + _revisions = Column('revisions', UnicodeText(20500)) # 500 revisions max + org_repo_id = Column('org_repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False) + org_ref = Column('org_ref', Unicode(256), nullable=False) + other_repo_id = Column('other_repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False) + other_ref = Column('other_ref', Unicode(256), nullable=False) + + @hybrid_property + def revisions(self): + return self._revisions.split(':') + + @revisions.setter + def revisions(self, val): + self._revisions = ':'.join(val) + + reviewers = relationship('PullRequestReviewers') + org_repo = relationship('Repository', primaryjoin='PullRequest.org_repo_id==Repository.repo_id') + other_repo = relationship('Repository', primaryjoin='PullRequest.other_repo_id==Repository.repo_id') + + def __json__(self): + return dict( + revisions=self.revisions + ) + + +class PullRequestReviewers(Base, BaseModel): + __tablename__ = 'pull_request_reviewers' + __table_args__ = ( + {'extend_existing': True, 'mysql_engine': 'InnoDB', + 'mysql_charset': 'utf8'}, + ) + + def __init__(self, user=None, pull_request=None): + self.user = user + self.pull_request = pull_request + + pull_requests_reviewers_id = Column('pull_requests_reviewers_id', Integer(), nullable=False, primary_key=True) + pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=False) + user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True) + + user = relationship('User') + pull_request = relationship('PullRequest') + + class Notification(Base, BaseModel): __tablename__ = 'notifications' __table_args__ = ( diff --git a/rhodecode/model/notification.py b/rhodecode/model/notification.py --- a/rhodecode/model/notification.py +++ b/rhodecode/model/notification.py @@ -226,6 +226,7 @@ class EmailNotificationModel(BaseModel): TYPE_CHANGESET_COMMENT = Notification.TYPE_CHANGESET_COMMENT TYPE_PASSWORD_RESET = 'passoword_link' TYPE_REGISTRATION = Notification.TYPE_REGISTRATION + TYPE_PULL_REQUEST = Notification.TYPE_PULL_REQUEST TYPE_DEFAULT = 'default' def __init__(self): diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py new file mode 100644 --- /dev/null +++ b/rhodecode/model/pull_request.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +""" + rhodecode.model.pull_reuquest + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + pull request model for RhodeCode + + :created_on: Jun 6, 2012 + :author: marcink + :copyright: (C) 2012-2012 Marcin Kuzminski + :license: GPLv3, see COPYING for more details. +""" +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import logging +from pylons.i18n.translation import _ + +from rhodecode.lib import helpers as h +from rhodecode.model import BaseModel +from rhodecode.model.db import PullRequest, PullRequestReviewers, Notification +from rhodecode.model.notification import NotificationModel +from rhodecode.lib.utils2 import safe_unicode + +log = logging.getLogger(__name__) + + +class PullRequestModel(BaseModel): + + def create(self, created_by, org_repo, org_ref, other_repo, + other_ref, revisions, reviewers, title, description=None): + + new = PullRequest() + new.org_repo = self._get_repo(org_repo) + new.org_ref = org_ref + new.other_repo = self._get_repo(other_repo) + new.other_ref = other_ref + new.revisions = revisions + new.title = title + new.description = description + + self.sa.add(new) + + #members + for member in reviewers: + _usr = self._get_user(member) + reviewer = PullRequestReviewers(_usr, new) + self.sa.add(reviewer) + + #notification to reviewers + notif = NotificationModel() + created_by_user = self._get_user(created_by) + subject = safe_unicode( + h.link_to( + _('%(user)s wants you to review pull request #%(pr_id)s') % \ + {'user': created_by_user.username, + 'pr_id': new.pull_request_id}, + h.url('pullrequest_show', repo_name=other_repo, + pull_request_id=new.pull_request_id, + qualified=True, + ) + ) + ) + body = description + notif.create(created_by=created_by, subject=subject, body=body, + recipients=reviewers, + type_=Notification.TYPE_PULL_REQUEST,) + + return new diff --git a/rhodecode/templates/compare/compare_cs.html b/rhodecode/templates/compare/compare_cs.html --- a/rhodecode/templates/compare/compare_cs.html +++ b/rhodecode/templates/compare/compare_cs.html @@ -11,8 +11,12 @@ %if cs.raw_id in c.statuses:
%endif - - ${h.link_to('r%s:%s' % (cs.revision,h.short_id(cs.raw_id)),h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))} + + ${h.link_to('r%s:%s' % (cs.revision,h.short_id(cs.raw_id)),h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))} + %if c.as_form: + ${h.hidden('revisions',cs.raw_id)} + %endif +
${h.person(cs.author)}
${cs.date}
${h.urlify_commit(h.wrap_paragraphs(cs.message),c.repo_name)}
@@ -20,4 +24,4 @@ %endfor %endif - \ No newline at end of file + diff --git a/rhodecode/templates/compare/compare_diff.html b/rhodecode/templates/compare/compare_diff.html --- a/rhodecode/templates/compare/compare_diff.html +++ b/rhodecode/templates/compare/compare_diff.html @@ -34,13 +34,13 @@
##CS -
${_('Changesets')}
+
${_('Outgoing changesets')}
<%include file="compare_cs.html" /> ## FILES
${_('Files affected')}
- %for fid, change, f, stat in c.files: + %for fid, change, f, stat in c.files:
${h.link_to(h.safe_unicode(f),h.url.current(anchor=fid))}
${h.fancy_file_stats(stat)}
@@ -49,13 +49,13 @@
- + ## diff block <%namespace name="diff_block" file="/changeset/diff_block.html"/> %for fid, change, f, stat in c.files: ${diff_block.diff_block_simple([c.changes[fid]])} %endfor - + diff --git a/rhodecode/templates/pullrequests/pullrequest_show.html b/rhodecode/templates/pullrequests/pullrequest_show.html new file mode 100644 --- /dev/null +++ b/rhodecode/templates/pullrequests/pullrequest_show.html @@ -0,0 +1,32 @@ +<%inherit file="/base/base.html"/> + +<%def name="title()"> + ${c.repo_name} ${_('Pull request #%s') % c.pull_request.pull_request_id} + + +<%def name="breadcrumbs_links()"> + ${h.link_to(u'Home',h.url('/'))} + » + ${h.link_to(c.repo_name,h.url('changelog_home',repo_name=c.repo_name))} + » + ${_('Pull request #%s') % c.pull_request.pull_request_id} + + +<%def name="main()"> + +
+ +
+ ${self.breadcrumbs()} +
+ + pull request ${c.pull_request} overview... + +
+ + + + diff --git a/rhodecode/tests/functional/test_compare.py b/rhodecode/tests/functional/test_compare.py --- a/rhodecode/tests/functional/test_compare.py +++ b/rhodecode/tests/functional/test_compare.py @@ -1,7 +1,52 @@ from rhodecode.tests import * + class TestCompareController(TestController): - def test_index(self): - response = self.app.get(url(controller='compare', action='index')) - # Test response... + def test_index_tag(self): + self.log_user() + tag1='0.1.3' + tag2='0.1.2' + response = self.app.get(url(controller='compare', action='index', + repo_name=HG_REPO, + org_ref_type="tag", + org_ref=tag1, + other_ref_type="tag", + other_ref=tag2, + )) + response.mustcontain('%s@%s -> %s@%s' % (HG_REPO, tag1, HG_REPO, tag2)) + ## outgoing changesets between tags + response.mustcontain('''r120:17544fbfcd33''' % HG_REPO) + response.mustcontain('''r119:36e0fc9d2808''' % HG_REPO) + response.mustcontain('''r118:bb1a3ab98cc4''' % HG_REPO) + response.mustcontain('''r117:41fda979f02f''' % HG_REPO) + response.mustcontain('''r116:9749bfbfc0d2''' % HG_REPO) + response.mustcontain('''r115:70d4cef8a376''' % HG_REPO) + response.mustcontain('''r112:c5ddebc06eaa''' % HG_REPO) + + ## files diff + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + response.mustcontain('''''' % (HG_REPO, tag1, tag2)) + + def test_index_branch(self): + self.log_user() + response = self.app.get(url(controller='compare', action='index', + repo_name=HG_REPO, + org_ref_type="branch", + org_ref='default', + other_ref_type="branch", + other_ref='default', + )) + + response.mustcontain('%s@default -> %s@default' % (HG_REPO, HG_REPO)) + # branch are equal + response.mustcontain('No changesets') diff --git a/rhodecode/tests/functional/test_pullrequests.py b/rhodecode/tests/functional/test_pullrequests.py --- a/rhodecode/tests/functional/test_pullrequests.py +++ b/rhodecode/tests/functional/test_pullrequests.py @@ -1,7 +1,9 @@ from rhodecode.tests import * + class TestPullrequestsController(TestController): def test_index(self): - response = self.app.get(url(controller='pullrequests', action='index')) - # Test response... + self.log_user() + response = self.app.get(url(controller='pullrequests', action='index', + repo_name=HG_REPO)) diff --git a/rhodecode/tests/test_models.py b/rhodecode/tests/test_models.py --- a/rhodecode/tests/test_models.py +++ b/rhodecode/tests/test_models.py @@ -181,7 +181,7 @@ class TestUser(unittest.TestCase): super(TestUser, self).__init__(methodName=methodName) def test_create_and_remove(self): - usr = UserModel().create_or_update(username=u'test_user', + usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe', email=u'u232@rhodecode.org', name=u'u1', lastname=u'u1') @@ -203,7 +203,7 @@ class TestUser(unittest.TestCase): self.assertEqual(UsersGroupMember.query().all(), []) def test_additonal_email_as_main(self): - usr = UserModel().create_or_update(username=u'test_user', + usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe', email=u'main_email@rhodecode.org', name=u'u1', lastname=u'u1') @@ -221,7 +221,7 @@ class TestUser(unittest.TestCase): Session.commit() def test_extra_email_map(self): - usr = UserModel().create_or_update(username=u'test_user', + usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe', email=u'main_email@rhodecode.org', name=u'u1', lastname=u'u1')