##// END OF EJS Templates
reviewers: changes for new author/commit author logic....
milka -
r4559:36905c9f default
parent child Browse files
Show More
@@ -0,0 +1,78 b''
1 # -*- coding: utf-8 -*-
2
3 import logging
4 from sqlalchemy import *
5
6 from alembic.migration import MigrationContext
7 from alembic.operations import Operations
8
9 from rhodecode.lib.dbmigrate.versions import _reset_base
10 from rhodecode.model import meta, init_model_encryption
11
12
13 log = logging.getLogger(__name__)
14
15
16 def upgrade(migrate_engine):
17 """
18 Upgrade operations go here.
19 Don't create your own engine; bind migrate_engine to your metadata
20 """
21 _reset_base(migrate_engine)
22 from rhodecode.lib.dbmigrate.schema import db_4_20_0_0 as db
23
24 init_model_encryption(db)
25
26 context = MigrationContext.configure(migrate_engine.connect())
27 op = Operations(context)
28
29 table = db.RepoReviewRule.__table__
30 with op.batch_alter_table(table.name) as batch_op:
31
32 new_column = Column('pr_author', UnicodeText().with_variant(UnicodeText(255), 'mysql'), nullable=True)
33 batch_op.add_column(new_column)
34
35 new_column = Column('commit_author', UnicodeText().with_variant(UnicodeText(255), 'mysql'), nullable=True)
36 batch_op.add_column(new_column)
37
38 _migrate_review_flags_to_new_cols(op, meta.Session)
39
40
41 def downgrade(migrate_engine):
42 meta = MetaData()
43 meta.bind = migrate_engine
44
45
46 def fixups(models, _SESSION):
47 pass
48
49
50 def _migrate_review_flags_to_new_cols(op, session):
51
52 # set defaults for pr_author
53 query = text(
54 'UPDATE repo_review_rules SET pr_author = :val'
55 ).bindparams(val='no_rule')
56 op.execute(query)
57
58 # set defaults for commit_author
59 query = text(
60 'UPDATE repo_review_rules SET commit_author = :val'
61 ).bindparams(val='no_rule')
62 op.execute(query)
63
64 session().commit()
65
66 # now change the flags to forbid based on
67 # forbid_author_to_review, forbid_commit_author_to_review
68 query = text(
69 'UPDATE repo_review_rules SET pr_author = :val WHERE forbid_author_to_review = TRUE'
70 ).bindparams(val='forbid_pr_author')
71 op.execute(query)
72
73 query = text(
74 'UPDATE repo_review_rules SET commit_author = :val WHERE forbid_commit_author_to_review = TRUE'
75 ).bindparams(val='forbid_commit_author')
76 op.execute(query)
77
78 session().commit()
@@ -1,60 +1,60 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2020 RhodeCode GmbH
3 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import os
21 import os
22 from collections import OrderedDict
22 from collections import OrderedDict
23
23
24 import sys
24 import sys
25 import platform
25 import platform
26
26
27 VERSION = tuple(open(os.path.join(
27 VERSION = tuple(open(os.path.join(
28 os.path.dirname(__file__), 'VERSION')).read().split('.'))
28 os.path.dirname(__file__), 'VERSION')).read().split('.'))
29
29
30 BACKENDS = OrderedDict()
30 BACKENDS = OrderedDict()
31
31
32 BACKENDS['hg'] = 'Mercurial repository'
32 BACKENDS['hg'] = 'Mercurial repository'
33 BACKENDS['git'] = 'Git repository'
33 BACKENDS['git'] = 'Git repository'
34 BACKENDS['svn'] = 'Subversion repository'
34 BACKENDS['svn'] = 'Subversion repository'
35
35
36
36
37 CELERY_ENABLED = False
37 CELERY_ENABLED = False
38 CELERY_EAGER = False
38 CELERY_EAGER = False
39
39
40 # link to config for pyramid
40 # link to config for pyramid
41 CONFIG = {}
41 CONFIG = {}
42
42
43 # Populated with the settings dictionary from application init in
43 # Populated with the settings dictionary from application init in
44 # rhodecode.conf.environment.load_pyramid_environment
44 # rhodecode.conf.environment.load_pyramid_environment
45 PYRAMID_SETTINGS = {}
45 PYRAMID_SETTINGS = {}
46
46
47 # Linked module for extensions
47 # Linked module for extensions
48 EXTENSIONS = {}
48 EXTENSIONS = {}
49
49
50 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
50 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
51 __dbversion__ = 111 # defines current db version for migrations
51 __dbversion__ = 112 # defines current db version for migrations
52 __platform__ = platform.system()
52 __platform__ = platform.system()
53 __license__ = 'AGPLv3, and Commercial License'
53 __license__ = 'AGPLv3, and Commercial License'
54 __author__ = 'RhodeCode GmbH'
54 __author__ = 'RhodeCode GmbH'
55 __url__ = 'https://code.rhodecode.com'
55 __url__ = 'https://code.rhodecode.com'
56
56
57 is_windows = __platform__ in ['Windows']
57 is_windows = __platform__ in ['Windows']
58 is_unix = not is_windows
58 is_unix = not is_windows
59 is_test = False
59 is_test = False
60 disable_error_handler = False
60 disable_error_handler = False
@@ -1,111 +1,113 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2020 RhodeCode GmbH
3 # Copyright (C) 2016-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 from rhodecode.lib import helpers as h, rc_cache
21 from rhodecode.lib import helpers as h, rc_cache
22 from rhodecode.lib.utils2 import safe_int
22 from rhodecode.lib.utils2 import safe_int
23 from rhodecode.model.pull_request import get_diff_info
23 from rhodecode.model.pull_request import get_diff_info
24 from rhodecode.model.db import PullRequestReviewers
24 from rhodecode.model.db import PullRequestReviewers
25 # V3 - Reviewers, with default rules data
25 # V3 - Reviewers, with default rules data
26 # v4 - Added observers metadata
26 # v4 - Added observers metadata
27 REVIEWER_API_VERSION = 'V4'
27 # v5 - pr_author/commit_author include/exclude logic
28 REVIEWER_API_VERSION = 'V5'
28
29
29
30
30 def reviewer_as_json(user, reasons=None, role=None, mandatory=False, rules=None, user_group=None):
31 def reviewer_as_json(user, reasons=None, role=None, mandatory=False, rules=None, user_group=None):
31 """
32 """
32 Returns json struct of a reviewer for frontend
33 Returns json struct of a reviewer for frontend
33
34
34 :param user: the reviewer
35 :param user: the reviewer
35 :param reasons: list of strings of why they are reviewers
36 :param reasons: list of strings of why they are reviewers
36 :param mandatory: bool, to set user as mandatory
37 :param mandatory: bool, to set user as mandatory
37 """
38 """
38 role = role or PullRequestReviewers.ROLE_REVIEWER
39 role = role or PullRequestReviewers.ROLE_REVIEWER
39 if role not in PullRequestReviewers.ROLES:
40 if role not in PullRequestReviewers.ROLES:
40 raise ValueError('role is not one of %s', PullRequestReviewers.ROLES)
41 raise ValueError('role is not one of %s', PullRequestReviewers.ROLES)
41
42
42 return {
43 return {
43 'user_id': user.user_id,
44 'user_id': user.user_id,
44 'reasons': reasons or [],
45 'reasons': reasons or [],
45 'rules': rules or [],
46 'rules': rules or [],
46 'role': role,
47 'role': role,
47 'mandatory': mandatory,
48 'mandatory': mandatory,
48 'user_group': user_group,
49 'user_group': user_group,
49 'username': user.username,
50 'username': user.username,
50 'first_name': user.first_name,
51 'first_name': user.first_name,
51 'last_name': user.last_name,
52 'last_name': user.last_name,
52 'user_link': h.link_to_user(user),
53 'user_link': h.link_to_user(user),
53 'gravatar_link': h.gravatar_url(user.email, 14),
54 'gravatar_link': h.gravatar_url(user.email, 14),
54 }
55 }
55
56
56
57
57 def to_reviewers(e):
58 def to_reviewers(e):
58 if isinstance(e, (tuple, list)):
59 if isinstance(e, (tuple, list)):
59 return map(reviewer_as_json, e)
60 return map(reviewer_as_json, e)
60 else:
61 else:
61 return reviewer_as_json(e)
62 return reviewer_as_json(e)
62
63
63
64
64 def get_default_reviewers_data(current_user, source_repo, source_ref, target_repo, target_ref,
65 def get_default_reviewers_data(current_user, source_repo, source_ref, target_repo, target_ref,
65 include_diff_info=True):
66 include_diff_info=True):
66 """
67 """
67 Return json for default reviewers of a repository
68 Return json for default reviewers of a repository
68 """
69 """
69
70
70 diff_info = {}
71 diff_info = {}
71 if include_diff_info:
72 if include_diff_info:
72 diff_info = get_diff_info(
73 diff_info = get_diff_info(
73 source_repo, source_ref.commit_id, target_repo, target_ref.commit_id)
74 source_repo, source_ref.commit_id, target_repo, target_ref.commit_id)
74
75
75 reasons = ['Default reviewer', 'Repository owner']
76 reasons = ['Default reviewer', 'Repository owner']
76 json_reviewers = [reviewer_as_json(
77 json_reviewers = [reviewer_as_json(
77 user=target_repo.user, reasons=reasons, mandatory=False, rules=None, role=None)]
78 user=target_repo.user, reasons=reasons, mandatory=False, rules=None, role=None)]
78
79
79 compute_key = rc_cache.utils.compute_key_from_params(
80 compute_key = rc_cache.utils.compute_key_from_params(
80 current_user.user_id, source_repo.repo_id, source_ref.type, source_ref.name,
81 current_user.user_id, source_repo.repo_id, source_ref.type, source_ref.name,
81 source_ref.commit_id, target_repo.repo_id, target_ref.type, target_ref.name,
82 source_ref.commit_id, target_repo.repo_id, target_ref.type, target_ref.name,
82 target_ref.commit_id)
83 target_ref.commit_id)
83
84
84 return {
85 return {
85 'api_ver': REVIEWER_API_VERSION, # define version for later possible schema upgrade
86 'api_ver': REVIEWER_API_VERSION, # define version for later possible schema upgrade
86 'compute_key': compute_key,
87 'compute_key': compute_key,
87 'diff_info': diff_info,
88 'diff_info': diff_info,
88 'reviewers': json_reviewers,
89 'reviewers': json_reviewers,
89 'rules': {},
90 'rules': {},
90 'rules_data': {},
91 'rules_data': {},
92 'rules_humanized': [],
91 }
93 }
92
94
93
95
94 def validate_default_reviewers(review_members, reviewer_rules):
96 def validate_default_reviewers(review_members, reviewer_rules):
95 """
97 """
96 Function to validate submitted reviewers against the saved rules
98 Function to validate submitted reviewers against the saved rules
97 """
99 """
98 reviewers = []
100 reviewers = []
99 reviewer_by_id = {}
101 reviewer_by_id = {}
100 for r in review_members:
102 for r in review_members:
101 reviewer_user_id = safe_int(r['user_id'])
103 reviewer_user_id = safe_int(r['user_id'])
102 entry = (reviewer_user_id, r['reasons'], r['mandatory'], r['role'], r['rules'])
104 entry = (reviewer_user_id, r['reasons'], r['mandatory'], r['role'], r['rules'])
103
105
104 reviewer_by_id[reviewer_user_id] = entry
106 reviewer_by_id[reviewer_user_id] = entry
105 reviewers.append(entry)
107 reviewers.append(entry)
106
108
107 return reviewers
109 return reviewers
108
110
109
111
110 def validate_observers(observer_members, reviewer_rules):
112 def validate_observers(observer_members, reviewer_rules):
111 return {}
113 return {}
@@ -1,1855 +1,1851 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22 import collections
22 import collections
23
23
24 import formencode
24 import formencode
25 import formencode.htmlfill
25 import formencode.htmlfill
26 import peppercorn
26 import peppercorn
27 from pyramid.httpexceptions import (
27 from pyramid.httpexceptions import (
28 HTTPFound, HTTPNotFound, HTTPForbidden, HTTPBadRequest, HTTPConflict)
28 HTTPFound, HTTPNotFound, HTTPForbidden, HTTPBadRequest, HTTPConflict)
29 from pyramid.view import view_config
29 from pyramid.view import view_config
30 from pyramid.renderers import render
30 from pyramid.renderers import render
31
31
32 from rhodecode.apps._base import RepoAppView, DataGridAppView
32 from rhodecode.apps._base import RepoAppView, DataGridAppView
33
33
34 from rhodecode.lib import helpers as h, diffs, codeblocks, channelstream
34 from rhodecode.lib import helpers as h, diffs, codeblocks, channelstream
35 from rhodecode.lib.base import vcs_operation_context
35 from rhodecode.lib.base import vcs_operation_context
36 from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist
36 from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist
37 from rhodecode.lib.exceptions import CommentVersionMismatch
37 from rhodecode.lib.exceptions import CommentVersionMismatch
38 from rhodecode.lib.ext_json import json
38 from rhodecode.lib.ext_json import json
39 from rhodecode.lib.auth import (
39 from rhodecode.lib.auth import (
40 LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator,
40 LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator,
41 NotAnonymous, CSRFRequired)
41 NotAnonymous, CSRFRequired)
42 from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode, safe_int, aslist
42 from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode, safe_int, aslist
43 from rhodecode.lib.vcs.backends.base import (
43 from rhodecode.lib.vcs.backends.base import (
44 EmptyCommit, UpdateFailureReason, unicode_to_reference)
44 EmptyCommit, UpdateFailureReason, unicode_to_reference)
45 from rhodecode.lib.vcs.exceptions import (
45 from rhodecode.lib.vcs.exceptions import (
46 CommitDoesNotExistError, RepositoryRequirementError, EmptyRepositoryError)
46 CommitDoesNotExistError, RepositoryRequirementError, EmptyRepositoryError)
47 from rhodecode.model.changeset_status import ChangesetStatusModel
47 from rhodecode.model.changeset_status import ChangesetStatusModel
48 from rhodecode.model.comment import CommentsModel
48 from rhodecode.model.comment import CommentsModel
49 from rhodecode.model.db import (
49 from rhodecode.model.db import (
50 func, false, or_, PullRequest, ChangesetComment, ChangesetStatus, Repository,
50 func, false, or_, PullRequest, ChangesetComment, ChangesetStatus, Repository,
51 PullRequestReviewers)
51 PullRequestReviewers)
52 from rhodecode.model.forms import PullRequestForm
52 from rhodecode.model.forms import PullRequestForm
53 from rhodecode.model.meta import Session
53 from rhodecode.model.meta import Session
54 from rhodecode.model.pull_request import PullRequestModel, MergeCheck
54 from rhodecode.model.pull_request import PullRequestModel, MergeCheck
55 from rhodecode.model.scm import ScmModel
55 from rhodecode.model.scm import ScmModel
56
56
57 log = logging.getLogger(__name__)
57 log = logging.getLogger(__name__)
58
58
59
59
60 class RepoPullRequestsView(RepoAppView, DataGridAppView):
60 class RepoPullRequestsView(RepoAppView, DataGridAppView):
61
61
62 def load_default_context(self):
62 def load_default_context(self):
63 c = self._get_local_tmpl_context(include_app_defaults=True)
63 c = self._get_local_tmpl_context(include_app_defaults=True)
64 c.REVIEW_STATUS_APPROVED = ChangesetStatus.STATUS_APPROVED
64 c.REVIEW_STATUS_APPROVED = ChangesetStatus.STATUS_APPROVED
65 c.REVIEW_STATUS_REJECTED = ChangesetStatus.STATUS_REJECTED
65 c.REVIEW_STATUS_REJECTED = ChangesetStatus.STATUS_REJECTED
66 # backward compat., we use for OLD PRs a plain renderer
66 # backward compat., we use for OLD PRs a plain renderer
67 c.renderer = 'plain'
67 c.renderer = 'plain'
68 return c
68 return c
69
69
70 def _get_pull_requests_list(
70 def _get_pull_requests_list(
71 self, repo_name, source, filter_type, opened_by, statuses):
71 self, repo_name, source, filter_type, opened_by, statuses):
72
72
73 draw, start, limit = self._extract_chunk(self.request)
73 draw, start, limit = self._extract_chunk(self.request)
74 search_q, order_by, order_dir = self._extract_ordering(self.request)
74 search_q, order_by, order_dir = self._extract_ordering(self.request)
75 _render = self.request.get_partial_renderer(
75 _render = self.request.get_partial_renderer(
76 'rhodecode:templates/data_table/_dt_elements.mako')
76 'rhodecode:templates/data_table/_dt_elements.mako')
77
77
78 # pagination
78 # pagination
79
79
80 if filter_type == 'awaiting_review':
80 if filter_type == 'awaiting_review':
81 pull_requests = PullRequestModel().get_awaiting_review(
81 pull_requests = PullRequestModel().get_awaiting_review(
82 repo_name, search_q=search_q, source=source, opened_by=opened_by,
82 repo_name, search_q=search_q, source=source, opened_by=opened_by,
83 statuses=statuses, offset=start, length=limit,
83 statuses=statuses, offset=start, length=limit,
84 order_by=order_by, order_dir=order_dir)
84 order_by=order_by, order_dir=order_dir)
85 pull_requests_total_count = PullRequestModel().count_awaiting_review(
85 pull_requests_total_count = PullRequestModel().count_awaiting_review(
86 repo_name, search_q=search_q, source=source, statuses=statuses,
86 repo_name, search_q=search_q, source=source, statuses=statuses,
87 opened_by=opened_by)
87 opened_by=opened_by)
88 elif filter_type == 'awaiting_my_review':
88 elif filter_type == 'awaiting_my_review':
89 pull_requests = PullRequestModel().get_awaiting_my_review(
89 pull_requests = PullRequestModel().get_awaiting_my_review(
90 repo_name, search_q=search_q, source=source, opened_by=opened_by,
90 repo_name, search_q=search_q, source=source, opened_by=opened_by,
91 user_id=self._rhodecode_user.user_id, statuses=statuses,
91 user_id=self._rhodecode_user.user_id, statuses=statuses,
92 offset=start, length=limit, order_by=order_by,
92 offset=start, length=limit, order_by=order_by,
93 order_dir=order_dir)
93 order_dir=order_dir)
94 pull_requests_total_count = PullRequestModel().count_awaiting_my_review(
94 pull_requests_total_count = PullRequestModel().count_awaiting_my_review(
95 repo_name, search_q=search_q, source=source, user_id=self._rhodecode_user.user_id,
95 repo_name, search_q=search_q, source=source, user_id=self._rhodecode_user.user_id,
96 statuses=statuses, opened_by=opened_by)
96 statuses=statuses, opened_by=opened_by)
97 else:
97 else:
98 pull_requests = PullRequestModel().get_all(
98 pull_requests = PullRequestModel().get_all(
99 repo_name, search_q=search_q, source=source, opened_by=opened_by,
99 repo_name, search_q=search_q, source=source, opened_by=opened_by,
100 statuses=statuses, offset=start, length=limit,
100 statuses=statuses, offset=start, length=limit,
101 order_by=order_by, order_dir=order_dir)
101 order_by=order_by, order_dir=order_dir)
102 pull_requests_total_count = PullRequestModel().count_all(
102 pull_requests_total_count = PullRequestModel().count_all(
103 repo_name, search_q=search_q, source=source, statuses=statuses,
103 repo_name, search_q=search_q, source=source, statuses=statuses,
104 opened_by=opened_by)
104 opened_by=opened_by)
105
105
106 data = []
106 data = []
107 comments_model = CommentsModel()
107 comments_model = CommentsModel()
108 for pr in pull_requests:
108 for pr in pull_requests:
109 comments_count = comments_model.get_all_comments(
109 comments_count = comments_model.get_all_comments(
110 self.db_repo.repo_id, pull_request=pr,
110 self.db_repo.repo_id, pull_request=pr,
111 include_drafts=False, count_only=True)
111 include_drafts=False, count_only=True)
112
112
113 data.append({
113 data.append({
114 'name': _render('pullrequest_name',
114 'name': _render('pullrequest_name',
115 pr.pull_request_id, pr.pull_request_state,
115 pr.pull_request_id, pr.pull_request_state,
116 pr.work_in_progress, pr.target_repo.repo_name,
116 pr.work_in_progress, pr.target_repo.repo_name,
117 short=True),
117 short=True),
118 'name_raw': pr.pull_request_id,
118 'name_raw': pr.pull_request_id,
119 'status': _render('pullrequest_status',
119 'status': _render('pullrequest_status',
120 pr.calculated_review_status()),
120 pr.calculated_review_status()),
121 'title': _render('pullrequest_title', pr.title, pr.description),
121 'title': _render('pullrequest_title', pr.title, pr.description),
122 'description': h.escape(pr.description),
122 'description': h.escape(pr.description),
123 'updated_on': _render('pullrequest_updated_on',
123 'updated_on': _render('pullrequest_updated_on',
124 h.datetime_to_time(pr.updated_on),
124 h.datetime_to_time(pr.updated_on),
125 pr.versions_count),
125 pr.versions_count),
126 'updated_on_raw': h.datetime_to_time(pr.updated_on),
126 'updated_on_raw': h.datetime_to_time(pr.updated_on),
127 'created_on': _render('pullrequest_updated_on',
127 'created_on': _render('pullrequest_updated_on',
128 h.datetime_to_time(pr.created_on)),
128 h.datetime_to_time(pr.created_on)),
129 'created_on_raw': h.datetime_to_time(pr.created_on),
129 'created_on_raw': h.datetime_to_time(pr.created_on),
130 'state': pr.pull_request_state,
130 'state': pr.pull_request_state,
131 'author': _render('pullrequest_author',
131 'author': _render('pullrequest_author',
132 pr.author.full_contact, ),
132 pr.author.full_contact, ),
133 'author_raw': pr.author.full_name,
133 'author_raw': pr.author.full_name,
134 'comments': _render('pullrequest_comments', comments_count),
134 'comments': _render('pullrequest_comments', comments_count),
135 'comments_raw': comments_count,
135 'comments_raw': comments_count,
136 'closed': pr.is_closed(),
136 'closed': pr.is_closed(),
137 })
137 })
138
138
139 data = ({
139 data = ({
140 'draw': draw,
140 'draw': draw,
141 'data': data,
141 'data': data,
142 'recordsTotal': pull_requests_total_count,
142 'recordsTotal': pull_requests_total_count,
143 'recordsFiltered': pull_requests_total_count,
143 'recordsFiltered': pull_requests_total_count,
144 })
144 })
145 return data
145 return data
146
146
147 @LoginRequired()
147 @LoginRequired()
148 @HasRepoPermissionAnyDecorator(
148 @HasRepoPermissionAnyDecorator(
149 'repository.read', 'repository.write', 'repository.admin')
149 'repository.read', 'repository.write', 'repository.admin')
150 @view_config(
150 @view_config(
151 route_name='pullrequest_show_all', request_method='GET',
151 route_name='pullrequest_show_all', request_method='GET',
152 renderer='rhodecode:templates/pullrequests/pullrequests.mako')
152 renderer='rhodecode:templates/pullrequests/pullrequests.mako')
153 def pull_request_list(self):
153 def pull_request_list(self):
154 c = self.load_default_context()
154 c = self.load_default_context()
155
155
156 req_get = self.request.GET
156 req_get = self.request.GET
157 c.source = str2bool(req_get.get('source'))
157 c.source = str2bool(req_get.get('source'))
158 c.closed = str2bool(req_get.get('closed'))
158 c.closed = str2bool(req_get.get('closed'))
159 c.my = str2bool(req_get.get('my'))
159 c.my = str2bool(req_get.get('my'))
160 c.awaiting_review = str2bool(req_get.get('awaiting_review'))
160 c.awaiting_review = str2bool(req_get.get('awaiting_review'))
161 c.awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
161 c.awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
162
162
163 c.active = 'open'
163 c.active = 'open'
164 if c.my:
164 if c.my:
165 c.active = 'my'
165 c.active = 'my'
166 if c.closed:
166 if c.closed:
167 c.active = 'closed'
167 c.active = 'closed'
168 if c.awaiting_review and not c.source:
168 if c.awaiting_review and not c.source:
169 c.active = 'awaiting'
169 c.active = 'awaiting'
170 if c.source and not c.awaiting_review:
170 if c.source and not c.awaiting_review:
171 c.active = 'source'
171 c.active = 'source'
172 if c.awaiting_my_review:
172 if c.awaiting_my_review:
173 c.active = 'awaiting_my'
173 c.active = 'awaiting_my'
174
174
175 return self._get_template_context(c)
175 return self._get_template_context(c)
176
176
177 @LoginRequired()
177 @LoginRequired()
178 @HasRepoPermissionAnyDecorator(
178 @HasRepoPermissionAnyDecorator(
179 'repository.read', 'repository.write', 'repository.admin')
179 'repository.read', 'repository.write', 'repository.admin')
180 @view_config(
180 @view_config(
181 route_name='pullrequest_show_all_data', request_method='GET',
181 route_name='pullrequest_show_all_data', request_method='GET',
182 renderer='json_ext', xhr=True)
182 renderer='json_ext', xhr=True)
183 def pull_request_list_data(self):
183 def pull_request_list_data(self):
184 self.load_default_context()
184 self.load_default_context()
185
185
186 # additional filters
186 # additional filters
187 req_get = self.request.GET
187 req_get = self.request.GET
188 source = str2bool(req_get.get('source'))
188 source = str2bool(req_get.get('source'))
189 closed = str2bool(req_get.get('closed'))
189 closed = str2bool(req_get.get('closed'))
190 my = str2bool(req_get.get('my'))
190 my = str2bool(req_get.get('my'))
191 awaiting_review = str2bool(req_get.get('awaiting_review'))
191 awaiting_review = str2bool(req_get.get('awaiting_review'))
192 awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
192 awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
193
193
194 filter_type = 'awaiting_review' if awaiting_review \
194 filter_type = 'awaiting_review' if awaiting_review \
195 else 'awaiting_my_review' if awaiting_my_review \
195 else 'awaiting_my_review' if awaiting_my_review \
196 else None
196 else None
197
197
198 opened_by = None
198 opened_by = None
199 if my:
199 if my:
200 opened_by = [self._rhodecode_user.user_id]
200 opened_by = [self._rhodecode_user.user_id]
201
201
202 statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN]
202 statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN]
203 if closed:
203 if closed:
204 statuses = [PullRequest.STATUS_CLOSED]
204 statuses = [PullRequest.STATUS_CLOSED]
205
205
206 data = self._get_pull_requests_list(
206 data = self._get_pull_requests_list(
207 repo_name=self.db_repo_name, source=source,
207 repo_name=self.db_repo_name, source=source,
208 filter_type=filter_type, opened_by=opened_by, statuses=statuses)
208 filter_type=filter_type, opened_by=opened_by, statuses=statuses)
209
209
210 return data
210 return data
211
211
212 def _is_diff_cache_enabled(self, target_repo):
212 def _is_diff_cache_enabled(self, target_repo):
213 caching_enabled = self._get_general_setting(
213 caching_enabled = self._get_general_setting(
214 target_repo, 'rhodecode_diff_cache')
214 target_repo, 'rhodecode_diff_cache')
215 log.debug('Diff caching enabled: %s', caching_enabled)
215 log.debug('Diff caching enabled: %s', caching_enabled)
216 return caching_enabled
216 return caching_enabled
217
217
218 def _get_diffset(self, source_repo_name, source_repo,
218 def _get_diffset(self, source_repo_name, source_repo,
219 ancestor_commit,
219 ancestor_commit,
220 source_ref_id, target_ref_id,
220 source_ref_id, target_ref_id,
221 target_commit, source_commit, diff_limit, file_limit,
221 target_commit, source_commit, diff_limit, file_limit,
222 fulldiff, hide_whitespace_changes, diff_context, use_ancestor=True):
222 fulldiff, hide_whitespace_changes, diff_context, use_ancestor=True):
223
223
224 if use_ancestor:
224 if use_ancestor:
225 # we might want to not use it for versions
225 # we might want to not use it for versions
226 target_ref_id = ancestor_commit.raw_id
226 target_ref_id = ancestor_commit.raw_id
227
227
228 vcs_diff = PullRequestModel().get_diff(
228 vcs_diff = PullRequestModel().get_diff(
229 source_repo, source_ref_id, target_ref_id,
229 source_repo, source_ref_id, target_ref_id,
230 hide_whitespace_changes, diff_context)
230 hide_whitespace_changes, diff_context)
231
231
232 diff_processor = diffs.DiffProcessor(
232 diff_processor = diffs.DiffProcessor(
233 vcs_diff, format='newdiff', diff_limit=diff_limit,
233 vcs_diff, format='newdiff', diff_limit=diff_limit,
234 file_limit=file_limit, show_full_diff=fulldiff)
234 file_limit=file_limit, show_full_diff=fulldiff)
235
235
236 _parsed = diff_processor.prepare()
236 _parsed = diff_processor.prepare()
237
237
238 diffset = codeblocks.DiffSet(
238 diffset = codeblocks.DiffSet(
239 repo_name=self.db_repo_name,
239 repo_name=self.db_repo_name,
240 source_repo_name=source_repo_name,
240 source_repo_name=source_repo_name,
241 source_node_getter=codeblocks.diffset_node_getter(target_commit),
241 source_node_getter=codeblocks.diffset_node_getter(target_commit),
242 target_node_getter=codeblocks.diffset_node_getter(source_commit),
242 target_node_getter=codeblocks.diffset_node_getter(source_commit),
243 )
243 )
244 diffset = self.path_filter.render_patchset_filtered(
244 diffset = self.path_filter.render_patchset_filtered(
245 diffset, _parsed, target_commit.raw_id, source_commit.raw_id)
245 diffset, _parsed, target_commit.raw_id, source_commit.raw_id)
246
246
247 return diffset
247 return diffset
248
248
249 def _get_range_diffset(self, source_scm, source_repo,
249 def _get_range_diffset(self, source_scm, source_repo,
250 commit1, commit2, diff_limit, file_limit,
250 commit1, commit2, diff_limit, file_limit,
251 fulldiff, hide_whitespace_changes, diff_context):
251 fulldiff, hide_whitespace_changes, diff_context):
252 vcs_diff = source_scm.get_diff(
252 vcs_diff = source_scm.get_diff(
253 commit1, commit2,
253 commit1, commit2,
254 ignore_whitespace=hide_whitespace_changes,
254 ignore_whitespace=hide_whitespace_changes,
255 context=diff_context)
255 context=diff_context)
256
256
257 diff_processor = diffs.DiffProcessor(
257 diff_processor = diffs.DiffProcessor(
258 vcs_diff, format='newdiff', diff_limit=diff_limit,
258 vcs_diff, format='newdiff', diff_limit=diff_limit,
259 file_limit=file_limit, show_full_diff=fulldiff)
259 file_limit=file_limit, show_full_diff=fulldiff)
260
260
261 _parsed = diff_processor.prepare()
261 _parsed = diff_processor.prepare()
262
262
263 diffset = codeblocks.DiffSet(
263 diffset = codeblocks.DiffSet(
264 repo_name=source_repo.repo_name,
264 repo_name=source_repo.repo_name,
265 source_node_getter=codeblocks.diffset_node_getter(commit1),
265 source_node_getter=codeblocks.diffset_node_getter(commit1),
266 target_node_getter=codeblocks.diffset_node_getter(commit2))
266 target_node_getter=codeblocks.diffset_node_getter(commit2))
267
267
268 diffset = self.path_filter.render_patchset_filtered(
268 diffset = self.path_filter.render_patchset_filtered(
269 diffset, _parsed, commit1.raw_id, commit2.raw_id)
269 diffset, _parsed, commit1.raw_id, commit2.raw_id)
270
270
271 return diffset
271 return diffset
272
272
273 def register_comments_vars(self, c, pull_request, versions, include_drafts=True):
273 def register_comments_vars(self, c, pull_request, versions, include_drafts=True):
274 comments_model = CommentsModel()
274 comments_model = CommentsModel()
275
275
276 # GENERAL COMMENTS with versions #
276 # GENERAL COMMENTS with versions #
277 q = comments_model._all_general_comments_of_pull_request(pull_request)
277 q = comments_model._all_general_comments_of_pull_request(pull_request)
278 q = q.order_by(ChangesetComment.comment_id.asc())
278 q = q.order_by(ChangesetComment.comment_id.asc())
279 if not include_drafts:
279 if not include_drafts:
280 q = q.filter(ChangesetComment.draft == false())
280 q = q.filter(ChangesetComment.draft == false())
281 general_comments = q
281 general_comments = q
282
282
283 # pick comments we want to render at current version
283 # pick comments we want to render at current version
284 c.comment_versions = comments_model.aggregate_comments(
284 c.comment_versions = comments_model.aggregate_comments(
285 general_comments, versions, c.at_version_num)
285 general_comments, versions, c.at_version_num)
286
286
287 # INLINE COMMENTS with versions #
287 # INLINE COMMENTS with versions #
288 q = comments_model._all_inline_comments_of_pull_request(pull_request)
288 q = comments_model._all_inline_comments_of_pull_request(pull_request)
289 q = q.order_by(ChangesetComment.comment_id.asc())
289 q = q.order_by(ChangesetComment.comment_id.asc())
290 if not include_drafts:
290 if not include_drafts:
291 q = q.filter(ChangesetComment.draft == false())
291 q = q.filter(ChangesetComment.draft == false())
292 inline_comments = q
292 inline_comments = q
293
293
294 c.inline_versions = comments_model.aggregate_comments(
294 c.inline_versions = comments_model.aggregate_comments(
295 inline_comments, versions, c.at_version_num, inline=True)
295 inline_comments, versions, c.at_version_num, inline=True)
296
296
297 # Comments inline+general
297 # Comments inline+general
298 if c.at_version:
298 if c.at_version:
299 c.inline_comments_flat = c.inline_versions[c.at_version_num]['display']
299 c.inline_comments_flat = c.inline_versions[c.at_version_num]['display']
300 c.comments = c.comment_versions[c.at_version_num]['display']
300 c.comments = c.comment_versions[c.at_version_num]['display']
301 else:
301 else:
302 c.inline_comments_flat = c.inline_versions[c.at_version_num]['until']
302 c.inline_comments_flat = c.inline_versions[c.at_version_num]['until']
303 c.comments = c.comment_versions[c.at_version_num]['until']
303 c.comments = c.comment_versions[c.at_version_num]['until']
304
304
305 return general_comments, inline_comments
305 return general_comments, inline_comments
306
306
307 @LoginRequired()
307 @LoginRequired()
308 @HasRepoPermissionAnyDecorator(
308 @HasRepoPermissionAnyDecorator(
309 'repository.read', 'repository.write', 'repository.admin')
309 'repository.read', 'repository.write', 'repository.admin')
310 @view_config(
310 @view_config(
311 route_name='pullrequest_show', request_method='GET',
311 route_name='pullrequest_show', request_method='GET',
312 renderer='rhodecode:templates/pullrequests/pullrequest_show.mako')
312 renderer='rhodecode:templates/pullrequests/pullrequest_show.mako')
313 def pull_request_show(self):
313 def pull_request_show(self):
314 _ = self.request.translate
314 _ = self.request.translate
315 c = self.load_default_context()
315 c = self.load_default_context()
316
316
317 pull_request = PullRequest.get_or_404(
317 pull_request = PullRequest.get_or_404(
318 self.request.matchdict['pull_request_id'])
318 self.request.matchdict['pull_request_id'])
319 pull_request_id = pull_request.pull_request_id
319 pull_request_id = pull_request.pull_request_id
320
320
321 c.state_progressing = pull_request.is_state_changing()
321 c.state_progressing = pull_request.is_state_changing()
322 c.pr_broadcast_channel = channelstream.pr_channel(pull_request)
322 c.pr_broadcast_channel = channelstream.pr_channel(pull_request)
323
323
324 _new_state = {
324 _new_state = {
325 'created': PullRequest.STATE_CREATED,
325 'created': PullRequest.STATE_CREATED,
326 }.get(self.request.GET.get('force_state'))
326 }.get(self.request.GET.get('force_state'))
327
327
328 if c.is_super_admin and _new_state:
328 if c.is_super_admin and _new_state:
329 with pull_request.set_state(PullRequest.STATE_UPDATING, final_state=_new_state):
329 with pull_request.set_state(PullRequest.STATE_UPDATING, final_state=_new_state):
330 h.flash(
330 h.flash(
331 _('Pull Request state was force changed to `{}`').format(_new_state),
331 _('Pull Request state was force changed to `{}`').format(_new_state),
332 category='success')
332 category='success')
333 Session().commit()
333 Session().commit()
334
334
335 raise HTTPFound(h.route_path(
335 raise HTTPFound(h.route_path(
336 'pullrequest_show', repo_name=self.db_repo_name,
336 'pullrequest_show', repo_name=self.db_repo_name,
337 pull_request_id=pull_request_id))
337 pull_request_id=pull_request_id))
338
338
339 version = self.request.GET.get('version')
339 version = self.request.GET.get('version')
340 from_version = self.request.GET.get('from_version') or version
340 from_version = self.request.GET.get('from_version') or version
341 merge_checks = self.request.GET.get('merge_checks')
341 merge_checks = self.request.GET.get('merge_checks')
342 c.fulldiff = str2bool(self.request.GET.get('fulldiff'))
342 c.fulldiff = str2bool(self.request.GET.get('fulldiff'))
343 force_refresh = str2bool(self.request.GET.get('force_refresh'))
343 force_refresh = str2bool(self.request.GET.get('force_refresh'))
344 c.range_diff_on = self.request.GET.get('range-diff') == "1"
344 c.range_diff_on = self.request.GET.get('range-diff') == "1"
345
345
346 # fetch global flags of ignore ws or context lines
346 # fetch global flags of ignore ws or context lines
347 diff_context = diffs.get_diff_context(self.request)
347 diff_context = diffs.get_diff_context(self.request)
348 hide_whitespace_changes = diffs.get_diff_whitespace_flag(self.request)
348 hide_whitespace_changes = diffs.get_diff_whitespace_flag(self.request)
349
349
350 (pull_request_latest,
350 (pull_request_latest,
351 pull_request_at_ver,
351 pull_request_at_ver,
352 pull_request_display_obj,
352 pull_request_display_obj,
353 at_version) = PullRequestModel().get_pr_version(
353 at_version) = PullRequestModel().get_pr_version(
354 pull_request_id, version=version)
354 pull_request_id, version=version)
355
355
356 pr_closed = pull_request_latest.is_closed()
356 pr_closed = pull_request_latest.is_closed()
357
357
358 if pr_closed and (version or from_version):
358 if pr_closed and (version or from_version):
359 # not allow to browse versions for closed PR
359 # not allow to browse versions for closed PR
360 raise HTTPFound(h.route_path(
360 raise HTTPFound(h.route_path(
361 'pullrequest_show', repo_name=self.db_repo_name,
361 'pullrequest_show', repo_name=self.db_repo_name,
362 pull_request_id=pull_request_id))
362 pull_request_id=pull_request_id))
363
363
364 versions = pull_request_display_obj.versions()
364 versions = pull_request_display_obj.versions()
365 # used to store per-commit range diffs
365 # used to store per-commit range diffs
366 c.changes = collections.OrderedDict()
366 c.changes = collections.OrderedDict()
367
367
368 c.at_version = at_version
368 c.at_version = at_version
369 c.at_version_num = (at_version
369 c.at_version_num = (at_version
370 if at_version and at_version != PullRequest.LATEST_VER
370 if at_version and at_version != PullRequest.LATEST_VER
371 else None)
371 else None)
372
372
373 c.at_version_index = ChangesetComment.get_index_from_version(
373 c.at_version_index = ChangesetComment.get_index_from_version(
374 c.at_version_num, versions)
374 c.at_version_num, versions)
375
375
376 (prev_pull_request_latest,
376 (prev_pull_request_latest,
377 prev_pull_request_at_ver,
377 prev_pull_request_at_ver,
378 prev_pull_request_display_obj,
378 prev_pull_request_display_obj,
379 prev_at_version) = PullRequestModel().get_pr_version(
379 prev_at_version) = PullRequestModel().get_pr_version(
380 pull_request_id, version=from_version)
380 pull_request_id, version=from_version)
381
381
382 c.from_version = prev_at_version
382 c.from_version = prev_at_version
383 c.from_version_num = (prev_at_version
383 c.from_version_num = (prev_at_version
384 if prev_at_version and prev_at_version != PullRequest.LATEST_VER
384 if prev_at_version and prev_at_version != PullRequest.LATEST_VER
385 else None)
385 else None)
386 c.from_version_index = ChangesetComment.get_index_from_version(
386 c.from_version_index = ChangesetComment.get_index_from_version(
387 c.from_version_num, versions)
387 c.from_version_num, versions)
388
388
389 # define if we're in COMPARE mode or VIEW at version mode
389 # define if we're in COMPARE mode or VIEW at version mode
390 compare = at_version != prev_at_version
390 compare = at_version != prev_at_version
391
391
392 # pull_requests repo_name we opened it against
392 # pull_requests repo_name we opened it against
393 # ie. target_repo must match
393 # ie. target_repo must match
394 if self.db_repo_name != pull_request_at_ver.target_repo.repo_name:
394 if self.db_repo_name != pull_request_at_ver.target_repo.repo_name:
395 log.warning('Mismatch between the current repo: %s, and target %s',
395 log.warning('Mismatch between the current repo: %s, and target %s',
396 self.db_repo_name, pull_request_at_ver.target_repo.repo_name)
396 self.db_repo_name, pull_request_at_ver.target_repo.repo_name)
397 raise HTTPNotFound()
397 raise HTTPNotFound()
398
398
399 c.shadow_clone_url = PullRequestModel().get_shadow_clone_url(pull_request_at_ver)
399 c.shadow_clone_url = PullRequestModel().get_shadow_clone_url(pull_request_at_ver)
400
400
401 c.pull_request = pull_request_display_obj
401 c.pull_request = pull_request_display_obj
402 c.renderer = pull_request_at_ver.description_renderer or c.renderer
402 c.renderer = pull_request_at_ver.description_renderer or c.renderer
403 c.pull_request_latest = pull_request_latest
403 c.pull_request_latest = pull_request_latest
404
404
405 # inject latest version
405 # inject latest version
406 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
406 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
407 c.versions = versions + [latest_ver]
407 c.versions = versions + [latest_ver]
408
408
409 if compare or (at_version and not at_version == PullRequest.LATEST_VER):
409 if compare or (at_version and not at_version == PullRequest.LATEST_VER):
410 c.allowed_to_change_status = False
410 c.allowed_to_change_status = False
411 c.allowed_to_update = False
411 c.allowed_to_update = False
412 c.allowed_to_merge = False
412 c.allowed_to_merge = False
413 c.allowed_to_delete = False
413 c.allowed_to_delete = False
414 c.allowed_to_comment = False
414 c.allowed_to_comment = False
415 c.allowed_to_close = False
415 c.allowed_to_close = False
416 else:
416 else:
417 can_change_status = PullRequestModel().check_user_change_status(
417 can_change_status = PullRequestModel().check_user_change_status(
418 pull_request_at_ver, self._rhodecode_user)
418 pull_request_at_ver, self._rhodecode_user)
419 c.allowed_to_change_status = can_change_status and not pr_closed
419 c.allowed_to_change_status = can_change_status and not pr_closed
420
420
421 c.allowed_to_update = PullRequestModel().check_user_update(
421 c.allowed_to_update = PullRequestModel().check_user_update(
422 pull_request_latest, self._rhodecode_user) and not pr_closed
422 pull_request_latest, self._rhodecode_user) and not pr_closed
423 c.allowed_to_merge = PullRequestModel().check_user_merge(
423 c.allowed_to_merge = PullRequestModel().check_user_merge(
424 pull_request_latest, self._rhodecode_user) and not pr_closed
424 pull_request_latest, self._rhodecode_user) and not pr_closed
425 c.allowed_to_delete = PullRequestModel().check_user_delete(
425 c.allowed_to_delete = PullRequestModel().check_user_delete(
426 pull_request_latest, self._rhodecode_user) and not pr_closed
426 pull_request_latest, self._rhodecode_user) and not pr_closed
427 c.allowed_to_comment = not pr_closed
427 c.allowed_to_comment = not pr_closed
428 c.allowed_to_close = c.allowed_to_merge and not pr_closed
428 c.allowed_to_close = c.allowed_to_merge and not pr_closed
429
429
430 c.forbid_adding_reviewers = False
430 c.forbid_adding_reviewers = False
431 c.forbid_author_to_review = False
432 c.forbid_commit_author_to_review = False
433
431
434 if pull_request_latest.reviewer_data and \
432 if pull_request_latest.reviewer_data and \
435 'rules' in pull_request_latest.reviewer_data:
433 'rules' in pull_request_latest.reviewer_data:
436 rules = pull_request_latest.reviewer_data['rules'] or {}
434 rules = pull_request_latest.reviewer_data['rules'] or {}
437 try:
435 try:
438 c.forbid_adding_reviewers = rules.get('forbid_adding_reviewers')
436 c.forbid_adding_reviewers = rules.get('forbid_adding_reviewers')
439 c.forbid_author_to_review = rules.get('forbid_author_to_review')
440 c.forbid_commit_author_to_review = rules.get('forbid_commit_author_to_review')
441 except Exception:
437 except Exception:
442 pass
438 pass
443
439
444 # check merge capabilities
440 # check merge capabilities
445 _merge_check = MergeCheck.validate(
441 _merge_check = MergeCheck.validate(
446 pull_request_latest, auth_user=self._rhodecode_user,
442 pull_request_latest, auth_user=self._rhodecode_user,
447 translator=self.request.translate,
443 translator=self.request.translate,
448 force_shadow_repo_refresh=force_refresh)
444 force_shadow_repo_refresh=force_refresh)
449
445
450 c.pr_merge_errors = _merge_check.error_details
446 c.pr_merge_errors = _merge_check.error_details
451 c.pr_merge_possible = not _merge_check.failed
447 c.pr_merge_possible = not _merge_check.failed
452 c.pr_merge_message = _merge_check.merge_msg
448 c.pr_merge_message = _merge_check.merge_msg
453 c.pr_merge_source_commit = _merge_check.source_commit
449 c.pr_merge_source_commit = _merge_check.source_commit
454 c.pr_merge_target_commit = _merge_check.target_commit
450 c.pr_merge_target_commit = _merge_check.target_commit
455
451
456 c.pr_merge_info = MergeCheck.get_merge_conditions(
452 c.pr_merge_info = MergeCheck.get_merge_conditions(
457 pull_request_latest, translator=self.request.translate)
453 pull_request_latest, translator=self.request.translate)
458
454
459 c.pull_request_review_status = _merge_check.review_status
455 c.pull_request_review_status = _merge_check.review_status
460 if merge_checks:
456 if merge_checks:
461 self.request.override_renderer = \
457 self.request.override_renderer = \
462 'rhodecode:templates/pullrequests/pullrequest_merge_checks.mako'
458 'rhodecode:templates/pullrequests/pullrequest_merge_checks.mako'
463 return self._get_template_context(c)
459 return self._get_template_context(c)
464
460
465 c.reviewers_count = pull_request.reviewers_count
461 c.reviewers_count = pull_request.reviewers_count
466 c.observers_count = pull_request.observers_count
462 c.observers_count = pull_request.observers_count
467
463
468 # reviewers and statuses
464 # reviewers and statuses
469 c.pull_request_default_reviewers_data_json = json.dumps(pull_request.reviewer_data)
465 c.pull_request_default_reviewers_data_json = json.dumps(pull_request.reviewer_data)
470 c.pull_request_set_reviewers_data_json = collections.OrderedDict({'reviewers': []})
466 c.pull_request_set_reviewers_data_json = collections.OrderedDict({'reviewers': []})
471 c.pull_request_set_observers_data_json = collections.OrderedDict({'observers': []})
467 c.pull_request_set_observers_data_json = collections.OrderedDict({'observers': []})
472
468
473 for review_obj, member, reasons, mandatory, status in pull_request_at_ver.reviewers_statuses():
469 for review_obj, member, reasons, mandatory, status in pull_request_at_ver.reviewers_statuses():
474 member_reviewer = h.reviewer_as_json(
470 member_reviewer = h.reviewer_as_json(
475 member, reasons=reasons, mandatory=mandatory,
471 member, reasons=reasons, mandatory=mandatory,
476 role=review_obj.role,
472 role=review_obj.role,
477 user_group=review_obj.rule_user_group_data()
473 user_group=review_obj.rule_user_group_data()
478 )
474 )
479
475
480 current_review_status = status[0][1].status if status else ChangesetStatus.STATUS_NOT_REVIEWED
476 current_review_status = status[0][1].status if status else ChangesetStatus.STATUS_NOT_REVIEWED
481 member_reviewer['review_status'] = current_review_status
477 member_reviewer['review_status'] = current_review_status
482 member_reviewer['review_status_label'] = h.commit_status_lbl(current_review_status)
478 member_reviewer['review_status_label'] = h.commit_status_lbl(current_review_status)
483 member_reviewer['allowed_to_update'] = c.allowed_to_update
479 member_reviewer['allowed_to_update'] = c.allowed_to_update
484 c.pull_request_set_reviewers_data_json['reviewers'].append(member_reviewer)
480 c.pull_request_set_reviewers_data_json['reviewers'].append(member_reviewer)
485
481
486 c.pull_request_set_reviewers_data_json = json.dumps(c.pull_request_set_reviewers_data_json)
482 c.pull_request_set_reviewers_data_json = json.dumps(c.pull_request_set_reviewers_data_json)
487
483
488 for observer_obj, member in pull_request_at_ver.observers():
484 for observer_obj, member in pull_request_at_ver.observers():
489 member_observer = h.reviewer_as_json(
485 member_observer = h.reviewer_as_json(
490 member, reasons=[], mandatory=False,
486 member, reasons=[], mandatory=False,
491 role=observer_obj.role,
487 role=observer_obj.role,
492 user_group=observer_obj.rule_user_group_data()
488 user_group=observer_obj.rule_user_group_data()
493 )
489 )
494 member_observer['allowed_to_update'] = c.allowed_to_update
490 member_observer['allowed_to_update'] = c.allowed_to_update
495 c.pull_request_set_observers_data_json['observers'].append(member_observer)
491 c.pull_request_set_observers_data_json['observers'].append(member_observer)
496
492
497 c.pull_request_set_observers_data_json = json.dumps(c.pull_request_set_observers_data_json)
493 c.pull_request_set_observers_data_json = json.dumps(c.pull_request_set_observers_data_json)
498
494
499 general_comments, inline_comments = \
495 general_comments, inline_comments = \
500 self.register_comments_vars(c, pull_request_latest, versions)
496 self.register_comments_vars(c, pull_request_latest, versions)
501
497
502 # TODOs
498 # TODOs
503 c.unresolved_comments = CommentsModel() \
499 c.unresolved_comments = CommentsModel() \
504 .get_pull_request_unresolved_todos(pull_request_latest)
500 .get_pull_request_unresolved_todos(pull_request_latest)
505 c.resolved_comments = CommentsModel() \
501 c.resolved_comments = CommentsModel() \
506 .get_pull_request_resolved_todos(pull_request_latest)
502 .get_pull_request_resolved_todos(pull_request_latest)
507
503
508 # if we use version, then do not show later comments
504 # if we use version, then do not show later comments
509 # than current version
505 # than current version
510 display_inline_comments = collections.defaultdict(
506 display_inline_comments = collections.defaultdict(
511 lambda: collections.defaultdict(list))
507 lambda: collections.defaultdict(list))
512 for co in inline_comments:
508 for co in inline_comments:
513 if c.at_version_num:
509 if c.at_version_num:
514 # pick comments that are at least UPTO given version, so we
510 # pick comments that are at least UPTO given version, so we
515 # don't render comments for higher version
511 # don't render comments for higher version
516 should_render = co.pull_request_version_id and \
512 should_render = co.pull_request_version_id and \
517 co.pull_request_version_id <= c.at_version_num
513 co.pull_request_version_id <= c.at_version_num
518 else:
514 else:
519 # showing all, for 'latest'
515 # showing all, for 'latest'
520 should_render = True
516 should_render = True
521
517
522 if should_render:
518 if should_render:
523 display_inline_comments[co.f_path][co.line_no].append(co)
519 display_inline_comments[co.f_path][co.line_no].append(co)
524
520
525 # load diff data into template context, if we use compare mode then
521 # load diff data into template context, if we use compare mode then
526 # diff is calculated based on changes between versions of PR
522 # diff is calculated based on changes between versions of PR
527
523
528 source_repo = pull_request_at_ver.source_repo
524 source_repo = pull_request_at_ver.source_repo
529 source_ref_id = pull_request_at_ver.source_ref_parts.commit_id
525 source_ref_id = pull_request_at_ver.source_ref_parts.commit_id
530
526
531 target_repo = pull_request_at_ver.target_repo
527 target_repo = pull_request_at_ver.target_repo
532 target_ref_id = pull_request_at_ver.target_ref_parts.commit_id
528 target_ref_id = pull_request_at_ver.target_ref_parts.commit_id
533
529
534 if compare:
530 if compare:
535 # in compare switch the diff base to latest commit from prev version
531 # in compare switch the diff base to latest commit from prev version
536 target_ref_id = prev_pull_request_display_obj.revisions[0]
532 target_ref_id = prev_pull_request_display_obj.revisions[0]
537
533
538 # despite opening commits for bookmarks/branches/tags, we always
534 # despite opening commits for bookmarks/branches/tags, we always
539 # convert this to rev to prevent changes after bookmark or branch change
535 # convert this to rev to prevent changes after bookmark or branch change
540 c.source_ref_type = 'rev'
536 c.source_ref_type = 'rev'
541 c.source_ref = source_ref_id
537 c.source_ref = source_ref_id
542
538
543 c.target_ref_type = 'rev'
539 c.target_ref_type = 'rev'
544 c.target_ref = target_ref_id
540 c.target_ref = target_ref_id
545
541
546 c.source_repo = source_repo
542 c.source_repo = source_repo
547 c.target_repo = target_repo
543 c.target_repo = target_repo
548
544
549 c.commit_ranges = []
545 c.commit_ranges = []
550 source_commit = EmptyCommit()
546 source_commit = EmptyCommit()
551 target_commit = EmptyCommit()
547 target_commit = EmptyCommit()
552 c.missing_requirements = False
548 c.missing_requirements = False
553
549
554 source_scm = source_repo.scm_instance()
550 source_scm = source_repo.scm_instance()
555 target_scm = target_repo.scm_instance()
551 target_scm = target_repo.scm_instance()
556
552
557 shadow_scm = None
553 shadow_scm = None
558 try:
554 try:
559 shadow_scm = pull_request_latest.get_shadow_repo()
555 shadow_scm = pull_request_latest.get_shadow_repo()
560 except Exception:
556 except Exception:
561 log.debug('Failed to get shadow repo', exc_info=True)
557 log.debug('Failed to get shadow repo', exc_info=True)
562 # try first the existing source_repo, and then shadow
558 # try first the existing source_repo, and then shadow
563 # repo if we can obtain one
559 # repo if we can obtain one
564 commits_source_repo = source_scm
560 commits_source_repo = source_scm
565 if shadow_scm:
561 if shadow_scm:
566 commits_source_repo = shadow_scm
562 commits_source_repo = shadow_scm
567
563
568 c.commits_source_repo = commits_source_repo
564 c.commits_source_repo = commits_source_repo
569 c.ancestor = None # set it to None, to hide it from PR view
565 c.ancestor = None # set it to None, to hide it from PR view
570
566
571 # empty version means latest, so we keep this to prevent
567 # empty version means latest, so we keep this to prevent
572 # double caching
568 # double caching
573 version_normalized = version or PullRequest.LATEST_VER
569 version_normalized = version or PullRequest.LATEST_VER
574 from_version_normalized = from_version or PullRequest.LATEST_VER
570 from_version_normalized = from_version or PullRequest.LATEST_VER
575
571
576 cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(target_repo)
572 cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(target_repo)
577 cache_file_path = diff_cache_exist(
573 cache_file_path = diff_cache_exist(
578 cache_path, 'pull_request', pull_request_id, version_normalized,
574 cache_path, 'pull_request', pull_request_id, version_normalized,
579 from_version_normalized, source_ref_id, target_ref_id,
575 from_version_normalized, source_ref_id, target_ref_id,
580 hide_whitespace_changes, diff_context, c.fulldiff)
576 hide_whitespace_changes, diff_context, c.fulldiff)
581
577
582 caching_enabled = self._is_diff_cache_enabled(c.target_repo)
578 caching_enabled = self._is_diff_cache_enabled(c.target_repo)
583 force_recache = self.get_recache_flag()
579 force_recache = self.get_recache_flag()
584
580
585 cached_diff = None
581 cached_diff = None
586 if caching_enabled:
582 if caching_enabled:
587 cached_diff = load_cached_diff(cache_file_path)
583 cached_diff = load_cached_diff(cache_file_path)
588
584
589 has_proper_commit_cache = (
585 has_proper_commit_cache = (
590 cached_diff and cached_diff.get('commits')
586 cached_diff and cached_diff.get('commits')
591 and len(cached_diff.get('commits', [])) == 5
587 and len(cached_diff.get('commits', [])) == 5
592 and cached_diff.get('commits')[0]
588 and cached_diff.get('commits')[0]
593 and cached_diff.get('commits')[3])
589 and cached_diff.get('commits')[3])
594
590
595 if not force_recache and not c.range_diff_on and has_proper_commit_cache:
591 if not force_recache and not c.range_diff_on and has_proper_commit_cache:
596 diff_commit_cache = \
592 diff_commit_cache = \
597 (ancestor_commit, commit_cache, missing_requirements,
593 (ancestor_commit, commit_cache, missing_requirements,
598 source_commit, target_commit) = cached_diff['commits']
594 source_commit, target_commit) = cached_diff['commits']
599 else:
595 else:
600 # NOTE(marcink): we reach potentially unreachable errors when a PR has
596 # NOTE(marcink): we reach potentially unreachable errors when a PR has
601 # merge errors resulting in potentially hidden commits in the shadow repo.
597 # merge errors resulting in potentially hidden commits in the shadow repo.
602 maybe_unreachable = _merge_check.MERGE_CHECK in _merge_check.error_details \
598 maybe_unreachable = _merge_check.MERGE_CHECK in _merge_check.error_details \
603 and _merge_check.merge_response
599 and _merge_check.merge_response
604 maybe_unreachable = maybe_unreachable \
600 maybe_unreachable = maybe_unreachable \
605 and _merge_check.merge_response.metadata.get('unresolved_files')
601 and _merge_check.merge_response.metadata.get('unresolved_files')
606 log.debug("Using unreachable commits due to MERGE_CHECK in merge simulation")
602 log.debug("Using unreachable commits due to MERGE_CHECK in merge simulation")
607 diff_commit_cache = \
603 diff_commit_cache = \
608 (ancestor_commit, commit_cache, missing_requirements,
604 (ancestor_commit, commit_cache, missing_requirements,
609 source_commit, target_commit) = self.get_commits(
605 source_commit, target_commit) = self.get_commits(
610 commits_source_repo,
606 commits_source_repo,
611 pull_request_at_ver,
607 pull_request_at_ver,
612 source_commit,
608 source_commit,
613 source_ref_id,
609 source_ref_id,
614 source_scm,
610 source_scm,
615 target_commit,
611 target_commit,
616 target_ref_id,
612 target_ref_id,
617 target_scm,
613 target_scm,
618 maybe_unreachable=maybe_unreachable)
614 maybe_unreachable=maybe_unreachable)
619
615
620 # register our commit range
616 # register our commit range
621 for comm in commit_cache.values():
617 for comm in commit_cache.values():
622 c.commit_ranges.append(comm)
618 c.commit_ranges.append(comm)
623
619
624 c.missing_requirements = missing_requirements
620 c.missing_requirements = missing_requirements
625 c.ancestor_commit = ancestor_commit
621 c.ancestor_commit = ancestor_commit
626 c.statuses = source_repo.statuses(
622 c.statuses = source_repo.statuses(
627 [x.raw_id for x in c.commit_ranges])
623 [x.raw_id for x in c.commit_ranges])
628
624
629 # auto collapse if we have more than limit
625 # auto collapse if we have more than limit
630 collapse_limit = diffs.DiffProcessor._collapse_commits_over
626 collapse_limit = diffs.DiffProcessor._collapse_commits_over
631 c.collapse_all_commits = len(c.commit_ranges) > collapse_limit
627 c.collapse_all_commits = len(c.commit_ranges) > collapse_limit
632 c.compare_mode = compare
628 c.compare_mode = compare
633
629
634 # diff_limit is the old behavior, will cut off the whole diff
630 # diff_limit is the old behavior, will cut off the whole diff
635 # if the limit is applied otherwise will just hide the
631 # if the limit is applied otherwise will just hide the
636 # big files from the front-end
632 # big files from the front-end
637 diff_limit = c.visual.cut_off_limit_diff
633 diff_limit = c.visual.cut_off_limit_diff
638 file_limit = c.visual.cut_off_limit_file
634 file_limit = c.visual.cut_off_limit_file
639
635
640 c.missing_commits = False
636 c.missing_commits = False
641 if (c.missing_requirements
637 if (c.missing_requirements
642 or isinstance(source_commit, EmptyCommit)
638 or isinstance(source_commit, EmptyCommit)
643 or source_commit == target_commit):
639 or source_commit == target_commit):
644
640
645 c.missing_commits = True
641 c.missing_commits = True
646 else:
642 else:
647 c.inline_comments = display_inline_comments
643 c.inline_comments = display_inline_comments
648
644
649 use_ancestor = True
645 use_ancestor = True
650 if from_version_normalized != version_normalized:
646 if from_version_normalized != version_normalized:
651 use_ancestor = False
647 use_ancestor = False
652
648
653 has_proper_diff_cache = cached_diff and cached_diff.get('commits')
649 has_proper_diff_cache = cached_diff and cached_diff.get('commits')
654 if not force_recache and has_proper_diff_cache:
650 if not force_recache and has_proper_diff_cache:
655 c.diffset = cached_diff['diff']
651 c.diffset = cached_diff['diff']
656 else:
652 else:
657 try:
653 try:
658 c.diffset = self._get_diffset(
654 c.diffset = self._get_diffset(
659 c.source_repo.repo_name, commits_source_repo,
655 c.source_repo.repo_name, commits_source_repo,
660 c.ancestor_commit,
656 c.ancestor_commit,
661 source_ref_id, target_ref_id,
657 source_ref_id, target_ref_id,
662 target_commit, source_commit,
658 target_commit, source_commit,
663 diff_limit, file_limit, c.fulldiff,
659 diff_limit, file_limit, c.fulldiff,
664 hide_whitespace_changes, diff_context,
660 hide_whitespace_changes, diff_context,
665 use_ancestor=use_ancestor
661 use_ancestor=use_ancestor
666 )
662 )
667
663
668 # save cached diff
664 # save cached diff
669 if caching_enabled:
665 if caching_enabled:
670 cache_diff(cache_file_path, c.diffset, diff_commit_cache)
666 cache_diff(cache_file_path, c.diffset, diff_commit_cache)
671 except CommitDoesNotExistError:
667 except CommitDoesNotExistError:
672 log.exception('Failed to generate diffset')
668 log.exception('Failed to generate diffset')
673 c.missing_commits = True
669 c.missing_commits = True
674
670
675 if not c.missing_commits:
671 if not c.missing_commits:
676
672
677 c.limited_diff = c.diffset.limited_diff
673 c.limited_diff = c.diffset.limited_diff
678
674
679 # calculate removed files that are bound to comments
675 # calculate removed files that are bound to comments
680 comment_deleted_files = [
676 comment_deleted_files = [
681 fname for fname in display_inline_comments
677 fname for fname in display_inline_comments
682 if fname not in c.diffset.file_stats]
678 if fname not in c.diffset.file_stats]
683
679
684 c.deleted_files_comments = collections.defaultdict(dict)
680 c.deleted_files_comments = collections.defaultdict(dict)
685 for fname, per_line_comments in display_inline_comments.items():
681 for fname, per_line_comments in display_inline_comments.items():
686 if fname in comment_deleted_files:
682 if fname in comment_deleted_files:
687 c.deleted_files_comments[fname]['stats'] = 0
683 c.deleted_files_comments[fname]['stats'] = 0
688 c.deleted_files_comments[fname]['comments'] = list()
684 c.deleted_files_comments[fname]['comments'] = list()
689 for lno, comments in per_line_comments.items():
685 for lno, comments in per_line_comments.items():
690 c.deleted_files_comments[fname]['comments'].extend(comments)
686 c.deleted_files_comments[fname]['comments'].extend(comments)
691
687
692 # maybe calculate the range diff
688 # maybe calculate the range diff
693 if c.range_diff_on:
689 if c.range_diff_on:
694 # TODO(marcink): set whitespace/context
690 # TODO(marcink): set whitespace/context
695 context_lcl = 3
691 context_lcl = 3
696 ign_whitespace_lcl = False
692 ign_whitespace_lcl = False
697
693
698 for commit in c.commit_ranges:
694 for commit in c.commit_ranges:
699 commit2 = commit
695 commit2 = commit
700 commit1 = commit.first_parent
696 commit1 = commit.first_parent
701
697
702 range_diff_cache_file_path = diff_cache_exist(
698 range_diff_cache_file_path = diff_cache_exist(
703 cache_path, 'diff', commit.raw_id,
699 cache_path, 'diff', commit.raw_id,
704 ign_whitespace_lcl, context_lcl, c.fulldiff)
700 ign_whitespace_lcl, context_lcl, c.fulldiff)
705
701
706 cached_diff = None
702 cached_diff = None
707 if caching_enabled:
703 if caching_enabled:
708 cached_diff = load_cached_diff(range_diff_cache_file_path)
704 cached_diff = load_cached_diff(range_diff_cache_file_path)
709
705
710 has_proper_diff_cache = cached_diff and cached_diff.get('diff')
706 has_proper_diff_cache = cached_diff and cached_diff.get('diff')
711 if not force_recache and has_proper_diff_cache:
707 if not force_recache and has_proper_diff_cache:
712 diffset = cached_diff['diff']
708 diffset = cached_diff['diff']
713 else:
709 else:
714 diffset = self._get_range_diffset(
710 diffset = self._get_range_diffset(
715 commits_source_repo, source_repo,
711 commits_source_repo, source_repo,
716 commit1, commit2, diff_limit, file_limit,
712 commit1, commit2, diff_limit, file_limit,
717 c.fulldiff, ign_whitespace_lcl, context_lcl
713 c.fulldiff, ign_whitespace_lcl, context_lcl
718 )
714 )
719
715
720 # save cached diff
716 # save cached diff
721 if caching_enabled:
717 if caching_enabled:
722 cache_diff(range_diff_cache_file_path, diffset, None)
718 cache_diff(range_diff_cache_file_path, diffset, None)
723
719
724 c.changes[commit.raw_id] = diffset
720 c.changes[commit.raw_id] = diffset
725
721
726 # this is a hack to properly display links, when creating PR, the
722 # this is a hack to properly display links, when creating PR, the
727 # compare view and others uses different notation, and
723 # compare view and others uses different notation, and
728 # compare_commits.mako renders links based on the target_repo.
724 # compare_commits.mako renders links based on the target_repo.
729 # We need to swap that here to generate it properly on the html side
725 # We need to swap that here to generate it properly on the html side
730 c.target_repo = c.source_repo
726 c.target_repo = c.source_repo
731
727
732 c.commit_statuses = ChangesetStatus.STATUSES
728 c.commit_statuses = ChangesetStatus.STATUSES
733
729
734 c.show_version_changes = not pr_closed
730 c.show_version_changes = not pr_closed
735 if c.show_version_changes:
731 if c.show_version_changes:
736 cur_obj = pull_request_at_ver
732 cur_obj = pull_request_at_ver
737 prev_obj = prev_pull_request_at_ver
733 prev_obj = prev_pull_request_at_ver
738
734
739 old_commit_ids = prev_obj.revisions
735 old_commit_ids = prev_obj.revisions
740 new_commit_ids = cur_obj.revisions
736 new_commit_ids = cur_obj.revisions
741 commit_changes = PullRequestModel()._calculate_commit_id_changes(
737 commit_changes = PullRequestModel()._calculate_commit_id_changes(
742 old_commit_ids, new_commit_ids)
738 old_commit_ids, new_commit_ids)
743 c.commit_changes_summary = commit_changes
739 c.commit_changes_summary = commit_changes
744
740
745 # calculate the diff for commits between versions
741 # calculate the diff for commits between versions
746 c.commit_changes = []
742 c.commit_changes = []
747
743
748 def mark(cs, fw):
744 def mark(cs, fw):
749 return list(h.itertools.izip_longest([], cs, fillvalue=fw))
745 return list(h.itertools.izip_longest([], cs, fillvalue=fw))
750
746
751 for c_type, raw_id in mark(commit_changes.added, 'a') \
747 for c_type, raw_id in mark(commit_changes.added, 'a') \
752 + mark(commit_changes.removed, 'r') \
748 + mark(commit_changes.removed, 'r') \
753 + mark(commit_changes.common, 'c'):
749 + mark(commit_changes.common, 'c'):
754
750
755 if raw_id in commit_cache:
751 if raw_id in commit_cache:
756 commit = commit_cache[raw_id]
752 commit = commit_cache[raw_id]
757 else:
753 else:
758 try:
754 try:
759 commit = commits_source_repo.get_commit(raw_id)
755 commit = commits_source_repo.get_commit(raw_id)
760 except CommitDoesNotExistError:
756 except CommitDoesNotExistError:
761 # in case we fail extracting still use "dummy" commit
757 # in case we fail extracting still use "dummy" commit
762 # for display in commit diff
758 # for display in commit diff
763 commit = h.AttributeDict(
759 commit = h.AttributeDict(
764 {'raw_id': raw_id,
760 {'raw_id': raw_id,
765 'message': 'EMPTY or MISSING COMMIT'})
761 'message': 'EMPTY or MISSING COMMIT'})
766 c.commit_changes.append([c_type, commit])
762 c.commit_changes.append([c_type, commit])
767
763
768 # current user review statuses for each version
764 # current user review statuses for each version
769 c.review_versions = {}
765 c.review_versions = {}
770 is_reviewer = PullRequestModel().is_user_reviewer(
766 is_reviewer = PullRequestModel().is_user_reviewer(
771 pull_request, self._rhodecode_user)
767 pull_request, self._rhodecode_user)
772 if is_reviewer:
768 if is_reviewer:
773 for co in general_comments:
769 for co in general_comments:
774 if co.author.user_id == self._rhodecode_user.user_id:
770 if co.author.user_id == self._rhodecode_user.user_id:
775 status = co.status_change
771 status = co.status_change
776 if status:
772 if status:
777 _ver_pr = status[0].comment.pull_request_version_id
773 _ver_pr = status[0].comment.pull_request_version_id
778 c.review_versions[_ver_pr] = status[0]
774 c.review_versions[_ver_pr] = status[0]
779
775
780 return self._get_template_context(c)
776 return self._get_template_context(c)
781
777
782 def get_commits(
778 def get_commits(
783 self, commits_source_repo, pull_request_at_ver, source_commit,
779 self, commits_source_repo, pull_request_at_ver, source_commit,
784 source_ref_id, source_scm, target_commit, target_ref_id, target_scm,
780 source_ref_id, source_scm, target_commit, target_ref_id, target_scm,
785 maybe_unreachable=False):
781 maybe_unreachable=False):
786
782
787 commit_cache = collections.OrderedDict()
783 commit_cache = collections.OrderedDict()
788 missing_requirements = False
784 missing_requirements = False
789
785
790 try:
786 try:
791 pre_load = ["author", "date", "message", "branch", "parents"]
787 pre_load = ["author", "date", "message", "branch", "parents"]
792
788
793 pull_request_commits = pull_request_at_ver.revisions
789 pull_request_commits = pull_request_at_ver.revisions
794 log.debug('Loading %s commits from %s',
790 log.debug('Loading %s commits from %s',
795 len(pull_request_commits), commits_source_repo)
791 len(pull_request_commits), commits_source_repo)
796
792
797 for rev in pull_request_commits:
793 for rev in pull_request_commits:
798 comm = commits_source_repo.get_commit(commit_id=rev, pre_load=pre_load,
794 comm = commits_source_repo.get_commit(commit_id=rev, pre_load=pre_load,
799 maybe_unreachable=maybe_unreachable)
795 maybe_unreachable=maybe_unreachable)
800 commit_cache[comm.raw_id] = comm
796 commit_cache[comm.raw_id] = comm
801
797
802 # Order here matters, we first need to get target, and then
798 # Order here matters, we first need to get target, and then
803 # the source
799 # the source
804 target_commit = commits_source_repo.get_commit(
800 target_commit = commits_source_repo.get_commit(
805 commit_id=safe_str(target_ref_id))
801 commit_id=safe_str(target_ref_id))
806
802
807 source_commit = commits_source_repo.get_commit(
803 source_commit = commits_source_repo.get_commit(
808 commit_id=safe_str(source_ref_id), maybe_unreachable=True)
804 commit_id=safe_str(source_ref_id), maybe_unreachable=True)
809 except CommitDoesNotExistError:
805 except CommitDoesNotExistError:
810 log.warning('Failed to get commit from `{}` repo'.format(
806 log.warning('Failed to get commit from `{}` repo'.format(
811 commits_source_repo), exc_info=True)
807 commits_source_repo), exc_info=True)
812 except RepositoryRequirementError:
808 except RepositoryRequirementError:
813 log.warning('Failed to get all required data from repo', exc_info=True)
809 log.warning('Failed to get all required data from repo', exc_info=True)
814 missing_requirements = True
810 missing_requirements = True
815
811
816 pr_ancestor_id = pull_request_at_ver.common_ancestor_id
812 pr_ancestor_id = pull_request_at_ver.common_ancestor_id
817
813
818 try:
814 try:
819 ancestor_commit = source_scm.get_commit(pr_ancestor_id)
815 ancestor_commit = source_scm.get_commit(pr_ancestor_id)
820 except Exception:
816 except Exception:
821 ancestor_commit = None
817 ancestor_commit = None
822
818
823 return ancestor_commit, commit_cache, missing_requirements, source_commit, target_commit
819 return ancestor_commit, commit_cache, missing_requirements, source_commit, target_commit
824
820
825 def assure_not_empty_repo(self):
821 def assure_not_empty_repo(self):
826 _ = self.request.translate
822 _ = self.request.translate
827
823
828 try:
824 try:
829 self.db_repo.scm_instance().get_commit()
825 self.db_repo.scm_instance().get_commit()
830 except EmptyRepositoryError:
826 except EmptyRepositoryError:
831 h.flash(h.literal(_('There are no commits yet')),
827 h.flash(h.literal(_('There are no commits yet')),
832 category='warning')
828 category='warning')
833 raise HTTPFound(
829 raise HTTPFound(
834 h.route_path('repo_summary', repo_name=self.db_repo.repo_name))
830 h.route_path('repo_summary', repo_name=self.db_repo.repo_name))
835
831
836 @LoginRequired()
832 @LoginRequired()
837 @NotAnonymous()
833 @NotAnonymous()
838 @HasRepoPermissionAnyDecorator(
834 @HasRepoPermissionAnyDecorator(
839 'repository.read', 'repository.write', 'repository.admin')
835 'repository.read', 'repository.write', 'repository.admin')
840 @view_config(
836 @view_config(
841 route_name='pullrequest_new', request_method='GET',
837 route_name='pullrequest_new', request_method='GET',
842 renderer='rhodecode:templates/pullrequests/pullrequest.mako')
838 renderer='rhodecode:templates/pullrequests/pullrequest.mako')
843 def pull_request_new(self):
839 def pull_request_new(self):
844 _ = self.request.translate
840 _ = self.request.translate
845 c = self.load_default_context()
841 c = self.load_default_context()
846
842
847 self.assure_not_empty_repo()
843 self.assure_not_empty_repo()
848 source_repo = self.db_repo
844 source_repo = self.db_repo
849
845
850 commit_id = self.request.GET.get('commit')
846 commit_id = self.request.GET.get('commit')
851 branch_ref = self.request.GET.get('branch')
847 branch_ref = self.request.GET.get('branch')
852 bookmark_ref = self.request.GET.get('bookmark')
848 bookmark_ref = self.request.GET.get('bookmark')
853
849
854 try:
850 try:
855 source_repo_data = PullRequestModel().generate_repo_data(
851 source_repo_data = PullRequestModel().generate_repo_data(
856 source_repo, commit_id=commit_id,
852 source_repo, commit_id=commit_id,
857 branch=branch_ref, bookmark=bookmark_ref,
853 branch=branch_ref, bookmark=bookmark_ref,
858 translator=self.request.translate)
854 translator=self.request.translate)
859 except CommitDoesNotExistError as e:
855 except CommitDoesNotExistError as e:
860 log.exception(e)
856 log.exception(e)
861 h.flash(_('Commit does not exist'), 'error')
857 h.flash(_('Commit does not exist'), 'error')
862 raise HTTPFound(
858 raise HTTPFound(
863 h.route_path('pullrequest_new', repo_name=source_repo.repo_name))
859 h.route_path('pullrequest_new', repo_name=source_repo.repo_name))
864
860
865 default_target_repo = source_repo
861 default_target_repo = source_repo
866
862
867 if source_repo.parent and c.has_origin_repo_read_perm:
863 if source_repo.parent and c.has_origin_repo_read_perm:
868 parent_vcs_obj = source_repo.parent.scm_instance()
864 parent_vcs_obj = source_repo.parent.scm_instance()
869 if parent_vcs_obj and not parent_vcs_obj.is_empty():
865 if parent_vcs_obj and not parent_vcs_obj.is_empty():
870 # change default if we have a parent repo
866 # change default if we have a parent repo
871 default_target_repo = source_repo.parent
867 default_target_repo = source_repo.parent
872
868
873 target_repo_data = PullRequestModel().generate_repo_data(
869 target_repo_data = PullRequestModel().generate_repo_data(
874 default_target_repo, translator=self.request.translate)
870 default_target_repo, translator=self.request.translate)
875
871
876 selected_source_ref = source_repo_data['refs']['selected_ref']
872 selected_source_ref = source_repo_data['refs']['selected_ref']
877 title_source_ref = ''
873 title_source_ref = ''
878 if selected_source_ref:
874 if selected_source_ref:
879 title_source_ref = selected_source_ref.split(':', 2)[1]
875 title_source_ref = selected_source_ref.split(':', 2)[1]
880 c.default_title = PullRequestModel().generate_pullrequest_title(
876 c.default_title = PullRequestModel().generate_pullrequest_title(
881 source=source_repo.repo_name,
877 source=source_repo.repo_name,
882 source_ref=title_source_ref,
878 source_ref=title_source_ref,
883 target=default_target_repo.repo_name
879 target=default_target_repo.repo_name
884 )
880 )
885
881
886 c.default_repo_data = {
882 c.default_repo_data = {
887 'source_repo_name': source_repo.repo_name,
883 'source_repo_name': source_repo.repo_name,
888 'source_refs_json': json.dumps(source_repo_data),
884 'source_refs_json': json.dumps(source_repo_data),
889 'target_repo_name': default_target_repo.repo_name,
885 'target_repo_name': default_target_repo.repo_name,
890 'target_refs_json': json.dumps(target_repo_data),
886 'target_refs_json': json.dumps(target_repo_data),
891 }
887 }
892 c.default_source_ref = selected_source_ref
888 c.default_source_ref = selected_source_ref
893
889
894 return self._get_template_context(c)
890 return self._get_template_context(c)
895
891
896 @LoginRequired()
892 @LoginRequired()
897 @NotAnonymous()
893 @NotAnonymous()
898 @HasRepoPermissionAnyDecorator(
894 @HasRepoPermissionAnyDecorator(
899 'repository.read', 'repository.write', 'repository.admin')
895 'repository.read', 'repository.write', 'repository.admin')
900 @view_config(
896 @view_config(
901 route_name='pullrequest_repo_refs', request_method='GET',
897 route_name='pullrequest_repo_refs', request_method='GET',
902 renderer='json_ext', xhr=True)
898 renderer='json_ext', xhr=True)
903 def pull_request_repo_refs(self):
899 def pull_request_repo_refs(self):
904 self.load_default_context()
900 self.load_default_context()
905 target_repo_name = self.request.matchdict['target_repo_name']
901 target_repo_name = self.request.matchdict['target_repo_name']
906 repo = Repository.get_by_repo_name(target_repo_name)
902 repo = Repository.get_by_repo_name(target_repo_name)
907 if not repo:
903 if not repo:
908 raise HTTPNotFound()
904 raise HTTPNotFound()
909
905
910 target_perm = HasRepoPermissionAny(
906 target_perm = HasRepoPermissionAny(
911 'repository.read', 'repository.write', 'repository.admin')(
907 'repository.read', 'repository.write', 'repository.admin')(
912 target_repo_name)
908 target_repo_name)
913 if not target_perm:
909 if not target_perm:
914 raise HTTPNotFound()
910 raise HTTPNotFound()
915
911
916 return PullRequestModel().generate_repo_data(
912 return PullRequestModel().generate_repo_data(
917 repo, translator=self.request.translate)
913 repo, translator=self.request.translate)
918
914
919 @LoginRequired()
915 @LoginRequired()
920 @NotAnonymous()
916 @NotAnonymous()
921 @HasRepoPermissionAnyDecorator(
917 @HasRepoPermissionAnyDecorator(
922 'repository.read', 'repository.write', 'repository.admin')
918 'repository.read', 'repository.write', 'repository.admin')
923 @view_config(
919 @view_config(
924 route_name='pullrequest_repo_targets', request_method='GET',
920 route_name='pullrequest_repo_targets', request_method='GET',
925 renderer='json_ext', xhr=True)
921 renderer='json_ext', xhr=True)
926 def pullrequest_repo_targets(self):
922 def pullrequest_repo_targets(self):
927 _ = self.request.translate
923 _ = self.request.translate
928 filter_query = self.request.GET.get('query')
924 filter_query = self.request.GET.get('query')
929
925
930 # get the parents
926 # get the parents
931 parent_target_repos = []
927 parent_target_repos = []
932 if self.db_repo.parent:
928 if self.db_repo.parent:
933 parents_query = Repository.query() \
929 parents_query = Repository.query() \
934 .order_by(func.length(Repository.repo_name)) \
930 .order_by(func.length(Repository.repo_name)) \
935 .filter(Repository.fork_id == self.db_repo.parent.repo_id)
931 .filter(Repository.fork_id == self.db_repo.parent.repo_id)
936
932
937 if filter_query:
933 if filter_query:
938 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
934 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
939 parents_query = parents_query.filter(
935 parents_query = parents_query.filter(
940 Repository.repo_name.ilike(ilike_expression))
936 Repository.repo_name.ilike(ilike_expression))
941 parents = parents_query.limit(20).all()
937 parents = parents_query.limit(20).all()
942
938
943 for parent in parents:
939 for parent in parents:
944 parent_vcs_obj = parent.scm_instance()
940 parent_vcs_obj = parent.scm_instance()
945 if parent_vcs_obj and not parent_vcs_obj.is_empty():
941 if parent_vcs_obj and not parent_vcs_obj.is_empty():
946 parent_target_repos.append(parent)
942 parent_target_repos.append(parent)
947
943
948 # get other forks, and repo itself
944 # get other forks, and repo itself
949 query = Repository.query() \
945 query = Repository.query() \
950 .order_by(func.length(Repository.repo_name)) \
946 .order_by(func.length(Repository.repo_name)) \
951 .filter(
947 .filter(
952 or_(Repository.repo_id == self.db_repo.repo_id, # repo itself
948 or_(Repository.repo_id == self.db_repo.repo_id, # repo itself
953 Repository.fork_id == self.db_repo.repo_id) # forks of this repo
949 Repository.fork_id == self.db_repo.repo_id) # forks of this repo
954 ) \
950 ) \
955 .filter(~Repository.repo_id.in_([x.repo_id for x in parent_target_repos]))
951 .filter(~Repository.repo_id.in_([x.repo_id for x in parent_target_repos]))
956
952
957 if filter_query:
953 if filter_query:
958 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
954 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
959 query = query.filter(Repository.repo_name.ilike(ilike_expression))
955 query = query.filter(Repository.repo_name.ilike(ilike_expression))
960
956
961 limit = max(20 - len(parent_target_repos), 5) # not less then 5
957 limit = max(20 - len(parent_target_repos), 5) # not less then 5
962 target_repos = query.limit(limit).all()
958 target_repos = query.limit(limit).all()
963
959
964 all_target_repos = target_repos + parent_target_repos
960 all_target_repos = target_repos + parent_target_repos
965
961
966 repos = []
962 repos = []
967 # This checks permissions to the repositories
963 # This checks permissions to the repositories
968 for obj in ScmModel().get_repos(all_target_repos):
964 for obj in ScmModel().get_repos(all_target_repos):
969 repos.append({
965 repos.append({
970 'id': obj['name'],
966 'id': obj['name'],
971 'text': obj['name'],
967 'text': obj['name'],
972 'type': 'repo',
968 'type': 'repo',
973 'repo_id': obj['dbrepo']['repo_id'],
969 'repo_id': obj['dbrepo']['repo_id'],
974 'repo_type': obj['dbrepo']['repo_type'],
970 'repo_type': obj['dbrepo']['repo_type'],
975 'private': obj['dbrepo']['private'],
971 'private': obj['dbrepo']['private'],
976
972
977 })
973 })
978
974
979 data = {
975 data = {
980 'more': False,
976 'more': False,
981 'results': [{
977 'results': [{
982 'text': _('Repositories'),
978 'text': _('Repositories'),
983 'children': repos
979 'children': repos
984 }] if repos else []
980 }] if repos else []
985 }
981 }
986 return data
982 return data
987
983
988 @classmethod
984 @classmethod
989 def get_comment_ids(cls, post_data):
985 def get_comment_ids(cls, post_data):
990 return filter(lambda e: e > 0, map(safe_int, aslist(post_data.get('comments'), ',')))
986 return filter(lambda e: e > 0, map(safe_int, aslist(post_data.get('comments'), ',')))
991
987
992 @LoginRequired()
988 @LoginRequired()
993 @NotAnonymous()
989 @NotAnonymous()
994 @HasRepoPermissionAnyDecorator(
990 @HasRepoPermissionAnyDecorator(
995 'repository.read', 'repository.write', 'repository.admin')
991 'repository.read', 'repository.write', 'repository.admin')
996 @view_config(
992 @view_config(
997 route_name='pullrequest_comments', request_method='POST',
993 route_name='pullrequest_comments', request_method='POST',
998 renderer='string_html', xhr=True)
994 renderer='string_html', xhr=True)
999 def pullrequest_comments(self):
995 def pullrequest_comments(self):
1000 self.load_default_context()
996 self.load_default_context()
1001
997
1002 pull_request = PullRequest.get_or_404(
998 pull_request = PullRequest.get_or_404(
1003 self.request.matchdict['pull_request_id'])
999 self.request.matchdict['pull_request_id'])
1004 pull_request_id = pull_request.pull_request_id
1000 pull_request_id = pull_request.pull_request_id
1005 version = self.request.GET.get('version')
1001 version = self.request.GET.get('version')
1006
1002
1007 _render = self.request.get_partial_renderer(
1003 _render = self.request.get_partial_renderer(
1008 'rhodecode:templates/base/sidebar.mako')
1004 'rhodecode:templates/base/sidebar.mako')
1009 c = _render.get_call_context()
1005 c = _render.get_call_context()
1010
1006
1011 (pull_request_latest,
1007 (pull_request_latest,
1012 pull_request_at_ver,
1008 pull_request_at_ver,
1013 pull_request_display_obj,
1009 pull_request_display_obj,
1014 at_version) = PullRequestModel().get_pr_version(
1010 at_version) = PullRequestModel().get_pr_version(
1015 pull_request_id, version=version)
1011 pull_request_id, version=version)
1016 versions = pull_request_display_obj.versions()
1012 versions = pull_request_display_obj.versions()
1017 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
1013 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
1018 c.versions = versions + [latest_ver]
1014 c.versions = versions + [latest_ver]
1019
1015
1020 c.at_version = at_version
1016 c.at_version = at_version
1021 c.at_version_num = (at_version
1017 c.at_version_num = (at_version
1022 if at_version and at_version != PullRequest.LATEST_VER
1018 if at_version and at_version != PullRequest.LATEST_VER
1023 else None)
1019 else None)
1024
1020
1025 self.register_comments_vars(c, pull_request_latest, versions, include_drafts=False)
1021 self.register_comments_vars(c, pull_request_latest, versions, include_drafts=False)
1026 all_comments = c.inline_comments_flat + c.comments
1022 all_comments = c.inline_comments_flat + c.comments
1027
1023
1028 existing_ids = self.get_comment_ids(self.request.POST)
1024 existing_ids = self.get_comment_ids(self.request.POST)
1029 return _render('comments_table', all_comments, len(all_comments),
1025 return _render('comments_table', all_comments, len(all_comments),
1030 existing_ids=existing_ids)
1026 existing_ids=existing_ids)
1031
1027
1032 @LoginRequired()
1028 @LoginRequired()
1033 @NotAnonymous()
1029 @NotAnonymous()
1034 @HasRepoPermissionAnyDecorator(
1030 @HasRepoPermissionAnyDecorator(
1035 'repository.read', 'repository.write', 'repository.admin')
1031 'repository.read', 'repository.write', 'repository.admin')
1036 @view_config(
1032 @view_config(
1037 route_name='pullrequest_todos', request_method='POST',
1033 route_name='pullrequest_todos', request_method='POST',
1038 renderer='string_html', xhr=True)
1034 renderer='string_html', xhr=True)
1039 def pullrequest_todos(self):
1035 def pullrequest_todos(self):
1040 self.load_default_context()
1036 self.load_default_context()
1041
1037
1042 pull_request = PullRequest.get_or_404(
1038 pull_request = PullRequest.get_or_404(
1043 self.request.matchdict['pull_request_id'])
1039 self.request.matchdict['pull_request_id'])
1044 pull_request_id = pull_request.pull_request_id
1040 pull_request_id = pull_request.pull_request_id
1045 version = self.request.GET.get('version')
1041 version = self.request.GET.get('version')
1046
1042
1047 _render = self.request.get_partial_renderer(
1043 _render = self.request.get_partial_renderer(
1048 'rhodecode:templates/base/sidebar.mako')
1044 'rhodecode:templates/base/sidebar.mako')
1049 c = _render.get_call_context()
1045 c = _render.get_call_context()
1050 (pull_request_latest,
1046 (pull_request_latest,
1051 pull_request_at_ver,
1047 pull_request_at_ver,
1052 pull_request_display_obj,
1048 pull_request_display_obj,
1053 at_version) = PullRequestModel().get_pr_version(
1049 at_version) = PullRequestModel().get_pr_version(
1054 pull_request_id, version=version)
1050 pull_request_id, version=version)
1055 versions = pull_request_display_obj.versions()
1051 versions = pull_request_display_obj.versions()
1056 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
1052 latest_ver = PullRequest.get_pr_display_object(pull_request_latest, pull_request_latest)
1057 c.versions = versions + [latest_ver]
1053 c.versions = versions + [latest_ver]
1058
1054
1059 c.at_version = at_version
1055 c.at_version = at_version
1060 c.at_version_num = (at_version
1056 c.at_version_num = (at_version
1061 if at_version and at_version != PullRequest.LATEST_VER
1057 if at_version and at_version != PullRequest.LATEST_VER
1062 else None)
1058 else None)
1063
1059
1064 c.unresolved_comments = CommentsModel() \
1060 c.unresolved_comments = CommentsModel() \
1065 .get_pull_request_unresolved_todos(pull_request, include_drafts=False)
1061 .get_pull_request_unresolved_todos(pull_request, include_drafts=False)
1066 c.resolved_comments = CommentsModel() \
1062 c.resolved_comments = CommentsModel() \
1067 .get_pull_request_resolved_todos(pull_request, include_drafts=False)
1063 .get_pull_request_resolved_todos(pull_request, include_drafts=False)
1068
1064
1069 all_comments = c.unresolved_comments + c.resolved_comments
1065 all_comments = c.unresolved_comments + c.resolved_comments
1070 existing_ids = self.get_comment_ids(self.request.POST)
1066 existing_ids = self.get_comment_ids(self.request.POST)
1071 return _render('comments_table', all_comments, len(c.unresolved_comments),
1067 return _render('comments_table', all_comments, len(c.unresolved_comments),
1072 todo_comments=True, existing_ids=existing_ids)
1068 todo_comments=True, existing_ids=existing_ids)
1073
1069
1074 @LoginRequired()
1070 @LoginRequired()
1075 @NotAnonymous()
1071 @NotAnonymous()
1076 @HasRepoPermissionAnyDecorator(
1072 @HasRepoPermissionAnyDecorator(
1077 'repository.read', 'repository.write', 'repository.admin')
1073 'repository.read', 'repository.write', 'repository.admin')
1078 @CSRFRequired()
1074 @CSRFRequired()
1079 @view_config(
1075 @view_config(
1080 route_name='pullrequest_create', request_method='POST',
1076 route_name='pullrequest_create', request_method='POST',
1081 renderer=None)
1077 renderer=None)
1082 def pull_request_create(self):
1078 def pull_request_create(self):
1083 _ = self.request.translate
1079 _ = self.request.translate
1084 self.assure_not_empty_repo()
1080 self.assure_not_empty_repo()
1085 self.load_default_context()
1081 self.load_default_context()
1086
1082
1087 controls = peppercorn.parse(self.request.POST.items())
1083 controls = peppercorn.parse(self.request.POST.items())
1088
1084
1089 try:
1085 try:
1090 form = PullRequestForm(
1086 form = PullRequestForm(
1091 self.request.translate, self.db_repo.repo_id)()
1087 self.request.translate, self.db_repo.repo_id)()
1092 _form = form.to_python(controls)
1088 _form = form.to_python(controls)
1093 except formencode.Invalid as errors:
1089 except formencode.Invalid as errors:
1094 if errors.error_dict.get('revisions'):
1090 if errors.error_dict.get('revisions'):
1095 msg = 'Revisions: %s' % errors.error_dict['revisions']
1091 msg = 'Revisions: %s' % errors.error_dict['revisions']
1096 elif errors.error_dict.get('pullrequest_title'):
1092 elif errors.error_dict.get('pullrequest_title'):
1097 msg = errors.error_dict.get('pullrequest_title')
1093 msg = errors.error_dict.get('pullrequest_title')
1098 else:
1094 else:
1099 msg = _('Error creating pull request: {}').format(errors)
1095 msg = _('Error creating pull request: {}').format(errors)
1100 log.exception(msg)
1096 log.exception(msg)
1101 h.flash(msg, 'error')
1097 h.flash(msg, 'error')
1102
1098
1103 # would rather just go back to form ...
1099 # would rather just go back to form ...
1104 raise HTTPFound(
1100 raise HTTPFound(
1105 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
1101 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
1106
1102
1107 source_repo = _form['source_repo']
1103 source_repo = _form['source_repo']
1108 source_ref = _form['source_ref']
1104 source_ref = _form['source_ref']
1109 target_repo = _form['target_repo']
1105 target_repo = _form['target_repo']
1110 target_ref = _form['target_ref']
1106 target_ref = _form['target_ref']
1111 commit_ids = _form['revisions'][::-1]
1107 commit_ids = _form['revisions'][::-1]
1112 common_ancestor_id = _form['common_ancestor']
1108 common_ancestor_id = _form['common_ancestor']
1113
1109
1114 # find the ancestor for this pr
1110 # find the ancestor for this pr
1115 source_db_repo = Repository.get_by_repo_name(_form['source_repo'])
1111 source_db_repo = Repository.get_by_repo_name(_form['source_repo'])
1116 target_db_repo = Repository.get_by_repo_name(_form['target_repo'])
1112 target_db_repo = Repository.get_by_repo_name(_form['target_repo'])
1117
1113
1118 if not (source_db_repo or target_db_repo):
1114 if not (source_db_repo or target_db_repo):
1119 h.flash(_('source_repo or target repo not found'), category='error')
1115 h.flash(_('source_repo or target repo not found'), category='error')
1120 raise HTTPFound(
1116 raise HTTPFound(
1121 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
1117 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
1122
1118
1123 # re-check permissions again here
1119 # re-check permissions again here
1124 # source_repo we must have read permissions
1120 # source_repo we must have read permissions
1125
1121
1126 source_perm = HasRepoPermissionAny(
1122 source_perm = HasRepoPermissionAny(
1127 'repository.read', 'repository.write', 'repository.admin')(
1123 'repository.read', 'repository.write', 'repository.admin')(
1128 source_db_repo.repo_name)
1124 source_db_repo.repo_name)
1129 if not source_perm:
1125 if not source_perm:
1130 msg = _('Not Enough permissions to source repo `{}`.'.format(
1126 msg = _('Not Enough permissions to source repo `{}`.'.format(
1131 source_db_repo.repo_name))
1127 source_db_repo.repo_name))
1132 h.flash(msg, category='error')
1128 h.flash(msg, category='error')
1133 # copy the args back to redirect
1129 # copy the args back to redirect
1134 org_query = self.request.GET.mixed()
1130 org_query = self.request.GET.mixed()
1135 raise HTTPFound(
1131 raise HTTPFound(
1136 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1132 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1137 _query=org_query))
1133 _query=org_query))
1138
1134
1139 # target repo we must have read permissions, and also later on
1135 # target repo we must have read permissions, and also later on
1140 # we want to check branch permissions here
1136 # we want to check branch permissions here
1141 target_perm = HasRepoPermissionAny(
1137 target_perm = HasRepoPermissionAny(
1142 'repository.read', 'repository.write', 'repository.admin')(
1138 'repository.read', 'repository.write', 'repository.admin')(
1143 target_db_repo.repo_name)
1139 target_db_repo.repo_name)
1144 if not target_perm:
1140 if not target_perm:
1145 msg = _('Not Enough permissions to target repo `{}`.'.format(
1141 msg = _('Not Enough permissions to target repo `{}`.'.format(
1146 target_db_repo.repo_name))
1142 target_db_repo.repo_name))
1147 h.flash(msg, category='error')
1143 h.flash(msg, category='error')
1148 # copy the args back to redirect
1144 # copy the args back to redirect
1149 org_query = self.request.GET.mixed()
1145 org_query = self.request.GET.mixed()
1150 raise HTTPFound(
1146 raise HTTPFound(
1151 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1147 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1152 _query=org_query))
1148 _query=org_query))
1153
1149
1154 source_scm = source_db_repo.scm_instance()
1150 source_scm = source_db_repo.scm_instance()
1155 target_scm = target_db_repo.scm_instance()
1151 target_scm = target_db_repo.scm_instance()
1156
1152
1157 source_ref_obj = unicode_to_reference(source_ref)
1153 source_ref_obj = unicode_to_reference(source_ref)
1158 target_ref_obj = unicode_to_reference(target_ref)
1154 target_ref_obj = unicode_to_reference(target_ref)
1159
1155
1160 source_commit = source_scm.get_commit(source_ref_obj.commit_id)
1156 source_commit = source_scm.get_commit(source_ref_obj.commit_id)
1161 target_commit = target_scm.get_commit(target_ref_obj.commit_id)
1157 target_commit = target_scm.get_commit(target_ref_obj.commit_id)
1162
1158
1163 ancestor = source_scm.get_common_ancestor(
1159 ancestor = source_scm.get_common_ancestor(
1164 source_commit.raw_id, target_commit.raw_id, target_scm)
1160 source_commit.raw_id, target_commit.raw_id, target_scm)
1165
1161
1166 # recalculate target ref based on ancestor
1162 # recalculate target ref based on ancestor
1167 target_ref = ':'.join((target_ref_obj.type, target_ref_obj.name, ancestor))
1163 target_ref = ':'.join((target_ref_obj.type, target_ref_obj.name, ancestor))
1168
1164
1169 get_default_reviewers_data, validate_default_reviewers, validate_observers = \
1165 get_default_reviewers_data, validate_default_reviewers, validate_observers = \
1170 PullRequestModel().get_reviewer_functions()
1166 PullRequestModel().get_reviewer_functions()
1171
1167
1172 # recalculate reviewers logic, to make sure we can validate this
1168 # recalculate reviewers logic, to make sure we can validate this
1173 reviewer_rules = get_default_reviewers_data(
1169 reviewer_rules = get_default_reviewers_data(
1174 self._rhodecode_db_user,
1170 self._rhodecode_db_user,
1175 source_db_repo,
1171 source_db_repo,
1176 source_ref_obj,
1172 source_ref_obj,
1177 target_db_repo,
1173 target_db_repo,
1178 target_ref_obj,
1174 target_ref_obj,
1179 include_diff_info=False)
1175 include_diff_info=False)
1180
1176
1181 reviewers = validate_default_reviewers(_form['review_members'], reviewer_rules)
1177 reviewers = validate_default_reviewers(_form['review_members'], reviewer_rules)
1182 observers = validate_observers(_form['observer_members'], reviewer_rules)
1178 observers = validate_observers(_form['observer_members'], reviewer_rules)
1183
1179
1184 pullrequest_title = _form['pullrequest_title']
1180 pullrequest_title = _form['pullrequest_title']
1185 title_source_ref = source_ref_obj.name
1181 title_source_ref = source_ref_obj.name
1186 if not pullrequest_title:
1182 if not pullrequest_title:
1187 pullrequest_title = PullRequestModel().generate_pullrequest_title(
1183 pullrequest_title = PullRequestModel().generate_pullrequest_title(
1188 source=source_repo,
1184 source=source_repo,
1189 source_ref=title_source_ref,
1185 source_ref=title_source_ref,
1190 target=target_repo
1186 target=target_repo
1191 )
1187 )
1192
1188
1193 description = _form['pullrequest_desc']
1189 description = _form['pullrequest_desc']
1194 description_renderer = _form['description_renderer']
1190 description_renderer = _form['description_renderer']
1195
1191
1196 try:
1192 try:
1197 pull_request = PullRequestModel().create(
1193 pull_request = PullRequestModel().create(
1198 created_by=self._rhodecode_user.user_id,
1194 created_by=self._rhodecode_user.user_id,
1199 source_repo=source_repo,
1195 source_repo=source_repo,
1200 source_ref=source_ref,
1196 source_ref=source_ref,
1201 target_repo=target_repo,
1197 target_repo=target_repo,
1202 target_ref=target_ref,
1198 target_ref=target_ref,
1203 revisions=commit_ids,
1199 revisions=commit_ids,
1204 common_ancestor_id=common_ancestor_id,
1200 common_ancestor_id=common_ancestor_id,
1205 reviewers=reviewers,
1201 reviewers=reviewers,
1206 observers=observers,
1202 observers=observers,
1207 title=pullrequest_title,
1203 title=pullrequest_title,
1208 description=description,
1204 description=description,
1209 description_renderer=description_renderer,
1205 description_renderer=description_renderer,
1210 reviewer_data=reviewer_rules,
1206 reviewer_data=reviewer_rules,
1211 auth_user=self._rhodecode_user
1207 auth_user=self._rhodecode_user
1212 )
1208 )
1213 Session().commit()
1209 Session().commit()
1214
1210
1215 h.flash(_('Successfully opened new pull request'),
1211 h.flash(_('Successfully opened new pull request'),
1216 category='success')
1212 category='success')
1217 except Exception:
1213 except Exception:
1218 msg = _('Error occurred during creation of this pull request.')
1214 msg = _('Error occurred during creation of this pull request.')
1219 log.exception(msg)
1215 log.exception(msg)
1220 h.flash(msg, category='error')
1216 h.flash(msg, category='error')
1221
1217
1222 # copy the args back to redirect
1218 # copy the args back to redirect
1223 org_query = self.request.GET.mixed()
1219 org_query = self.request.GET.mixed()
1224 raise HTTPFound(
1220 raise HTTPFound(
1225 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1221 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
1226 _query=org_query))
1222 _query=org_query))
1227
1223
1228 raise HTTPFound(
1224 raise HTTPFound(
1229 h.route_path('pullrequest_show', repo_name=target_repo,
1225 h.route_path('pullrequest_show', repo_name=target_repo,
1230 pull_request_id=pull_request.pull_request_id))
1226 pull_request_id=pull_request.pull_request_id))
1231
1227
1232 @LoginRequired()
1228 @LoginRequired()
1233 @NotAnonymous()
1229 @NotAnonymous()
1234 @HasRepoPermissionAnyDecorator(
1230 @HasRepoPermissionAnyDecorator(
1235 'repository.read', 'repository.write', 'repository.admin')
1231 'repository.read', 'repository.write', 'repository.admin')
1236 @CSRFRequired()
1232 @CSRFRequired()
1237 @view_config(
1233 @view_config(
1238 route_name='pullrequest_update', request_method='POST',
1234 route_name='pullrequest_update', request_method='POST',
1239 renderer='json_ext')
1235 renderer='json_ext')
1240 def pull_request_update(self):
1236 def pull_request_update(self):
1241 pull_request = PullRequest.get_or_404(
1237 pull_request = PullRequest.get_or_404(
1242 self.request.matchdict['pull_request_id'])
1238 self.request.matchdict['pull_request_id'])
1243 _ = self.request.translate
1239 _ = self.request.translate
1244
1240
1245 c = self.load_default_context()
1241 c = self.load_default_context()
1246 redirect_url = None
1242 redirect_url = None
1247
1243
1248 if pull_request.is_closed():
1244 if pull_request.is_closed():
1249 log.debug('update: forbidden because pull request is closed')
1245 log.debug('update: forbidden because pull request is closed')
1250 msg = _(u'Cannot update closed pull requests.')
1246 msg = _(u'Cannot update closed pull requests.')
1251 h.flash(msg, category='error')
1247 h.flash(msg, category='error')
1252 return {'response': True,
1248 return {'response': True,
1253 'redirect_url': redirect_url}
1249 'redirect_url': redirect_url}
1254
1250
1255 is_state_changing = pull_request.is_state_changing()
1251 is_state_changing = pull_request.is_state_changing()
1256 c.pr_broadcast_channel = channelstream.pr_channel(pull_request)
1252 c.pr_broadcast_channel = channelstream.pr_channel(pull_request)
1257
1253
1258 # only owner or admin can update it
1254 # only owner or admin can update it
1259 allowed_to_update = PullRequestModel().check_user_update(
1255 allowed_to_update = PullRequestModel().check_user_update(
1260 pull_request, self._rhodecode_user)
1256 pull_request, self._rhodecode_user)
1261
1257
1262 if allowed_to_update:
1258 if allowed_to_update:
1263 controls = peppercorn.parse(self.request.POST.items())
1259 controls = peppercorn.parse(self.request.POST.items())
1264 force_refresh = str2bool(self.request.POST.get('force_refresh'))
1260 force_refresh = str2bool(self.request.POST.get('force_refresh'))
1265
1261
1266 if 'review_members' in controls:
1262 if 'review_members' in controls:
1267 self._update_reviewers(
1263 self._update_reviewers(
1268 c,
1264 c,
1269 pull_request, controls['review_members'],
1265 pull_request, controls['review_members'],
1270 pull_request.reviewer_data,
1266 pull_request.reviewer_data,
1271 PullRequestReviewers.ROLE_REVIEWER)
1267 PullRequestReviewers.ROLE_REVIEWER)
1272 elif 'observer_members' in controls:
1268 elif 'observer_members' in controls:
1273 self._update_reviewers(
1269 self._update_reviewers(
1274 c,
1270 c,
1275 pull_request, controls['observer_members'],
1271 pull_request, controls['observer_members'],
1276 pull_request.reviewer_data,
1272 pull_request.reviewer_data,
1277 PullRequestReviewers.ROLE_OBSERVER)
1273 PullRequestReviewers.ROLE_OBSERVER)
1278 elif str2bool(self.request.POST.get('update_commits', 'false')):
1274 elif str2bool(self.request.POST.get('update_commits', 'false')):
1279 if is_state_changing:
1275 if is_state_changing:
1280 log.debug('commits update: forbidden because pull request is in state %s',
1276 log.debug('commits update: forbidden because pull request is in state %s',
1281 pull_request.pull_request_state)
1277 pull_request.pull_request_state)
1282 msg = _(u'Cannot update pull requests commits in state other than `{}`. '
1278 msg = _(u'Cannot update pull requests commits in state other than `{}`. '
1283 u'Current state is: `{}`').format(
1279 u'Current state is: `{}`').format(
1284 PullRequest.STATE_CREATED, pull_request.pull_request_state)
1280 PullRequest.STATE_CREATED, pull_request.pull_request_state)
1285 h.flash(msg, category='error')
1281 h.flash(msg, category='error')
1286 return {'response': True,
1282 return {'response': True,
1287 'redirect_url': redirect_url}
1283 'redirect_url': redirect_url}
1288
1284
1289 self._update_commits(c, pull_request)
1285 self._update_commits(c, pull_request)
1290 if force_refresh:
1286 if force_refresh:
1291 redirect_url = h.route_path(
1287 redirect_url = h.route_path(
1292 'pullrequest_show', repo_name=self.db_repo_name,
1288 'pullrequest_show', repo_name=self.db_repo_name,
1293 pull_request_id=pull_request.pull_request_id,
1289 pull_request_id=pull_request.pull_request_id,
1294 _query={"force_refresh": 1})
1290 _query={"force_refresh": 1})
1295 elif str2bool(self.request.POST.get('edit_pull_request', 'false')):
1291 elif str2bool(self.request.POST.get('edit_pull_request', 'false')):
1296 self._edit_pull_request(pull_request)
1292 self._edit_pull_request(pull_request)
1297 else:
1293 else:
1298 log.error('Unhandled update data.')
1294 log.error('Unhandled update data.')
1299 raise HTTPBadRequest()
1295 raise HTTPBadRequest()
1300
1296
1301 return {'response': True,
1297 return {'response': True,
1302 'redirect_url': redirect_url}
1298 'redirect_url': redirect_url}
1303 raise HTTPForbidden()
1299 raise HTTPForbidden()
1304
1300
1305 def _edit_pull_request(self, pull_request):
1301 def _edit_pull_request(self, pull_request):
1306 """
1302 """
1307 Edit title and description
1303 Edit title and description
1308 """
1304 """
1309 _ = self.request.translate
1305 _ = self.request.translate
1310
1306
1311 try:
1307 try:
1312 PullRequestModel().edit(
1308 PullRequestModel().edit(
1313 pull_request,
1309 pull_request,
1314 self.request.POST.get('title'),
1310 self.request.POST.get('title'),
1315 self.request.POST.get('description'),
1311 self.request.POST.get('description'),
1316 self.request.POST.get('description_renderer'),
1312 self.request.POST.get('description_renderer'),
1317 self._rhodecode_user)
1313 self._rhodecode_user)
1318 except ValueError:
1314 except ValueError:
1319 msg = _(u'Cannot update closed pull requests.')
1315 msg = _(u'Cannot update closed pull requests.')
1320 h.flash(msg, category='error')
1316 h.flash(msg, category='error')
1321 return
1317 return
1322 else:
1318 else:
1323 Session().commit()
1319 Session().commit()
1324
1320
1325 msg = _(u'Pull request title & description updated.')
1321 msg = _(u'Pull request title & description updated.')
1326 h.flash(msg, category='success')
1322 h.flash(msg, category='success')
1327 return
1323 return
1328
1324
1329 def _update_commits(self, c, pull_request):
1325 def _update_commits(self, c, pull_request):
1330 _ = self.request.translate
1326 _ = self.request.translate
1331
1327
1332 with pull_request.set_state(PullRequest.STATE_UPDATING):
1328 with pull_request.set_state(PullRequest.STATE_UPDATING):
1333 resp = PullRequestModel().update_commits(
1329 resp = PullRequestModel().update_commits(
1334 pull_request, self._rhodecode_db_user)
1330 pull_request, self._rhodecode_db_user)
1335
1331
1336 if resp.executed:
1332 if resp.executed:
1337
1333
1338 if resp.target_changed and resp.source_changed:
1334 if resp.target_changed and resp.source_changed:
1339 changed = 'target and source repositories'
1335 changed = 'target and source repositories'
1340 elif resp.target_changed and not resp.source_changed:
1336 elif resp.target_changed and not resp.source_changed:
1341 changed = 'target repository'
1337 changed = 'target repository'
1342 elif not resp.target_changed and resp.source_changed:
1338 elif not resp.target_changed and resp.source_changed:
1343 changed = 'source repository'
1339 changed = 'source repository'
1344 else:
1340 else:
1345 changed = 'nothing'
1341 changed = 'nothing'
1346
1342
1347 msg = _(u'Pull request updated to "{source_commit_id}" with '
1343 msg = _(u'Pull request updated to "{source_commit_id}" with '
1348 u'{count_added} added, {count_removed} removed commits. '
1344 u'{count_added} added, {count_removed} removed commits. '
1349 u'Source of changes: {change_source}.')
1345 u'Source of changes: {change_source}.')
1350 msg = msg.format(
1346 msg = msg.format(
1351 source_commit_id=pull_request.source_ref_parts.commit_id,
1347 source_commit_id=pull_request.source_ref_parts.commit_id,
1352 count_added=len(resp.changes.added),
1348 count_added=len(resp.changes.added),
1353 count_removed=len(resp.changes.removed),
1349 count_removed=len(resp.changes.removed),
1354 change_source=changed)
1350 change_source=changed)
1355 h.flash(msg, category='success')
1351 h.flash(msg, category='success')
1356 channelstream.pr_update_channelstream_push(
1352 channelstream.pr_update_channelstream_push(
1357 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1353 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1358 else:
1354 else:
1359 msg = PullRequestModel.UPDATE_STATUS_MESSAGES[resp.reason]
1355 msg = PullRequestModel.UPDATE_STATUS_MESSAGES[resp.reason]
1360 warning_reasons = [
1356 warning_reasons = [
1361 UpdateFailureReason.NO_CHANGE,
1357 UpdateFailureReason.NO_CHANGE,
1362 UpdateFailureReason.WRONG_REF_TYPE,
1358 UpdateFailureReason.WRONG_REF_TYPE,
1363 ]
1359 ]
1364 category = 'warning' if resp.reason in warning_reasons else 'error'
1360 category = 'warning' if resp.reason in warning_reasons else 'error'
1365 h.flash(msg, category=category)
1361 h.flash(msg, category=category)
1366
1362
1367 def _update_reviewers(self, c, pull_request, review_members, reviewer_rules, role):
1363 def _update_reviewers(self, c, pull_request, review_members, reviewer_rules, role):
1368 _ = self.request.translate
1364 _ = self.request.translate
1369
1365
1370 get_default_reviewers_data, validate_default_reviewers, validate_observers = \
1366 get_default_reviewers_data, validate_default_reviewers, validate_observers = \
1371 PullRequestModel().get_reviewer_functions()
1367 PullRequestModel().get_reviewer_functions()
1372
1368
1373 if role == PullRequestReviewers.ROLE_REVIEWER:
1369 if role == PullRequestReviewers.ROLE_REVIEWER:
1374 try:
1370 try:
1375 reviewers = validate_default_reviewers(review_members, reviewer_rules)
1371 reviewers = validate_default_reviewers(review_members, reviewer_rules)
1376 except ValueError as e:
1372 except ValueError as e:
1377 log.error('Reviewers Validation: {}'.format(e))
1373 log.error('Reviewers Validation: {}'.format(e))
1378 h.flash(e, category='error')
1374 h.flash(e, category='error')
1379 return
1375 return
1380
1376
1381 old_calculated_status = pull_request.calculated_review_status()
1377 old_calculated_status = pull_request.calculated_review_status()
1382 PullRequestModel().update_reviewers(
1378 PullRequestModel().update_reviewers(
1383 pull_request, reviewers, self._rhodecode_db_user)
1379 pull_request, reviewers, self._rhodecode_db_user)
1384
1380
1385 Session().commit()
1381 Session().commit()
1386
1382
1387 msg = _('Pull request reviewers updated.')
1383 msg = _('Pull request reviewers updated.')
1388 h.flash(msg, category='success')
1384 h.flash(msg, category='success')
1389 channelstream.pr_update_channelstream_push(
1385 channelstream.pr_update_channelstream_push(
1390 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1386 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1391
1387
1392 # trigger status changed if change in reviewers changes the status
1388 # trigger status changed if change in reviewers changes the status
1393 calculated_status = pull_request.calculated_review_status()
1389 calculated_status = pull_request.calculated_review_status()
1394 if old_calculated_status != calculated_status:
1390 if old_calculated_status != calculated_status:
1395 PullRequestModel().trigger_pull_request_hook(
1391 PullRequestModel().trigger_pull_request_hook(
1396 pull_request, self._rhodecode_user, 'review_status_change',
1392 pull_request, self._rhodecode_user, 'review_status_change',
1397 data={'status': calculated_status})
1393 data={'status': calculated_status})
1398
1394
1399 elif role == PullRequestReviewers.ROLE_OBSERVER:
1395 elif role == PullRequestReviewers.ROLE_OBSERVER:
1400 try:
1396 try:
1401 observers = validate_observers(review_members, reviewer_rules)
1397 observers = validate_observers(review_members, reviewer_rules)
1402 except ValueError as e:
1398 except ValueError as e:
1403 log.error('Observers Validation: {}'.format(e))
1399 log.error('Observers Validation: {}'.format(e))
1404 h.flash(e, category='error')
1400 h.flash(e, category='error')
1405 return
1401 return
1406
1402
1407 PullRequestModel().update_observers(
1403 PullRequestModel().update_observers(
1408 pull_request, observers, self._rhodecode_db_user)
1404 pull_request, observers, self._rhodecode_db_user)
1409
1405
1410 Session().commit()
1406 Session().commit()
1411 msg = _('Pull request observers updated.')
1407 msg = _('Pull request observers updated.')
1412 h.flash(msg, category='success')
1408 h.flash(msg, category='success')
1413 channelstream.pr_update_channelstream_push(
1409 channelstream.pr_update_channelstream_push(
1414 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1410 self.request, c.pr_broadcast_channel, self._rhodecode_user, msg)
1415
1411
1416 @LoginRequired()
1412 @LoginRequired()
1417 @NotAnonymous()
1413 @NotAnonymous()
1418 @HasRepoPermissionAnyDecorator(
1414 @HasRepoPermissionAnyDecorator(
1419 'repository.read', 'repository.write', 'repository.admin')
1415 'repository.read', 'repository.write', 'repository.admin')
1420 @CSRFRequired()
1416 @CSRFRequired()
1421 @view_config(
1417 @view_config(
1422 route_name='pullrequest_merge', request_method='POST',
1418 route_name='pullrequest_merge', request_method='POST',
1423 renderer='json_ext')
1419 renderer='json_ext')
1424 def pull_request_merge(self):
1420 def pull_request_merge(self):
1425 """
1421 """
1426 Merge will perform a server-side merge of the specified
1422 Merge will perform a server-side merge of the specified
1427 pull request, if the pull request is approved and mergeable.
1423 pull request, if the pull request is approved and mergeable.
1428 After successful merging, the pull request is automatically
1424 After successful merging, the pull request is automatically
1429 closed, with a relevant comment.
1425 closed, with a relevant comment.
1430 """
1426 """
1431 pull_request = PullRequest.get_or_404(
1427 pull_request = PullRequest.get_or_404(
1432 self.request.matchdict['pull_request_id'])
1428 self.request.matchdict['pull_request_id'])
1433 _ = self.request.translate
1429 _ = self.request.translate
1434
1430
1435 if pull_request.is_state_changing():
1431 if pull_request.is_state_changing():
1436 log.debug('show: forbidden because pull request is in state %s',
1432 log.debug('show: forbidden because pull request is in state %s',
1437 pull_request.pull_request_state)
1433 pull_request.pull_request_state)
1438 msg = _(u'Cannot merge pull requests in state other than `{}`. '
1434 msg = _(u'Cannot merge pull requests in state other than `{}`. '
1439 u'Current state is: `{}`').format(PullRequest.STATE_CREATED,
1435 u'Current state is: `{}`').format(PullRequest.STATE_CREATED,
1440 pull_request.pull_request_state)
1436 pull_request.pull_request_state)
1441 h.flash(msg, category='error')
1437 h.flash(msg, category='error')
1442 raise HTTPFound(
1438 raise HTTPFound(
1443 h.route_path('pullrequest_show',
1439 h.route_path('pullrequest_show',
1444 repo_name=pull_request.target_repo.repo_name,
1440 repo_name=pull_request.target_repo.repo_name,
1445 pull_request_id=pull_request.pull_request_id))
1441 pull_request_id=pull_request.pull_request_id))
1446
1442
1447 self.load_default_context()
1443 self.load_default_context()
1448
1444
1449 with pull_request.set_state(PullRequest.STATE_UPDATING):
1445 with pull_request.set_state(PullRequest.STATE_UPDATING):
1450 check = MergeCheck.validate(
1446 check = MergeCheck.validate(
1451 pull_request, auth_user=self._rhodecode_user,
1447 pull_request, auth_user=self._rhodecode_user,
1452 translator=self.request.translate)
1448 translator=self.request.translate)
1453 merge_possible = not check.failed
1449 merge_possible = not check.failed
1454
1450
1455 for err_type, error_msg in check.errors:
1451 for err_type, error_msg in check.errors:
1456 h.flash(error_msg, category=err_type)
1452 h.flash(error_msg, category=err_type)
1457
1453
1458 if merge_possible:
1454 if merge_possible:
1459 log.debug("Pre-conditions checked, trying to merge.")
1455 log.debug("Pre-conditions checked, trying to merge.")
1460 extras = vcs_operation_context(
1456 extras = vcs_operation_context(
1461 self.request.environ, repo_name=pull_request.target_repo.repo_name,
1457 self.request.environ, repo_name=pull_request.target_repo.repo_name,
1462 username=self._rhodecode_db_user.username, action='push',
1458 username=self._rhodecode_db_user.username, action='push',
1463 scm=pull_request.target_repo.repo_type)
1459 scm=pull_request.target_repo.repo_type)
1464 with pull_request.set_state(PullRequest.STATE_UPDATING):
1460 with pull_request.set_state(PullRequest.STATE_UPDATING):
1465 self._merge_pull_request(
1461 self._merge_pull_request(
1466 pull_request, self._rhodecode_db_user, extras)
1462 pull_request, self._rhodecode_db_user, extras)
1467 else:
1463 else:
1468 log.debug("Pre-conditions failed, NOT merging.")
1464 log.debug("Pre-conditions failed, NOT merging.")
1469
1465
1470 raise HTTPFound(
1466 raise HTTPFound(
1471 h.route_path('pullrequest_show',
1467 h.route_path('pullrequest_show',
1472 repo_name=pull_request.target_repo.repo_name,
1468 repo_name=pull_request.target_repo.repo_name,
1473 pull_request_id=pull_request.pull_request_id))
1469 pull_request_id=pull_request.pull_request_id))
1474
1470
1475 def _merge_pull_request(self, pull_request, user, extras):
1471 def _merge_pull_request(self, pull_request, user, extras):
1476 _ = self.request.translate
1472 _ = self.request.translate
1477 merge_resp = PullRequestModel().merge_repo(pull_request, user, extras=extras)
1473 merge_resp = PullRequestModel().merge_repo(pull_request, user, extras=extras)
1478
1474
1479 if merge_resp.executed:
1475 if merge_resp.executed:
1480 log.debug("The merge was successful, closing the pull request.")
1476 log.debug("The merge was successful, closing the pull request.")
1481 PullRequestModel().close_pull_request(
1477 PullRequestModel().close_pull_request(
1482 pull_request.pull_request_id, user)
1478 pull_request.pull_request_id, user)
1483 Session().commit()
1479 Session().commit()
1484 msg = _('Pull request was successfully merged and closed.')
1480 msg = _('Pull request was successfully merged and closed.')
1485 h.flash(msg, category='success')
1481 h.flash(msg, category='success')
1486 else:
1482 else:
1487 log.debug(
1483 log.debug(
1488 "The merge was not successful. Merge response: %s", merge_resp)
1484 "The merge was not successful. Merge response: %s", merge_resp)
1489 msg = merge_resp.merge_status_message
1485 msg = merge_resp.merge_status_message
1490 h.flash(msg, category='error')
1486 h.flash(msg, category='error')
1491
1487
1492 @LoginRequired()
1488 @LoginRequired()
1493 @NotAnonymous()
1489 @NotAnonymous()
1494 @HasRepoPermissionAnyDecorator(
1490 @HasRepoPermissionAnyDecorator(
1495 'repository.read', 'repository.write', 'repository.admin')
1491 'repository.read', 'repository.write', 'repository.admin')
1496 @CSRFRequired()
1492 @CSRFRequired()
1497 @view_config(
1493 @view_config(
1498 route_name='pullrequest_delete', request_method='POST',
1494 route_name='pullrequest_delete', request_method='POST',
1499 renderer='json_ext')
1495 renderer='json_ext')
1500 def pull_request_delete(self):
1496 def pull_request_delete(self):
1501 _ = self.request.translate
1497 _ = self.request.translate
1502
1498
1503 pull_request = PullRequest.get_or_404(
1499 pull_request = PullRequest.get_or_404(
1504 self.request.matchdict['pull_request_id'])
1500 self.request.matchdict['pull_request_id'])
1505 self.load_default_context()
1501 self.load_default_context()
1506
1502
1507 pr_closed = pull_request.is_closed()
1503 pr_closed = pull_request.is_closed()
1508 allowed_to_delete = PullRequestModel().check_user_delete(
1504 allowed_to_delete = PullRequestModel().check_user_delete(
1509 pull_request, self._rhodecode_user) and not pr_closed
1505 pull_request, self._rhodecode_user) and not pr_closed
1510
1506
1511 # only owner can delete it !
1507 # only owner can delete it !
1512 if allowed_to_delete:
1508 if allowed_to_delete:
1513 PullRequestModel().delete(pull_request, self._rhodecode_user)
1509 PullRequestModel().delete(pull_request, self._rhodecode_user)
1514 Session().commit()
1510 Session().commit()
1515 h.flash(_('Successfully deleted pull request'),
1511 h.flash(_('Successfully deleted pull request'),
1516 category='success')
1512 category='success')
1517 raise HTTPFound(h.route_path('pullrequest_show_all',
1513 raise HTTPFound(h.route_path('pullrequest_show_all',
1518 repo_name=self.db_repo_name))
1514 repo_name=self.db_repo_name))
1519
1515
1520 log.warning('user %s tried to delete pull request without access',
1516 log.warning('user %s tried to delete pull request without access',
1521 self._rhodecode_user)
1517 self._rhodecode_user)
1522 raise HTTPNotFound()
1518 raise HTTPNotFound()
1523
1519
1524 def _pull_request_comments_create(self, pull_request, comments):
1520 def _pull_request_comments_create(self, pull_request, comments):
1525 _ = self.request.translate
1521 _ = self.request.translate
1526 data = {}
1522 data = {}
1527 if not comments:
1523 if not comments:
1528 return
1524 return
1529 pull_request_id = pull_request.pull_request_id
1525 pull_request_id = pull_request.pull_request_id
1530
1526
1531 all_drafts = len([x for x in comments if str2bool(x['is_draft'])]) == len(comments)
1527 all_drafts = len([x for x in comments if str2bool(x['is_draft'])]) == len(comments)
1532
1528
1533 for entry in comments:
1529 for entry in comments:
1534 c = self.load_default_context()
1530 c = self.load_default_context()
1535 comment_type = entry['comment_type']
1531 comment_type = entry['comment_type']
1536 text = entry['text']
1532 text = entry['text']
1537 status = entry['status']
1533 status = entry['status']
1538 is_draft = str2bool(entry['is_draft'])
1534 is_draft = str2bool(entry['is_draft'])
1539 resolves_comment_id = entry['resolves_comment_id']
1535 resolves_comment_id = entry['resolves_comment_id']
1540 close_pull_request = entry['close_pull_request']
1536 close_pull_request = entry['close_pull_request']
1541 f_path = entry['f_path']
1537 f_path = entry['f_path']
1542 line_no = entry['line']
1538 line_no = entry['line']
1543 target_elem_id = 'file-{}'.format(h.safeid(h.safe_unicode(f_path)))
1539 target_elem_id = 'file-{}'.format(h.safeid(h.safe_unicode(f_path)))
1544
1540
1545 # the logic here should work like following, if we submit close
1541 # the logic here should work like following, if we submit close
1546 # pr comment, use `close_pull_request_with_comment` function
1542 # pr comment, use `close_pull_request_with_comment` function
1547 # else handle regular comment logic
1543 # else handle regular comment logic
1548
1544
1549 if close_pull_request:
1545 if close_pull_request:
1550 # only owner or admin or person with write permissions
1546 # only owner or admin or person with write permissions
1551 allowed_to_close = PullRequestModel().check_user_update(
1547 allowed_to_close = PullRequestModel().check_user_update(
1552 pull_request, self._rhodecode_user)
1548 pull_request, self._rhodecode_user)
1553 if not allowed_to_close:
1549 if not allowed_to_close:
1554 log.debug('comment: forbidden because not allowed to close '
1550 log.debug('comment: forbidden because not allowed to close '
1555 'pull request %s', pull_request_id)
1551 'pull request %s', pull_request_id)
1556 raise HTTPForbidden()
1552 raise HTTPForbidden()
1557
1553
1558 # This also triggers `review_status_change`
1554 # This also triggers `review_status_change`
1559 comment, status = PullRequestModel().close_pull_request_with_comment(
1555 comment, status = PullRequestModel().close_pull_request_with_comment(
1560 pull_request, self._rhodecode_user, self.db_repo, message=text,
1556 pull_request, self._rhodecode_user, self.db_repo, message=text,
1561 auth_user=self._rhodecode_user)
1557 auth_user=self._rhodecode_user)
1562 Session().flush()
1558 Session().flush()
1563 is_inline = comment.is_inline
1559 is_inline = comment.is_inline
1564
1560
1565 PullRequestModel().trigger_pull_request_hook(
1561 PullRequestModel().trigger_pull_request_hook(
1566 pull_request, self._rhodecode_user, 'comment',
1562 pull_request, self._rhodecode_user, 'comment',
1567 data={'comment': comment})
1563 data={'comment': comment})
1568
1564
1569 else:
1565 else:
1570 # regular comment case, could be inline, or one with status.
1566 # regular comment case, could be inline, or one with status.
1571 # for that one we check also permissions
1567 # for that one we check also permissions
1572 # Additionally ENSURE if somehow draft is sent we're then unable to change status
1568 # Additionally ENSURE if somehow draft is sent we're then unable to change status
1573 allowed_to_change_status = PullRequestModel().check_user_change_status(
1569 allowed_to_change_status = PullRequestModel().check_user_change_status(
1574 pull_request, self._rhodecode_user) and not is_draft
1570 pull_request, self._rhodecode_user) and not is_draft
1575
1571
1576 if status and allowed_to_change_status:
1572 if status and allowed_to_change_status:
1577 message = (_('Status change %(transition_icon)s %(status)s')
1573 message = (_('Status change %(transition_icon)s %(status)s')
1578 % {'transition_icon': '>',
1574 % {'transition_icon': '>',
1579 'status': ChangesetStatus.get_status_lbl(status)})
1575 'status': ChangesetStatus.get_status_lbl(status)})
1580 text = text or message
1576 text = text or message
1581
1577
1582 comment = CommentsModel().create(
1578 comment = CommentsModel().create(
1583 text=text,
1579 text=text,
1584 repo=self.db_repo.repo_id,
1580 repo=self.db_repo.repo_id,
1585 user=self._rhodecode_user.user_id,
1581 user=self._rhodecode_user.user_id,
1586 pull_request=pull_request,
1582 pull_request=pull_request,
1587 f_path=f_path,
1583 f_path=f_path,
1588 line_no=line_no,
1584 line_no=line_no,
1589 status_change=(ChangesetStatus.get_status_lbl(status)
1585 status_change=(ChangesetStatus.get_status_lbl(status)
1590 if status and allowed_to_change_status else None),
1586 if status and allowed_to_change_status else None),
1591 status_change_type=(status
1587 status_change_type=(status
1592 if status and allowed_to_change_status else None),
1588 if status and allowed_to_change_status else None),
1593 comment_type=comment_type,
1589 comment_type=comment_type,
1594 is_draft=is_draft,
1590 is_draft=is_draft,
1595 resolves_comment_id=resolves_comment_id,
1591 resolves_comment_id=resolves_comment_id,
1596 auth_user=self._rhodecode_user,
1592 auth_user=self._rhodecode_user,
1597 send_email=not is_draft, # skip notification for draft comments
1593 send_email=not is_draft, # skip notification for draft comments
1598 )
1594 )
1599 is_inline = comment.is_inline
1595 is_inline = comment.is_inline
1600
1596
1601 if allowed_to_change_status:
1597 if allowed_to_change_status:
1602 # calculate old status before we change it
1598 # calculate old status before we change it
1603 old_calculated_status = pull_request.calculated_review_status()
1599 old_calculated_status = pull_request.calculated_review_status()
1604
1600
1605 # get status if set !
1601 # get status if set !
1606 if status:
1602 if status:
1607 ChangesetStatusModel().set_status(
1603 ChangesetStatusModel().set_status(
1608 self.db_repo.repo_id,
1604 self.db_repo.repo_id,
1609 status,
1605 status,
1610 self._rhodecode_user.user_id,
1606 self._rhodecode_user.user_id,
1611 comment,
1607 comment,
1612 pull_request=pull_request
1608 pull_request=pull_request
1613 )
1609 )
1614
1610
1615 Session().flush()
1611 Session().flush()
1616 # this is somehow required to get access to some relationship
1612 # this is somehow required to get access to some relationship
1617 # loaded on comment
1613 # loaded on comment
1618 Session().refresh(comment)
1614 Session().refresh(comment)
1619
1615
1620 # skip notifications for drafts
1616 # skip notifications for drafts
1621 if not is_draft:
1617 if not is_draft:
1622 PullRequestModel().trigger_pull_request_hook(
1618 PullRequestModel().trigger_pull_request_hook(
1623 pull_request, self._rhodecode_user, 'comment',
1619 pull_request, self._rhodecode_user, 'comment',
1624 data={'comment': comment})
1620 data={'comment': comment})
1625
1621
1626 # we now calculate the status of pull request, and based on that
1622 # we now calculate the status of pull request, and based on that
1627 # calculation we set the commits status
1623 # calculation we set the commits status
1628 calculated_status = pull_request.calculated_review_status()
1624 calculated_status = pull_request.calculated_review_status()
1629 if old_calculated_status != calculated_status:
1625 if old_calculated_status != calculated_status:
1630 PullRequestModel().trigger_pull_request_hook(
1626 PullRequestModel().trigger_pull_request_hook(
1631 pull_request, self._rhodecode_user, 'review_status_change',
1627 pull_request, self._rhodecode_user, 'review_status_change',
1632 data={'status': calculated_status})
1628 data={'status': calculated_status})
1633
1629
1634 comment_id = comment.comment_id
1630 comment_id = comment.comment_id
1635 data[comment_id] = {
1631 data[comment_id] = {
1636 'target_id': target_elem_id
1632 'target_id': target_elem_id
1637 }
1633 }
1638 Session().flush()
1634 Session().flush()
1639
1635
1640 c.co = comment
1636 c.co = comment
1641 c.at_version_num = None
1637 c.at_version_num = None
1642 c.is_new = True
1638 c.is_new = True
1643 rendered_comment = render(
1639 rendered_comment = render(
1644 'rhodecode:templates/changeset/changeset_comment_block.mako',
1640 'rhodecode:templates/changeset/changeset_comment_block.mako',
1645 self._get_template_context(c), self.request)
1641 self._get_template_context(c), self.request)
1646
1642
1647 data[comment_id].update(comment.get_dict())
1643 data[comment_id].update(comment.get_dict())
1648 data[comment_id].update({'rendered_text': rendered_comment})
1644 data[comment_id].update({'rendered_text': rendered_comment})
1649
1645
1650 Session().commit()
1646 Session().commit()
1651
1647
1652 # skip channelstream for draft comments
1648 # skip channelstream for draft comments
1653 if not all_drafts:
1649 if not all_drafts:
1654 comment_broadcast_channel = channelstream.comment_channel(
1650 comment_broadcast_channel = channelstream.comment_channel(
1655 self.db_repo_name, pull_request_obj=pull_request)
1651 self.db_repo_name, pull_request_obj=pull_request)
1656
1652
1657 comment_data = data
1653 comment_data = data
1658 posted_comment_type = 'inline' if is_inline else 'general'
1654 posted_comment_type = 'inline' if is_inline else 'general'
1659 if len(data) == 1:
1655 if len(data) == 1:
1660 msg = _('posted {} new {} comment').format(len(data), posted_comment_type)
1656 msg = _('posted {} new {} comment').format(len(data), posted_comment_type)
1661 else:
1657 else:
1662 msg = _('posted {} new {} comments').format(len(data), posted_comment_type)
1658 msg = _('posted {} new {} comments').format(len(data), posted_comment_type)
1663
1659
1664 channelstream.comment_channelstream_push(
1660 channelstream.comment_channelstream_push(
1665 self.request, comment_broadcast_channel, self._rhodecode_user, msg,
1661 self.request, comment_broadcast_channel, self._rhodecode_user, msg,
1666 comment_data=comment_data)
1662 comment_data=comment_data)
1667
1663
1668 return data
1664 return data
1669
1665
1670 @LoginRequired()
1666 @LoginRequired()
1671 @NotAnonymous()
1667 @NotAnonymous()
1672 @HasRepoPermissionAnyDecorator(
1668 @HasRepoPermissionAnyDecorator(
1673 'repository.read', 'repository.write', 'repository.admin')
1669 'repository.read', 'repository.write', 'repository.admin')
1674 @CSRFRequired()
1670 @CSRFRequired()
1675 @view_config(
1671 @view_config(
1676 route_name='pullrequest_comment_create', request_method='POST',
1672 route_name='pullrequest_comment_create', request_method='POST',
1677 renderer='json_ext')
1673 renderer='json_ext')
1678 def pull_request_comment_create(self):
1674 def pull_request_comment_create(self):
1679 _ = self.request.translate
1675 _ = self.request.translate
1680
1676
1681 pull_request = PullRequest.get_or_404(self.request.matchdict['pull_request_id'])
1677 pull_request = PullRequest.get_or_404(self.request.matchdict['pull_request_id'])
1682
1678
1683 if pull_request.is_closed():
1679 if pull_request.is_closed():
1684 log.debug('comment: forbidden because pull request is closed')
1680 log.debug('comment: forbidden because pull request is closed')
1685 raise HTTPForbidden()
1681 raise HTTPForbidden()
1686
1682
1687 allowed_to_comment = PullRequestModel().check_user_comment(
1683 allowed_to_comment = PullRequestModel().check_user_comment(
1688 pull_request, self._rhodecode_user)
1684 pull_request, self._rhodecode_user)
1689 if not allowed_to_comment:
1685 if not allowed_to_comment:
1690 log.debug('comment: forbidden because pull request is from forbidden repo')
1686 log.debug('comment: forbidden because pull request is from forbidden repo')
1691 raise HTTPForbidden()
1687 raise HTTPForbidden()
1692
1688
1693 comment_data = {
1689 comment_data = {
1694 'comment_type': self.request.POST.get('comment_type'),
1690 'comment_type': self.request.POST.get('comment_type'),
1695 'text': self.request.POST.get('text'),
1691 'text': self.request.POST.get('text'),
1696 'status': self.request.POST.get('changeset_status', None),
1692 'status': self.request.POST.get('changeset_status', None),
1697 'is_draft': self.request.POST.get('draft'),
1693 'is_draft': self.request.POST.get('draft'),
1698 'resolves_comment_id': self.request.POST.get('resolves_comment_id', None),
1694 'resolves_comment_id': self.request.POST.get('resolves_comment_id', None),
1699 'close_pull_request': self.request.POST.get('close_pull_request'),
1695 'close_pull_request': self.request.POST.get('close_pull_request'),
1700 'f_path': self.request.POST.get('f_path'),
1696 'f_path': self.request.POST.get('f_path'),
1701 'line': self.request.POST.get('line'),
1697 'line': self.request.POST.get('line'),
1702 }
1698 }
1703 data = self._pull_request_comments_create(pull_request, [comment_data])
1699 data = self._pull_request_comments_create(pull_request, [comment_data])
1704
1700
1705 return data
1701 return data
1706
1702
1707 @LoginRequired()
1703 @LoginRequired()
1708 @NotAnonymous()
1704 @NotAnonymous()
1709 @HasRepoPermissionAnyDecorator(
1705 @HasRepoPermissionAnyDecorator(
1710 'repository.read', 'repository.write', 'repository.admin')
1706 'repository.read', 'repository.write', 'repository.admin')
1711 @CSRFRequired()
1707 @CSRFRequired()
1712 @view_config(
1708 @view_config(
1713 route_name='pullrequest_comment_delete', request_method='POST',
1709 route_name='pullrequest_comment_delete', request_method='POST',
1714 renderer='json_ext')
1710 renderer='json_ext')
1715 def pull_request_comment_delete(self):
1711 def pull_request_comment_delete(self):
1716 pull_request = PullRequest.get_or_404(
1712 pull_request = PullRequest.get_or_404(
1717 self.request.matchdict['pull_request_id'])
1713 self.request.matchdict['pull_request_id'])
1718
1714
1719 comment = ChangesetComment.get_or_404(
1715 comment = ChangesetComment.get_or_404(
1720 self.request.matchdict['comment_id'])
1716 self.request.matchdict['comment_id'])
1721 comment_id = comment.comment_id
1717 comment_id = comment.comment_id
1722
1718
1723 if comment.immutable:
1719 if comment.immutable:
1724 # don't allow deleting comments that are immutable
1720 # don't allow deleting comments that are immutable
1725 raise HTTPForbidden()
1721 raise HTTPForbidden()
1726
1722
1727 if pull_request.is_closed():
1723 if pull_request.is_closed():
1728 log.debug('comment: forbidden because pull request is closed')
1724 log.debug('comment: forbidden because pull request is closed')
1729 raise HTTPForbidden()
1725 raise HTTPForbidden()
1730
1726
1731 if not comment:
1727 if not comment:
1732 log.debug('Comment with id:%s not found, skipping', comment_id)
1728 log.debug('Comment with id:%s not found, skipping', comment_id)
1733 # comment already deleted in another call probably
1729 # comment already deleted in another call probably
1734 return True
1730 return True
1735
1731
1736 if comment.pull_request.is_closed():
1732 if comment.pull_request.is_closed():
1737 # don't allow deleting comments on closed pull request
1733 # don't allow deleting comments on closed pull request
1738 raise HTTPForbidden()
1734 raise HTTPForbidden()
1739
1735
1740 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1736 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1741 super_admin = h.HasPermissionAny('hg.admin')()
1737 super_admin = h.HasPermissionAny('hg.admin')()
1742 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1738 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1743 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1739 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1744 comment_repo_admin = is_repo_admin and is_repo_comment
1740 comment_repo_admin = is_repo_admin and is_repo_comment
1745
1741
1746 if super_admin or comment_owner or comment_repo_admin:
1742 if super_admin or comment_owner or comment_repo_admin:
1747 old_calculated_status = comment.pull_request.calculated_review_status()
1743 old_calculated_status = comment.pull_request.calculated_review_status()
1748 CommentsModel().delete(comment=comment, auth_user=self._rhodecode_user)
1744 CommentsModel().delete(comment=comment, auth_user=self._rhodecode_user)
1749 Session().commit()
1745 Session().commit()
1750 calculated_status = comment.pull_request.calculated_review_status()
1746 calculated_status = comment.pull_request.calculated_review_status()
1751 if old_calculated_status != calculated_status:
1747 if old_calculated_status != calculated_status:
1752 PullRequestModel().trigger_pull_request_hook(
1748 PullRequestModel().trigger_pull_request_hook(
1753 comment.pull_request, self._rhodecode_user, 'review_status_change',
1749 comment.pull_request, self._rhodecode_user, 'review_status_change',
1754 data={'status': calculated_status})
1750 data={'status': calculated_status})
1755 return True
1751 return True
1756 else:
1752 else:
1757 log.warning('No permissions for user %s to delete comment_id: %s',
1753 log.warning('No permissions for user %s to delete comment_id: %s',
1758 self._rhodecode_db_user, comment_id)
1754 self._rhodecode_db_user, comment_id)
1759 raise HTTPNotFound()
1755 raise HTTPNotFound()
1760
1756
1761 @LoginRequired()
1757 @LoginRequired()
1762 @NotAnonymous()
1758 @NotAnonymous()
1763 @HasRepoPermissionAnyDecorator(
1759 @HasRepoPermissionAnyDecorator(
1764 'repository.read', 'repository.write', 'repository.admin')
1760 'repository.read', 'repository.write', 'repository.admin')
1765 @CSRFRequired()
1761 @CSRFRequired()
1766 @view_config(
1762 @view_config(
1767 route_name='pullrequest_comment_edit', request_method='POST',
1763 route_name='pullrequest_comment_edit', request_method='POST',
1768 renderer='json_ext')
1764 renderer='json_ext')
1769 def pull_request_comment_edit(self):
1765 def pull_request_comment_edit(self):
1770 self.load_default_context()
1766 self.load_default_context()
1771
1767
1772 pull_request = PullRequest.get_or_404(
1768 pull_request = PullRequest.get_or_404(
1773 self.request.matchdict['pull_request_id']
1769 self.request.matchdict['pull_request_id']
1774 )
1770 )
1775 comment = ChangesetComment.get_or_404(
1771 comment = ChangesetComment.get_or_404(
1776 self.request.matchdict['comment_id']
1772 self.request.matchdict['comment_id']
1777 )
1773 )
1778 comment_id = comment.comment_id
1774 comment_id = comment.comment_id
1779
1775
1780 if comment.immutable:
1776 if comment.immutable:
1781 # don't allow deleting comments that are immutable
1777 # don't allow deleting comments that are immutable
1782 raise HTTPForbidden()
1778 raise HTTPForbidden()
1783
1779
1784 if pull_request.is_closed():
1780 if pull_request.is_closed():
1785 log.debug('comment: forbidden because pull request is closed')
1781 log.debug('comment: forbidden because pull request is closed')
1786 raise HTTPForbidden()
1782 raise HTTPForbidden()
1787
1783
1788 if comment.pull_request.is_closed():
1784 if comment.pull_request.is_closed():
1789 # don't allow deleting comments on closed pull request
1785 # don't allow deleting comments on closed pull request
1790 raise HTTPForbidden()
1786 raise HTTPForbidden()
1791
1787
1792 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1788 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1793 super_admin = h.HasPermissionAny('hg.admin')()
1789 super_admin = h.HasPermissionAny('hg.admin')()
1794 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1790 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1795 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1791 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1796 comment_repo_admin = is_repo_admin and is_repo_comment
1792 comment_repo_admin = is_repo_admin and is_repo_comment
1797
1793
1798 if super_admin or comment_owner or comment_repo_admin:
1794 if super_admin or comment_owner or comment_repo_admin:
1799 text = self.request.POST.get('text')
1795 text = self.request.POST.get('text')
1800 version = self.request.POST.get('version')
1796 version = self.request.POST.get('version')
1801 if text == comment.text:
1797 if text == comment.text:
1802 log.warning(
1798 log.warning(
1803 'Comment(PR): '
1799 'Comment(PR): '
1804 'Trying to create new version '
1800 'Trying to create new version '
1805 'with the same comment body {}'.format(
1801 'with the same comment body {}'.format(
1806 comment_id,
1802 comment_id,
1807 )
1803 )
1808 )
1804 )
1809 raise HTTPNotFound()
1805 raise HTTPNotFound()
1810
1806
1811 if version.isdigit():
1807 if version.isdigit():
1812 version = int(version)
1808 version = int(version)
1813 else:
1809 else:
1814 log.warning(
1810 log.warning(
1815 'Comment(PR): Wrong version type {} {} '
1811 'Comment(PR): Wrong version type {} {} '
1816 'for comment {}'.format(
1812 'for comment {}'.format(
1817 version,
1813 version,
1818 type(version),
1814 type(version),
1819 comment_id,
1815 comment_id,
1820 )
1816 )
1821 )
1817 )
1822 raise HTTPNotFound()
1818 raise HTTPNotFound()
1823
1819
1824 try:
1820 try:
1825 comment_history = CommentsModel().edit(
1821 comment_history = CommentsModel().edit(
1826 comment_id=comment_id,
1822 comment_id=comment_id,
1827 text=text,
1823 text=text,
1828 auth_user=self._rhodecode_user,
1824 auth_user=self._rhodecode_user,
1829 version=version,
1825 version=version,
1830 )
1826 )
1831 except CommentVersionMismatch:
1827 except CommentVersionMismatch:
1832 raise HTTPConflict()
1828 raise HTTPConflict()
1833
1829
1834 if not comment_history:
1830 if not comment_history:
1835 raise HTTPNotFound()
1831 raise HTTPNotFound()
1836
1832
1837 Session().commit()
1833 Session().commit()
1838 if not comment.draft:
1834 if not comment.draft:
1839 PullRequestModel().trigger_pull_request_hook(
1835 PullRequestModel().trigger_pull_request_hook(
1840 pull_request, self._rhodecode_user, 'comment_edit',
1836 pull_request, self._rhodecode_user, 'comment_edit',
1841 data={'comment': comment})
1837 data={'comment': comment})
1842
1838
1843 return {
1839 return {
1844 'comment_history_id': comment_history.comment_history_id,
1840 'comment_history_id': comment_history.comment_history_id,
1845 'comment_id': comment.comment_id,
1841 'comment_id': comment.comment_id,
1846 'comment_version': comment_history.version,
1842 'comment_version': comment_history.version,
1847 'comment_author_username': comment_history.author.username,
1843 'comment_author_username': comment_history.author.username,
1848 'comment_author_gravatar': h.gravatar_url(comment_history.author.email, 16),
1844 'comment_author_gravatar': h.gravatar_url(comment_history.author.email, 16),
1849 'comment_created_on': h.age_component(comment_history.created_on,
1845 'comment_created_on': h.age_component(comment_history.created_on,
1850 time_is_local=True),
1846 time_is_local=True),
1851 }
1847 }
1852 else:
1848 else:
1853 log.warning('No permissions for user %s to edit comment_id: %s',
1849 log.warning('No permissions for user %s to edit comment_id: %s',
1854 self._rhodecode_db_user, comment_id)
1850 self._rhodecode_db_user, comment_id)
1855 raise HTTPNotFound()
1851 raise HTTPNotFound()
@@ -1,5784 +1,5806 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2020 RhodeCode GmbH
3 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 """
21 """
22 Database Models for RhodeCode Enterprise
22 Database Models for RhodeCode Enterprise
23 """
23 """
24
24
25 import re
25 import re
26 import os
26 import os
27 import time
27 import time
28 import string
28 import string
29 import hashlib
29 import hashlib
30 import logging
30 import logging
31 import datetime
31 import datetime
32 import uuid
32 import uuid
33 import warnings
33 import warnings
34 import ipaddress
34 import ipaddress
35 import functools
35 import functools
36 import traceback
36 import traceback
37 import collections
37 import collections
38
38
39 from sqlalchemy import (
39 from sqlalchemy import (
40 or_, and_, not_, func, cast, TypeDecorator, event,
40 or_, and_, not_, func, cast, TypeDecorator, event,
41 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
41 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
42 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
42 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
43 Text, Float, PickleType, BigInteger)
43 Text, Float, PickleType, BigInteger)
44 from sqlalchemy.sql.expression import true, false, case
44 from sqlalchemy.sql.expression import true, false, case
45 from sqlalchemy.sql.functions import coalesce, count # pragma: no cover
45 from sqlalchemy.sql.functions import coalesce, count # pragma: no cover
46 from sqlalchemy.orm import (
46 from sqlalchemy.orm import (
47 relationship, joinedload, class_mapper, validates, aliased)
47 relationship, joinedload, class_mapper, validates, aliased)
48 from sqlalchemy.ext.declarative import declared_attr
48 from sqlalchemy.ext.declarative import declared_attr
49 from sqlalchemy.ext.hybrid import hybrid_property
49 from sqlalchemy.ext.hybrid import hybrid_property
50 from sqlalchemy.exc import IntegrityError # pragma: no cover
50 from sqlalchemy.exc import IntegrityError # pragma: no cover
51 from sqlalchemy.dialects.mysql import LONGTEXT
51 from sqlalchemy.dialects.mysql import LONGTEXT
52 from zope.cachedescriptors.property import Lazy as LazyProperty
52 from zope.cachedescriptors.property import Lazy as LazyProperty
53 from pyramid import compat
53 from pyramid import compat
54 from pyramid.threadlocal import get_current_request
54 from pyramid.threadlocal import get_current_request
55 from webhelpers2.text import remove_formatting
55 from webhelpers2.text import remove_formatting
56
56
57 from rhodecode.translation import _
57 from rhodecode.translation import _
58 from rhodecode.lib.vcs import get_vcs_instance, VCSError
58 from rhodecode.lib.vcs import get_vcs_instance, VCSError
59 from rhodecode.lib.vcs.backends.base import (
59 from rhodecode.lib.vcs.backends.base import (
60 EmptyCommit, Reference, unicode_to_reference, reference_to_unicode)
60 EmptyCommit, Reference, unicode_to_reference, reference_to_unicode)
61 from rhodecode.lib.utils2 import (
61 from rhodecode.lib.utils2 import (
62 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
62 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
63 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
63 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
64 glob2re, StrictAttributeDict, cleaned_uri, datetime_to_time, OrderedDefaultDict)
64 glob2re, StrictAttributeDict, cleaned_uri, datetime_to_time, OrderedDefaultDict)
65 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
65 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
66 JsonRaw
66 JsonRaw
67 from rhodecode.lib.ext_json import json
67 from rhodecode.lib.ext_json import json
68 from rhodecode.lib.caching_query import FromCache
68 from rhodecode.lib.caching_query import FromCache
69 from rhodecode.lib.encrypt import AESCipher, validate_and_get_enc_data
69 from rhodecode.lib.encrypt import AESCipher, validate_and_get_enc_data
70 from rhodecode.lib.encrypt2 import Encryptor
70 from rhodecode.lib.encrypt2 import Encryptor
71 from rhodecode.lib.exceptions import (
71 from rhodecode.lib.exceptions import (
72 ArtifactMetadataDuplicate, ArtifactMetadataBadValueType)
72 ArtifactMetadataDuplicate, ArtifactMetadataBadValueType)
73 from rhodecode.model.meta import Base, Session
73 from rhodecode.model.meta import Base, Session
74
74
75 URL_SEP = '/'
75 URL_SEP = '/'
76 log = logging.getLogger(__name__)
76 log = logging.getLogger(__name__)
77
77
78 # =============================================================================
78 # =============================================================================
79 # BASE CLASSES
79 # BASE CLASSES
80 # =============================================================================
80 # =============================================================================
81
81
82 # this is propagated from .ini file rhodecode.encrypted_values.secret or
82 # this is propagated from .ini file rhodecode.encrypted_values.secret or
83 # beaker.session.secret if first is not set.
83 # beaker.session.secret if first is not set.
84 # and initialized at environment.py
84 # and initialized at environment.py
85 ENCRYPTION_KEY = None
85 ENCRYPTION_KEY = None
86
86
87 # used to sort permissions by types, '#' used here is not allowed to be in
87 # used to sort permissions by types, '#' used here is not allowed to be in
88 # usernames, and it's very early in sorted string.printable table.
88 # usernames, and it's very early in sorted string.printable table.
89 PERMISSION_TYPE_SORT = {
89 PERMISSION_TYPE_SORT = {
90 'admin': '####',
90 'admin': '####',
91 'write': '###',
91 'write': '###',
92 'read': '##',
92 'read': '##',
93 'none': '#',
93 'none': '#',
94 }
94 }
95
95
96
96
97 def display_user_sort(obj):
97 def display_user_sort(obj):
98 """
98 """
99 Sort function used to sort permissions in .permissions() function of
99 Sort function used to sort permissions in .permissions() function of
100 Repository, RepoGroup, UserGroup. Also it put the default user in front
100 Repository, RepoGroup, UserGroup. Also it put the default user in front
101 of all other resources
101 of all other resources
102 """
102 """
103
103
104 if obj.username == User.DEFAULT_USER:
104 if obj.username == User.DEFAULT_USER:
105 return '#####'
105 return '#####'
106 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
106 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
107 extra_sort_num = '1' # default
107 extra_sort_num = '1' # default
108
108
109 # NOTE(dan): inactive duplicates goes last
109 # NOTE(dan): inactive duplicates goes last
110 if getattr(obj, 'duplicate_perm', None):
110 if getattr(obj, 'duplicate_perm', None):
111 extra_sort_num = '9'
111 extra_sort_num = '9'
112 return prefix + extra_sort_num + obj.username
112 return prefix + extra_sort_num + obj.username
113
113
114
114
115 def display_user_group_sort(obj):
115 def display_user_group_sort(obj):
116 """
116 """
117 Sort function used to sort permissions in .permissions() function of
117 Sort function used to sort permissions in .permissions() function of
118 Repository, RepoGroup, UserGroup. Also it put the default user in front
118 Repository, RepoGroup, UserGroup. Also it put the default user in front
119 of all other resources
119 of all other resources
120 """
120 """
121
121
122 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
122 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
123 return prefix + obj.users_group_name
123 return prefix + obj.users_group_name
124
124
125
125
126 def _hash_key(k):
126 def _hash_key(k):
127 return sha1_safe(k)
127 return sha1_safe(k)
128
128
129
129
130 def in_filter_generator(qry, items, limit=500):
130 def in_filter_generator(qry, items, limit=500):
131 """
131 """
132 Splits IN() into multiple with OR
132 Splits IN() into multiple with OR
133 e.g.::
133 e.g.::
134 cnt = Repository.query().filter(
134 cnt = Repository.query().filter(
135 or_(
135 or_(
136 *in_filter_generator(Repository.repo_id, range(100000))
136 *in_filter_generator(Repository.repo_id, range(100000))
137 )).count()
137 )).count()
138 """
138 """
139 if not items:
139 if not items:
140 # empty list will cause empty query which might cause security issues
140 # empty list will cause empty query which might cause security issues
141 # this can lead to hidden unpleasant results
141 # this can lead to hidden unpleasant results
142 items = [-1]
142 items = [-1]
143
143
144 parts = []
144 parts = []
145 for chunk in xrange(0, len(items), limit):
145 for chunk in xrange(0, len(items), limit):
146 parts.append(
146 parts.append(
147 qry.in_(items[chunk: chunk + limit])
147 qry.in_(items[chunk: chunk + limit])
148 )
148 )
149
149
150 return parts
150 return parts
151
151
152
152
153 base_table_args = {
153 base_table_args = {
154 'extend_existing': True,
154 'extend_existing': True,
155 'mysql_engine': 'InnoDB',
155 'mysql_engine': 'InnoDB',
156 'mysql_charset': 'utf8',
156 'mysql_charset': 'utf8',
157 'sqlite_autoincrement': True
157 'sqlite_autoincrement': True
158 }
158 }
159
159
160
160
161 class EncryptedTextValue(TypeDecorator):
161 class EncryptedTextValue(TypeDecorator):
162 """
162 """
163 Special column for encrypted long text data, use like::
163 Special column for encrypted long text data, use like::
164
164
165 value = Column("encrypted_value", EncryptedValue(), nullable=False)
165 value = Column("encrypted_value", EncryptedValue(), nullable=False)
166
166
167 This column is intelligent so if value is in unencrypted form it return
167 This column is intelligent so if value is in unencrypted form it return
168 unencrypted form, but on save it always encrypts
168 unencrypted form, but on save it always encrypts
169 """
169 """
170 impl = Text
170 impl = Text
171
171
172 def process_bind_param(self, value, dialect):
172 def process_bind_param(self, value, dialect):
173 """
173 """
174 Setter for storing value
174 Setter for storing value
175 """
175 """
176 import rhodecode
176 import rhodecode
177 if not value:
177 if not value:
178 return value
178 return value
179
179
180 # protect against double encrypting if values is already encrypted
180 # protect against double encrypting if values is already encrypted
181 if value.startswith('enc$aes$') \
181 if value.startswith('enc$aes$') \
182 or value.startswith('enc$aes_hmac$') \
182 or value.startswith('enc$aes_hmac$') \
183 or value.startswith('enc2$'):
183 or value.startswith('enc2$'):
184 raise ValueError('value needs to be in unencrypted format, '
184 raise ValueError('value needs to be in unencrypted format, '
185 'ie. not starting with enc$ or enc2$')
185 'ie. not starting with enc$ or enc2$')
186
186
187 algo = rhodecode.CONFIG.get('rhodecode.encrypted_values.algorithm') or 'aes'
187 algo = rhodecode.CONFIG.get('rhodecode.encrypted_values.algorithm') or 'aes'
188 if algo == 'aes':
188 if algo == 'aes':
189 return 'enc$aes_hmac$%s' % AESCipher(ENCRYPTION_KEY, hmac=True).encrypt(value)
189 return 'enc$aes_hmac$%s' % AESCipher(ENCRYPTION_KEY, hmac=True).encrypt(value)
190 elif algo == 'fernet':
190 elif algo == 'fernet':
191 return Encryptor(ENCRYPTION_KEY).encrypt(value)
191 return Encryptor(ENCRYPTION_KEY).encrypt(value)
192 else:
192 else:
193 ValueError('Bad encryption algorithm, should be fernet or aes, got: {}'.format(algo))
193 ValueError('Bad encryption algorithm, should be fernet or aes, got: {}'.format(algo))
194
194
195 def process_result_value(self, value, dialect):
195 def process_result_value(self, value, dialect):
196 """
196 """
197 Getter for retrieving value
197 Getter for retrieving value
198 """
198 """
199
199
200 import rhodecode
200 import rhodecode
201 if not value:
201 if not value:
202 return value
202 return value
203
203
204 algo = rhodecode.CONFIG.get('rhodecode.encrypted_values.algorithm') or 'aes'
204 algo = rhodecode.CONFIG.get('rhodecode.encrypted_values.algorithm') or 'aes'
205 enc_strict_mode = str2bool(rhodecode.CONFIG.get('rhodecode.encrypted_values.strict') or True)
205 enc_strict_mode = str2bool(rhodecode.CONFIG.get('rhodecode.encrypted_values.strict') or True)
206 if algo == 'aes':
206 if algo == 'aes':
207 decrypted_data = validate_and_get_enc_data(value, ENCRYPTION_KEY, enc_strict_mode)
207 decrypted_data = validate_and_get_enc_data(value, ENCRYPTION_KEY, enc_strict_mode)
208 elif algo == 'fernet':
208 elif algo == 'fernet':
209 return Encryptor(ENCRYPTION_KEY).decrypt(value)
209 return Encryptor(ENCRYPTION_KEY).decrypt(value)
210 else:
210 else:
211 ValueError('Bad encryption algorithm, should be fernet or aes, got: {}'.format(algo))
211 ValueError('Bad encryption algorithm, should be fernet or aes, got: {}'.format(algo))
212 return decrypted_data
212 return decrypted_data
213
213
214
214
215 class BaseModel(object):
215 class BaseModel(object):
216 """
216 """
217 Base Model for all classes
217 Base Model for all classes
218 """
218 """
219
219
220 @classmethod
220 @classmethod
221 def _get_keys(cls):
221 def _get_keys(cls):
222 """return column names for this model """
222 """return column names for this model """
223 return class_mapper(cls).c.keys()
223 return class_mapper(cls).c.keys()
224
224
225 def get_dict(self):
225 def get_dict(self):
226 """
226 """
227 return dict with keys and values corresponding
227 return dict with keys and values corresponding
228 to this model data """
228 to this model data """
229
229
230 d = {}
230 d = {}
231 for k in self._get_keys():
231 for k in self._get_keys():
232 d[k] = getattr(self, k)
232 d[k] = getattr(self, k)
233
233
234 # also use __json__() if present to get additional fields
234 # also use __json__() if present to get additional fields
235 _json_attr = getattr(self, '__json__', None)
235 _json_attr = getattr(self, '__json__', None)
236 if _json_attr:
236 if _json_attr:
237 # update with attributes from __json__
237 # update with attributes from __json__
238 if callable(_json_attr):
238 if callable(_json_attr):
239 _json_attr = _json_attr()
239 _json_attr = _json_attr()
240 for k, val in _json_attr.iteritems():
240 for k, val in _json_attr.iteritems():
241 d[k] = val
241 d[k] = val
242 return d
242 return d
243
243
244 def get_appstruct(self):
244 def get_appstruct(self):
245 """return list with keys and values tuples corresponding
245 """return list with keys and values tuples corresponding
246 to this model data """
246 to this model data """
247
247
248 lst = []
248 lst = []
249 for k in self._get_keys():
249 for k in self._get_keys():
250 lst.append((k, getattr(self, k),))
250 lst.append((k, getattr(self, k),))
251 return lst
251 return lst
252
252
253 def populate_obj(self, populate_dict):
253 def populate_obj(self, populate_dict):
254 """populate model with data from given populate_dict"""
254 """populate model with data from given populate_dict"""
255
255
256 for k in self._get_keys():
256 for k in self._get_keys():
257 if k in populate_dict:
257 if k in populate_dict:
258 setattr(self, k, populate_dict[k])
258 setattr(self, k, populate_dict[k])
259
259
260 @classmethod
260 @classmethod
261 def query(cls):
261 def query(cls):
262 return Session().query(cls)
262 return Session().query(cls)
263
263
264 @classmethod
264 @classmethod
265 def get(cls, id_):
265 def get(cls, id_):
266 if id_:
266 if id_:
267 return cls.query().get(id_)
267 return cls.query().get(id_)
268
268
269 @classmethod
269 @classmethod
270 def get_or_404(cls, id_):
270 def get_or_404(cls, id_):
271 from pyramid.httpexceptions import HTTPNotFound
271 from pyramid.httpexceptions import HTTPNotFound
272
272
273 try:
273 try:
274 id_ = int(id_)
274 id_ = int(id_)
275 except (TypeError, ValueError):
275 except (TypeError, ValueError):
276 raise HTTPNotFound()
276 raise HTTPNotFound()
277
277
278 res = cls.query().get(id_)
278 res = cls.query().get(id_)
279 if not res:
279 if not res:
280 raise HTTPNotFound()
280 raise HTTPNotFound()
281 return res
281 return res
282
282
283 @classmethod
283 @classmethod
284 def getAll(cls):
284 def getAll(cls):
285 # deprecated and left for backward compatibility
285 # deprecated and left for backward compatibility
286 return cls.get_all()
286 return cls.get_all()
287
287
288 @classmethod
288 @classmethod
289 def get_all(cls):
289 def get_all(cls):
290 return cls.query().all()
290 return cls.query().all()
291
291
292 @classmethod
292 @classmethod
293 def delete(cls, id_):
293 def delete(cls, id_):
294 obj = cls.query().get(id_)
294 obj = cls.query().get(id_)
295 Session().delete(obj)
295 Session().delete(obj)
296
296
297 @classmethod
297 @classmethod
298 def identity_cache(cls, session, attr_name, value):
298 def identity_cache(cls, session, attr_name, value):
299 exist_in_session = []
299 exist_in_session = []
300 for (item_cls, pkey), instance in session.identity_map.items():
300 for (item_cls, pkey), instance in session.identity_map.items():
301 if cls == item_cls and getattr(instance, attr_name) == value:
301 if cls == item_cls and getattr(instance, attr_name) == value:
302 exist_in_session.append(instance)
302 exist_in_session.append(instance)
303 if exist_in_session:
303 if exist_in_session:
304 if len(exist_in_session) == 1:
304 if len(exist_in_session) == 1:
305 return exist_in_session[0]
305 return exist_in_session[0]
306 log.exception(
306 log.exception(
307 'multiple objects with attr %s and '
307 'multiple objects with attr %s and '
308 'value %s found with same name: %r',
308 'value %s found with same name: %r',
309 attr_name, value, exist_in_session)
309 attr_name, value, exist_in_session)
310
310
311 def __repr__(self):
311 def __repr__(self):
312 if hasattr(self, '__unicode__'):
312 if hasattr(self, '__unicode__'):
313 # python repr needs to return str
313 # python repr needs to return str
314 try:
314 try:
315 return safe_str(self.__unicode__())
315 return safe_str(self.__unicode__())
316 except UnicodeDecodeError:
316 except UnicodeDecodeError:
317 pass
317 pass
318 return '<DB:%s>' % (self.__class__.__name__)
318 return '<DB:%s>' % (self.__class__.__name__)
319
319
320
320
321 class RhodeCodeSetting(Base, BaseModel):
321 class RhodeCodeSetting(Base, BaseModel):
322 __tablename__ = 'rhodecode_settings'
322 __tablename__ = 'rhodecode_settings'
323 __table_args__ = (
323 __table_args__ = (
324 UniqueConstraint('app_settings_name'),
324 UniqueConstraint('app_settings_name'),
325 base_table_args
325 base_table_args
326 )
326 )
327
327
328 SETTINGS_TYPES = {
328 SETTINGS_TYPES = {
329 'str': safe_str,
329 'str': safe_str,
330 'int': safe_int,
330 'int': safe_int,
331 'unicode': safe_unicode,
331 'unicode': safe_unicode,
332 'bool': str2bool,
332 'bool': str2bool,
333 'list': functools.partial(aslist, sep=',')
333 'list': functools.partial(aslist, sep=',')
334 }
334 }
335 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
335 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
336 GLOBAL_CONF_KEY = 'app_settings'
336 GLOBAL_CONF_KEY = 'app_settings'
337
337
338 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
338 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
339 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
339 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
340 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
340 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
341 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
341 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
342
342
343 def __init__(self, key='', val='', type='unicode'):
343 def __init__(self, key='', val='', type='unicode'):
344 self.app_settings_name = key
344 self.app_settings_name = key
345 self.app_settings_type = type
345 self.app_settings_type = type
346 self.app_settings_value = val
346 self.app_settings_value = val
347
347
348 @validates('_app_settings_value')
348 @validates('_app_settings_value')
349 def validate_settings_value(self, key, val):
349 def validate_settings_value(self, key, val):
350 assert type(val) == unicode
350 assert type(val) == unicode
351 return val
351 return val
352
352
353 @hybrid_property
353 @hybrid_property
354 def app_settings_value(self):
354 def app_settings_value(self):
355 v = self._app_settings_value
355 v = self._app_settings_value
356 _type = self.app_settings_type
356 _type = self.app_settings_type
357 if _type:
357 if _type:
358 _type = self.app_settings_type.split('.')[0]
358 _type = self.app_settings_type.split('.')[0]
359 # decode the encrypted value
359 # decode the encrypted value
360 if 'encrypted' in self.app_settings_type:
360 if 'encrypted' in self.app_settings_type:
361 cipher = EncryptedTextValue()
361 cipher = EncryptedTextValue()
362 v = safe_unicode(cipher.process_result_value(v, None))
362 v = safe_unicode(cipher.process_result_value(v, None))
363
363
364 converter = self.SETTINGS_TYPES.get(_type) or \
364 converter = self.SETTINGS_TYPES.get(_type) or \
365 self.SETTINGS_TYPES['unicode']
365 self.SETTINGS_TYPES['unicode']
366 return converter(v)
366 return converter(v)
367
367
368 @app_settings_value.setter
368 @app_settings_value.setter
369 def app_settings_value(self, val):
369 def app_settings_value(self, val):
370 """
370 """
371 Setter that will always make sure we use unicode in app_settings_value
371 Setter that will always make sure we use unicode in app_settings_value
372
372
373 :param val:
373 :param val:
374 """
374 """
375 val = safe_unicode(val)
375 val = safe_unicode(val)
376 # encode the encrypted value
376 # encode the encrypted value
377 if 'encrypted' in self.app_settings_type:
377 if 'encrypted' in self.app_settings_type:
378 cipher = EncryptedTextValue()
378 cipher = EncryptedTextValue()
379 val = safe_unicode(cipher.process_bind_param(val, None))
379 val = safe_unicode(cipher.process_bind_param(val, None))
380 self._app_settings_value = val
380 self._app_settings_value = val
381
381
382 @hybrid_property
382 @hybrid_property
383 def app_settings_type(self):
383 def app_settings_type(self):
384 return self._app_settings_type
384 return self._app_settings_type
385
385
386 @app_settings_type.setter
386 @app_settings_type.setter
387 def app_settings_type(self, val):
387 def app_settings_type(self, val):
388 if val.split('.')[0] not in self.SETTINGS_TYPES:
388 if val.split('.')[0] not in self.SETTINGS_TYPES:
389 raise Exception('type must be one of %s got %s'
389 raise Exception('type must be one of %s got %s'
390 % (self.SETTINGS_TYPES.keys(), val))
390 % (self.SETTINGS_TYPES.keys(), val))
391 self._app_settings_type = val
391 self._app_settings_type = val
392
392
393 @classmethod
393 @classmethod
394 def get_by_prefix(cls, prefix):
394 def get_by_prefix(cls, prefix):
395 return RhodeCodeSetting.query()\
395 return RhodeCodeSetting.query()\
396 .filter(RhodeCodeSetting.app_settings_name.startswith(prefix))\
396 .filter(RhodeCodeSetting.app_settings_name.startswith(prefix))\
397 .all()
397 .all()
398
398
399 def __unicode__(self):
399 def __unicode__(self):
400 return u"<%s('%s:%s[%s]')>" % (
400 return u"<%s('%s:%s[%s]')>" % (
401 self.__class__.__name__,
401 self.__class__.__name__,
402 self.app_settings_name, self.app_settings_value,
402 self.app_settings_name, self.app_settings_value,
403 self.app_settings_type
403 self.app_settings_type
404 )
404 )
405
405
406
406
407 class RhodeCodeUi(Base, BaseModel):
407 class RhodeCodeUi(Base, BaseModel):
408 __tablename__ = 'rhodecode_ui'
408 __tablename__ = 'rhodecode_ui'
409 __table_args__ = (
409 __table_args__ = (
410 UniqueConstraint('ui_key'),
410 UniqueConstraint('ui_key'),
411 base_table_args
411 base_table_args
412 )
412 )
413
413
414 HOOK_REPO_SIZE = 'changegroup.repo_size'
414 HOOK_REPO_SIZE = 'changegroup.repo_size'
415 # HG
415 # HG
416 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
416 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
417 HOOK_PULL = 'outgoing.pull_logger'
417 HOOK_PULL = 'outgoing.pull_logger'
418 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
418 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
419 HOOK_PRETX_PUSH = 'pretxnchangegroup.pre_push'
419 HOOK_PRETX_PUSH = 'pretxnchangegroup.pre_push'
420 HOOK_PUSH = 'changegroup.push_logger'
420 HOOK_PUSH = 'changegroup.push_logger'
421 HOOK_PUSH_KEY = 'pushkey.key_push'
421 HOOK_PUSH_KEY = 'pushkey.key_push'
422
422
423 HOOKS_BUILTIN = [
423 HOOKS_BUILTIN = [
424 HOOK_PRE_PULL,
424 HOOK_PRE_PULL,
425 HOOK_PULL,
425 HOOK_PULL,
426 HOOK_PRE_PUSH,
426 HOOK_PRE_PUSH,
427 HOOK_PRETX_PUSH,
427 HOOK_PRETX_PUSH,
428 HOOK_PUSH,
428 HOOK_PUSH,
429 HOOK_PUSH_KEY,
429 HOOK_PUSH_KEY,
430 ]
430 ]
431
431
432 # TODO: johbo: Unify way how hooks are configured for git and hg,
432 # TODO: johbo: Unify way how hooks are configured for git and hg,
433 # git part is currently hardcoded.
433 # git part is currently hardcoded.
434
434
435 # SVN PATTERNS
435 # SVN PATTERNS
436 SVN_BRANCH_ID = 'vcs_svn_branch'
436 SVN_BRANCH_ID = 'vcs_svn_branch'
437 SVN_TAG_ID = 'vcs_svn_tag'
437 SVN_TAG_ID = 'vcs_svn_tag'
438
438
439 ui_id = Column(
439 ui_id = Column(
440 "ui_id", Integer(), nullable=False, unique=True, default=None,
440 "ui_id", Integer(), nullable=False, unique=True, default=None,
441 primary_key=True)
441 primary_key=True)
442 ui_section = Column(
442 ui_section = Column(
443 "ui_section", String(255), nullable=True, unique=None, default=None)
443 "ui_section", String(255), nullable=True, unique=None, default=None)
444 ui_key = Column(
444 ui_key = Column(
445 "ui_key", String(255), nullable=True, unique=None, default=None)
445 "ui_key", String(255), nullable=True, unique=None, default=None)
446 ui_value = Column(
446 ui_value = Column(
447 "ui_value", String(255), nullable=True, unique=None, default=None)
447 "ui_value", String(255), nullable=True, unique=None, default=None)
448 ui_active = Column(
448 ui_active = Column(
449 "ui_active", Boolean(), nullable=True, unique=None, default=True)
449 "ui_active", Boolean(), nullable=True, unique=None, default=True)
450
450
451 def __repr__(self):
451 def __repr__(self):
452 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
452 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
453 self.ui_key, self.ui_value)
453 self.ui_key, self.ui_value)
454
454
455
455
456 class RepoRhodeCodeSetting(Base, BaseModel):
456 class RepoRhodeCodeSetting(Base, BaseModel):
457 __tablename__ = 'repo_rhodecode_settings'
457 __tablename__ = 'repo_rhodecode_settings'
458 __table_args__ = (
458 __table_args__ = (
459 UniqueConstraint(
459 UniqueConstraint(
460 'app_settings_name', 'repository_id',
460 'app_settings_name', 'repository_id',
461 name='uq_repo_rhodecode_setting_name_repo_id'),
461 name='uq_repo_rhodecode_setting_name_repo_id'),
462 base_table_args
462 base_table_args
463 )
463 )
464
464
465 repository_id = Column(
465 repository_id = Column(
466 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
466 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
467 nullable=False)
467 nullable=False)
468 app_settings_id = Column(
468 app_settings_id = Column(
469 "app_settings_id", Integer(), nullable=False, unique=True,
469 "app_settings_id", Integer(), nullable=False, unique=True,
470 default=None, primary_key=True)
470 default=None, primary_key=True)
471 app_settings_name = Column(
471 app_settings_name = Column(
472 "app_settings_name", String(255), nullable=True, unique=None,
472 "app_settings_name", String(255), nullable=True, unique=None,
473 default=None)
473 default=None)
474 _app_settings_value = Column(
474 _app_settings_value = Column(
475 "app_settings_value", String(4096), nullable=True, unique=None,
475 "app_settings_value", String(4096), nullable=True, unique=None,
476 default=None)
476 default=None)
477 _app_settings_type = Column(
477 _app_settings_type = Column(
478 "app_settings_type", String(255), nullable=True, unique=None,
478 "app_settings_type", String(255), nullable=True, unique=None,
479 default=None)
479 default=None)
480
480
481 repository = relationship('Repository')
481 repository = relationship('Repository')
482
482
483 def __init__(self, repository_id, key='', val='', type='unicode'):
483 def __init__(self, repository_id, key='', val='', type='unicode'):
484 self.repository_id = repository_id
484 self.repository_id = repository_id
485 self.app_settings_name = key
485 self.app_settings_name = key
486 self.app_settings_type = type
486 self.app_settings_type = type
487 self.app_settings_value = val
487 self.app_settings_value = val
488
488
489 @validates('_app_settings_value')
489 @validates('_app_settings_value')
490 def validate_settings_value(self, key, val):
490 def validate_settings_value(self, key, val):
491 assert type(val) == unicode
491 assert type(val) == unicode
492 return val
492 return val
493
493
494 @hybrid_property
494 @hybrid_property
495 def app_settings_value(self):
495 def app_settings_value(self):
496 v = self._app_settings_value
496 v = self._app_settings_value
497 type_ = self.app_settings_type
497 type_ = self.app_settings_type
498 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
498 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
499 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
499 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
500 return converter(v)
500 return converter(v)
501
501
502 @app_settings_value.setter
502 @app_settings_value.setter
503 def app_settings_value(self, val):
503 def app_settings_value(self, val):
504 """
504 """
505 Setter that will always make sure we use unicode in app_settings_value
505 Setter that will always make sure we use unicode in app_settings_value
506
506
507 :param val:
507 :param val:
508 """
508 """
509 self._app_settings_value = safe_unicode(val)
509 self._app_settings_value = safe_unicode(val)
510
510
511 @hybrid_property
511 @hybrid_property
512 def app_settings_type(self):
512 def app_settings_type(self):
513 return self._app_settings_type
513 return self._app_settings_type
514
514
515 @app_settings_type.setter
515 @app_settings_type.setter
516 def app_settings_type(self, val):
516 def app_settings_type(self, val):
517 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
517 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
518 if val not in SETTINGS_TYPES:
518 if val not in SETTINGS_TYPES:
519 raise Exception('type must be one of %s got %s'
519 raise Exception('type must be one of %s got %s'
520 % (SETTINGS_TYPES.keys(), val))
520 % (SETTINGS_TYPES.keys(), val))
521 self._app_settings_type = val
521 self._app_settings_type = val
522
522
523 def __unicode__(self):
523 def __unicode__(self):
524 return u"<%s('%s:%s:%s[%s]')>" % (
524 return u"<%s('%s:%s:%s[%s]')>" % (
525 self.__class__.__name__, self.repository.repo_name,
525 self.__class__.__name__, self.repository.repo_name,
526 self.app_settings_name, self.app_settings_value,
526 self.app_settings_name, self.app_settings_value,
527 self.app_settings_type
527 self.app_settings_type
528 )
528 )
529
529
530
530
531 class RepoRhodeCodeUi(Base, BaseModel):
531 class RepoRhodeCodeUi(Base, BaseModel):
532 __tablename__ = 'repo_rhodecode_ui'
532 __tablename__ = 'repo_rhodecode_ui'
533 __table_args__ = (
533 __table_args__ = (
534 UniqueConstraint(
534 UniqueConstraint(
535 'repository_id', 'ui_section', 'ui_key',
535 'repository_id', 'ui_section', 'ui_key',
536 name='uq_repo_rhodecode_ui_repository_id_section_key'),
536 name='uq_repo_rhodecode_ui_repository_id_section_key'),
537 base_table_args
537 base_table_args
538 )
538 )
539
539
540 repository_id = Column(
540 repository_id = Column(
541 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
541 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
542 nullable=False)
542 nullable=False)
543 ui_id = Column(
543 ui_id = Column(
544 "ui_id", Integer(), nullable=False, unique=True, default=None,
544 "ui_id", Integer(), nullable=False, unique=True, default=None,
545 primary_key=True)
545 primary_key=True)
546 ui_section = Column(
546 ui_section = Column(
547 "ui_section", String(255), nullable=True, unique=None, default=None)
547 "ui_section", String(255), nullable=True, unique=None, default=None)
548 ui_key = Column(
548 ui_key = Column(
549 "ui_key", String(255), nullable=True, unique=None, default=None)
549 "ui_key", String(255), nullable=True, unique=None, default=None)
550 ui_value = Column(
550 ui_value = Column(
551 "ui_value", String(255), nullable=True, unique=None, default=None)
551 "ui_value", String(255), nullable=True, unique=None, default=None)
552 ui_active = Column(
552 ui_active = Column(
553 "ui_active", Boolean(), nullable=True, unique=None, default=True)
553 "ui_active", Boolean(), nullable=True, unique=None, default=True)
554
554
555 repository = relationship('Repository')
555 repository = relationship('Repository')
556
556
557 def __repr__(self):
557 def __repr__(self):
558 return '<%s[%s:%s]%s=>%s]>' % (
558 return '<%s[%s:%s]%s=>%s]>' % (
559 self.__class__.__name__, self.repository.repo_name,
559 self.__class__.__name__, self.repository.repo_name,
560 self.ui_section, self.ui_key, self.ui_value)
560 self.ui_section, self.ui_key, self.ui_value)
561
561
562
562
563 class User(Base, BaseModel):
563 class User(Base, BaseModel):
564 __tablename__ = 'users'
564 __tablename__ = 'users'
565 __table_args__ = (
565 __table_args__ = (
566 UniqueConstraint('username'), UniqueConstraint('email'),
566 UniqueConstraint('username'), UniqueConstraint('email'),
567 Index('u_username_idx', 'username'),
567 Index('u_username_idx', 'username'),
568 Index('u_email_idx', 'email'),
568 Index('u_email_idx', 'email'),
569 base_table_args
569 base_table_args
570 )
570 )
571
571
572 DEFAULT_USER = 'default'
572 DEFAULT_USER = 'default'
573 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
573 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
574 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
574 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
575
575
576 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
576 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
577 username = Column("username", String(255), nullable=True, unique=None, default=None)
577 username = Column("username", String(255), nullable=True, unique=None, default=None)
578 password = Column("password", String(255), nullable=True, unique=None, default=None)
578 password = Column("password", String(255), nullable=True, unique=None, default=None)
579 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
579 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
580 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
580 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
581 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
581 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
582 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
582 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
583 _email = Column("email", String(255), nullable=True, unique=None, default=None)
583 _email = Column("email", String(255), nullable=True, unique=None, default=None)
584 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
584 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
585 last_activity = Column('last_activity', DateTime(timezone=False), nullable=True, unique=None, default=None)
585 last_activity = Column('last_activity', DateTime(timezone=False), nullable=True, unique=None, default=None)
586 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
586 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
587
587
588 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
588 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
589 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
589 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
590 _api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
590 _api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
591 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
591 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
592 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
592 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
593 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
593 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
594
594
595 user_log = relationship('UserLog')
595 user_log = relationship('UserLog')
596 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all, delete-orphan')
596 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all, delete-orphan')
597
597
598 repositories = relationship('Repository')
598 repositories = relationship('Repository')
599 repository_groups = relationship('RepoGroup')
599 repository_groups = relationship('RepoGroup')
600 user_groups = relationship('UserGroup')
600 user_groups = relationship('UserGroup')
601
601
602 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
602 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
603 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
603 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
604
604
605 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all, delete-orphan')
605 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all, delete-orphan')
606 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all, delete-orphan')
606 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all, delete-orphan')
607 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all, delete-orphan')
607 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all, delete-orphan')
608
608
609 group_member = relationship('UserGroupMember', cascade='all')
609 group_member = relationship('UserGroupMember', cascade='all')
610
610
611 notifications = relationship('UserNotification', cascade='all')
611 notifications = relationship('UserNotification', cascade='all')
612 # notifications assigned to this user
612 # notifications assigned to this user
613 user_created_notifications = relationship('Notification', cascade='all')
613 user_created_notifications = relationship('Notification', cascade='all')
614 # comments created by this user
614 # comments created by this user
615 user_comments = relationship('ChangesetComment', cascade='all')
615 user_comments = relationship('ChangesetComment', cascade='all')
616 # user profile extra info
616 # user profile extra info
617 user_emails = relationship('UserEmailMap', cascade='all')
617 user_emails = relationship('UserEmailMap', cascade='all')
618 user_ip_map = relationship('UserIpMap', cascade='all')
618 user_ip_map = relationship('UserIpMap', cascade='all')
619 user_auth_tokens = relationship('UserApiKeys', cascade='all')
619 user_auth_tokens = relationship('UserApiKeys', cascade='all')
620 user_ssh_keys = relationship('UserSshKeys', cascade='all')
620 user_ssh_keys = relationship('UserSshKeys', cascade='all')
621
621
622 # gists
622 # gists
623 user_gists = relationship('Gist', cascade='all')
623 user_gists = relationship('Gist', cascade='all')
624 # user pull requests
624 # user pull requests
625 user_pull_requests = relationship('PullRequest', cascade='all')
625 user_pull_requests = relationship('PullRequest', cascade='all')
626
626
627 # external identities
627 # external identities
628 external_identities = relationship(
628 external_identities = relationship(
629 'ExternalIdentity',
629 'ExternalIdentity',
630 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
630 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
631 cascade='all')
631 cascade='all')
632 # review rules
632 # review rules
633 user_review_rules = relationship('RepoReviewRuleUser', cascade='all')
633 user_review_rules = relationship('RepoReviewRuleUser', cascade='all')
634
634
635 # artifacts owned
635 # artifacts owned
636 artifacts = relationship('FileStore', primaryjoin='FileStore.user_id==User.user_id')
636 artifacts = relationship('FileStore', primaryjoin='FileStore.user_id==User.user_id')
637
637
638 # no cascade, set NULL
638 # no cascade, set NULL
639 scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_user_id==User.user_id')
639 scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_user_id==User.user_id')
640
640
641 def __unicode__(self):
641 def __unicode__(self):
642 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
642 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
643 self.user_id, self.username)
643 self.user_id, self.username)
644
644
645 @hybrid_property
645 @hybrid_property
646 def email(self):
646 def email(self):
647 return self._email
647 return self._email
648
648
649 @email.setter
649 @email.setter
650 def email(self, val):
650 def email(self, val):
651 self._email = val.lower() if val else None
651 self._email = val.lower() if val else None
652
652
653 @hybrid_property
653 @hybrid_property
654 def first_name(self):
654 def first_name(self):
655 from rhodecode.lib import helpers as h
655 from rhodecode.lib import helpers as h
656 if self.name:
656 if self.name:
657 return h.escape(self.name)
657 return h.escape(self.name)
658 return self.name
658 return self.name
659
659
660 @hybrid_property
660 @hybrid_property
661 def last_name(self):
661 def last_name(self):
662 from rhodecode.lib import helpers as h
662 from rhodecode.lib import helpers as h
663 if self.lastname:
663 if self.lastname:
664 return h.escape(self.lastname)
664 return h.escape(self.lastname)
665 return self.lastname
665 return self.lastname
666
666
667 @hybrid_property
667 @hybrid_property
668 def api_key(self):
668 def api_key(self):
669 """
669 """
670 Fetch if exist an auth-token with role ALL connected to this user
670 Fetch if exist an auth-token with role ALL connected to this user
671 """
671 """
672 user_auth_token = UserApiKeys.query()\
672 user_auth_token = UserApiKeys.query()\
673 .filter(UserApiKeys.user_id == self.user_id)\
673 .filter(UserApiKeys.user_id == self.user_id)\
674 .filter(or_(UserApiKeys.expires == -1,
674 .filter(or_(UserApiKeys.expires == -1,
675 UserApiKeys.expires >= time.time()))\
675 UserApiKeys.expires >= time.time()))\
676 .filter(UserApiKeys.role == UserApiKeys.ROLE_ALL).first()
676 .filter(UserApiKeys.role == UserApiKeys.ROLE_ALL).first()
677 if user_auth_token:
677 if user_auth_token:
678 user_auth_token = user_auth_token.api_key
678 user_auth_token = user_auth_token.api_key
679
679
680 return user_auth_token
680 return user_auth_token
681
681
682 @api_key.setter
682 @api_key.setter
683 def api_key(self, val):
683 def api_key(self, val):
684 # don't allow to set API key this is deprecated for now
684 # don't allow to set API key this is deprecated for now
685 self._api_key = None
685 self._api_key = None
686
686
687 @property
687 @property
688 def reviewer_pull_requests(self):
688 def reviewer_pull_requests(self):
689 return PullRequestReviewers.query() \
689 return PullRequestReviewers.query() \
690 .options(joinedload(PullRequestReviewers.pull_request)) \
690 .options(joinedload(PullRequestReviewers.pull_request)) \
691 .filter(PullRequestReviewers.user_id == self.user_id) \
691 .filter(PullRequestReviewers.user_id == self.user_id) \
692 .all()
692 .all()
693
693
694 @property
694 @property
695 def firstname(self):
695 def firstname(self):
696 # alias for future
696 # alias for future
697 return self.name
697 return self.name
698
698
699 @property
699 @property
700 def emails(self):
700 def emails(self):
701 other = UserEmailMap.query()\
701 other = UserEmailMap.query()\
702 .filter(UserEmailMap.user == self) \
702 .filter(UserEmailMap.user == self) \
703 .order_by(UserEmailMap.email_id.asc()) \
703 .order_by(UserEmailMap.email_id.asc()) \
704 .all()
704 .all()
705 return [self.email] + [x.email for x in other]
705 return [self.email] + [x.email for x in other]
706
706
707 def emails_cached(self):
707 def emails_cached(self):
708 emails = UserEmailMap.query()\
708 emails = UserEmailMap.query()\
709 .filter(UserEmailMap.user == self) \
709 .filter(UserEmailMap.user == self) \
710 .order_by(UserEmailMap.email_id.asc())
710 .order_by(UserEmailMap.email_id.asc())
711
711
712 emails = emails.options(
712 emails = emails.options(
713 FromCache("sql_cache_short", "get_user_{}_emails".format(self.user_id))
713 FromCache("sql_cache_short", "get_user_{}_emails".format(self.user_id))
714 )
714 )
715
715
716 return [self.email] + [x.email for x in emails]
716 return [self.email] + [x.email for x in emails]
717
717
718 @property
718 @property
719 def auth_tokens(self):
719 def auth_tokens(self):
720 auth_tokens = self.get_auth_tokens()
720 auth_tokens = self.get_auth_tokens()
721 return [x.api_key for x in auth_tokens]
721 return [x.api_key for x in auth_tokens]
722
722
723 def get_auth_tokens(self):
723 def get_auth_tokens(self):
724 return UserApiKeys.query()\
724 return UserApiKeys.query()\
725 .filter(UserApiKeys.user == self)\
725 .filter(UserApiKeys.user == self)\
726 .order_by(UserApiKeys.user_api_key_id.asc())\
726 .order_by(UserApiKeys.user_api_key_id.asc())\
727 .all()
727 .all()
728
728
729 @LazyProperty
729 @LazyProperty
730 def feed_token(self):
730 def feed_token(self):
731 return self.get_feed_token()
731 return self.get_feed_token()
732
732
733 def get_feed_token(self, cache=True):
733 def get_feed_token(self, cache=True):
734 feed_tokens = UserApiKeys.query()\
734 feed_tokens = UserApiKeys.query()\
735 .filter(UserApiKeys.user == self)\
735 .filter(UserApiKeys.user == self)\
736 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)
736 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)
737 if cache:
737 if cache:
738 feed_tokens = feed_tokens.options(
738 feed_tokens = feed_tokens.options(
739 FromCache("sql_cache_short", "get_user_feed_token_%s" % self.user_id))
739 FromCache("sql_cache_short", "get_user_feed_token_%s" % self.user_id))
740
740
741 feed_tokens = feed_tokens.all()
741 feed_tokens = feed_tokens.all()
742 if feed_tokens:
742 if feed_tokens:
743 return feed_tokens[0].api_key
743 return feed_tokens[0].api_key
744 return 'NO_FEED_TOKEN_AVAILABLE'
744 return 'NO_FEED_TOKEN_AVAILABLE'
745
745
746 @LazyProperty
746 @LazyProperty
747 def artifact_token(self):
747 def artifact_token(self):
748 return self.get_artifact_token()
748 return self.get_artifact_token()
749
749
750 def get_artifact_token(self, cache=True):
750 def get_artifact_token(self, cache=True):
751 artifacts_tokens = UserApiKeys.query()\
751 artifacts_tokens = UserApiKeys.query()\
752 .filter(UserApiKeys.user == self)\
752 .filter(UserApiKeys.user == self)\
753 .filter(UserApiKeys.role == UserApiKeys.ROLE_ARTIFACT_DOWNLOAD)
753 .filter(UserApiKeys.role == UserApiKeys.ROLE_ARTIFACT_DOWNLOAD)
754 if cache:
754 if cache:
755 artifacts_tokens = artifacts_tokens.options(
755 artifacts_tokens = artifacts_tokens.options(
756 FromCache("sql_cache_short", "get_user_artifact_token_%s" % self.user_id))
756 FromCache("sql_cache_short", "get_user_artifact_token_%s" % self.user_id))
757
757
758 artifacts_tokens = artifacts_tokens.all()
758 artifacts_tokens = artifacts_tokens.all()
759 if artifacts_tokens:
759 if artifacts_tokens:
760 return artifacts_tokens[0].api_key
760 return artifacts_tokens[0].api_key
761 return 'NO_ARTIFACT_TOKEN_AVAILABLE'
761 return 'NO_ARTIFACT_TOKEN_AVAILABLE'
762
762
763 @classmethod
763 @classmethod
764 def get(cls, user_id, cache=False):
764 def get(cls, user_id, cache=False):
765 if not user_id:
765 if not user_id:
766 return
766 return
767
767
768 user = cls.query()
768 user = cls.query()
769 if cache:
769 if cache:
770 user = user.options(
770 user = user.options(
771 FromCache("sql_cache_short", "get_users_%s" % user_id))
771 FromCache("sql_cache_short", "get_users_%s" % user_id))
772 return user.get(user_id)
772 return user.get(user_id)
773
773
774 @classmethod
774 @classmethod
775 def extra_valid_auth_tokens(cls, user, role=None):
775 def extra_valid_auth_tokens(cls, user, role=None):
776 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
776 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
777 .filter(or_(UserApiKeys.expires == -1,
777 .filter(or_(UserApiKeys.expires == -1,
778 UserApiKeys.expires >= time.time()))
778 UserApiKeys.expires >= time.time()))
779 if role:
779 if role:
780 tokens = tokens.filter(or_(UserApiKeys.role == role,
780 tokens = tokens.filter(or_(UserApiKeys.role == role,
781 UserApiKeys.role == UserApiKeys.ROLE_ALL))
781 UserApiKeys.role == UserApiKeys.ROLE_ALL))
782 return tokens.all()
782 return tokens.all()
783
783
784 def authenticate_by_token(self, auth_token, roles=None, scope_repo_id=None):
784 def authenticate_by_token(self, auth_token, roles=None, scope_repo_id=None):
785 from rhodecode.lib import auth
785 from rhodecode.lib import auth
786
786
787 log.debug('Trying to authenticate user: %s via auth-token, '
787 log.debug('Trying to authenticate user: %s via auth-token, '
788 'and roles: %s', self, roles)
788 'and roles: %s', self, roles)
789
789
790 if not auth_token:
790 if not auth_token:
791 return False
791 return False
792
792
793 roles = (roles or []) + [UserApiKeys.ROLE_ALL]
793 roles = (roles or []) + [UserApiKeys.ROLE_ALL]
794 tokens_q = UserApiKeys.query()\
794 tokens_q = UserApiKeys.query()\
795 .filter(UserApiKeys.user_id == self.user_id)\
795 .filter(UserApiKeys.user_id == self.user_id)\
796 .filter(or_(UserApiKeys.expires == -1,
796 .filter(or_(UserApiKeys.expires == -1,
797 UserApiKeys.expires >= time.time()))
797 UserApiKeys.expires >= time.time()))
798
798
799 tokens_q = tokens_q.filter(UserApiKeys.role.in_(roles))
799 tokens_q = tokens_q.filter(UserApiKeys.role.in_(roles))
800
800
801 crypto_backend = auth.crypto_backend()
801 crypto_backend = auth.crypto_backend()
802 enc_token_map = {}
802 enc_token_map = {}
803 plain_token_map = {}
803 plain_token_map = {}
804 for token in tokens_q:
804 for token in tokens_q:
805 if token.api_key.startswith(crypto_backend.ENC_PREF):
805 if token.api_key.startswith(crypto_backend.ENC_PREF):
806 enc_token_map[token.api_key] = token
806 enc_token_map[token.api_key] = token
807 else:
807 else:
808 plain_token_map[token.api_key] = token
808 plain_token_map[token.api_key] = token
809 log.debug(
809 log.debug(
810 'Found %s plain and %s encrypted tokens to check for authentication for this user',
810 'Found %s plain and %s encrypted tokens to check for authentication for this user',
811 len(plain_token_map), len(enc_token_map))
811 len(plain_token_map), len(enc_token_map))
812
812
813 # plain token match comes first
813 # plain token match comes first
814 match = plain_token_map.get(auth_token)
814 match = plain_token_map.get(auth_token)
815
815
816 # check encrypted tokens now
816 # check encrypted tokens now
817 if not match:
817 if not match:
818 for token_hash, token in enc_token_map.items():
818 for token_hash, token in enc_token_map.items():
819 # NOTE(marcink): this is expensive to calculate, but most secure
819 # NOTE(marcink): this is expensive to calculate, but most secure
820 if crypto_backend.hash_check(auth_token, token_hash):
820 if crypto_backend.hash_check(auth_token, token_hash):
821 match = token
821 match = token
822 break
822 break
823
823
824 if match:
824 if match:
825 log.debug('Found matching token %s', match)
825 log.debug('Found matching token %s', match)
826 if match.repo_id:
826 if match.repo_id:
827 log.debug('Found scope, checking for scope match of token %s', match)
827 log.debug('Found scope, checking for scope match of token %s', match)
828 if match.repo_id == scope_repo_id:
828 if match.repo_id == scope_repo_id:
829 return True
829 return True
830 else:
830 else:
831 log.debug(
831 log.debug(
832 'AUTH_TOKEN: scope mismatch, token has a set repo scope: %s, '
832 'AUTH_TOKEN: scope mismatch, token has a set repo scope: %s, '
833 'and calling scope is:%s, skipping further checks',
833 'and calling scope is:%s, skipping further checks',
834 match.repo, scope_repo_id)
834 match.repo, scope_repo_id)
835 return False
835 return False
836 else:
836 else:
837 return True
837 return True
838
838
839 return False
839 return False
840
840
841 @property
841 @property
842 def ip_addresses(self):
842 def ip_addresses(self):
843 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
843 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
844 return [x.ip_addr for x in ret]
844 return [x.ip_addr for x in ret]
845
845
846 @property
846 @property
847 def username_and_name(self):
847 def username_and_name(self):
848 return '%s (%s %s)' % (self.username, self.first_name, self.last_name)
848 return '%s (%s %s)' % (self.username, self.first_name, self.last_name)
849
849
850 @property
850 @property
851 def username_or_name_or_email(self):
851 def username_or_name_or_email(self):
852 full_name = self.full_name if self.full_name is not ' ' else None
852 full_name = self.full_name if self.full_name is not ' ' else None
853 return self.username or full_name or self.email
853 return self.username or full_name or self.email
854
854
855 @property
855 @property
856 def full_name(self):
856 def full_name(self):
857 return '%s %s' % (self.first_name, self.last_name)
857 return '%s %s' % (self.first_name, self.last_name)
858
858
859 @property
859 @property
860 def full_name_or_username(self):
860 def full_name_or_username(self):
861 return ('%s %s' % (self.first_name, self.last_name)
861 return ('%s %s' % (self.first_name, self.last_name)
862 if (self.first_name and self.last_name) else self.username)
862 if (self.first_name and self.last_name) else self.username)
863
863
864 @property
864 @property
865 def full_contact(self):
865 def full_contact(self):
866 return '%s %s <%s>' % (self.first_name, self.last_name, self.email)
866 return '%s %s <%s>' % (self.first_name, self.last_name, self.email)
867
867
868 @property
868 @property
869 def short_contact(self):
869 def short_contact(self):
870 return '%s %s' % (self.first_name, self.last_name)
870 return '%s %s' % (self.first_name, self.last_name)
871
871
872 @property
872 @property
873 def is_admin(self):
873 def is_admin(self):
874 return self.admin
874 return self.admin
875
875
876 @property
876 @property
877 def language(self):
877 def language(self):
878 return self.user_data.get('language')
878 return self.user_data.get('language')
879
879
880 def AuthUser(self, **kwargs):
880 def AuthUser(self, **kwargs):
881 """
881 """
882 Returns instance of AuthUser for this user
882 Returns instance of AuthUser for this user
883 """
883 """
884 from rhodecode.lib.auth import AuthUser
884 from rhodecode.lib.auth import AuthUser
885 return AuthUser(user_id=self.user_id, username=self.username, **kwargs)
885 return AuthUser(user_id=self.user_id, username=self.username, **kwargs)
886
886
887 @hybrid_property
887 @hybrid_property
888 def user_data(self):
888 def user_data(self):
889 if not self._user_data:
889 if not self._user_data:
890 return {}
890 return {}
891
891
892 try:
892 try:
893 return json.loads(self._user_data)
893 return json.loads(self._user_data)
894 except TypeError:
894 except TypeError:
895 return {}
895 return {}
896
896
897 @user_data.setter
897 @user_data.setter
898 def user_data(self, val):
898 def user_data(self, val):
899 if not isinstance(val, dict):
899 if not isinstance(val, dict):
900 raise Exception('user_data must be dict, got %s' % type(val))
900 raise Exception('user_data must be dict, got %s' % type(val))
901 try:
901 try:
902 self._user_data = json.dumps(val)
902 self._user_data = json.dumps(val)
903 except Exception:
903 except Exception:
904 log.error(traceback.format_exc())
904 log.error(traceback.format_exc())
905
905
906 @classmethod
906 @classmethod
907 def get_by_username(cls, username, case_insensitive=False,
907 def get_by_username(cls, username, case_insensitive=False,
908 cache=False, identity_cache=False):
908 cache=False, identity_cache=False):
909 session = Session()
909 session = Session()
910
910
911 if case_insensitive:
911 if case_insensitive:
912 q = cls.query().filter(
912 q = cls.query().filter(
913 func.lower(cls.username) == func.lower(username))
913 func.lower(cls.username) == func.lower(username))
914 else:
914 else:
915 q = cls.query().filter(cls.username == username)
915 q = cls.query().filter(cls.username == username)
916
916
917 if cache:
917 if cache:
918 if identity_cache:
918 if identity_cache:
919 val = cls.identity_cache(session, 'username', username)
919 val = cls.identity_cache(session, 'username', username)
920 if val:
920 if val:
921 return val
921 return val
922 else:
922 else:
923 cache_key = "get_user_by_name_%s" % _hash_key(username)
923 cache_key = "get_user_by_name_%s" % _hash_key(username)
924 q = q.options(
924 q = q.options(
925 FromCache("sql_cache_short", cache_key))
925 FromCache("sql_cache_short", cache_key))
926
926
927 return q.scalar()
927 return q.scalar()
928
928
929 @classmethod
929 @classmethod
930 def get_by_auth_token(cls, auth_token, cache=False):
930 def get_by_auth_token(cls, auth_token, cache=False):
931 q = UserApiKeys.query()\
931 q = UserApiKeys.query()\
932 .filter(UserApiKeys.api_key == auth_token)\
932 .filter(UserApiKeys.api_key == auth_token)\
933 .filter(or_(UserApiKeys.expires == -1,
933 .filter(or_(UserApiKeys.expires == -1,
934 UserApiKeys.expires >= time.time()))
934 UserApiKeys.expires >= time.time()))
935 if cache:
935 if cache:
936 q = q.options(
936 q = q.options(
937 FromCache("sql_cache_short", "get_auth_token_%s" % auth_token))
937 FromCache("sql_cache_short", "get_auth_token_%s" % auth_token))
938
938
939 match = q.first()
939 match = q.first()
940 if match:
940 if match:
941 return match.user
941 return match.user
942
942
943 @classmethod
943 @classmethod
944 def get_by_email(cls, email, case_insensitive=False, cache=False):
944 def get_by_email(cls, email, case_insensitive=False, cache=False):
945
945
946 if case_insensitive:
946 if case_insensitive:
947 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
947 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
948
948
949 else:
949 else:
950 q = cls.query().filter(cls.email == email)
950 q = cls.query().filter(cls.email == email)
951
951
952 email_key = _hash_key(email)
952 email_key = _hash_key(email)
953 if cache:
953 if cache:
954 q = q.options(
954 q = q.options(
955 FromCache("sql_cache_short", "get_email_key_%s" % email_key))
955 FromCache("sql_cache_short", "get_email_key_%s" % email_key))
956
956
957 ret = q.scalar()
957 ret = q.scalar()
958 if ret is None:
958 if ret is None:
959 q = UserEmailMap.query()
959 q = UserEmailMap.query()
960 # try fetching in alternate email map
960 # try fetching in alternate email map
961 if case_insensitive:
961 if case_insensitive:
962 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
962 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
963 else:
963 else:
964 q = q.filter(UserEmailMap.email == email)
964 q = q.filter(UserEmailMap.email == email)
965 q = q.options(joinedload(UserEmailMap.user))
965 q = q.options(joinedload(UserEmailMap.user))
966 if cache:
966 if cache:
967 q = q.options(
967 q = q.options(
968 FromCache("sql_cache_short", "get_email_map_key_%s" % email_key))
968 FromCache("sql_cache_short", "get_email_map_key_%s" % email_key))
969 ret = getattr(q.scalar(), 'user', None)
969 ret = getattr(q.scalar(), 'user', None)
970
970
971 return ret
971 return ret
972
972
973 @classmethod
973 @classmethod
974 def get_from_cs_author(cls, author):
974 def get_from_cs_author(cls, author):
975 """
975 """
976 Tries to get User objects out of commit author string
976 Tries to get User objects out of commit author string
977
977
978 :param author:
978 :param author:
979 """
979 """
980 from rhodecode.lib.helpers import email, author_name
980 from rhodecode.lib.helpers import email, author_name
981 # Valid email in the attribute passed, see if they're in the system
981 # Valid email in the attribute passed, see if they're in the system
982 _email = email(author)
982 _email = email(author)
983 if _email:
983 if _email:
984 user = cls.get_by_email(_email, case_insensitive=True)
984 user = cls.get_by_email(_email, case_insensitive=True)
985 if user:
985 if user:
986 return user
986 return user
987 # Maybe we can match by username?
987 # Maybe we can match by username?
988 _author = author_name(author)
988 _author = author_name(author)
989 user = cls.get_by_username(_author, case_insensitive=True)
989 user = cls.get_by_username(_author, case_insensitive=True)
990 if user:
990 if user:
991 return user
991 return user
992
992
993 def update_userdata(self, **kwargs):
993 def update_userdata(self, **kwargs):
994 usr = self
994 usr = self
995 old = usr.user_data
995 old = usr.user_data
996 old.update(**kwargs)
996 old.update(**kwargs)
997 usr.user_data = old
997 usr.user_data = old
998 Session().add(usr)
998 Session().add(usr)
999 log.debug('updated userdata with %s', kwargs)
999 log.debug('updated userdata with %s', kwargs)
1000
1000
1001 def update_lastlogin(self):
1001 def update_lastlogin(self):
1002 """Update user lastlogin"""
1002 """Update user lastlogin"""
1003 self.last_login = datetime.datetime.now()
1003 self.last_login = datetime.datetime.now()
1004 Session().add(self)
1004 Session().add(self)
1005 log.debug('updated user %s lastlogin', self.username)
1005 log.debug('updated user %s lastlogin', self.username)
1006
1006
1007 def update_password(self, new_password):
1007 def update_password(self, new_password):
1008 from rhodecode.lib.auth import get_crypt_password
1008 from rhodecode.lib.auth import get_crypt_password
1009
1009
1010 self.password = get_crypt_password(new_password)
1010 self.password = get_crypt_password(new_password)
1011 Session().add(self)
1011 Session().add(self)
1012
1012
1013 @classmethod
1013 @classmethod
1014 def get_first_super_admin(cls):
1014 def get_first_super_admin(cls):
1015 user = User.query()\
1015 user = User.query()\
1016 .filter(User.admin == true()) \
1016 .filter(User.admin == true()) \
1017 .order_by(User.user_id.asc()) \
1017 .order_by(User.user_id.asc()) \
1018 .first()
1018 .first()
1019
1019
1020 if user is None:
1020 if user is None:
1021 raise Exception('FATAL: Missing administrative account!')
1021 raise Exception('FATAL: Missing administrative account!')
1022 return user
1022 return user
1023
1023
1024 @classmethod
1024 @classmethod
1025 def get_all_super_admins(cls, only_active=False):
1025 def get_all_super_admins(cls, only_active=False):
1026 """
1026 """
1027 Returns all admin accounts sorted by username
1027 Returns all admin accounts sorted by username
1028 """
1028 """
1029 qry = User.query().filter(User.admin == true()).order_by(User.username.asc())
1029 qry = User.query().filter(User.admin == true()).order_by(User.username.asc())
1030 if only_active:
1030 if only_active:
1031 qry = qry.filter(User.active == true())
1031 qry = qry.filter(User.active == true())
1032 return qry.all()
1032 return qry.all()
1033
1033
1034 @classmethod
1034 @classmethod
1035 def get_all_user_ids(cls, only_active=True):
1035 def get_all_user_ids(cls, only_active=True):
1036 """
1036 """
1037 Returns all users IDs
1037 Returns all users IDs
1038 """
1038 """
1039 qry = Session().query(User.user_id)
1039 qry = Session().query(User.user_id)
1040
1040
1041 if only_active:
1041 if only_active:
1042 qry = qry.filter(User.active == true())
1042 qry = qry.filter(User.active == true())
1043 return [x.user_id for x in qry]
1043 return [x.user_id for x in qry]
1044
1044
1045 @classmethod
1045 @classmethod
1046 def get_default_user(cls, cache=False, refresh=False):
1046 def get_default_user(cls, cache=False, refresh=False):
1047 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
1047 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
1048 if user is None:
1048 if user is None:
1049 raise Exception('FATAL: Missing default account!')
1049 raise Exception('FATAL: Missing default account!')
1050 if refresh:
1050 if refresh:
1051 # The default user might be based on outdated state which
1051 # The default user might be based on outdated state which
1052 # has been loaded from the cache.
1052 # has been loaded from the cache.
1053 # A call to refresh() ensures that the
1053 # A call to refresh() ensures that the
1054 # latest state from the database is used.
1054 # latest state from the database is used.
1055 Session().refresh(user)
1055 Session().refresh(user)
1056 return user
1056 return user
1057
1057
1058 @classmethod
1058 @classmethod
1059 def get_default_user_id(cls):
1059 def get_default_user_id(cls):
1060 import rhodecode
1060 import rhodecode
1061 return rhodecode.CONFIG['default_user_id']
1061 return rhodecode.CONFIG['default_user_id']
1062
1062
1063 def _get_default_perms(self, user, suffix=''):
1063 def _get_default_perms(self, user, suffix=''):
1064 from rhodecode.model.permission import PermissionModel
1064 from rhodecode.model.permission import PermissionModel
1065 return PermissionModel().get_default_perms(user.user_perms, suffix)
1065 return PermissionModel().get_default_perms(user.user_perms, suffix)
1066
1066
1067 def get_default_perms(self, suffix=''):
1067 def get_default_perms(self, suffix=''):
1068 return self._get_default_perms(self, suffix)
1068 return self._get_default_perms(self, suffix)
1069
1069
1070 def get_api_data(self, include_secrets=False, details='full'):
1070 def get_api_data(self, include_secrets=False, details='full'):
1071 """
1071 """
1072 Common function for generating user related data for API
1072 Common function for generating user related data for API
1073
1073
1074 :param include_secrets: By default secrets in the API data will be replaced
1074 :param include_secrets: By default secrets in the API data will be replaced
1075 by a placeholder value to prevent exposing this data by accident. In case
1075 by a placeholder value to prevent exposing this data by accident. In case
1076 this data shall be exposed, set this flag to ``True``.
1076 this data shall be exposed, set this flag to ``True``.
1077
1077
1078 :param details: details can be 'basic|full' basic gives only a subset of
1078 :param details: details can be 'basic|full' basic gives only a subset of
1079 the available user information that includes user_id, name and emails.
1079 the available user information that includes user_id, name and emails.
1080 """
1080 """
1081 user = self
1081 user = self
1082 user_data = self.user_data
1082 user_data = self.user_data
1083 data = {
1083 data = {
1084 'user_id': user.user_id,
1084 'user_id': user.user_id,
1085 'username': user.username,
1085 'username': user.username,
1086 'firstname': user.name,
1086 'firstname': user.name,
1087 'lastname': user.lastname,
1087 'lastname': user.lastname,
1088 'description': user.description,
1088 'description': user.description,
1089 'email': user.email,
1089 'email': user.email,
1090 'emails': user.emails,
1090 'emails': user.emails,
1091 }
1091 }
1092 if details == 'basic':
1092 if details == 'basic':
1093 return data
1093 return data
1094
1094
1095 auth_token_length = 40
1095 auth_token_length = 40
1096 auth_token_replacement = '*' * auth_token_length
1096 auth_token_replacement = '*' * auth_token_length
1097
1097
1098 extras = {
1098 extras = {
1099 'auth_tokens': [auth_token_replacement],
1099 'auth_tokens': [auth_token_replacement],
1100 'active': user.active,
1100 'active': user.active,
1101 'admin': user.admin,
1101 'admin': user.admin,
1102 'extern_type': user.extern_type,
1102 'extern_type': user.extern_type,
1103 'extern_name': user.extern_name,
1103 'extern_name': user.extern_name,
1104 'last_login': user.last_login,
1104 'last_login': user.last_login,
1105 'last_activity': user.last_activity,
1105 'last_activity': user.last_activity,
1106 'ip_addresses': user.ip_addresses,
1106 'ip_addresses': user.ip_addresses,
1107 'language': user_data.get('language')
1107 'language': user_data.get('language')
1108 }
1108 }
1109 data.update(extras)
1109 data.update(extras)
1110
1110
1111 if include_secrets:
1111 if include_secrets:
1112 data['auth_tokens'] = user.auth_tokens
1112 data['auth_tokens'] = user.auth_tokens
1113 return data
1113 return data
1114
1114
1115 def __json__(self):
1115 def __json__(self):
1116 data = {
1116 data = {
1117 'full_name': self.full_name,
1117 'full_name': self.full_name,
1118 'full_name_or_username': self.full_name_or_username,
1118 'full_name_or_username': self.full_name_or_username,
1119 'short_contact': self.short_contact,
1119 'short_contact': self.short_contact,
1120 'full_contact': self.full_contact,
1120 'full_contact': self.full_contact,
1121 }
1121 }
1122 data.update(self.get_api_data())
1122 data.update(self.get_api_data())
1123 return data
1123 return data
1124
1124
1125
1125
1126 class UserApiKeys(Base, BaseModel):
1126 class UserApiKeys(Base, BaseModel):
1127 __tablename__ = 'user_api_keys'
1127 __tablename__ = 'user_api_keys'
1128 __table_args__ = (
1128 __table_args__ = (
1129 Index('uak_api_key_idx', 'api_key'),
1129 Index('uak_api_key_idx', 'api_key'),
1130 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
1130 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
1131 base_table_args
1131 base_table_args
1132 )
1132 )
1133 __mapper_args__ = {}
1133 __mapper_args__ = {}
1134
1134
1135 # ApiKey role
1135 # ApiKey role
1136 ROLE_ALL = 'token_role_all'
1136 ROLE_ALL = 'token_role_all'
1137 ROLE_VCS = 'token_role_vcs'
1137 ROLE_VCS = 'token_role_vcs'
1138 ROLE_API = 'token_role_api'
1138 ROLE_API = 'token_role_api'
1139 ROLE_HTTP = 'token_role_http'
1139 ROLE_HTTP = 'token_role_http'
1140 ROLE_FEED = 'token_role_feed'
1140 ROLE_FEED = 'token_role_feed'
1141 ROLE_ARTIFACT_DOWNLOAD = 'role_artifact_download'
1141 ROLE_ARTIFACT_DOWNLOAD = 'role_artifact_download'
1142 # The last one is ignored in the list as we only
1142 # The last one is ignored in the list as we only
1143 # use it for one action, and cannot be created by users
1143 # use it for one action, and cannot be created by users
1144 ROLE_PASSWORD_RESET = 'token_password_reset'
1144 ROLE_PASSWORD_RESET = 'token_password_reset'
1145
1145
1146 ROLES = [ROLE_ALL, ROLE_VCS, ROLE_API, ROLE_HTTP, ROLE_FEED, ROLE_ARTIFACT_DOWNLOAD]
1146 ROLES = [ROLE_ALL, ROLE_VCS, ROLE_API, ROLE_HTTP, ROLE_FEED, ROLE_ARTIFACT_DOWNLOAD]
1147
1147
1148 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1148 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1149 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1149 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1150 api_key = Column("api_key", String(255), nullable=False, unique=True)
1150 api_key = Column("api_key", String(255), nullable=False, unique=True)
1151 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1151 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1152 expires = Column('expires', Float(53), nullable=False)
1152 expires = Column('expires', Float(53), nullable=False)
1153 role = Column('role', String(255), nullable=True)
1153 role = Column('role', String(255), nullable=True)
1154 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1154 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1155
1155
1156 # scope columns
1156 # scope columns
1157 repo_id = Column(
1157 repo_id = Column(
1158 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
1158 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
1159 nullable=True, unique=None, default=None)
1159 nullable=True, unique=None, default=None)
1160 repo = relationship('Repository', lazy='joined')
1160 repo = relationship('Repository', lazy='joined')
1161
1161
1162 repo_group_id = Column(
1162 repo_group_id = Column(
1163 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
1163 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
1164 nullable=True, unique=None, default=None)
1164 nullable=True, unique=None, default=None)
1165 repo_group = relationship('RepoGroup', lazy='joined')
1165 repo_group = relationship('RepoGroup', lazy='joined')
1166
1166
1167 user = relationship('User', lazy='joined')
1167 user = relationship('User', lazy='joined')
1168
1168
1169 def __unicode__(self):
1169 def __unicode__(self):
1170 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1170 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1171
1171
1172 def __json__(self):
1172 def __json__(self):
1173 data = {
1173 data = {
1174 'auth_token': self.api_key,
1174 'auth_token': self.api_key,
1175 'role': self.role,
1175 'role': self.role,
1176 'scope': self.scope_humanized,
1176 'scope': self.scope_humanized,
1177 'expired': self.expired
1177 'expired': self.expired
1178 }
1178 }
1179 return data
1179 return data
1180
1180
1181 def get_api_data(self, include_secrets=False):
1181 def get_api_data(self, include_secrets=False):
1182 data = self.__json__()
1182 data = self.__json__()
1183 if include_secrets:
1183 if include_secrets:
1184 return data
1184 return data
1185 else:
1185 else:
1186 data['auth_token'] = self.token_obfuscated
1186 data['auth_token'] = self.token_obfuscated
1187 return data
1187 return data
1188
1188
1189 @hybrid_property
1189 @hybrid_property
1190 def description_safe(self):
1190 def description_safe(self):
1191 from rhodecode.lib import helpers as h
1191 from rhodecode.lib import helpers as h
1192 return h.escape(self.description)
1192 return h.escape(self.description)
1193
1193
1194 @property
1194 @property
1195 def expired(self):
1195 def expired(self):
1196 if self.expires == -1:
1196 if self.expires == -1:
1197 return False
1197 return False
1198 return time.time() > self.expires
1198 return time.time() > self.expires
1199
1199
1200 @classmethod
1200 @classmethod
1201 def _get_role_name(cls, role):
1201 def _get_role_name(cls, role):
1202 return {
1202 return {
1203 cls.ROLE_ALL: _('all'),
1203 cls.ROLE_ALL: _('all'),
1204 cls.ROLE_HTTP: _('http/web interface'),
1204 cls.ROLE_HTTP: _('http/web interface'),
1205 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
1205 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
1206 cls.ROLE_API: _('api calls'),
1206 cls.ROLE_API: _('api calls'),
1207 cls.ROLE_FEED: _('feed access'),
1207 cls.ROLE_FEED: _('feed access'),
1208 cls.ROLE_ARTIFACT_DOWNLOAD: _('artifacts downloads'),
1208 cls.ROLE_ARTIFACT_DOWNLOAD: _('artifacts downloads'),
1209 }.get(role, role)
1209 }.get(role, role)
1210
1210
1211 @classmethod
1211 @classmethod
1212 def _get_role_description(cls, role):
1212 def _get_role_description(cls, role):
1213 return {
1213 return {
1214 cls.ROLE_ALL: _('Token for all actions.'),
1214 cls.ROLE_ALL: _('Token for all actions.'),
1215 cls.ROLE_HTTP: _('Token to access RhodeCode pages via web interface without '
1215 cls.ROLE_HTTP: _('Token to access RhodeCode pages via web interface without '
1216 'login using `api_access_controllers_whitelist` functionality.'),
1216 'login using `api_access_controllers_whitelist` functionality.'),
1217 cls.ROLE_VCS: _('Token to interact over git/hg/svn protocols. '
1217 cls.ROLE_VCS: _('Token to interact over git/hg/svn protocols. '
1218 'Requires auth_token authentication plugin to be active. <br/>'
1218 'Requires auth_token authentication plugin to be active. <br/>'
1219 'Such Token should be used then instead of a password to '
1219 'Such Token should be used then instead of a password to '
1220 'interact with a repository, and additionally can be '
1220 'interact with a repository, and additionally can be '
1221 'limited to single repository using repo scope.'),
1221 'limited to single repository using repo scope.'),
1222 cls.ROLE_API: _('Token limited to api calls.'),
1222 cls.ROLE_API: _('Token limited to api calls.'),
1223 cls.ROLE_FEED: _('Token to read RSS/ATOM feed.'),
1223 cls.ROLE_FEED: _('Token to read RSS/ATOM feed.'),
1224 cls.ROLE_ARTIFACT_DOWNLOAD: _('Token for artifacts downloads.'),
1224 cls.ROLE_ARTIFACT_DOWNLOAD: _('Token for artifacts downloads.'),
1225 }.get(role, role)
1225 }.get(role, role)
1226
1226
1227 @property
1227 @property
1228 def role_humanized(self):
1228 def role_humanized(self):
1229 return self._get_role_name(self.role)
1229 return self._get_role_name(self.role)
1230
1230
1231 def _get_scope(self):
1231 def _get_scope(self):
1232 if self.repo:
1232 if self.repo:
1233 return 'Repository: {}'.format(self.repo.repo_name)
1233 return 'Repository: {}'.format(self.repo.repo_name)
1234 if self.repo_group:
1234 if self.repo_group:
1235 return 'RepositoryGroup: {} (recursive)'.format(self.repo_group.group_name)
1235 return 'RepositoryGroup: {} (recursive)'.format(self.repo_group.group_name)
1236 return 'Global'
1236 return 'Global'
1237
1237
1238 @property
1238 @property
1239 def scope_humanized(self):
1239 def scope_humanized(self):
1240 return self._get_scope()
1240 return self._get_scope()
1241
1241
1242 @property
1242 @property
1243 def token_obfuscated(self):
1243 def token_obfuscated(self):
1244 if self.api_key:
1244 if self.api_key:
1245 return self.api_key[:4] + "****"
1245 return self.api_key[:4] + "****"
1246
1246
1247
1247
1248 class UserEmailMap(Base, BaseModel):
1248 class UserEmailMap(Base, BaseModel):
1249 __tablename__ = 'user_email_map'
1249 __tablename__ = 'user_email_map'
1250 __table_args__ = (
1250 __table_args__ = (
1251 Index('uem_email_idx', 'email'),
1251 Index('uem_email_idx', 'email'),
1252 UniqueConstraint('email'),
1252 UniqueConstraint('email'),
1253 base_table_args
1253 base_table_args
1254 )
1254 )
1255 __mapper_args__ = {}
1255 __mapper_args__ = {}
1256
1256
1257 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1257 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1258 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1258 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1259 _email = Column("email", String(255), nullable=True, unique=False, default=None)
1259 _email = Column("email", String(255), nullable=True, unique=False, default=None)
1260 user = relationship('User', lazy='joined')
1260 user = relationship('User', lazy='joined')
1261
1261
1262 @validates('_email')
1262 @validates('_email')
1263 def validate_email(self, key, email):
1263 def validate_email(self, key, email):
1264 # check if this email is not main one
1264 # check if this email is not main one
1265 main_email = Session().query(User).filter(User.email == email).scalar()
1265 main_email = Session().query(User).filter(User.email == email).scalar()
1266 if main_email is not None:
1266 if main_email is not None:
1267 raise AttributeError('email %s is present is user table' % email)
1267 raise AttributeError('email %s is present is user table' % email)
1268 return email
1268 return email
1269
1269
1270 @hybrid_property
1270 @hybrid_property
1271 def email(self):
1271 def email(self):
1272 return self._email
1272 return self._email
1273
1273
1274 @email.setter
1274 @email.setter
1275 def email(self, val):
1275 def email(self, val):
1276 self._email = val.lower() if val else None
1276 self._email = val.lower() if val else None
1277
1277
1278
1278
1279 class UserIpMap(Base, BaseModel):
1279 class UserIpMap(Base, BaseModel):
1280 __tablename__ = 'user_ip_map'
1280 __tablename__ = 'user_ip_map'
1281 __table_args__ = (
1281 __table_args__ = (
1282 UniqueConstraint('user_id', 'ip_addr'),
1282 UniqueConstraint('user_id', 'ip_addr'),
1283 base_table_args
1283 base_table_args
1284 )
1284 )
1285 __mapper_args__ = {}
1285 __mapper_args__ = {}
1286
1286
1287 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1287 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1288 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1288 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1289 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
1289 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
1290 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
1290 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
1291 description = Column("description", String(10000), nullable=True, unique=None, default=None)
1291 description = Column("description", String(10000), nullable=True, unique=None, default=None)
1292 user = relationship('User', lazy='joined')
1292 user = relationship('User', lazy='joined')
1293
1293
1294 @hybrid_property
1294 @hybrid_property
1295 def description_safe(self):
1295 def description_safe(self):
1296 from rhodecode.lib import helpers as h
1296 from rhodecode.lib import helpers as h
1297 return h.escape(self.description)
1297 return h.escape(self.description)
1298
1298
1299 @classmethod
1299 @classmethod
1300 def _get_ip_range(cls, ip_addr):
1300 def _get_ip_range(cls, ip_addr):
1301 net = ipaddress.ip_network(safe_unicode(ip_addr), strict=False)
1301 net = ipaddress.ip_network(safe_unicode(ip_addr), strict=False)
1302 return [str(net.network_address), str(net.broadcast_address)]
1302 return [str(net.network_address), str(net.broadcast_address)]
1303
1303
1304 def __json__(self):
1304 def __json__(self):
1305 return {
1305 return {
1306 'ip_addr': self.ip_addr,
1306 'ip_addr': self.ip_addr,
1307 'ip_range': self._get_ip_range(self.ip_addr),
1307 'ip_range': self._get_ip_range(self.ip_addr),
1308 }
1308 }
1309
1309
1310 def __unicode__(self):
1310 def __unicode__(self):
1311 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1311 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1312 self.user_id, self.ip_addr)
1312 self.user_id, self.ip_addr)
1313
1313
1314
1314
1315 class UserSshKeys(Base, BaseModel):
1315 class UserSshKeys(Base, BaseModel):
1316 __tablename__ = 'user_ssh_keys'
1316 __tablename__ = 'user_ssh_keys'
1317 __table_args__ = (
1317 __table_args__ = (
1318 Index('usk_ssh_key_fingerprint_idx', 'ssh_key_fingerprint'),
1318 Index('usk_ssh_key_fingerprint_idx', 'ssh_key_fingerprint'),
1319
1319
1320 UniqueConstraint('ssh_key_fingerprint'),
1320 UniqueConstraint('ssh_key_fingerprint'),
1321
1321
1322 base_table_args
1322 base_table_args
1323 )
1323 )
1324 __mapper_args__ = {}
1324 __mapper_args__ = {}
1325
1325
1326 ssh_key_id = Column('ssh_key_id', Integer(), nullable=False, unique=True, default=None, primary_key=True)
1326 ssh_key_id = Column('ssh_key_id', Integer(), nullable=False, unique=True, default=None, primary_key=True)
1327 ssh_key_data = Column('ssh_key_data', String(10240), nullable=False, unique=None, default=None)
1327 ssh_key_data = Column('ssh_key_data', String(10240), nullable=False, unique=None, default=None)
1328 ssh_key_fingerprint = Column('ssh_key_fingerprint', String(255), nullable=False, unique=None, default=None)
1328 ssh_key_fingerprint = Column('ssh_key_fingerprint', String(255), nullable=False, unique=None, default=None)
1329
1329
1330 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1330 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1331
1331
1332 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1332 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1333 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True, default=None)
1333 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True, default=None)
1334 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1334 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1335
1335
1336 user = relationship('User', lazy='joined')
1336 user = relationship('User', lazy='joined')
1337
1337
1338 def __json__(self):
1338 def __json__(self):
1339 data = {
1339 data = {
1340 'ssh_fingerprint': self.ssh_key_fingerprint,
1340 'ssh_fingerprint': self.ssh_key_fingerprint,
1341 'description': self.description,
1341 'description': self.description,
1342 'created_on': self.created_on
1342 'created_on': self.created_on
1343 }
1343 }
1344 return data
1344 return data
1345
1345
1346 def get_api_data(self):
1346 def get_api_data(self):
1347 data = self.__json__()
1347 data = self.__json__()
1348 return data
1348 return data
1349
1349
1350
1350
1351 class UserLog(Base, BaseModel):
1351 class UserLog(Base, BaseModel):
1352 __tablename__ = 'user_logs'
1352 __tablename__ = 'user_logs'
1353 __table_args__ = (
1353 __table_args__ = (
1354 base_table_args,
1354 base_table_args,
1355 )
1355 )
1356
1356
1357 VERSION_1 = 'v1'
1357 VERSION_1 = 'v1'
1358 VERSION_2 = 'v2'
1358 VERSION_2 = 'v2'
1359 VERSIONS = [VERSION_1, VERSION_2]
1359 VERSIONS = [VERSION_1, VERSION_2]
1360
1360
1361 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1361 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1362 user_id = Column("user_id", Integer(), ForeignKey('users.user_id',ondelete='SET NULL'), nullable=True, unique=None, default=None)
1362 user_id = Column("user_id", Integer(), ForeignKey('users.user_id',ondelete='SET NULL'), nullable=True, unique=None, default=None)
1363 username = Column("username", String(255), nullable=True, unique=None, default=None)
1363 username = Column("username", String(255), nullable=True, unique=None, default=None)
1364 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id', ondelete='SET NULL'), nullable=True, unique=None, default=None)
1364 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id', ondelete='SET NULL'), nullable=True, unique=None, default=None)
1365 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1365 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1366 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1366 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1367 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1367 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1368 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1368 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1369
1369
1370 version = Column("version", String(255), nullable=True, default=VERSION_1)
1370 version = Column("version", String(255), nullable=True, default=VERSION_1)
1371 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1371 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1372 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1372 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1373
1373
1374 def __unicode__(self):
1374 def __unicode__(self):
1375 return u"<%s('id:%s:%s')>" % (
1375 return u"<%s('id:%s:%s')>" % (
1376 self.__class__.__name__, self.repository_name, self.action)
1376 self.__class__.__name__, self.repository_name, self.action)
1377
1377
1378 def __json__(self):
1378 def __json__(self):
1379 return {
1379 return {
1380 'user_id': self.user_id,
1380 'user_id': self.user_id,
1381 'username': self.username,
1381 'username': self.username,
1382 'repository_id': self.repository_id,
1382 'repository_id': self.repository_id,
1383 'repository_name': self.repository_name,
1383 'repository_name': self.repository_name,
1384 'user_ip': self.user_ip,
1384 'user_ip': self.user_ip,
1385 'action_date': self.action_date,
1385 'action_date': self.action_date,
1386 'action': self.action,
1386 'action': self.action,
1387 }
1387 }
1388
1388
1389 @hybrid_property
1389 @hybrid_property
1390 def entry_id(self):
1390 def entry_id(self):
1391 return self.user_log_id
1391 return self.user_log_id
1392
1392
1393 @property
1393 @property
1394 def action_as_day(self):
1394 def action_as_day(self):
1395 return datetime.date(*self.action_date.timetuple()[:3])
1395 return datetime.date(*self.action_date.timetuple()[:3])
1396
1396
1397 user = relationship('User')
1397 user = relationship('User')
1398 repository = relationship('Repository', cascade='')
1398 repository = relationship('Repository', cascade='')
1399
1399
1400
1400
1401 class UserGroup(Base, BaseModel):
1401 class UserGroup(Base, BaseModel):
1402 __tablename__ = 'users_groups'
1402 __tablename__ = 'users_groups'
1403 __table_args__ = (
1403 __table_args__ = (
1404 base_table_args,
1404 base_table_args,
1405 )
1405 )
1406
1406
1407 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1407 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1408 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1408 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1409 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1409 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1410 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1410 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1411 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1411 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1412 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1412 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1413 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1413 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1414 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1414 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1415
1415
1416 members = relationship('UserGroupMember', cascade="all, delete-orphan", lazy="joined")
1416 members = relationship('UserGroupMember', cascade="all, delete-orphan", lazy="joined")
1417 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1417 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1418 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1418 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1419 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1419 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1420 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1420 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1421 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1421 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1422
1422
1423 user_group_review_rules = relationship('RepoReviewRuleUserGroup', cascade='all')
1423 user_group_review_rules = relationship('RepoReviewRuleUserGroup', cascade='all')
1424 user = relationship('User', primaryjoin="User.user_id==UserGroup.user_id")
1424 user = relationship('User', primaryjoin="User.user_id==UserGroup.user_id")
1425
1425
1426 @classmethod
1426 @classmethod
1427 def _load_group_data(cls, column):
1427 def _load_group_data(cls, column):
1428 if not column:
1428 if not column:
1429 return {}
1429 return {}
1430
1430
1431 try:
1431 try:
1432 return json.loads(column) or {}
1432 return json.loads(column) or {}
1433 except TypeError:
1433 except TypeError:
1434 return {}
1434 return {}
1435
1435
1436 @hybrid_property
1436 @hybrid_property
1437 def description_safe(self):
1437 def description_safe(self):
1438 from rhodecode.lib import helpers as h
1438 from rhodecode.lib import helpers as h
1439 return h.escape(self.user_group_description)
1439 return h.escape(self.user_group_description)
1440
1440
1441 @hybrid_property
1441 @hybrid_property
1442 def group_data(self):
1442 def group_data(self):
1443 return self._load_group_data(self._group_data)
1443 return self._load_group_data(self._group_data)
1444
1444
1445 @group_data.expression
1445 @group_data.expression
1446 def group_data(self, **kwargs):
1446 def group_data(self, **kwargs):
1447 return self._group_data
1447 return self._group_data
1448
1448
1449 @group_data.setter
1449 @group_data.setter
1450 def group_data(self, val):
1450 def group_data(self, val):
1451 try:
1451 try:
1452 self._group_data = json.dumps(val)
1452 self._group_data = json.dumps(val)
1453 except Exception:
1453 except Exception:
1454 log.error(traceback.format_exc())
1454 log.error(traceback.format_exc())
1455
1455
1456 @classmethod
1456 @classmethod
1457 def _load_sync(cls, group_data):
1457 def _load_sync(cls, group_data):
1458 if group_data:
1458 if group_data:
1459 return group_data.get('extern_type')
1459 return group_data.get('extern_type')
1460
1460
1461 @property
1461 @property
1462 def sync(self):
1462 def sync(self):
1463 return self._load_sync(self.group_data)
1463 return self._load_sync(self.group_data)
1464
1464
1465 def __unicode__(self):
1465 def __unicode__(self):
1466 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1466 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1467 self.users_group_id,
1467 self.users_group_id,
1468 self.users_group_name)
1468 self.users_group_name)
1469
1469
1470 @classmethod
1470 @classmethod
1471 def get_by_group_name(cls, group_name, cache=False,
1471 def get_by_group_name(cls, group_name, cache=False,
1472 case_insensitive=False):
1472 case_insensitive=False):
1473 if case_insensitive:
1473 if case_insensitive:
1474 q = cls.query().filter(func.lower(cls.users_group_name) ==
1474 q = cls.query().filter(func.lower(cls.users_group_name) ==
1475 func.lower(group_name))
1475 func.lower(group_name))
1476
1476
1477 else:
1477 else:
1478 q = cls.query().filter(cls.users_group_name == group_name)
1478 q = cls.query().filter(cls.users_group_name == group_name)
1479 if cache:
1479 if cache:
1480 q = q.options(
1480 q = q.options(
1481 FromCache("sql_cache_short", "get_group_%s" % _hash_key(group_name)))
1481 FromCache("sql_cache_short", "get_group_%s" % _hash_key(group_name)))
1482 return q.scalar()
1482 return q.scalar()
1483
1483
1484 @classmethod
1484 @classmethod
1485 def get(cls, user_group_id, cache=False):
1485 def get(cls, user_group_id, cache=False):
1486 if not user_group_id:
1486 if not user_group_id:
1487 return
1487 return
1488
1488
1489 user_group = cls.query()
1489 user_group = cls.query()
1490 if cache:
1490 if cache:
1491 user_group = user_group.options(
1491 user_group = user_group.options(
1492 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1492 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1493 return user_group.get(user_group_id)
1493 return user_group.get(user_group_id)
1494
1494
1495 def permissions(self, with_admins=True, with_owner=True,
1495 def permissions(self, with_admins=True, with_owner=True,
1496 expand_from_user_groups=False):
1496 expand_from_user_groups=False):
1497 """
1497 """
1498 Permissions for user groups
1498 Permissions for user groups
1499 """
1499 """
1500 _admin_perm = 'usergroup.admin'
1500 _admin_perm = 'usergroup.admin'
1501
1501
1502 owner_row = []
1502 owner_row = []
1503 if with_owner:
1503 if with_owner:
1504 usr = AttributeDict(self.user.get_dict())
1504 usr = AttributeDict(self.user.get_dict())
1505 usr.owner_row = True
1505 usr.owner_row = True
1506 usr.permission = _admin_perm
1506 usr.permission = _admin_perm
1507 owner_row.append(usr)
1507 owner_row.append(usr)
1508
1508
1509 super_admin_ids = []
1509 super_admin_ids = []
1510 super_admin_rows = []
1510 super_admin_rows = []
1511 if with_admins:
1511 if with_admins:
1512 for usr in User.get_all_super_admins():
1512 for usr in User.get_all_super_admins():
1513 super_admin_ids.append(usr.user_id)
1513 super_admin_ids.append(usr.user_id)
1514 # if this admin is also owner, don't double the record
1514 # if this admin is also owner, don't double the record
1515 if usr.user_id == owner_row[0].user_id:
1515 if usr.user_id == owner_row[0].user_id:
1516 owner_row[0].admin_row = True
1516 owner_row[0].admin_row = True
1517 else:
1517 else:
1518 usr = AttributeDict(usr.get_dict())
1518 usr = AttributeDict(usr.get_dict())
1519 usr.admin_row = True
1519 usr.admin_row = True
1520 usr.permission = _admin_perm
1520 usr.permission = _admin_perm
1521 super_admin_rows.append(usr)
1521 super_admin_rows.append(usr)
1522
1522
1523 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1523 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1524 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1524 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1525 joinedload(UserUserGroupToPerm.user),
1525 joinedload(UserUserGroupToPerm.user),
1526 joinedload(UserUserGroupToPerm.permission),)
1526 joinedload(UserUserGroupToPerm.permission),)
1527
1527
1528 # get owners and admins and permissions. We do a trick of re-writing
1528 # get owners and admins and permissions. We do a trick of re-writing
1529 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1529 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1530 # has a global reference and changing one object propagates to all
1530 # has a global reference and changing one object propagates to all
1531 # others. This means if admin is also an owner admin_row that change
1531 # others. This means if admin is also an owner admin_row that change
1532 # would propagate to both objects
1532 # would propagate to both objects
1533 perm_rows = []
1533 perm_rows = []
1534 for _usr in q.all():
1534 for _usr in q.all():
1535 usr = AttributeDict(_usr.user.get_dict())
1535 usr = AttributeDict(_usr.user.get_dict())
1536 # if this user is also owner/admin, mark as duplicate record
1536 # if this user is also owner/admin, mark as duplicate record
1537 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
1537 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
1538 usr.duplicate_perm = True
1538 usr.duplicate_perm = True
1539 usr.permission = _usr.permission.permission_name
1539 usr.permission = _usr.permission.permission_name
1540 perm_rows.append(usr)
1540 perm_rows.append(usr)
1541
1541
1542 # filter the perm rows by 'default' first and then sort them by
1542 # filter the perm rows by 'default' first and then sort them by
1543 # admin,write,read,none permissions sorted again alphabetically in
1543 # admin,write,read,none permissions sorted again alphabetically in
1544 # each group
1544 # each group
1545 perm_rows = sorted(perm_rows, key=display_user_sort)
1545 perm_rows = sorted(perm_rows, key=display_user_sort)
1546
1546
1547 user_groups_rows = []
1547 user_groups_rows = []
1548 if expand_from_user_groups:
1548 if expand_from_user_groups:
1549 for ug in self.permission_user_groups(with_members=True):
1549 for ug in self.permission_user_groups(with_members=True):
1550 for user_data in ug.members:
1550 for user_data in ug.members:
1551 user_groups_rows.append(user_data)
1551 user_groups_rows.append(user_data)
1552
1552
1553 return super_admin_rows + owner_row + perm_rows + user_groups_rows
1553 return super_admin_rows + owner_row + perm_rows + user_groups_rows
1554
1554
1555 def permission_user_groups(self, with_members=False):
1555 def permission_user_groups(self, with_members=False):
1556 q = UserGroupUserGroupToPerm.query()\
1556 q = UserGroupUserGroupToPerm.query()\
1557 .filter(UserGroupUserGroupToPerm.target_user_group == self)
1557 .filter(UserGroupUserGroupToPerm.target_user_group == self)
1558 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1558 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1559 joinedload(UserGroupUserGroupToPerm.target_user_group),
1559 joinedload(UserGroupUserGroupToPerm.target_user_group),
1560 joinedload(UserGroupUserGroupToPerm.permission),)
1560 joinedload(UserGroupUserGroupToPerm.permission),)
1561
1561
1562 perm_rows = []
1562 perm_rows = []
1563 for _user_group in q.all():
1563 for _user_group in q.all():
1564 entry = AttributeDict(_user_group.user_group.get_dict())
1564 entry = AttributeDict(_user_group.user_group.get_dict())
1565 entry.permission = _user_group.permission.permission_name
1565 entry.permission = _user_group.permission.permission_name
1566 if with_members:
1566 if with_members:
1567 entry.members = [x.user.get_dict()
1567 entry.members = [x.user.get_dict()
1568 for x in _user_group.user_group.members]
1568 for x in _user_group.user_group.members]
1569 perm_rows.append(entry)
1569 perm_rows.append(entry)
1570
1570
1571 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1571 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1572 return perm_rows
1572 return perm_rows
1573
1573
1574 def _get_default_perms(self, user_group, suffix=''):
1574 def _get_default_perms(self, user_group, suffix=''):
1575 from rhodecode.model.permission import PermissionModel
1575 from rhodecode.model.permission import PermissionModel
1576 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1576 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1577
1577
1578 def get_default_perms(self, suffix=''):
1578 def get_default_perms(self, suffix=''):
1579 return self._get_default_perms(self, suffix)
1579 return self._get_default_perms(self, suffix)
1580
1580
1581 def get_api_data(self, with_group_members=True, include_secrets=False):
1581 def get_api_data(self, with_group_members=True, include_secrets=False):
1582 """
1582 """
1583 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1583 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1584 basically forwarded.
1584 basically forwarded.
1585
1585
1586 """
1586 """
1587 user_group = self
1587 user_group = self
1588 data = {
1588 data = {
1589 'users_group_id': user_group.users_group_id,
1589 'users_group_id': user_group.users_group_id,
1590 'group_name': user_group.users_group_name,
1590 'group_name': user_group.users_group_name,
1591 'group_description': user_group.user_group_description,
1591 'group_description': user_group.user_group_description,
1592 'active': user_group.users_group_active,
1592 'active': user_group.users_group_active,
1593 'owner': user_group.user.username,
1593 'owner': user_group.user.username,
1594 'sync': user_group.sync,
1594 'sync': user_group.sync,
1595 'owner_email': user_group.user.email,
1595 'owner_email': user_group.user.email,
1596 }
1596 }
1597
1597
1598 if with_group_members:
1598 if with_group_members:
1599 users = []
1599 users = []
1600 for user in user_group.members:
1600 for user in user_group.members:
1601 user = user.user
1601 user = user.user
1602 users.append(user.get_api_data(include_secrets=include_secrets))
1602 users.append(user.get_api_data(include_secrets=include_secrets))
1603 data['users'] = users
1603 data['users'] = users
1604
1604
1605 return data
1605 return data
1606
1606
1607
1607
1608 class UserGroupMember(Base, BaseModel):
1608 class UserGroupMember(Base, BaseModel):
1609 __tablename__ = 'users_groups_members'
1609 __tablename__ = 'users_groups_members'
1610 __table_args__ = (
1610 __table_args__ = (
1611 base_table_args,
1611 base_table_args,
1612 )
1612 )
1613
1613
1614 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1614 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1615 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1615 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1616 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1616 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1617
1617
1618 user = relationship('User', lazy='joined')
1618 user = relationship('User', lazy='joined')
1619 users_group = relationship('UserGroup')
1619 users_group = relationship('UserGroup')
1620
1620
1621 def __init__(self, gr_id='', u_id=''):
1621 def __init__(self, gr_id='', u_id=''):
1622 self.users_group_id = gr_id
1622 self.users_group_id = gr_id
1623 self.user_id = u_id
1623 self.user_id = u_id
1624
1624
1625
1625
1626 class RepositoryField(Base, BaseModel):
1626 class RepositoryField(Base, BaseModel):
1627 __tablename__ = 'repositories_fields'
1627 __tablename__ = 'repositories_fields'
1628 __table_args__ = (
1628 __table_args__ = (
1629 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1629 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1630 base_table_args,
1630 base_table_args,
1631 )
1631 )
1632
1632
1633 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1633 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1634
1634
1635 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1635 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1636 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1636 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1637 field_key = Column("field_key", String(250))
1637 field_key = Column("field_key", String(250))
1638 field_label = Column("field_label", String(1024), nullable=False)
1638 field_label = Column("field_label", String(1024), nullable=False)
1639 field_value = Column("field_value", String(10000), nullable=False)
1639 field_value = Column("field_value", String(10000), nullable=False)
1640 field_desc = Column("field_desc", String(1024), nullable=False)
1640 field_desc = Column("field_desc", String(1024), nullable=False)
1641 field_type = Column("field_type", String(255), nullable=False, unique=None)
1641 field_type = Column("field_type", String(255), nullable=False, unique=None)
1642 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1642 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1643
1643
1644 repository = relationship('Repository')
1644 repository = relationship('Repository')
1645
1645
1646 @property
1646 @property
1647 def field_key_prefixed(self):
1647 def field_key_prefixed(self):
1648 return 'ex_%s' % self.field_key
1648 return 'ex_%s' % self.field_key
1649
1649
1650 @classmethod
1650 @classmethod
1651 def un_prefix_key(cls, key):
1651 def un_prefix_key(cls, key):
1652 if key.startswith(cls.PREFIX):
1652 if key.startswith(cls.PREFIX):
1653 return key[len(cls.PREFIX):]
1653 return key[len(cls.PREFIX):]
1654 return key
1654 return key
1655
1655
1656 @classmethod
1656 @classmethod
1657 def get_by_key_name(cls, key, repo):
1657 def get_by_key_name(cls, key, repo):
1658 row = cls.query()\
1658 row = cls.query()\
1659 .filter(cls.repository == repo)\
1659 .filter(cls.repository == repo)\
1660 .filter(cls.field_key == key).scalar()
1660 .filter(cls.field_key == key).scalar()
1661 return row
1661 return row
1662
1662
1663
1663
1664 class Repository(Base, BaseModel):
1664 class Repository(Base, BaseModel):
1665 __tablename__ = 'repositories'
1665 __tablename__ = 'repositories'
1666 __table_args__ = (
1666 __table_args__ = (
1667 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1667 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1668 base_table_args,
1668 base_table_args,
1669 )
1669 )
1670 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1670 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1671 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1671 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1672 DEFAULT_CLONE_URI_SSH = 'ssh://{sys_user}@{hostname}/{repo}'
1672 DEFAULT_CLONE_URI_SSH = 'ssh://{sys_user}@{hostname}/{repo}'
1673
1673
1674 STATE_CREATED = 'repo_state_created'
1674 STATE_CREATED = 'repo_state_created'
1675 STATE_PENDING = 'repo_state_pending'
1675 STATE_PENDING = 'repo_state_pending'
1676 STATE_ERROR = 'repo_state_error'
1676 STATE_ERROR = 'repo_state_error'
1677
1677
1678 LOCK_AUTOMATIC = 'lock_auto'
1678 LOCK_AUTOMATIC = 'lock_auto'
1679 LOCK_API = 'lock_api'
1679 LOCK_API = 'lock_api'
1680 LOCK_WEB = 'lock_web'
1680 LOCK_WEB = 'lock_web'
1681 LOCK_PULL = 'lock_pull'
1681 LOCK_PULL = 'lock_pull'
1682
1682
1683 NAME_SEP = URL_SEP
1683 NAME_SEP = URL_SEP
1684
1684
1685 repo_id = Column(
1685 repo_id = Column(
1686 "repo_id", Integer(), nullable=False, unique=True, default=None,
1686 "repo_id", Integer(), nullable=False, unique=True, default=None,
1687 primary_key=True)
1687 primary_key=True)
1688 _repo_name = Column(
1688 _repo_name = Column(
1689 "repo_name", Text(), nullable=False, default=None)
1689 "repo_name", Text(), nullable=False, default=None)
1690 repo_name_hash = Column(
1690 repo_name_hash = Column(
1691 "repo_name_hash", String(255), nullable=False, unique=True)
1691 "repo_name_hash", String(255), nullable=False, unique=True)
1692 repo_state = Column("repo_state", String(255), nullable=True)
1692 repo_state = Column("repo_state", String(255), nullable=True)
1693
1693
1694 clone_uri = Column(
1694 clone_uri = Column(
1695 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1695 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1696 default=None)
1696 default=None)
1697 push_uri = Column(
1697 push_uri = Column(
1698 "push_uri", EncryptedTextValue(), nullable=True, unique=False,
1698 "push_uri", EncryptedTextValue(), nullable=True, unique=False,
1699 default=None)
1699 default=None)
1700 repo_type = Column(
1700 repo_type = Column(
1701 "repo_type", String(255), nullable=False, unique=False, default=None)
1701 "repo_type", String(255), nullable=False, unique=False, default=None)
1702 user_id = Column(
1702 user_id = Column(
1703 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1703 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1704 unique=False, default=None)
1704 unique=False, default=None)
1705 private = Column(
1705 private = Column(
1706 "private", Boolean(), nullable=True, unique=None, default=None)
1706 "private", Boolean(), nullable=True, unique=None, default=None)
1707 archived = Column(
1707 archived = Column(
1708 "archived", Boolean(), nullable=True, unique=None, default=None)
1708 "archived", Boolean(), nullable=True, unique=None, default=None)
1709 enable_statistics = Column(
1709 enable_statistics = Column(
1710 "statistics", Boolean(), nullable=True, unique=None, default=True)
1710 "statistics", Boolean(), nullable=True, unique=None, default=True)
1711 enable_downloads = Column(
1711 enable_downloads = Column(
1712 "downloads", Boolean(), nullable=True, unique=None, default=True)
1712 "downloads", Boolean(), nullable=True, unique=None, default=True)
1713 description = Column(
1713 description = Column(
1714 "description", String(10000), nullable=True, unique=None, default=None)
1714 "description", String(10000), nullable=True, unique=None, default=None)
1715 created_on = Column(
1715 created_on = Column(
1716 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1716 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1717 default=datetime.datetime.now)
1717 default=datetime.datetime.now)
1718 updated_on = Column(
1718 updated_on = Column(
1719 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1719 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1720 default=datetime.datetime.now)
1720 default=datetime.datetime.now)
1721 _landing_revision = Column(
1721 _landing_revision = Column(
1722 "landing_revision", String(255), nullable=False, unique=False,
1722 "landing_revision", String(255), nullable=False, unique=False,
1723 default=None)
1723 default=None)
1724 enable_locking = Column(
1724 enable_locking = Column(
1725 "enable_locking", Boolean(), nullable=False, unique=None,
1725 "enable_locking", Boolean(), nullable=False, unique=None,
1726 default=False)
1726 default=False)
1727 _locked = Column(
1727 _locked = Column(
1728 "locked", String(255), nullable=True, unique=False, default=None)
1728 "locked", String(255), nullable=True, unique=False, default=None)
1729 _changeset_cache = Column(
1729 _changeset_cache = Column(
1730 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1730 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1731
1731
1732 fork_id = Column(
1732 fork_id = Column(
1733 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1733 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1734 nullable=True, unique=False, default=None)
1734 nullable=True, unique=False, default=None)
1735 group_id = Column(
1735 group_id = Column(
1736 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1736 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1737 unique=False, default=None)
1737 unique=False, default=None)
1738
1738
1739 user = relationship('User', lazy='joined')
1739 user = relationship('User', lazy='joined')
1740 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1740 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1741 group = relationship('RepoGroup', lazy='joined')
1741 group = relationship('RepoGroup', lazy='joined')
1742 repo_to_perm = relationship(
1742 repo_to_perm = relationship(
1743 'UserRepoToPerm', cascade='all',
1743 'UserRepoToPerm', cascade='all',
1744 order_by='UserRepoToPerm.repo_to_perm_id')
1744 order_by='UserRepoToPerm.repo_to_perm_id')
1745 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1745 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1746 stats = relationship('Statistics', cascade='all', uselist=False)
1746 stats = relationship('Statistics', cascade='all', uselist=False)
1747
1747
1748 followers = relationship(
1748 followers = relationship(
1749 'UserFollowing',
1749 'UserFollowing',
1750 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1750 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1751 cascade='all')
1751 cascade='all')
1752 extra_fields = relationship(
1752 extra_fields = relationship(
1753 'RepositoryField', cascade="all, delete-orphan")
1753 'RepositoryField', cascade="all, delete-orphan")
1754 logs = relationship('UserLog')
1754 logs = relationship('UserLog')
1755 comments = relationship(
1755 comments = relationship(
1756 'ChangesetComment', cascade="all, delete-orphan")
1756 'ChangesetComment', cascade="all, delete-orphan")
1757 pull_requests_source = relationship(
1757 pull_requests_source = relationship(
1758 'PullRequest',
1758 'PullRequest',
1759 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1759 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1760 cascade="all, delete-orphan")
1760 cascade="all, delete-orphan")
1761 pull_requests_target = relationship(
1761 pull_requests_target = relationship(
1762 'PullRequest',
1762 'PullRequest',
1763 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1763 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1764 cascade="all, delete-orphan")
1764 cascade="all, delete-orphan")
1765 ui = relationship('RepoRhodeCodeUi', cascade="all")
1765 ui = relationship('RepoRhodeCodeUi', cascade="all")
1766 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1766 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1767 integrations = relationship('Integration', cascade="all, delete-orphan")
1767 integrations = relationship('Integration', cascade="all, delete-orphan")
1768
1768
1769 scoped_tokens = relationship('UserApiKeys', cascade="all")
1769 scoped_tokens = relationship('UserApiKeys', cascade="all")
1770
1770
1771 # no cascade, set NULL
1771 # no cascade, set NULL
1772 artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_id==Repository.repo_id')
1772 artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_id==Repository.repo_id')
1773
1773
1774 def __unicode__(self):
1774 def __unicode__(self):
1775 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1775 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1776 safe_unicode(self.repo_name))
1776 safe_unicode(self.repo_name))
1777
1777
1778 @hybrid_property
1778 @hybrid_property
1779 def description_safe(self):
1779 def description_safe(self):
1780 from rhodecode.lib import helpers as h
1780 from rhodecode.lib import helpers as h
1781 return h.escape(self.description)
1781 return h.escape(self.description)
1782
1782
1783 @hybrid_property
1783 @hybrid_property
1784 def landing_rev(self):
1784 def landing_rev(self):
1785 # always should return [rev_type, rev], e.g ['branch', 'master']
1785 # always should return [rev_type, rev], e.g ['branch', 'master']
1786 if self._landing_revision:
1786 if self._landing_revision:
1787 _rev_info = self._landing_revision.split(':')
1787 _rev_info = self._landing_revision.split(':')
1788 if len(_rev_info) < 2:
1788 if len(_rev_info) < 2:
1789 _rev_info.insert(0, 'rev')
1789 _rev_info.insert(0, 'rev')
1790 return [_rev_info[0], _rev_info[1]]
1790 return [_rev_info[0], _rev_info[1]]
1791 return [None, None]
1791 return [None, None]
1792
1792
1793 @property
1793 @property
1794 def landing_ref_type(self):
1794 def landing_ref_type(self):
1795 return self.landing_rev[0]
1795 return self.landing_rev[0]
1796
1796
1797 @property
1797 @property
1798 def landing_ref_name(self):
1798 def landing_ref_name(self):
1799 return self.landing_rev[1]
1799 return self.landing_rev[1]
1800
1800
1801 @landing_rev.setter
1801 @landing_rev.setter
1802 def landing_rev(self, val):
1802 def landing_rev(self, val):
1803 if ':' not in val:
1803 if ':' not in val:
1804 raise ValueError('value must be delimited with `:` and consist '
1804 raise ValueError('value must be delimited with `:` and consist '
1805 'of <rev_type>:<rev>, got %s instead' % val)
1805 'of <rev_type>:<rev>, got %s instead' % val)
1806 self._landing_revision = val
1806 self._landing_revision = val
1807
1807
1808 @hybrid_property
1808 @hybrid_property
1809 def locked(self):
1809 def locked(self):
1810 if self._locked:
1810 if self._locked:
1811 user_id, timelocked, reason = self._locked.split(':')
1811 user_id, timelocked, reason = self._locked.split(':')
1812 lock_values = int(user_id), timelocked, reason
1812 lock_values = int(user_id), timelocked, reason
1813 else:
1813 else:
1814 lock_values = [None, None, None]
1814 lock_values = [None, None, None]
1815 return lock_values
1815 return lock_values
1816
1816
1817 @locked.setter
1817 @locked.setter
1818 def locked(self, val):
1818 def locked(self, val):
1819 if val and isinstance(val, (list, tuple)):
1819 if val and isinstance(val, (list, tuple)):
1820 self._locked = ':'.join(map(str, val))
1820 self._locked = ':'.join(map(str, val))
1821 else:
1821 else:
1822 self._locked = None
1822 self._locked = None
1823
1823
1824 @classmethod
1824 @classmethod
1825 def _load_changeset_cache(cls, repo_id, changeset_cache_raw):
1825 def _load_changeset_cache(cls, repo_id, changeset_cache_raw):
1826 from rhodecode.lib.vcs.backends.base import EmptyCommit
1826 from rhodecode.lib.vcs.backends.base import EmptyCommit
1827 dummy = EmptyCommit().__json__()
1827 dummy = EmptyCommit().__json__()
1828 if not changeset_cache_raw:
1828 if not changeset_cache_raw:
1829 dummy['source_repo_id'] = repo_id
1829 dummy['source_repo_id'] = repo_id
1830 return json.loads(json.dumps(dummy))
1830 return json.loads(json.dumps(dummy))
1831
1831
1832 try:
1832 try:
1833 return json.loads(changeset_cache_raw)
1833 return json.loads(changeset_cache_raw)
1834 except TypeError:
1834 except TypeError:
1835 return dummy
1835 return dummy
1836 except Exception:
1836 except Exception:
1837 log.error(traceback.format_exc())
1837 log.error(traceback.format_exc())
1838 return dummy
1838 return dummy
1839
1839
1840 @hybrid_property
1840 @hybrid_property
1841 def changeset_cache(self):
1841 def changeset_cache(self):
1842 return self._load_changeset_cache(self.repo_id, self._changeset_cache)
1842 return self._load_changeset_cache(self.repo_id, self._changeset_cache)
1843
1843
1844 @changeset_cache.setter
1844 @changeset_cache.setter
1845 def changeset_cache(self, val):
1845 def changeset_cache(self, val):
1846 try:
1846 try:
1847 self._changeset_cache = json.dumps(val)
1847 self._changeset_cache = json.dumps(val)
1848 except Exception:
1848 except Exception:
1849 log.error(traceback.format_exc())
1849 log.error(traceback.format_exc())
1850
1850
1851 @hybrid_property
1851 @hybrid_property
1852 def repo_name(self):
1852 def repo_name(self):
1853 return self._repo_name
1853 return self._repo_name
1854
1854
1855 @repo_name.setter
1855 @repo_name.setter
1856 def repo_name(self, value):
1856 def repo_name(self, value):
1857 self._repo_name = value
1857 self._repo_name = value
1858 self.repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1858 self.repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1859
1859
1860 @classmethod
1860 @classmethod
1861 def normalize_repo_name(cls, repo_name):
1861 def normalize_repo_name(cls, repo_name):
1862 """
1862 """
1863 Normalizes os specific repo_name to the format internally stored inside
1863 Normalizes os specific repo_name to the format internally stored inside
1864 database using URL_SEP
1864 database using URL_SEP
1865
1865
1866 :param cls:
1866 :param cls:
1867 :param repo_name:
1867 :param repo_name:
1868 """
1868 """
1869 return cls.NAME_SEP.join(repo_name.split(os.sep))
1869 return cls.NAME_SEP.join(repo_name.split(os.sep))
1870
1870
1871 @classmethod
1871 @classmethod
1872 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1872 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1873 session = Session()
1873 session = Session()
1874 q = session.query(cls).filter(cls.repo_name == repo_name)
1874 q = session.query(cls).filter(cls.repo_name == repo_name)
1875
1875
1876 if cache:
1876 if cache:
1877 if identity_cache:
1877 if identity_cache:
1878 val = cls.identity_cache(session, 'repo_name', repo_name)
1878 val = cls.identity_cache(session, 'repo_name', repo_name)
1879 if val:
1879 if val:
1880 return val
1880 return val
1881 else:
1881 else:
1882 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
1882 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
1883 q = q.options(
1883 q = q.options(
1884 FromCache("sql_cache_short", cache_key))
1884 FromCache("sql_cache_short", cache_key))
1885
1885
1886 return q.scalar()
1886 return q.scalar()
1887
1887
1888 @classmethod
1888 @classmethod
1889 def get_by_id_or_repo_name(cls, repoid):
1889 def get_by_id_or_repo_name(cls, repoid):
1890 if isinstance(repoid, (int, long)):
1890 if isinstance(repoid, (int, long)):
1891 try:
1891 try:
1892 repo = cls.get(repoid)
1892 repo = cls.get(repoid)
1893 except ValueError:
1893 except ValueError:
1894 repo = None
1894 repo = None
1895 else:
1895 else:
1896 repo = cls.get_by_repo_name(repoid)
1896 repo = cls.get_by_repo_name(repoid)
1897 return repo
1897 return repo
1898
1898
1899 @classmethod
1899 @classmethod
1900 def get_by_full_path(cls, repo_full_path):
1900 def get_by_full_path(cls, repo_full_path):
1901 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1901 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1902 repo_name = cls.normalize_repo_name(repo_name)
1902 repo_name = cls.normalize_repo_name(repo_name)
1903 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1903 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1904
1904
1905 @classmethod
1905 @classmethod
1906 def get_repo_forks(cls, repo_id):
1906 def get_repo_forks(cls, repo_id):
1907 return cls.query().filter(Repository.fork_id == repo_id)
1907 return cls.query().filter(Repository.fork_id == repo_id)
1908
1908
1909 @classmethod
1909 @classmethod
1910 def base_path(cls):
1910 def base_path(cls):
1911 """
1911 """
1912 Returns base path when all repos are stored
1912 Returns base path when all repos are stored
1913
1913
1914 :param cls:
1914 :param cls:
1915 """
1915 """
1916 q = Session().query(RhodeCodeUi)\
1916 q = Session().query(RhodeCodeUi)\
1917 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1917 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1918 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1918 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1919 return q.one().ui_value
1919 return q.one().ui_value
1920
1920
1921 @classmethod
1921 @classmethod
1922 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1922 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1923 case_insensitive=True, archived=False):
1923 case_insensitive=True, archived=False):
1924 q = Repository.query()
1924 q = Repository.query()
1925
1925
1926 if not archived:
1926 if not archived:
1927 q = q.filter(Repository.archived.isnot(true()))
1927 q = q.filter(Repository.archived.isnot(true()))
1928
1928
1929 if not isinstance(user_id, Optional):
1929 if not isinstance(user_id, Optional):
1930 q = q.filter(Repository.user_id == user_id)
1930 q = q.filter(Repository.user_id == user_id)
1931
1931
1932 if not isinstance(group_id, Optional):
1932 if not isinstance(group_id, Optional):
1933 q = q.filter(Repository.group_id == group_id)
1933 q = q.filter(Repository.group_id == group_id)
1934
1934
1935 if case_insensitive:
1935 if case_insensitive:
1936 q = q.order_by(func.lower(Repository.repo_name))
1936 q = q.order_by(func.lower(Repository.repo_name))
1937 else:
1937 else:
1938 q = q.order_by(Repository.repo_name)
1938 q = q.order_by(Repository.repo_name)
1939
1939
1940 return q.all()
1940 return q.all()
1941
1941
1942 @property
1942 @property
1943 def repo_uid(self):
1943 def repo_uid(self):
1944 return '_{}'.format(self.repo_id)
1944 return '_{}'.format(self.repo_id)
1945
1945
1946 @property
1946 @property
1947 def forks(self):
1947 def forks(self):
1948 """
1948 """
1949 Return forks of this repo
1949 Return forks of this repo
1950 """
1950 """
1951 return Repository.get_repo_forks(self.repo_id)
1951 return Repository.get_repo_forks(self.repo_id)
1952
1952
1953 @property
1953 @property
1954 def parent(self):
1954 def parent(self):
1955 """
1955 """
1956 Returns fork parent
1956 Returns fork parent
1957 """
1957 """
1958 return self.fork
1958 return self.fork
1959
1959
1960 @property
1960 @property
1961 def just_name(self):
1961 def just_name(self):
1962 return self.repo_name.split(self.NAME_SEP)[-1]
1962 return self.repo_name.split(self.NAME_SEP)[-1]
1963
1963
1964 @property
1964 @property
1965 def groups_with_parents(self):
1965 def groups_with_parents(self):
1966 groups = []
1966 groups = []
1967 if self.group is None:
1967 if self.group is None:
1968 return groups
1968 return groups
1969
1969
1970 cur_gr = self.group
1970 cur_gr = self.group
1971 groups.insert(0, cur_gr)
1971 groups.insert(0, cur_gr)
1972 while 1:
1972 while 1:
1973 gr = getattr(cur_gr, 'parent_group', None)
1973 gr = getattr(cur_gr, 'parent_group', None)
1974 cur_gr = cur_gr.parent_group
1974 cur_gr = cur_gr.parent_group
1975 if gr is None:
1975 if gr is None:
1976 break
1976 break
1977 groups.insert(0, gr)
1977 groups.insert(0, gr)
1978
1978
1979 return groups
1979 return groups
1980
1980
1981 @property
1981 @property
1982 def groups_and_repo(self):
1982 def groups_and_repo(self):
1983 return self.groups_with_parents, self
1983 return self.groups_with_parents, self
1984
1984
1985 @LazyProperty
1985 @LazyProperty
1986 def repo_path(self):
1986 def repo_path(self):
1987 """
1987 """
1988 Returns base full path for that repository means where it actually
1988 Returns base full path for that repository means where it actually
1989 exists on a filesystem
1989 exists on a filesystem
1990 """
1990 """
1991 q = Session().query(RhodeCodeUi).filter(
1991 q = Session().query(RhodeCodeUi).filter(
1992 RhodeCodeUi.ui_key == self.NAME_SEP)
1992 RhodeCodeUi.ui_key == self.NAME_SEP)
1993 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1993 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1994 return q.one().ui_value
1994 return q.one().ui_value
1995
1995
1996 @property
1996 @property
1997 def repo_full_path(self):
1997 def repo_full_path(self):
1998 p = [self.repo_path]
1998 p = [self.repo_path]
1999 # we need to split the name by / since this is how we store the
1999 # we need to split the name by / since this is how we store the
2000 # names in the database, but that eventually needs to be converted
2000 # names in the database, but that eventually needs to be converted
2001 # into a valid system path
2001 # into a valid system path
2002 p += self.repo_name.split(self.NAME_SEP)
2002 p += self.repo_name.split(self.NAME_SEP)
2003 return os.path.join(*map(safe_unicode, p))
2003 return os.path.join(*map(safe_unicode, p))
2004
2004
2005 @property
2005 @property
2006 def cache_keys(self):
2006 def cache_keys(self):
2007 """
2007 """
2008 Returns associated cache keys for that repo
2008 Returns associated cache keys for that repo
2009 """
2009 """
2010 invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
2010 invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
2011 repo_id=self.repo_id)
2011 repo_id=self.repo_id)
2012 return CacheKey.query()\
2012 return CacheKey.query()\
2013 .filter(CacheKey.cache_args == invalidation_namespace)\
2013 .filter(CacheKey.cache_args == invalidation_namespace)\
2014 .order_by(CacheKey.cache_key)\
2014 .order_by(CacheKey.cache_key)\
2015 .all()
2015 .all()
2016
2016
2017 @property
2017 @property
2018 def cached_diffs_relative_dir(self):
2018 def cached_diffs_relative_dir(self):
2019 """
2019 """
2020 Return a relative to the repository store path of cached diffs
2020 Return a relative to the repository store path of cached diffs
2021 used for safe display for users, who shouldn't know the absolute store
2021 used for safe display for users, who shouldn't know the absolute store
2022 path
2022 path
2023 """
2023 """
2024 return os.path.join(
2024 return os.path.join(
2025 os.path.dirname(self.repo_name),
2025 os.path.dirname(self.repo_name),
2026 self.cached_diffs_dir.split(os.path.sep)[-1])
2026 self.cached_diffs_dir.split(os.path.sep)[-1])
2027
2027
2028 @property
2028 @property
2029 def cached_diffs_dir(self):
2029 def cached_diffs_dir(self):
2030 path = self.repo_full_path
2030 path = self.repo_full_path
2031 return os.path.join(
2031 return os.path.join(
2032 os.path.dirname(path),
2032 os.path.dirname(path),
2033 '.__shadow_diff_cache_repo_{}'.format(self.repo_id))
2033 '.__shadow_diff_cache_repo_{}'.format(self.repo_id))
2034
2034
2035 def cached_diffs(self):
2035 def cached_diffs(self):
2036 diff_cache_dir = self.cached_diffs_dir
2036 diff_cache_dir = self.cached_diffs_dir
2037 if os.path.isdir(diff_cache_dir):
2037 if os.path.isdir(diff_cache_dir):
2038 return os.listdir(diff_cache_dir)
2038 return os.listdir(diff_cache_dir)
2039 return []
2039 return []
2040
2040
2041 def shadow_repos(self):
2041 def shadow_repos(self):
2042 shadow_repos_pattern = '.__shadow_repo_{}'.format(self.repo_id)
2042 shadow_repos_pattern = '.__shadow_repo_{}'.format(self.repo_id)
2043 return [
2043 return [
2044 x for x in os.listdir(os.path.dirname(self.repo_full_path))
2044 x for x in os.listdir(os.path.dirname(self.repo_full_path))
2045 if x.startswith(shadow_repos_pattern)]
2045 if x.startswith(shadow_repos_pattern)]
2046
2046
2047 def get_new_name(self, repo_name):
2047 def get_new_name(self, repo_name):
2048 """
2048 """
2049 returns new full repository name based on assigned group and new new
2049 returns new full repository name based on assigned group and new new
2050
2050
2051 :param group_name:
2051 :param group_name:
2052 """
2052 """
2053 path_prefix = self.group.full_path_splitted if self.group else []
2053 path_prefix = self.group.full_path_splitted if self.group else []
2054 return self.NAME_SEP.join(path_prefix + [repo_name])
2054 return self.NAME_SEP.join(path_prefix + [repo_name])
2055
2055
2056 @property
2056 @property
2057 def _config(self):
2057 def _config(self):
2058 """
2058 """
2059 Returns db based config object.
2059 Returns db based config object.
2060 """
2060 """
2061 from rhodecode.lib.utils import make_db_config
2061 from rhodecode.lib.utils import make_db_config
2062 return make_db_config(clear_session=False, repo=self)
2062 return make_db_config(clear_session=False, repo=self)
2063
2063
2064 def permissions(self, with_admins=True, with_owner=True,
2064 def permissions(self, with_admins=True, with_owner=True,
2065 expand_from_user_groups=False):
2065 expand_from_user_groups=False):
2066 """
2066 """
2067 Permissions for repositories
2067 Permissions for repositories
2068 """
2068 """
2069 _admin_perm = 'repository.admin'
2069 _admin_perm = 'repository.admin'
2070
2070
2071 owner_row = []
2071 owner_row = []
2072 if with_owner:
2072 if with_owner:
2073 usr = AttributeDict(self.user.get_dict())
2073 usr = AttributeDict(self.user.get_dict())
2074 usr.owner_row = True
2074 usr.owner_row = True
2075 usr.permission = _admin_perm
2075 usr.permission = _admin_perm
2076 usr.permission_id = None
2076 usr.permission_id = None
2077 owner_row.append(usr)
2077 owner_row.append(usr)
2078
2078
2079 super_admin_ids = []
2079 super_admin_ids = []
2080 super_admin_rows = []
2080 super_admin_rows = []
2081 if with_admins:
2081 if with_admins:
2082 for usr in User.get_all_super_admins():
2082 for usr in User.get_all_super_admins():
2083 super_admin_ids.append(usr.user_id)
2083 super_admin_ids.append(usr.user_id)
2084 # if this admin is also owner, don't double the record
2084 # if this admin is also owner, don't double the record
2085 if usr.user_id == owner_row[0].user_id:
2085 if usr.user_id == owner_row[0].user_id:
2086 owner_row[0].admin_row = True
2086 owner_row[0].admin_row = True
2087 else:
2087 else:
2088 usr = AttributeDict(usr.get_dict())
2088 usr = AttributeDict(usr.get_dict())
2089 usr.admin_row = True
2089 usr.admin_row = True
2090 usr.permission = _admin_perm
2090 usr.permission = _admin_perm
2091 usr.permission_id = None
2091 usr.permission_id = None
2092 super_admin_rows.append(usr)
2092 super_admin_rows.append(usr)
2093
2093
2094 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
2094 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
2095 q = q.options(joinedload(UserRepoToPerm.repository),
2095 q = q.options(joinedload(UserRepoToPerm.repository),
2096 joinedload(UserRepoToPerm.user),
2096 joinedload(UserRepoToPerm.user),
2097 joinedload(UserRepoToPerm.permission),)
2097 joinedload(UserRepoToPerm.permission),)
2098
2098
2099 # get owners and admins and permissions. We do a trick of re-writing
2099 # get owners and admins and permissions. We do a trick of re-writing
2100 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2100 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2101 # has a global reference and changing one object propagates to all
2101 # has a global reference and changing one object propagates to all
2102 # others. This means if admin is also an owner admin_row that change
2102 # others. This means if admin is also an owner admin_row that change
2103 # would propagate to both objects
2103 # would propagate to both objects
2104 perm_rows = []
2104 perm_rows = []
2105 for _usr in q.all():
2105 for _usr in q.all():
2106 usr = AttributeDict(_usr.user.get_dict())
2106 usr = AttributeDict(_usr.user.get_dict())
2107 # if this user is also owner/admin, mark as duplicate record
2107 # if this user is also owner/admin, mark as duplicate record
2108 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
2108 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
2109 usr.duplicate_perm = True
2109 usr.duplicate_perm = True
2110 # also check if this permission is maybe used by branch_permissions
2110 # also check if this permission is maybe used by branch_permissions
2111 if _usr.branch_perm_entry:
2111 if _usr.branch_perm_entry:
2112 usr.branch_rules = [x.branch_rule_id for x in _usr.branch_perm_entry]
2112 usr.branch_rules = [x.branch_rule_id for x in _usr.branch_perm_entry]
2113
2113
2114 usr.permission = _usr.permission.permission_name
2114 usr.permission = _usr.permission.permission_name
2115 usr.permission_id = _usr.repo_to_perm_id
2115 usr.permission_id = _usr.repo_to_perm_id
2116 perm_rows.append(usr)
2116 perm_rows.append(usr)
2117
2117
2118 # filter the perm rows by 'default' first and then sort them by
2118 # filter the perm rows by 'default' first and then sort them by
2119 # admin,write,read,none permissions sorted again alphabetically in
2119 # admin,write,read,none permissions sorted again alphabetically in
2120 # each group
2120 # each group
2121 perm_rows = sorted(perm_rows, key=display_user_sort)
2121 perm_rows = sorted(perm_rows, key=display_user_sort)
2122
2122
2123 user_groups_rows = []
2123 user_groups_rows = []
2124 if expand_from_user_groups:
2124 if expand_from_user_groups:
2125 for ug in self.permission_user_groups(with_members=True):
2125 for ug in self.permission_user_groups(with_members=True):
2126 for user_data in ug.members:
2126 for user_data in ug.members:
2127 user_groups_rows.append(user_data)
2127 user_groups_rows.append(user_data)
2128
2128
2129 return super_admin_rows + owner_row + perm_rows + user_groups_rows
2129 return super_admin_rows + owner_row + perm_rows + user_groups_rows
2130
2130
2131 def permission_user_groups(self, with_members=True):
2131 def permission_user_groups(self, with_members=True):
2132 q = UserGroupRepoToPerm.query()\
2132 q = UserGroupRepoToPerm.query()\
2133 .filter(UserGroupRepoToPerm.repository == self)
2133 .filter(UserGroupRepoToPerm.repository == self)
2134 q = q.options(joinedload(UserGroupRepoToPerm.repository),
2134 q = q.options(joinedload(UserGroupRepoToPerm.repository),
2135 joinedload(UserGroupRepoToPerm.users_group),
2135 joinedload(UserGroupRepoToPerm.users_group),
2136 joinedload(UserGroupRepoToPerm.permission),)
2136 joinedload(UserGroupRepoToPerm.permission),)
2137
2137
2138 perm_rows = []
2138 perm_rows = []
2139 for _user_group in q.all():
2139 for _user_group in q.all():
2140 entry = AttributeDict(_user_group.users_group.get_dict())
2140 entry = AttributeDict(_user_group.users_group.get_dict())
2141 entry.permission = _user_group.permission.permission_name
2141 entry.permission = _user_group.permission.permission_name
2142 if with_members:
2142 if with_members:
2143 entry.members = [x.user.get_dict()
2143 entry.members = [x.user.get_dict()
2144 for x in _user_group.users_group.members]
2144 for x in _user_group.users_group.members]
2145 perm_rows.append(entry)
2145 perm_rows.append(entry)
2146
2146
2147 perm_rows = sorted(perm_rows, key=display_user_group_sort)
2147 perm_rows = sorted(perm_rows, key=display_user_group_sort)
2148 return perm_rows
2148 return perm_rows
2149
2149
2150 def get_api_data(self, include_secrets=False):
2150 def get_api_data(self, include_secrets=False):
2151 """
2151 """
2152 Common function for generating repo api data
2152 Common function for generating repo api data
2153
2153
2154 :param include_secrets: See :meth:`User.get_api_data`.
2154 :param include_secrets: See :meth:`User.get_api_data`.
2155
2155
2156 """
2156 """
2157 # TODO: mikhail: Here there is an anti-pattern, we probably need to
2157 # TODO: mikhail: Here there is an anti-pattern, we probably need to
2158 # move this methods on models level.
2158 # move this methods on models level.
2159 from rhodecode.model.settings import SettingsModel
2159 from rhodecode.model.settings import SettingsModel
2160 from rhodecode.model.repo import RepoModel
2160 from rhodecode.model.repo import RepoModel
2161
2161
2162 repo = self
2162 repo = self
2163 _user_id, _time, _reason = self.locked
2163 _user_id, _time, _reason = self.locked
2164
2164
2165 data = {
2165 data = {
2166 'repo_id': repo.repo_id,
2166 'repo_id': repo.repo_id,
2167 'repo_name': repo.repo_name,
2167 'repo_name': repo.repo_name,
2168 'repo_type': repo.repo_type,
2168 'repo_type': repo.repo_type,
2169 'clone_uri': repo.clone_uri or '',
2169 'clone_uri': repo.clone_uri or '',
2170 'push_uri': repo.push_uri or '',
2170 'push_uri': repo.push_uri or '',
2171 'url': RepoModel().get_url(self),
2171 'url': RepoModel().get_url(self),
2172 'private': repo.private,
2172 'private': repo.private,
2173 'created_on': repo.created_on,
2173 'created_on': repo.created_on,
2174 'description': repo.description_safe,
2174 'description': repo.description_safe,
2175 'landing_rev': repo.landing_rev,
2175 'landing_rev': repo.landing_rev,
2176 'owner': repo.user.username,
2176 'owner': repo.user.username,
2177 'fork_of': repo.fork.repo_name if repo.fork else None,
2177 'fork_of': repo.fork.repo_name if repo.fork else None,
2178 'fork_of_id': repo.fork.repo_id if repo.fork else None,
2178 'fork_of_id': repo.fork.repo_id if repo.fork else None,
2179 'enable_statistics': repo.enable_statistics,
2179 'enable_statistics': repo.enable_statistics,
2180 'enable_locking': repo.enable_locking,
2180 'enable_locking': repo.enable_locking,
2181 'enable_downloads': repo.enable_downloads,
2181 'enable_downloads': repo.enable_downloads,
2182 'last_changeset': repo.changeset_cache,
2182 'last_changeset': repo.changeset_cache,
2183 'locked_by': User.get(_user_id).get_api_data(
2183 'locked_by': User.get(_user_id).get_api_data(
2184 include_secrets=include_secrets) if _user_id else None,
2184 include_secrets=include_secrets) if _user_id else None,
2185 'locked_date': time_to_datetime(_time) if _time else None,
2185 'locked_date': time_to_datetime(_time) if _time else None,
2186 'lock_reason': _reason if _reason else None,
2186 'lock_reason': _reason if _reason else None,
2187 }
2187 }
2188
2188
2189 # TODO: mikhail: should be per-repo settings here
2189 # TODO: mikhail: should be per-repo settings here
2190 rc_config = SettingsModel().get_all_settings()
2190 rc_config = SettingsModel().get_all_settings()
2191 repository_fields = str2bool(
2191 repository_fields = str2bool(
2192 rc_config.get('rhodecode_repository_fields'))
2192 rc_config.get('rhodecode_repository_fields'))
2193 if repository_fields:
2193 if repository_fields:
2194 for f in self.extra_fields:
2194 for f in self.extra_fields:
2195 data[f.field_key_prefixed] = f.field_value
2195 data[f.field_key_prefixed] = f.field_value
2196
2196
2197 return data
2197 return data
2198
2198
2199 @classmethod
2199 @classmethod
2200 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
2200 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
2201 if not lock_time:
2201 if not lock_time:
2202 lock_time = time.time()
2202 lock_time = time.time()
2203 if not lock_reason:
2203 if not lock_reason:
2204 lock_reason = cls.LOCK_AUTOMATIC
2204 lock_reason = cls.LOCK_AUTOMATIC
2205 repo.locked = [user_id, lock_time, lock_reason]
2205 repo.locked = [user_id, lock_time, lock_reason]
2206 Session().add(repo)
2206 Session().add(repo)
2207 Session().commit()
2207 Session().commit()
2208
2208
2209 @classmethod
2209 @classmethod
2210 def unlock(cls, repo):
2210 def unlock(cls, repo):
2211 repo.locked = None
2211 repo.locked = None
2212 Session().add(repo)
2212 Session().add(repo)
2213 Session().commit()
2213 Session().commit()
2214
2214
2215 @classmethod
2215 @classmethod
2216 def getlock(cls, repo):
2216 def getlock(cls, repo):
2217 return repo.locked
2217 return repo.locked
2218
2218
2219 def is_user_lock(self, user_id):
2219 def is_user_lock(self, user_id):
2220 if self.lock[0]:
2220 if self.lock[0]:
2221 lock_user_id = safe_int(self.lock[0])
2221 lock_user_id = safe_int(self.lock[0])
2222 user_id = safe_int(user_id)
2222 user_id = safe_int(user_id)
2223 # both are ints, and they are equal
2223 # both are ints, and they are equal
2224 return all([lock_user_id, user_id]) and lock_user_id == user_id
2224 return all([lock_user_id, user_id]) and lock_user_id == user_id
2225
2225
2226 return False
2226 return False
2227
2227
2228 def get_locking_state(self, action, user_id, only_when_enabled=True):
2228 def get_locking_state(self, action, user_id, only_when_enabled=True):
2229 """
2229 """
2230 Checks locking on this repository, if locking is enabled and lock is
2230 Checks locking on this repository, if locking is enabled and lock is
2231 present returns a tuple of make_lock, locked, locked_by.
2231 present returns a tuple of make_lock, locked, locked_by.
2232 make_lock can have 3 states None (do nothing) True, make lock
2232 make_lock can have 3 states None (do nothing) True, make lock
2233 False release lock, This value is later propagated to hooks, which
2233 False release lock, This value is later propagated to hooks, which
2234 do the locking. Think about this as signals passed to hooks what to do.
2234 do the locking. Think about this as signals passed to hooks what to do.
2235
2235
2236 """
2236 """
2237 # TODO: johbo: This is part of the business logic and should be moved
2237 # TODO: johbo: This is part of the business logic and should be moved
2238 # into the RepositoryModel.
2238 # into the RepositoryModel.
2239
2239
2240 if action not in ('push', 'pull'):
2240 if action not in ('push', 'pull'):
2241 raise ValueError("Invalid action value: %s" % repr(action))
2241 raise ValueError("Invalid action value: %s" % repr(action))
2242
2242
2243 # defines if locked error should be thrown to user
2243 # defines if locked error should be thrown to user
2244 currently_locked = False
2244 currently_locked = False
2245 # defines if new lock should be made, tri-state
2245 # defines if new lock should be made, tri-state
2246 make_lock = None
2246 make_lock = None
2247 repo = self
2247 repo = self
2248 user = User.get(user_id)
2248 user = User.get(user_id)
2249
2249
2250 lock_info = repo.locked
2250 lock_info = repo.locked
2251
2251
2252 if repo and (repo.enable_locking or not only_when_enabled):
2252 if repo and (repo.enable_locking or not only_when_enabled):
2253 if action == 'push':
2253 if action == 'push':
2254 # check if it's already locked !, if it is compare users
2254 # check if it's already locked !, if it is compare users
2255 locked_by_user_id = lock_info[0]
2255 locked_by_user_id = lock_info[0]
2256 if user.user_id == locked_by_user_id:
2256 if user.user_id == locked_by_user_id:
2257 log.debug(
2257 log.debug(
2258 'Got `push` action from user %s, now unlocking', user)
2258 'Got `push` action from user %s, now unlocking', user)
2259 # unlock if we have push from user who locked
2259 # unlock if we have push from user who locked
2260 make_lock = False
2260 make_lock = False
2261 else:
2261 else:
2262 # we're not the same user who locked, ban with
2262 # we're not the same user who locked, ban with
2263 # code defined in settings (default is 423 HTTP Locked) !
2263 # code defined in settings (default is 423 HTTP Locked) !
2264 log.debug('Repo %s is currently locked by %s', repo, user)
2264 log.debug('Repo %s is currently locked by %s', repo, user)
2265 currently_locked = True
2265 currently_locked = True
2266 elif action == 'pull':
2266 elif action == 'pull':
2267 # [0] user [1] date
2267 # [0] user [1] date
2268 if lock_info[0] and lock_info[1]:
2268 if lock_info[0] and lock_info[1]:
2269 log.debug('Repo %s is currently locked by %s', repo, user)
2269 log.debug('Repo %s is currently locked by %s', repo, user)
2270 currently_locked = True
2270 currently_locked = True
2271 else:
2271 else:
2272 log.debug('Setting lock on repo %s by %s', repo, user)
2272 log.debug('Setting lock on repo %s by %s', repo, user)
2273 make_lock = True
2273 make_lock = True
2274
2274
2275 else:
2275 else:
2276 log.debug('Repository %s do not have locking enabled', repo)
2276 log.debug('Repository %s do not have locking enabled', repo)
2277
2277
2278 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
2278 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
2279 make_lock, currently_locked, lock_info)
2279 make_lock, currently_locked, lock_info)
2280
2280
2281 from rhodecode.lib.auth import HasRepoPermissionAny
2281 from rhodecode.lib.auth import HasRepoPermissionAny
2282 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
2282 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
2283 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
2283 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
2284 # if we don't have at least write permission we cannot make a lock
2284 # if we don't have at least write permission we cannot make a lock
2285 log.debug('lock state reset back to FALSE due to lack '
2285 log.debug('lock state reset back to FALSE due to lack '
2286 'of at least read permission')
2286 'of at least read permission')
2287 make_lock = False
2287 make_lock = False
2288
2288
2289 return make_lock, currently_locked, lock_info
2289 return make_lock, currently_locked, lock_info
2290
2290
2291 @property
2291 @property
2292 def last_commit_cache_update_diff(self):
2292 def last_commit_cache_update_diff(self):
2293 return time.time() - (safe_int(self.changeset_cache.get('updated_on')) or 0)
2293 return time.time() - (safe_int(self.changeset_cache.get('updated_on')) or 0)
2294
2294
2295 @classmethod
2295 @classmethod
2296 def _load_commit_change(cls, last_commit_cache):
2296 def _load_commit_change(cls, last_commit_cache):
2297 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2297 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2298 empty_date = datetime.datetime.fromtimestamp(0)
2298 empty_date = datetime.datetime.fromtimestamp(0)
2299 date_latest = last_commit_cache.get('date', empty_date)
2299 date_latest = last_commit_cache.get('date', empty_date)
2300 try:
2300 try:
2301 return parse_datetime(date_latest)
2301 return parse_datetime(date_latest)
2302 except Exception:
2302 except Exception:
2303 return empty_date
2303 return empty_date
2304
2304
2305 @property
2305 @property
2306 def last_commit_change(self):
2306 def last_commit_change(self):
2307 return self._load_commit_change(self.changeset_cache)
2307 return self._load_commit_change(self.changeset_cache)
2308
2308
2309 @property
2309 @property
2310 def last_db_change(self):
2310 def last_db_change(self):
2311 return self.updated_on
2311 return self.updated_on
2312
2312
2313 @property
2313 @property
2314 def clone_uri_hidden(self):
2314 def clone_uri_hidden(self):
2315 clone_uri = self.clone_uri
2315 clone_uri = self.clone_uri
2316 if clone_uri:
2316 if clone_uri:
2317 import urlobject
2317 import urlobject
2318 url_obj = urlobject.URLObject(cleaned_uri(clone_uri))
2318 url_obj = urlobject.URLObject(cleaned_uri(clone_uri))
2319 if url_obj.password:
2319 if url_obj.password:
2320 clone_uri = url_obj.with_password('*****')
2320 clone_uri = url_obj.with_password('*****')
2321 return clone_uri
2321 return clone_uri
2322
2322
2323 @property
2323 @property
2324 def push_uri_hidden(self):
2324 def push_uri_hidden(self):
2325 push_uri = self.push_uri
2325 push_uri = self.push_uri
2326 if push_uri:
2326 if push_uri:
2327 import urlobject
2327 import urlobject
2328 url_obj = urlobject.URLObject(cleaned_uri(push_uri))
2328 url_obj = urlobject.URLObject(cleaned_uri(push_uri))
2329 if url_obj.password:
2329 if url_obj.password:
2330 push_uri = url_obj.with_password('*****')
2330 push_uri = url_obj.with_password('*****')
2331 return push_uri
2331 return push_uri
2332
2332
2333 def clone_url(self, **override):
2333 def clone_url(self, **override):
2334 from rhodecode.model.settings import SettingsModel
2334 from rhodecode.model.settings import SettingsModel
2335
2335
2336 uri_tmpl = None
2336 uri_tmpl = None
2337 if 'with_id' in override:
2337 if 'with_id' in override:
2338 uri_tmpl = self.DEFAULT_CLONE_URI_ID
2338 uri_tmpl = self.DEFAULT_CLONE_URI_ID
2339 del override['with_id']
2339 del override['with_id']
2340
2340
2341 if 'uri_tmpl' in override:
2341 if 'uri_tmpl' in override:
2342 uri_tmpl = override['uri_tmpl']
2342 uri_tmpl = override['uri_tmpl']
2343 del override['uri_tmpl']
2343 del override['uri_tmpl']
2344
2344
2345 ssh = False
2345 ssh = False
2346 if 'ssh' in override:
2346 if 'ssh' in override:
2347 ssh = True
2347 ssh = True
2348 del override['ssh']
2348 del override['ssh']
2349
2349
2350 # we didn't override our tmpl from **overrides
2350 # we didn't override our tmpl from **overrides
2351 request = get_current_request()
2351 request = get_current_request()
2352 if not uri_tmpl:
2352 if not uri_tmpl:
2353 if hasattr(request, 'call_context') and hasattr(request.call_context, 'rc_config'):
2353 if hasattr(request, 'call_context') and hasattr(request.call_context, 'rc_config'):
2354 rc_config = request.call_context.rc_config
2354 rc_config = request.call_context.rc_config
2355 else:
2355 else:
2356 rc_config = SettingsModel().get_all_settings(cache=True)
2356 rc_config = SettingsModel().get_all_settings(cache=True)
2357
2357
2358 if ssh:
2358 if ssh:
2359 uri_tmpl = rc_config.get(
2359 uri_tmpl = rc_config.get(
2360 'rhodecode_clone_uri_ssh_tmpl') or self.DEFAULT_CLONE_URI_SSH
2360 'rhodecode_clone_uri_ssh_tmpl') or self.DEFAULT_CLONE_URI_SSH
2361
2361
2362 else:
2362 else:
2363 uri_tmpl = rc_config.get(
2363 uri_tmpl = rc_config.get(
2364 'rhodecode_clone_uri_tmpl') or self.DEFAULT_CLONE_URI
2364 'rhodecode_clone_uri_tmpl') or self.DEFAULT_CLONE_URI
2365
2365
2366 return get_clone_url(request=request,
2366 return get_clone_url(request=request,
2367 uri_tmpl=uri_tmpl,
2367 uri_tmpl=uri_tmpl,
2368 repo_name=self.repo_name,
2368 repo_name=self.repo_name,
2369 repo_id=self.repo_id,
2369 repo_id=self.repo_id,
2370 repo_type=self.repo_type,
2370 repo_type=self.repo_type,
2371 **override)
2371 **override)
2372
2372
2373 def set_state(self, state):
2373 def set_state(self, state):
2374 self.repo_state = state
2374 self.repo_state = state
2375 Session().add(self)
2375 Session().add(self)
2376 #==========================================================================
2376 #==========================================================================
2377 # SCM PROPERTIES
2377 # SCM PROPERTIES
2378 #==========================================================================
2378 #==========================================================================
2379
2379
2380 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None, maybe_unreachable=False):
2380 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None, maybe_unreachable=False):
2381 return get_commit_safe(
2381 return get_commit_safe(
2382 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load,
2382 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load,
2383 maybe_unreachable=maybe_unreachable)
2383 maybe_unreachable=maybe_unreachable)
2384
2384
2385 def get_changeset(self, rev=None, pre_load=None):
2385 def get_changeset(self, rev=None, pre_load=None):
2386 warnings.warn("Use get_commit", DeprecationWarning)
2386 warnings.warn("Use get_commit", DeprecationWarning)
2387 commit_id = None
2387 commit_id = None
2388 commit_idx = None
2388 commit_idx = None
2389 if isinstance(rev, compat.string_types):
2389 if isinstance(rev, compat.string_types):
2390 commit_id = rev
2390 commit_id = rev
2391 else:
2391 else:
2392 commit_idx = rev
2392 commit_idx = rev
2393 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
2393 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
2394 pre_load=pre_load)
2394 pre_load=pre_load)
2395
2395
2396 def get_landing_commit(self):
2396 def get_landing_commit(self):
2397 """
2397 """
2398 Returns landing commit, or if that doesn't exist returns the tip
2398 Returns landing commit, or if that doesn't exist returns the tip
2399 """
2399 """
2400 _rev_type, _rev = self.landing_rev
2400 _rev_type, _rev = self.landing_rev
2401 commit = self.get_commit(_rev)
2401 commit = self.get_commit(_rev)
2402 if isinstance(commit, EmptyCommit):
2402 if isinstance(commit, EmptyCommit):
2403 return self.get_commit()
2403 return self.get_commit()
2404 return commit
2404 return commit
2405
2405
2406 def flush_commit_cache(self):
2406 def flush_commit_cache(self):
2407 self.update_commit_cache(cs_cache={'raw_id':'0'})
2407 self.update_commit_cache(cs_cache={'raw_id':'0'})
2408 self.update_commit_cache()
2408 self.update_commit_cache()
2409
2409
2410 def update_commit_cache(self, cs_cache=None, config=None):
2410 def update_commit_cache(self, cs_cache=None, config=None):
2411 """
2411 """
2412 Update cache of last commit for repository
2412 Update cache of last commit for repository
2413 cache_keys should be::
2413 cache_keys should be::
2414
2414
2415 source_repo_id
2415 source_repo_id
2416 short_id
2416 short_id
2417 raw_id
2417 raw_id
2418 revision
2418 revision
2419 parents
2419 parents
2420 message
2420 message
2421 date
2421 date
2422 author
2422 author
2423 updated_on
2423 updated_on
2424
2424
2425 """
2425 """
2426 from rhodecode.lib.vcs.backends.base import BaseChangeset
2426 from rhodecode.lib.vcs.backends.base import BaseChangeset
2427 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2427 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2428 empty_date = datetime.datetime.fromtimestamp(0)
2428 empty_date = datetime.datetime.fromtimestamp(0)
2429
2429
2430 if cs_cache is None:
2430 if cs_cache is None:
2431 # use no-cache version here
2431 # use no-cache version here
2432 try:
2432 try:
2433 scm_repo = self.scm_instance(cache=False, config=config)
2433 scm_repo = self.scm_instance(cache=False, config=config)
2434 except VCSError:
2434 except VCSError:
2435 scm_repo = None
2435 scm_repo = None
2436 empty = scm_repo is None or scm_repo.is_empty()
2436 empty = scm_repo is None or scm_repo.is_empty()
2437
2437
2438 if not empty:
2438 if not empty:
2439 cs_cache = scm_repo.get_commit(
2439 cs_cache = scm_repo.get_commit(
2440 pre_load=["author", "date", "message", "parents", "branch"])
2440 pre_load=["author", "date", "message", "parents", "branch"])
2441 else:
2441 else:
2442 cs_cache = EmptyCommit()
2442 cs_cache = EmptyCommit()
2443
2443
2444 if isinstance(cs_cache, BaseChangeset):
2444 if isinstance(cs_cache, BaseChangeset):
2445 cs_cache = cs_cache.__json__()
2445 cs_cache = cs_cache.__json__()
2446
2446
2447 def is_outdated(new_cs_cache):
2447 def is_outdated(new_cs_cache):
2448 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
2448 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
2449 new_cs_cache['revision'] != self.changeset_cache['revision']):
2449 new_cs_cache['revision'] != self.changeset_cache['revision']):
2450 return True
2450 return True
2451 return False
2451 return False
2452
2452
2453 # check if we have maybe already latest cached revision
2453 # check if we have maybe already latest cached revision
2454 if is_outdated(cs_cache) or not self.changeset_cache:
2454 if is_outdated(cs_cache) or not self.changeset_cache:
2455 _current_datetime = datetime.datetime.utcnow()
2455 _current_datetime = datetime.datetime.utcnow()
2456 last_change = cs_cache.get('date') or _current_datetime
2456 last_change = cs_cache.get('date') or _current_datetime
2457 # we check if last update is newer than the new value
2457 # we check if last update is newer than the new value
2458 # if yes, we use the current timestamp instead. Imagine you get
2458 # if yes, we use the current timestamp instead. Imagine you get
2459 # old commit pushed 1y ago, we'd set last update 1y to ago.
2459 # old commit pushed 1y ago, we'd set last update 1y to ago.
2460 last_change_timestamp = datetime_to_time(last_change)
2460 last_change_timestamp = datetime_to_time(last_change)
2461 current_timestamp = datetime_to_time(last_change)
2461 current_timestamp = datetime_to_time(last_change)
2462 if last_change_timestamp > current_timestamp and not empty:
2462 if last_change_timestamp > current_timestamp and not empty:
2463 cs_cache['date'] = _current_datetime
2463 cs_cache['date'] = _current_datetime
2464
2464
2465 _date_latest = parse_datetime(cs_cache.get('date') or empty_date)
2465 _date_latest = parse_datetime(cs_cache.get('date') or empty_date)
2466 cs_cache['updated_on'] = time.time()
2466 cs_cache['updated_on'] = time.time()
2467 self.changeset_cache = cs_cache
2467 self.changeset_cache = cs_cache
2468 self.updated_on = last_change
2468 self.updated_on = last_change
2469 Session().add(self)
2469 Session().add(self)
2470 Session().commit()
2470 Session().commit()
2471
2471
2472 else:
2472 else:
2473 if empty:
2473 if empty:
2474 cs_cache = EmptyCommit().__json__()
2474 cs_cache = EmptyCommit().__json__()
2475 else:
2475 else:
2476 cs_cache = self.changeset_cache
2476 cs_cache = self.changeset_cache
2477
2477
2478 _date_latest = parse_datetime(cs_cache.get('date') or empty_date)
2478 _date_latest = parse_datetime(cs_cache.get('date') or empty_date)
2479
2479
2480 cs_cache['updated_on'] = time.time()
2480 cs_cache['updated_on'] = time.time()
2481 self.changeset_cache = cs_cache
2481 self.changeset_cache = cs_cache
2482 self.updated_on = _date_latest
2482 self.updated_on = _date_latest
2483 Session().add(self)
2483 Session().add(self)
2484 Session().commit()
2484 Session().commit()
2485
2485
2486 log.debug('updated repo `%s` with new commit cache %s, and last update_date: %s',
2486 log.debug('updated repo `%s` with new commit cache %s, and last update_date: %s',
2487 self.repo_name, cs_cache, _date_latest)
2487 self.repo_name, cs_cache, _date_latest)
2488
2488
2489 @property
2489 @property
2490 def tip(self):
2490 def tip(self):
2491 return self.get_commit('tip')
2491 return self.get_commit('tip')
2492
2492
2493 @property
2493 @property
2494 def author(self):
2494 def author(self):
2495 return self.tip.author
2495 return self.tip.author
2496
2496
2497 @property
2497 @property
2498 def last_change(self):
2498 def last_change(self):
2499 return self.scm_instance().last_change
2499 return self.scm_instance().last_change
2500
2500
2501 def get_comments(self, revisions=None):
2501 def get_comments(self, revisions=None):
2502 """
2502 """
2503 Returns comments for this repository grouped by revisions
2503 Returns comments for this repository grouped by revisions
2504
2504
2505 :param revisions: filter query by revisions only
2505 :param revisions: filter query by revisions only
2506 """
2506 """
2507 cmts = ChangesetComment.query()\
2507 cmts = ChangesetComment.query()\
2508 .filter(ChangesetComment.repo == self)
2508 .filter(ChangesetComment.repo == self)
2509 if revisions:
2509 if revisions:
2510 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
2510 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
2511 grouped = collections.defaultdict(list)
2511 grouped = collections.defaultdict(list)
2512 for cmt in cmts.all():
2512 for cmt in cmts.all():
2513 grouped[cmt.revision].append(cmt)
2513 grouped[cmt.revision].append(cmt)
2514 return grouped
2514 return grouped
2515
2515
2516 def statuses(self, revisions=None):
2516 def statuses(self, revisions=None):
2517 """
2517 """
2518 Returns statuses for this repository
2518 Returns statuses for this repository
2519
2519
2520 :param revisions: list of revisions to get statuses for
2520 :param revisions: list of revisions to get statuses for
2521 """
2521 """
2522 statuses = ChangesetStatus.query()\
2522 statuses = ChangesetStatus.query()\
2523 .filter(ChangesetStatus.repo == self)\
2523 .filter(ChangesetStatus.repo == self)\
2524 .filter(ChangesetStatus.version == 0)
2524 .filter(ChangesetStatus.version == 0)
2525
2525
2526 if revisions:
2526 if revisions:
2527 # Try doing the filtering in chunks to avoid hitting limits
2527 # Try doing the filtering in chunks to avoid hitting limits
2528 size = 500
2528 size = 500
2529 status_results = []
2529 status_results = []
2530 for chunk in xrange(0, len(revisions), size):
2530 for chunk in xrange(0, len(revisions), size):
2531 status_results += statuses.filter(
2531 status_results += statuses.filter(
2532 ChangesetStatus.revision.in_(
2532 ChangesetStatus.revision.in_(
2533 revisions[chunk: chunk+size])
2533 revisions[chunk: chunk+size])
2534 ).all()
2534 ).all()
2535 else:
2535 else:
2536 status_results = statuses.all()
2536 status_results = statuses.all()
2537
2537
2538 grouped = {}
2538 grouped = {}
2539
2539
2540 # maybe we have open new pullrequest without a status?
2540 # maybe we have open new pullrequest without a status?
2541 stat = ChangesetStatus.STATUS_UNDER_REVIEW
2541 stat = ChangesetStatus.STATUS_UNDER_REVIEW
2542 status_lbl = ChangesetStatus.get_status_lbl(stat)
2542 status_lbl = ChangesetStatus.get_status_lbl(stat)
2543 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
2543 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
2544 for rev in pr.revisions:
2544 for rev in pr.revisions:
2545 pr_id = pr.pull_request_id
2545 pr_id = pr.pull_request_id
2546 pr_repo = pr.target_repo.repo_name
2546 pr_repo = pr.target_repo.repo_name
2547 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
2547 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
2548
2548
2549 for stat in status_results:
2549 for stat in status_results:
2550 pr_id = pr_repo = None
2550 pr_id = pr_repo = None
2551 if stat.pull_request:
2551 if stat.pull_request:
2552 pr_id = stat.pull_request.pull_request_id
2552 pr_id = stat.pull_request.pull_request_id
2553 pr_repo = stat.pull_request.target_repo.repo_name
2553 pr_repo = stat.pull_request.target_repo.repo_name
2554 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
2554 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
2555 pr_id, pr_repo]
2555 pr_id, pr_repo]
2556 return grouped
2556 return grouped
2557
2557
2558 # ==========================================================================
2558 # ==========================================================================
2559 # SCM CACHE INSTANCE
2559 # SCM CACHE INSTANCE
2560 # ==========================================================================
2560 # ==========================================================================
2561
2561
2562 def scm_instance(self, **kwargs):
2562 def scm_instance(self, **kwargs):
2563 import rhodecode
2563 import rhodecode
2564
2564
2565 # Passing a config will not hit the cache currently only used
2565 # Passing a config will not hit the cache currently only used
2566 # for repo2dbmapper
2566 # for repo2dbmapper
2567 config = kwargs.pop('config', None)
2567 config = kwargs.pop('config', None)
2568 cache = kwargs.pop('cache', None)
2568 cache = kwargs.pop('cache', None)
2569 vcs_full_cache = kwargs.pop('vcs_full_cache', None)
2569 vcs_full_cache = kwargs.pop('vcs_full_cache', None)
2570 if vcs_full_cache is not None:
2570 if vcs_full_cache is not None:
2571 # allows override global config
2571 # allows override global config
2572 full_cache = vcs_full_cache
2572 full_cache = vcs_full_cache
2573 else:
2573 else:
2574 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
2574 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
2575 # if cache is NOT defined use default global, else we have a full
2575 # if cache is NOT defined use default global, else we have a full
2576 # control over cache behaviour
2576 # control over cache behaviour
2577 if cache is None and full_cache and not config:
2577 if cache is None and full_cache and not config:
2578 log.debug('Initializing pure cached instance for %s', self.repo_path)
2578 log.debug('Initializing pure cached instance for %s', self.repo_path)
2579 return self._get_instance_cached()
2579 return self._get_instance_cached()
2580
2580
2581 # cache here is sent to the "vcs server"
2581 # cache here is sent to the "vcs server"
2582 return self._get_instance(cache=bool(cache), config=config)
2582 return self._get_instance(cache=bool(cache), config=config)
2583
2583
2584 def _get_instance_cached(self):
2584 def _get_instance_cached(self):
2585 from rhodecode.lib import rc_cache
2585 from rhodecode.lib import rc_cache
2586
2586
2587 cache_namespace_uid = 'cache_repo_instance.{}'.format(self.repo_id)
2587 cache_namespace_uid = 'cache_repo_instance.{}'.format(self.repo_id)
2588 invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
2588 invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
2589 repo_id=self.repo_id)
2589 repo_id=self.repo_id)
2590 region = rc_cache.get_or_create_region('cache_repo_longterm', cache_namespace_uid)
2590 region = rc_cache.get_or_create_region('cache_repo_longterm', cache_namespace_uid)
2591
2591
2592 @region.conditional_cache_on_arguments(namespace=cache_namespace_uid)
2592 @region.conditional_cache_on_arguments(namespace=cache_namespace_uid)
2593 def get_instance_cached(repo_id, context_id, _cache_state_uid):
2593 def get_instance_cached(repo_id, context_id, _cache_state_uid):
2594 return self._get_instance(repo_state_uid=_cache_state_uid)
2594 return self._get_instance(repo_state_uid=_cache_state_uid)
2595
2595
2596 # we must use thread scoped cache here,
2596 # we must use thread scoped cache here,
2597 # because each thread of gevent needs it's own not shared connection and cache
2597 # because each thread of gevent needs it's own not shared connection and cache
2598 # we also alter `args` so the cache key is individual for every green thread.
2598 # we also alter `args` so the cache key is individual for every green thread.
2599 inv_context_manager = rc_cache.InvalidationContext(
2599 inv_context_manager = rc_cache.InvalidationContext(
2600 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace,
2600 uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace,
2601 thread_scoped=True)
2601 thread_scoped=True)
2602 with inv_context_manager as invalidation_context:
2602 with inv_context_manager as invalidation_context:
2603 cache_state_uid = invalidation_context.cache_data['cache_state_uid']
2603 cache_state_uid = invalidation_context.cache_data['cache_state_uid']
2604 args = (self.repo_id, inv_context_manager.cache_key, cache_state_uid)
2604 args = (self.repo_id, inv_context_manager.cache_key, cache_state_uid)
2605
2605
2606 # re-compute and store cache if we get invalidate signal
2606 # re-compute and store cache if we get invalidate signal
2607 if invalidation_context.should_invalidate():
2607 if invalidation_context.should_invalidate():
2608 instance = get_instance_cached.refresh(*args)
2608 instance = get_instance_cached.refresh(*args)
2609 else:
2609 else:
2610 instance = get_instance_cached(*args)
2610 instance = get_instance_cached(*args)
2611
2611
2612 log.debug('Repo instance fetched in %.4fs', inv_context_manager.compute_time)
2612 log.debug('Repo instance fetched in %.4fs', inv_context_manager.compute_time)
2613 return instance
2613 return instance
2614
2614
2615 def _get_instance(self, cache=True, config=None, repo_state_uid=None):
2615 def _get_instance(self, cache=True, config=None, repo_state_uid=None):
2616 log.debug('Initializing %s instance `%s` with cache flag set to: %s',
2616 log.debug('Initializing %s instance `%s` with cache flag set to: %s',
2617 self.repo_type, self.repo_path, cache)
2617 self.repo_type, self.repo_path, cache)
2618 config = config or self._config
2618 config = config or self._config
2619 custom_wire = {
2619 custom_wire = {
2620 'cache': cache, # controls the vcs.remote cache
2620 'cache': cache, # controls the vcs.remote cache
2621 'repo_state_uid': repo_state_uid
2621 'repo_state_uid': repo_state_uid
2622 }
2622 }
2623 repo = get_vcs_instance(
2623 repo = get_vcs_instance(
2624 repo_path=safe_str(self.repo_full_path),
2624 repo_path=safe_str(self.repo_full_path),
2625 config=config,
2625 config=config,
2626 with_wire=custom_wire,
2626 with_wire=custom_wire,
2627 create=False,
2627 create=False,
2628 _vcs_alias=self.repo_type)
2628 _vcs_alias=self.repo_type)
2629 if repo is not None:
2629 if repo is not None:
2630 repo.count() # cache rebuild
2630 repo.count() # cache rebuild
2631 return repo
2631 return repo
2632
2632
2633 def get_shadow_repository_path(self, workspace_id):
2633 def get_shadow_repository_path(self, workspace_id):
2634 from rhodecode.lib.vcs.backends.base import BaseRepository
2634 from rhodecode.lib.vcs.backends.base import BaseRepository
2635 shadow_repo_path = BaseRepository._get_shadow_repository_path(
2635 shadow_repo_path = BaseRepository._get_shadow_repository_path(
2636 self.repo_full_path, self.repo_id, workspace_id)
2636 self.repo_full_path, self.repo_id, workspace_id)
2637 return shadow_repo_path
2637 return shadow_repo_path
2638
2638
2639 def __json__(self):
2639 def __json__(self):
2640 return {'landing_rev': self.landing_rev}
2640 return {'landing_rev': self.landing_rev}
2641
2641
2642 def get_dict(self):
2642 def get_dict(self):
2643
2643
2644 # Since we transformed `repo_name` to a hybrid property, we need to
2644 # Since we transformed `repo_name` to a hybrid property, we need to
2645 # keep compatibility with the code which uses `repo_name` field.
2645 # keep compatibility with the code which uses `repo_name` field.
2646
2646
2647 result = super(Repository, self).get_dict()
2647 result = super(Repository, self).get_dict()
2648 result['repo_name'] = result.pop('_repo_name', None)
2648 result['repo_name'] = result.pop('_repo_name', None)
2649 return result
2649 return result
2650
2650
2651
2651
2652 class RepoGroup(Base, BaseModel):
2652 class RepoGroup(Base, BaseModel):
2653 __tablename__ = 'groups'
2653 __tablename__ = 'groups'
2654 __table_args__ = (
2654 __table_args__ = (
2655 UniqueConstraint('group_name', 'group_parent_id'),
2655 UniqueConstraint('group_name', 'group_parent_id'),
2656 base_table_args,
2656 base_table_args,
2657 )
2657 )
2658 __mapper_args__ = {'order_by': 'group_name'}
2658 __mapper_args__ = {'order_by': 'group_name'}
2659
2659
2660 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2660 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2661
2661
2662 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2662 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2663 _group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2663 _group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2664 group_name_hash = Column("repo_group_name_hash", String(1024), nullable=False, unique=False)
2664 group_name_hash = Column("repo_group_name_hash", String(1024), nullable=False, unique=False)
2665 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2665 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2666 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2666 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2667 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2667 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2668 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2668 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2669 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2669 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2670 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2670 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2671 personal = Column('personal', Boolean(), nullable=True, unique=None, default=None)
2671 personal = Column('personal', Boolean(), nullable=True, unique=None, default=None)
2672 _changeset_cache = Column("changeset_cache", LargeBinary(), nullable=True) # JSON data
2672 _changeset_cache = Column("changeset_cache", LargeBinary(), nullable=True) # JSON data
2673
2673
2674 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2674 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2675 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2675 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2676 parent_group = relationship('RepoGroup', remote_side=group_id)
2676 parent_group = relationship('RepoGroup', remote_side=group_id)
2677 user = relationship('User')
2677 user = relationship('User')
2678 integrations = relationship('Integration', cascade="all, delete-orphan")
2678 integrations = relationship('Integration', cascade="all, delete-orphan")
2679
2679
2680 # no cascade, set NULL
2680 # no cascade, set NULL
2681 scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_group_id==RepoGroup.group_id')
2681 scope_artifacts = relationship('FileStore', primaryjoin='FileStore.scope_repo_group_id==RepoGroup.group_id')
2682
2682
2683 def __init__(self, group_name='', parent_group=None):
2683 def __init__(self, group_name='', parent_group=None):
2684 self.group_name = group_name
2684 self.group_name = group_name
2685 self.parent_group = parent_group
2685 self.parent_group = parent_group
2686
2686
2687 def __unicode__(self):
2687 def __unicode__(self):
2688 return u"<%s('id:%s:%s')>" % (
2688 return u"<%s('id:%s:%s')>" % (
2689 self.__class__.__name__, self.group_id, self.group_name)
2689 self.__class__.__name__, self.group_id, self.group_name)
2690
2690
2691 @hybrid_property
2691 @hybrid_property
2692 def group_name(self):
2692 def group_name(self):
2693 return self._group_name
2693 return self._group_name
2694
2694
2695 @group_name.setter
2695 @group_name.setter
2696 def group_name(self, value):
2696 def group_name(self, value):
2697 self._group_name = value
2697 self._group_name = value
2698 self.group_name_hash = self.hash_repo_group_name(value)
2698 self.group_name_hash = self.hash_repo_group_name(value)
2699
2699
2700 @classmethod
2700 @classmethod
2701 def _load_changeset_cache(cls, repo_id, changeset_cache_raw):
2701 def _load_changeset_cache(cls, repo_id, changeset_cache_raw):
2702 from rhodecode.lib.vcs.backends.base import EmptyCommit
2702 from rhodecode.lib.vcs.backends.base import EmptyCommit
2703 dummy = EmptyCommit().__json__()
2703 dummy = EmptyCommit().__json__()
2704 if not changeset_cache_raw:
2704 if not changeset_cache_raw:
2705 dummy['source_repo_id'] = repo_id
2705 dummy['source_repo_id'] = repo_id
2706 return json.loads(json.dumps(dummy))
2706 return json.loads(json.dumps(dummy))
2707
2707
2708 try:
2708 try:
2709 return json.loads(changeset_cache_raw)
2709 return json.loads(changeset_cache_raw)
2710 except TypeError:
2710 except TypeError:
2711 return dummy
2711 return dummy
2712 except Exception:
2712 except Exception:
2713 log.error(traceback.format_exc())
2713 log.error(traceback.format_exc())
2714 return dummy
2714 return dummy
2715
2715
2716 @hybrid_property
2716 @hybrid_property
2717 def changeset_cache(self):
2717 def changeset_cache(self):
2718 return self._load_changeset_cache('', self._changeset_cache)
2718 return self._load_changeset_cache('', self._changeset_cache)
2719
2719
2720 @changeset_cache.setter
2720 @changeset_cache.setter
2721 def changeset_cache(self, val):
2721 def changeset_cache(self, val):
2722 try:
2722 try:
2723 self._changeset_cache = json.dumps(val)
2723 self._changeset_cache = json.dumps(val)
2724 except Exception:
2724 except Exception:
2725 log.error(traceback.format_exc())
2725 log.error(traceback.format_exc())
2726
2726
2727 @validates('group_parent_id')
2727 @validates('group_parent_id')
2728 def validate_group_parent_id(self, key, val):
2728 def validate_group_parent_id(self, key, val):
2729 """
2729 """
2730 Check cycle references for a parent group to self
2730 Check cycle references for a parent group to self
2731 """
2731 """
2732 if self.group_id and val:
2732 if self.group_id and val:
2733 assert val != self.group_id
2733 assert val != self.group_id
2734
2734
2735 return val
2735 return val
2736
2736
2737 @hybrid_property
2737 @hybrid_property
2738 def description_safe(self):
2738 def description_safe(self):
2739 from rhodecode.lib import helpers as h
2739 from rhodecode.lib import helpers as h
2740 return h.escape(self.group_description)
2740 return h.escape(self.group_description)
2741
2741
2742 @classmethod
2742 @classmethod
2743 def hash_repo_group_name(cls, repo_group_name):
2743 def hash_repo_group_name(cls, repo_group_name):
2744 val = remove_formatting(repo_group_name)
2744 val = remove_formatting(repo_group_name)
2745 val = safe_str(val).lower()
2745 val = safe_str(val).lower()
2746 chars = []
2746 chars = []
2747 for c in val:
2747 for c in val:
2748 if c not in string.ascii_letters:
2748 if c not in string.ascii_letters:
2749 c = str(ord(c))
2749 c = str(ord(c))
2750 chars.append(c)
2750 chars.append(c)
2751
2751
2752 return ''.join(chars)
2752 return ''.join(chars)
2753
2753
2754 @classmethod
2754 @classmethod
2755 def _generate_choice(cls, repo_group):
2755 def _generate_choice(cls, repo_group):
2756 from webhelpers2.html import literal as _literal
2756 from webhelpers2.html import literal as _literal
2757 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2757 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2758 return repo_group.group_id, _name(repo_group.full_path_splitted)
2758 return repo_group.group_id, _name(repo_group.full_path_splitted)
2759
2759
2760 @classmethod
2760 @classmethod
2761 def groups_choices(cls, groups=None, show_empty_group=True):
2761 def groups_choices(cls, groups=None, show_empty_group=True):
2762 if not groups:
2762 if not groups:
2763 groups = cls.query().all()
2763 groups = cls.query().all()
2764
2764
2765 repo_groups = []
2765 repo_groups = []
2766 if show_empty_group:
2766 if show_empty_group:
2767 repo_groups = [(-1, u'-- %s --' % _('No parent'))]
2767 repo_groups = [(-1, u'-- %s --' % _('No parent'))]
2768
2768
2769 repo_groups.extend([cls._generate_choice(x) for x in groups])
2769 repo_groups.extend([cls._generate_choice(x) for x in groups])
2770
2770
2771 repo_groups = sorted(
2771 repo_groups = sorted(
2772 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2772 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2773 return repo_groups
2773 return repo_groups
2774
2774
2775 @classmethod
2775 @classmethod
2776 def url_sep(cls):
2776 def url_sep(cls):
2777 return URL_SEP
2777 return URL_SEP
2778
2778
2779 @classmethod
2779 @classmethod
2780 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2780 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2781 if case_insensitive:
2781 if case_insensitive:
2782 gr = cls.query().filter(func.lower(cls.group_name)
2782 gr = cls.query().filter(func.lower(cls.group_name)
2783 == func.lower(group_name))
2783 == func.lower(group_name))
2784 else:
2784 else:
2785 gr = cls.query().filter(cls.group_name == group_name)
2785 gr = cls.query().filter(cls.group_name == group_name)
2786 if cache:
2786 if cache:
2787 name_key = _hash_key(group_name)
2787 name_key = _hash_key(group_name)
2788 gr = gr.options(
2788 gr = gr.options(
2789 FromCache("sql_cache_short", "get_group_%s" % name_key))
2789 FromCache("sql_cache_short", "get_group_%s" % name_key))
2790 return gr.scalar()
2790 return gr.scalar()
2791
2791
2792 @classmethod
2792 @classmethod
2793 def get_user_personal_repo_group(cls, user_id):
2793 def get_user_personal_repo_group(cls, user_id):
2794 user = User.get(user_id)
2794 user = User.get(user_id)
2795 if user.username == User.DEFAULT_USER:
2795 if user.username == User.DEFAULT_USER:
2796 return None
2796 return None
2797
2797
2798 return cls.query()\
2798 return cls.query()\
2799 .filter(cls.personal == true()) \
2799 .filter(cls.personal == true()) \
2800 .filter(cls.user == user) \
2800 .filter(cls.user == user) \
2801 .order_by(cls.group_id.asc()) \
2801 .order_by(cls.group_id.asc()) \
2802 .first()
2802 .first()
2803
2803
2804 @classmethod
2804 @classmethod
2805 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2805 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2806 case_insensitive=True):
2806 case_insensitive=True):
2807 q = RepoGroup.query()
2807 q = RepoGroup.query()
2808
2808
2809 if not isinstance(user_id, Optional):
2809 if not isinstance(user_id, Optional):
2810 q = q.filter(RepoGroup.user_id == user_id)
2810 q = q.filter(RepoGroup.user_id == user_id)
2811
2811
2812 if not isinstance(group_id, Optional):
2812 if not isinstance(group_id, Optional):
2813 q = q.filter(RepoGroup.group_parent_id == group_id)
2813 q = q.filter(RepoGroup.group_parent_id == group_id)
2814
2814
2815 if case_insensitive:
2815 if case_insensitive:
2816 q = q.order_by(func.lower(RepoGroup.group_name))
2816 q = q.order_by(func.lower(RepoGroup.group_name))
2817 else:
2817 else:
2818 q = q.order_by(RepoGroup.group_name)
2818 q = q.order_by(RepoGroup.group_name)
2819 return q.all()
2819 return q.all()
2820
2820
2821 @property
2821 @property
2822 def parents(self, parents_recursion_limit=10):
2822 def parents(self, parents_recursion_limit=10):
2823 groups = []
2823 groups = []
2824 if self.parent_group is None:
2824 if self.parent_group is None:
2825 return groups
2825 return groups
2826 cur_gr = self.parent_group
2826 cur_gr = self.parent_group
2827 groups.insert(0, cur_gr)
2827 groups.insert(0, cur_gr)
2828 cnt = 0
2828 cnt = 0
2829 while 1:
2829 while 1:
2830 cnt += 1
2830 cnt += 1
2831 gr = getattr(cur_gr, 'parent_group', None)
2831 gr = getattr(cur_gr, 'parent_group', None)
2832 cur_gr = cur_gr.parent_group
2832 cur_gr = cur_gr.parent_group
2833 if gr is None:
2833 if gr is None:
2834 break
2834 break
2835 if cnt == parents_recursion_limit:
2835 if cnt == parents_recursion_limit:
2836 # this will prevent accidental infinit loops
2836 # this will prevent accidental infinit loops
2837 log.error('more than %s parents found for group %s, stopping '
2837 log.error('more than %s parents found for group %s, stopping '
2838 'recursive parent fetching', parents_recursion_limit, self)
2838 'recursive parent fetching', parents_recursion_limit, self)
2839 break
2839 break
2840
2840
2841 groups.insert(0, gr)
2841 groups.insert(0, gr)
2842 return groups
2842 return groups
2843
2843
2844 @property
2844 @property
2845 def last_commit_cache_update_diff(self):
2845 def last_commit_cache_update_diff(self):
2846 return time.time() - (safe_int(self.changeset_cache.get('updated_on')) or 0)
2846 return time.time() - (safe_int(self.changeset_cache.get('updated_on')) or 0)
2847
2847
2848 @classmethod
2848 @classmethod
2849 def _load_commit_change(cls, last_commit_cache):
2849 def _load_commit_change(cls, last_commit_cache):
2850 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2850 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2851 empty_date = datetime.datetime.fromtimestamp(0)
2851 empty_date = datetime.datetime.fromtimestamp(0)
2852 date_latest = last_commit_cache.get('date', empty_date)
2852 date_latest = last_commit_cache.get('date', empty_date)
2853 try:
2853 try:
2854 return parse_datetime(date_latest)
2854 return parse_datetime(date_latest)
2855 except Exception:
2855 except Exception:
2856 return empty_date
2856 return empty_date
2857
2857
2858 @property
2858 @property
2859 def last_commit_change(self):
2859 def last_commit_change(self):
2860 return self._load_commit_change(self.changeset_cache)
2860 return self._load_commit_change(self.changeset_cache)
2861
2861
2862 @property
2862 @property
2863 def last_db_change(self):
2863 def last_db_change(self):
2864 return self.updated_on
2864 return self.updated_on
2865
2865
2866 @property
2866 @property
2867 def children(self):
2867 def children(self):
2868 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2868 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2869
2869
2870 @property
2870 @property
2871 def name(self):
2871 def name(self):
2872 return self.group_name.split(RepoGroup.url_sep())[-1]
2872 return self.group_name.split(RepoGroup.url_sep())[-1]
2873
2873
2874 @property
2874 @property
2875 def full_path(self):
2875 def full_path(self):
2876 return self.group_name
2876 return self.group_name
2877
2877
2878 @property
2878 @property
2879 def full_path_splitted(self):
2879 def full_path_splitted(self):
2880 return self.group_name.split(RepoGroup.url_sep())
2880 return self.group_name.split(RepoGroup.url_sep())
2881
2881
2882 @property
2882 @property
2883 def repositories(self):
2883 def repositories(self):
2884 return Repository.query()\
2884 return Repository.query()\
2885 .filter(Repository.group == self)\
2885 .filter(Repository.group == self)\
2886 .order_by(Repository.repo_name)
2886 .order_by(Repository.repo_name)
2887
2887
2888 @property
2888 @property
2889 def repositories_recursive_count(self):
2889 def repositories_recursive_count(self):
2890 cnt = self.repositories.count()
2890 cnt = self.repositories.count()
2891
2891
2892 def children_count(group):
2892 def children_count(group):
2893 cnt = 0
2893 cnt = 0
2894 for child in group.children:
2894 for child in group.children:
2895 cnt += child.repositories.count()
2895 cnt += child.repositories.count()
2896 cnt += children_count(child)
2896 cnt += children_count(child)
2897 return cnt
2897 return cnt
2898
2898
2899 return cnt + children_count(self)
2899 return cnt + children_count(self)
2900
2900
2901 def _recursive_objects(self, include_repos=True, include_groups=True):
2901 def _recursive_objects(self, include_repos=True, include_groups=True):
2902 all_ = []
2902 all_ = []
2903
2903
2904 def _get_members(root_gr):
2904 def _get_members(root_gr):
2905 if include_repos:
2905 if include_repos:
2906 for r in root_gr.repositories:
2906 for r in root_gr.repositories:
2907 all_.append(r)
2907 all_.append(r)
2908 childs = root_gr.children.all()
2908 childs = root_gr.children.all()
2909 if childs:
2909 if childs:
2910 for gr in childs:
2910 for gr in childs:
2911 if include_groups:
2911 if include_groups:
2912 all_.append(gr)
2912 all_.append(gr)
2913 _get_members(gr)
2913 _get_members(gr)
2914
2914
2915 root_group = []
2915 root_group = []
2916 if include_groups:
2916 if include_groups:
2917 root_group = [self]
2917 root_group = [self]
2918
2918
2919 _get_members(self)
2919 _get_members(self)
2920 return root_group + all_
2920 return root_group + all_
2921
2921
2922 def recursive_groups_and_repos(self):
2922 def recursive_groups_and_repos(self):
2923 """
2923 """
2924 Recursive return all groups, with repositories in those groups
2924 Recursive return all groups, with repositories in those groups
2925 """
2925 """
2926 return self._recursive_objects()
2926 return self._recursive_objects()
2927
2927
2928 def recursive_groups(self):
2928 def recursive_groups(self):
2929 """
2929 """
2930 Returns all children groups for this group including children of children
2930 Returns all children groups for this group including children of children
2931 """
2931 """
2932 return self._recursive_objects(include_repos=False)
2932 return self._recursive_objects(include_repos=False)
2933
2933
2934 def recursive_repos(self):
2934 def recursive_repos(self):
2935 """
2935 """
2936 Returns all children repositories for this group
2936 Returns all children repositories for this group
2937 """
2937 """
2938 return self._recursive_objects(include_groups=False)
2938 return self._recursive_objects(include_groups=False)
2939
2939
2940 def get_new_name(self, group_name):
2940 def get_new_name(self, group_name):
2941 """
2941 """
2942 returns new full group name based on parent and new name
2942 returns new full group name based on parent and new name
2943
2943
2944 :param group_name:
2944 :param group_name:
2945 """
2945 """
2946 path_prefix = (self.parent_group.full_path_splitted if
2946 path_prefix = (self.parent_group.full_path_splitted if
2947 self.parent_group else [])
2947 self.parent_group else [])
2948 return RepoGroup.url_sep().join(path_prefix + [group_name])
2948 return RepoGroup.url_sep().join(path_prefix + [group_name])
2949
2949
2950 def update_commit_cache(self, config=None):
2950 def update_commit_cache(self, config=None):
2951 """
2951 """
2952 Update cache of last commit for newest repository inside this repository group.
2952 Update cache of last commit for newest repository inside this repository group.
2953 cache_keys should be::
2953 cache_keys should be::
2954
2954
2955 source_repo_id
2955 source_repo_id
2956 short_id
2956 short_id
2957 raw_id
2957 raw_id
2958 revision
2958 revision
2959 parents
2959 parents
2960 message
2960 message
2961 date
2961 date
2962 author
2962 author
2963
2963
2964 """
2964 """
2965 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2965 from rhodecode.lib.vcs.utils.helpers import parse_datetime
2966 empty_date = datetime.datetime.fromtimestamp(0)
2966 empty_date = datetime.datetime.fromtimestamp(0)
2967
2967
2968 def repo_groups_and_repos(root_gr):
2968 def repo_groups_and_repos(root_gr):
2969 for _repo in root_gr.repositories:
2969 for _repo in root_gr.repositories:
2970 yield _repo
2970 yield _repo
2971 for child_group in root_gr.children.all():
2971 for child_group in root_gr.children.all():
2972 yield child_group
2972 yield child_group
2973
2973
2974 latest_repo_cs_cache = {}
2974 latest_repo_cs_cache = {}
2975 for obj in repo_groups_and_repos(self):
2975 for obj in repo_groups_and_repos(self):
2976 repo_cs_cache = obj.changeset_cache
2976 repo_cs_cache = obj.changeset_cache
2977 date_latest = latest_repo_cs_cache.get('date', empty_date)
2977 date_latest = latest_repo_cs_cache.get('date', empty_date)
2978 date_current = repo_cs_cache.get('date', empty_date)
2978 date_current = repo_cs_cache.get('date', empty_date)
2979 current_timestamp = datetime_to_time(parse_datetime(date_latest))
2979 current_timestamp = datetime_to_time(parse_datetime(date_latest))
2980 if current_timestamp < datetime_to_time(parse_datetime(date_current)):
2980 if current_timestamp < datetime_to_time(parse_datetime(date_current)):
2981 latest_repo_cs_cache = repo_cs_cache
2981 latest_repo_cs_cache = repo_cs_cache
2982 if hasattr(obj, 'repo_id'):
2982 if hasattr(obj, 'repo_id'):
2983 latest_repo_cs_cache['source_repo_id'] = obj.repo_id
2983 latest_repo_cs_cache['source_repo_id'] = obj.repo_id
2984 else:
2984 else:
2985 latest_repo_cs_cache['source_repo_id'] = repo_cs_cache.get('source_repo_id')
2985 latest_repo_cs_cache['source_repo_id'] = repo_cs_cache.get('source_repo_id')
2986
2986
2987 _date_latest = parse_datetime(latest_repo_cs_cache.get('date') or empty_date)
2987 _date_latest = parse_datetime(latest_repo_cs_cache.get('date') or empty_date)
2988
2988
2989 latest_repo_cs_cache['updated_on'] = time.time()
2989 latest_repo_cs_cache['updated_on'] = time.time()
2990 self.changeset_cache = latest_repo_cs_cache
2990 self.changeset_cache = latest_repo_cs_cache
2991 self.updated_on = _date_latest
2991 self.updated_on = _date_latest
2992 Session().add(self)
2992 Session().add(self)
2993 Session().commit()
2993 Session().commit()
2994
2994
2995 log.debug('updated repo group `%s` with new commit cache %s, and last update_date: %s',
2995 log.debug('updated repo group `%s` with new commit cache %s, and last update_date: %s',
2996 self.group_name, latest_repo_cs_cache, _date_latest)
2996 self.group_name, latest_repo_cs_cache, _date_latest)
2997
2997
2998 def permissions(self, with_admins=True, with_owner=True,
2998 def permissions(self, with_admins=True, with_owner=True,
2999 expand_from_user_groups=False):
2999 expand_from_user_groups=False):
3000 """
3000 """
3001 Permissions for repository groups
3001 Permissions for repository groups
3002 """
3002 """
3003 _admin_perm = 'group.admin'
3003 _admin_perm = 'group.admin'
3004
3004
3005 owner_row = []
3005 owner_row = []
3006 if with_owner:
3006 if with_owner:
3007 usr = AttributeDict(self.user.get_dict())
3007 usr = AttributeDict(self.user.get_dict())
3008 usr.owner_row = True
3008 usr.owner_row = True
3009 usr.permission = _admin_perm
3009 usr.permission = _admin_perm
3010 owner_row.append(usr)
3010 owner_row.append(usr)
3011
3011
3012 super_admin_ids = []
3012 super_admin_ids = []
3013 super_admin_rows = []
3013 super_admin_rows = []
3014 if with_admins:
3014 if with_admins:
3015 for usr in User.get_all_super_admins():
3015 for usr in User.get_all_super_admins():
3016 super_admin_ids.append(usr.user_id)
3016 super_admin_ids.append(usr.user_id)
3017 # if this admin is also owner, don't double the record
3017 # if this admin is also owner, don't double the record
3018 if usr.user_id == owner_row[0].user_id:
3018 if usr.user_id == owner_row[0].user_id:
3019 owner_row[0].admin_row = True
3019 owner_row[0].admin_row = True
3020 else:
3020 else:
3021 usr = AttributeDict(usr.get_dict())
3021 usr = AttributeDict(usr.get_dict())
3022 usr.admin_row = True
3022 usr.admin_row = True
3023 usr.permission = _admin_perm
3023 usr.permission = _admin_perm
3024 super_admin_rows.append(usr)
3024 super_admin_rows.append(usr)
3025
3025
3026 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
3026 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
3027 q = q.options(joinedload(UserRepoGroupToPerm.group),
3027 q = q.options(joinedload(UserRepoGroupToPerm.group),
3028 joinedload(UserRepoGroupToPerm.user),
3028 joinedload(UserRepoGroupToPerm.user),
3029 joinedload(UserRepoGroupToPerm.permission),)
3029 joinedload(UserRepoGroupToPerm.permission),)
3030
3030
3031 # get owners and admins and permissions. We do a trick of re-writing
3031 # get owners and admins and permissions. We do a trick of re-writing
3032 # objects from sqlalchemy to named-tuples due to sqlalchemy session
3032 # objects from sqlalchemy to named-tuples due to sqlalchemy session
3033 # has a global reference and changing one object propagates to all
3033 # has a global reference and changing one object propagates to all
3034 # others. This means if admin is also an owner admin_row that change
3034 # others. This means if admin is also an owner admin_row that change
3035 # would propagate to both objects
3035 # would propagate to both objects
3036 perm_rows = []
3036 perm_rows = []
3037 for _usr in q.all():
3037 for _usr in q.all():
3038 usr = AttributeDict(_usr.user.get_dict())
3038 usr = AttributeDict(_usr.user.get_dict())
3039 # if this user is also owner/admin, mark as duplicate record
3039 # if this user is also owner/admin, mark as duplicate record
3040 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
3040 if usr.user_id == owner_row[0].user_id or usr.user_id in super_admin_ids:
3041 usr.duplicate_perm = True
3041 usr.duplicate_perm = True
3042 usr.permission = _usr.permission.permission_name
3042 usr.permission = _usr.permission.permission_name
3043 perm_rows.append(usr)
3043 perm_rows.append(usr)
3044
3044
3045 # filter the perm rows by 'default' first and then sort them by
3045 # filter the perm rows by 'default' first and then sort them by
3046 # admin,write,read,none permissions sorted again alphabetically in
3046 # admin,write,read,none permissions sorted again alphabetically in
3047 # each group
3047 # each group
3048 perm_rows = sorted(perm_rows, key=display_user_sort)
3048 perm_rows = sorted(perm_rows, key=display_user_sort)
3049
3049
3050 user_groups_rows = []
3050 user_groups_rows = []
3051 if expand_from_user_groups:
3051 if expand_from_user_groups:
3052 for ug in self.permission_user_groups(with_members=True):
3052 for ug in self.permission_user_groups(with_members=True):
3053 for user_data in ug.members:
3053 for user_data in ug.members:
3054 user_groups_rows.append(user_data)
3054 user_groups_rows.append(user_data)
3055
3055
3056 return super_admin_rows + owner_row + perm_rows + user_groups_rows
3056 return super_admin_rows + owner_row + perm_rows + user_groups_rows
3057
3057
3058 def permission_user_groups(self, with_members=False):
3058 def permission_user_groups(self, with_members=False):
3059 q = UserGroupRepoGroupToPerm.query()\
3059 q = UserGroupRepoGroupToPerm.query()\
3060 .filter(UserGroupRepoGroupToPerm.group == self)
3060 .filter(UserGroupRepoGroupToPerm.group == self)
3061 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
3061 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
3062 joinedload(UserGroupRepoGroupToPerm.users_group),
3062 joinedload(UserGroupRepoGroupToPerm.users_group),
3063 joinedload(UserGroupRepoGroupToPerm.permission),)
3063 joinedload(UserGroupRepoGroupToPerm.permission),)
3064
3064
3065 perm_rows = []
3065 perm_rows = []
3066 for _user_group in q.all():
3066 for _user_group in q.all():
3067 entry = AttributeDict(_user_group.users_group.get_dict())
3067 entry = AttributeDict(_user_group.users_group.get_dict())
3068 entry.permission = _user_group.permission.permission_name
3068 entry.permission = _user_group.permission.permission_name
3069 if with_members:
3069 if with_members:
3070 entry.members = [x.user.get_dict()
3070 entry.members = [x.user.get_dict()
3071 for x in _user_group.users_group.members]
3071 for x in _user_group.users_group.members]
3072 perm_rows.append(entry)
3072 perm_rows.append(entry)
3073
3073
3074 perm_rows = sorted(perm_rows, key=display_user_group_sort)
3074 perm_rows = sorted(perm_rows, key=display_user_group_sort)
3075 return perm_rows
3075 return perm_rows
3076
3076
3077 def get_api_data(self):
3077 def get_api_data(self):
3078 """
3078 """
3079 Common function for generating api data
3079 Common function for generating api data
3080
3080
3081 """
3081 """
3082 group = self
3082 group = self
3083 data = {
3083 data = {
3084 'group_id': group.group_id,
3084 'group_id': group.group_id,
3085 'group_name': group.group_name,
3085 'group_name': group.group_name,
3086 'group_description': group.description_safe,
3086 'group_description': group.description_safe,
3087 'parent_group': group.parent_group.group_name if group.parent_group else None,
3087 'parent_group': group.parent_group.group_name if group.parent_group else None,
3088 'repositories': [x.repo_name for x in group.repositories],
3088 'repositories': [x.repo_name for x in group.repositories],
3089 'owner': group.user.username,
3089 'owner': group.user.username,
3090 }
3090 }
3091 return data
3091 return data
3092
3092
3093 def get_dict(self):
3093 def get_dict(self):
3094 # Since we transformed `group_name` to a hybrid property, we need to
3094 # Since we transformed `group_name` to a hybrid property, we need to
3095 # keep compatibility with the code which uses `group_name` field.
3095 # keep compatibility with the code which uses `group_name` field.
3096 result = super(RepoGroup, self).get_dict()
3096 result = super(RepoGroup, self).get_dict()
3097 result['group_name'] = result.pop('_group_name', None)
3097 result['group_name'] = result.pop('_group_name', None)
3098 return result
3098 return result
3099
3099
3100
3100
3101 class Permission(Base, BaseModel):
3101 class Permission(Base, BaseModel):
3102 __tablename__ = 'permissions'
3102 __tablename__ = 'permissions'
3103 __table_args__ = (
3103 __table_args__ = (
3104 Index('p_perm_name_idx', 'permission_name'),
3104 Index('p_perm_name_idx', 'permission_name'),
3105 base_table_args,
3105 base_table_args,
3106 )
3106 )
3107
3107
3108 PERMS = [
3108 PERMS = [
3109 ('hg.admin', _('RhodeCode Super Administrator')),
3109 ('hg.admin', _('RhodeCode Super Administrator')),
3110
3110
3111 ('repository.none', _('Repository no access')),
3111 ('repository.none', _('Repository no access')),
3112 ('repository.read', _('Repository read access')),
3112 ('repository.read', _('Repository read access')),
3113 ('repository.write', _('Repository write access')),
3113 ('repository.write', _('Repository write access')),
3114 ('repository.admin', _('Repository admin access')),
3114 ('repository.admin', _('Repository admin access')),
3115
3115
3116 ('group.none', _('Repository group no access')),
3116 ('group.none', _('Repository group no access')),
3117 ('group.read', _('Repository group read access')),
3117 ('group.read', _('Repository group read access')),
3118 ('group.write', _('Repository group write access')),
3118 ('group.write', _('Repository group write access')),
3119 ('group.admin', _('Repository group admin access')),
3119 ('group.admin', _('Repository group admin access')),
3120
3120
3121 ('usergroup.none', _('User group no access')),
3121 ('usergroup.none', _('User group no access')),
3122 ('usergroup.read', _('User group read access')),
3122 ('usergroup.read', _('User group read access')),
3123 ('usergroup.write', _('User group write access')),
3123 ('usergroup.write', _('User group write access')),
3124 ('usergroup.admin', _('User group admin access')),
3124 ('usergroup.admin', _('User group admin access')),
3125
3125
3126 ('branch.none', _('Branch no permissions')),
3126 ('branch.none', _('Branch no permissions')),
3127 ('branch.merge', _('Branch access by web merge')),
3127 ('branch.merge', _('Branch access by web merge')),
3128 ('branch.push', _('Branch access by push')),
3128 ('branch.push', _('Branch access by push')),
3129 ('branch.push_force', _('Branch access by push with force')),
3129 ('branch.push_force', _('Branch access by push with force')),
3130
3130
3131 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
3131 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
3132 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
3132 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
3133
3133
3134 ('hg.usergroup.create.false', _('User Group creation disabled')),
3134 ('hg.usergroup.create.false', _('User Group creation disabled')),
3135 ('hg.usergroup.create.true', _('User Group creation enabled')),
3135 ('hg.usergroup.create.true', _('User Group creation enabled')),
3136
3136
3137 ('hg.create.none', _('Repository creation disabled')),
3137 ('hg.create.none', _('Repository creation disabled')),
3138 ('hg.create.repository', _('Repository creation enabled')),
3138 ('hg.create.repository', _('Repository creation enabled')),
3139 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
3139 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
3140 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
3140 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
3141
3141
3142 ('hg.fork.none', _('Repository forking disabled')),
3142 ('hg.fork.none', _('Repository forking disabled')),
3143 ('hg.fork.repository', _('Repository forking enabled')),
3143 ('hg.fork.repository', _('Repository forking enabled')),
3144
3144
3145 ('hg.register.none', _('Registration disabled')),
3145 ('hg.register.none', _('Registration disabled')),
3146 ('hg.register.manual_activate', _('User Registration with manual account activation')),
3146 ('hg.register.manual_activate', _('User Registration with manual account activation')),
3147 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
3147 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
3148
3148
3149 ('hg.password_reset.enabled', _('Password reset enabled')),
3149 ('hg.password_reset.enabled', _('Password reset enabled')),
3150 ('hg.password_reset.hidden', _('Password reset hidden')),
3150 ('hg.password_reset.hidden', _('Password reset hidden')),
3151 ('hg.password_reset.disabled', _('Password reset disabled')),
3151 ('hg.password_reset.disabled', _('Password reset disabled')),
3152
3152
3153 ('hg.extern_activate.manual', _('Manual activation of external account')),
3153 ('hg.extern_activate.manual', _('Manual activation of external account')),
3154 ('hg.extern_activate.auto', _('Automatic activation of external account')),
3154 ('hg.extern_activate.auto', _('Automatic activation of external account')),
3155
3155
3156 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
3156 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
3157 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
3157 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
3158 ]
3158 ]
3159
3159
3160 # definition of system default permissions for DEFAULT user, created on
3160 # definition of system default permissions for DEFAULT user, created on
3161 # system setup
3161 # system setup
3162 DEFAULT_USER_PERMISSIONS = [
3162 DEFAULT_USER_PERMISSIONS = [
3163 # object perms
3163 # object perms
3164 'repository.read',
3164 'repository.read',
3165 'group.read',
3165 'group.read',
3166 'usergroup.read',
3166 'usergroup.read',
3167 # branch, for backward compat we need same value as before so forced pushed
3167 # branch, for backward compat we need same value as before so forced pushed
3168 'branch.push_force',
3168 'branch.push_force',
3169 # global
3169 # global
3170 'hg.create.repository',
3170 'hg.create.repository',
3171 'hg.repogroup.create.false',
3171 'hg.repogroup.create.false',
3172 'hg.usergroup.create.false',
3172 'hg.usergroup.create.false',
3173 'hg.create.write_on_repogroup.true',
3173 'hg.create.write_on_repogroup.true',
3174 'hg.fork.repository',
3174 'hg.fork.repository',
3175 'hg.register.manual_activate',
3175 'hg.register.manual_activate',
3176 'hg.password_reset.enabled',
3176 'hg.password_reset.enabled',
3177 'hg.extern_activate.auto',
3177 'hg.extern_activate.auto',
3178 'hg.inherit_default_perms.true',
3178 'hg.inherit_default_perms.true',
3179 ]
3179 ]
3180
3180
3181 # defines which permissions are more important higher the more important
3181 # defines which permissions are more important higher the more important
3182 # Weight defines which permissions are more important.
3182 # Weight defines which permissions are more important.
3183 # The higher number the more important.
3183 # The higher number the more important.
3184 PERM_WEIGHTS = {
3184 PERM_WEIGHTS = {
3185 'repository.none': 0,
3185 'repository.none': 0,
3186 'repository.read': 1,
3186 'repository.read': 1,
3187 'repository.write': 3,
3187 'repository.write': 3,
3188 'repository.admin': 4,
3188 'repository.admin': 4,
3189
3189
3190 'group.none': 0,
3190 'group.none': 0,
3191 'group.read': 1,
3191 'group.read': 1,
3192 'group.write': 3,
3192 'group.write': 3,
3193 'group.admin': 4,
3193 'group.admin': 4,
3194
3194
3195 'usergroup.none': 0,
3195 'usergroup.none': 0,
3196 'usergroup.read': 1,
3196 'usergroup.read': 1,
3197 'usergroup.write': 3,
3197 'usergroup.write': 3,
3198 'usergroup.admin': 4,
3198 'usergroup.admin': 4,
3199
3199
3200 'branch.none': 0,
3200 'branch.none': 0,
3201 'branch.merge': 1,
3201 'branch.merge': 1,
3202 'branch.push': 3,
3202 'branch.push': 3,
3203 'branch.push_force': 4,
3203 'branch.push_force': 4,
3204
3204
3205 'hg.repogroup.create.false': 0,
3205 'hg.repogroup.create.false': 0,
3206 'hg.repogroup.create.true': 1,
3206 'hg.repogroup.create.true': 1,
3207
3207
3208 'hg.usergroup.create.false': 0,
3208 'hg.usergroup.create.false': 0,
3209 'hg.usergroup.create.true': 1,
3209 'hg.usergroup.create.true': 1,
3210
3210
3211 'hg.fork.none': 0,
3211 'hg.fork.none': 0,
3212 'hg.fork.repository': 1,
3212 'hg.fork.repository': 1,
3213 'hg.create.none': 0,
3213 'hg.create.none': 0,
3214 'hg.create.repository': 1
3214 'hg.create.repository': 1
3215 }
3215 }
3216
3216
3217 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3217 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3218 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
3218 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
3219 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
3219 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
3220
3220
3221 def __unicode__(self):
3221 def __unicode__(self):
3222 return u"<%s('%s:%s')>" % (
3222 return u"<%s('%s:%s')>" % (
3223 self.__class__.__name__, self.permission_id, self.permission_name
3223 self.__class__.__name__, self.permission_id, self.permission_name
3224 )
3224 )
3225
3225
3226 @classmethod
3226 @classmethod
3227 def get_by_key(cls, key):
3227 def get_by_key(cls, key):
3228 return cls.query().filter(cls.permission_name == key).scalar()
3228 return cls.query().filter(cls.permission_name == key).scalar()
3229
3229
3230 @classmethod
3230 @classmethod
3231 def get_default_repo_perms(cls, user_id, repo_id=None):
3231 def get_default_repo_perms(cls, user_id, repo_id=None):
3232 q = Session().query(UserRepoToPerm, Repository, Permission)\
3232 q = Session().query(UserRepoToPerm, Repository, Permission)\
3233 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
3233 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
3234 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
3234 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
3235 .filter(UserRepoToPerm.user_id == user_id)
3235 .filter(UserRepoToPerm.user_id == user_id)
3236 if repo_id:
3236 if repo_id:
3237 q = q.filter(UserRepoToPerm.repository_id == repo_id)
3237 q = q.filter(UserRepoToPerm.repository_id == repo_id)
3238 return q.all()
3238 return q.all()
3239
3239
3240 @classmethod
3240 @classmethod
3241 def get_default_repo_branch_perms(cls, user_id, repo_id=None):
3241 def get_default_repo_branch_perms(cls, user_id, repo_id=None):
3242 q = Session().query(UserToRepoBranchPermission, UserRepoToPerm, Permission) \
3242 q = Session().query(UserToRepoBranchPermission, UserRepoToPerm, Permission) \
3243 .join(
3243 .join(
3244 Permission,
3244 Permission,
3245 UserToRepoBranchPermission.permission_id == Permission.permission_id) \
3245 UserToRepoBranchPermission.permission_id == Permission.permission_id) \
3246 .join(
3246 .join(
3247 UserRepoToPerm,
3247 UserRepoToPerm,
3248 UserToRepoBranchPermission.rule_to_perm_id == UserRepoToPerm.repo_to_perm_id) \
3248 UserToRepoBranchPermission.rule_to_perm_id == UserRepoToPerm.repo_to_perm_id) \
3249 .filter(UserRepoToPerm.user_id == user_id)
3249 .filter(UserRepoToPerm.user_id == user_id)
3250
3250
3251 if repo_id:
3251 if repo_id:
3252 q = q.filter(UserToRepoBranchPermission.repository_id == repo_id)
3252 q = q.filter(UserToRepoBranchPermission.repository_id == repo_id)
3253 return q.order_by(UserToRepoBranchPermission.rule_order).all()
3253 return q.order_by(UserToRepoBranchPermission.rule_order).all()
3254
3254
3255 @classmethod
3255 @classmethod
3256 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
3256 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
3257 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
3257 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
3258 .join(
3258 .join(
3259 Permission,
3259 Permission,
3260 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
3260 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
3261 .join(
3261 .join(
3262 Repository,
3262 Repository,
3263 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
3263 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
3264 .join(
3264 .join(
3265 UserGroup,
3265 UserGroup,
3266 UserGroupRepoToPerm.users_group_id ==
3266 UserGroupRepoToPerm.users_group_id ==
3267 UserGroup.users_group_id)\
3267 UserGroup.users_group_id)\
3268 .join(
3268 .join(
3269 UserGroupMember,
3269 UserGroupMember,
3270 UserGroupRepoToPerm.users_group_id ==
3270 UserGroupRepoToPerm.users_group_id ==
3271 UserGroupMember.users_group_id)\
3271 UserGroupMember.users_group_id)\
3272 .filter(
3272 .filter(
3273 UserGroupMember.user_id == user_id,
3273 UserGroupMember.user_id == user_id,
3274 UserGroup.users_group_active == true())
3274 UserGroup.users_group_active == true())
3275 if repo_id:
3275 if repo_id:
3276 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
3276 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
3277 return q.all()
3277 return q.all()
3278
3278
3279 @classmethod
3279 @classmethod
3280 def get_default_repo_branch_perms_from_user_group(cls, user_id, repo_id=None):
3280 def get_default_repo_branch_perms_from_user_group(cls, user_id, repo_id=None):
3281 q = Session().query(UserGroupToRepoBranchPermission, UserGroupRepoToPerm, Permission) \
3281 q = Session().query(UserGroupToRepoBranchPermission, UserGroupRepoToPerm, Permission) \
3282 .join(
3282 .join(
3283 Permission,
3283 Permission,
3284 UserGroupToRepoBranchPermission.permission_id == Permission.permission_id) \
3284 UserGroupToRepoBranchPermission.permission_id == Permission.permission_id) \
3285 .join(
3285 .join(
3286 UserGroupRepoToPerm,
3286 UserGroupRepoToPerm,
3287 UserGroupToRepoBranchPermission.rule_to_perm_id == UserGroupRepoToPerm.users_group_to_perm_id) \
3287 UserGroupToRepoBranchPermission.rule_to_perm_id == UserGroupRepoToPerm.users_group_to_perm_id) \
3288 .join(
3288 .join(
3289 UserGroup,
3289 UserGroup,
3290 UserGroupRepoToPerm.users_group_id == UserGroup.users_group_id) \
3290 UserGroupRepoToPerm.users_group_id == UserGroup.users_group_id) \
3291 .join(
3291 .join(
3292 UserGroupMember,
3292 UserGroupMember,
3293 UserGroupRepoToPerm.users_group_id == UserGroupMember.users_group_id) \
3293 UserGroupRepoToPerm.users_group_id == UserGroupMember.users_group_id) \
3294 .filter(
3294 .filter(
3295 UserGroupMember.user_id == user_id,
3295 UserGroupMember.user_id == user_id,
3296 UserGroup.users_group_active == true())
3296 UserGroup.users_group_active == true())
3297
3297
3298 if repo_id:
3298 if repo_id:
3299 q = q.filter(UserGroupToRepoBranchPermission.repository_id == repo_id)
3299 q = q.filter(UserGroupToRepoBranchPermission.repository_id == repo_id)
3300 return q.order_by(UserGroupToRepoBranchPermission.rule_order).all()
3300 return q.order_by(UserGroupToRepoBranchPermission.rule_order).all()
3301
3301
3302 @classmethod
3302 @classmethod
3303 def get_default_group_perms(cls, user_id, repo_group_id=None):
3303 def get_default_group_perms(cls, user_id, repo_group_id=None):
3304 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
3304 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
3305 .join(
3305 .join(
3306 Permission,
3306 Permission,
3307 UserRepoGroupToPerm.permission_id == Permission.permission_id)\
3307 UserRepoGroupToPerm.permission_id == Permission.permission_id)\
3308 .join(
3308 .join(
3309 RepoGroup,
3309 RepoGroup,
3310 UserRepoGroupToPerm.group_id == RepoGroup.group_id)\
3310 UserRepoGroupToPerm.group_id == RepoGroup.group_id)\
3311 .filter(UserRepoGroupToPerm.user_id == user_id)
3311 .filter(UserRepoGroupToPerm.user_id == user_id)
3312 if repo_group_id:
3312 if repo_group_id:
3313 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
3313 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
3314 return q.all()
3314 return q.all()
3315
3315
3316 @classmethod
3316 @classmethod
3317 def get_default_group_perms_from_user_group(
3317 def get_default_group_perms_from_user_group(
3318 cls, user_id, repo_group_id=None):
3318 cls, user_id, repo_group_id=None):
3319 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
3319 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
3320 .join(
3320 .join(
3321 Permission,
3321 Permission,
3322 UserGroupRepoGroupToPerm.permission_id ==
3322 UserGroupRepoGroupToPerm.permission_id ==
3323 Permission.permission_id)\
3323 Permission.permission_id)\
3324 .join(
3324 .join(
3325 RepoGroup,
3325 RepoGroup,
3326 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
3326 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
3327 .join(
3327 .join(
3328 UserGroup,
3328 UserGroup,
3329 UserGroupRepoGroupToPerm.users_group_id ==
3329 UserGroupRepoGroupToPerm.users_group_id ==
3330 UserGroup.users_group_id)\
3330 UserGroup.users_group_id)\
3331 .join(
3331 .join(
3332 UserGroupMember,
3332 UserGroupMember,
3333 UserGroupRepoGroupToPerm.users_group_id ==
3333 UserGroupRepoGroupToPerm.users_group_id ==
3334 UserGroupMember.users_group_id)\
3334 UserGroupMember.users_group_id)\
3335 .filter(
3335 .filter(
3336 UserGroupMember.user_id == user_id,
3336 UserGroupMember.user_id == user_id,
3337 UserGroup.users_group_active == true())
3337 UserGroup.users_group_active == true())
3338 if repo_group_id:
3338 if repo_group_id:
3339 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
3339 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
3340 return q.all()
3340 return q.all()
3341
3341
3342 @classmethod
3342 @classmethod
3343 def get_default_user_group_perms(cls, user_id, user_group_id=None):
3343 def get_default_user_group_perms(cls, user_id, user_group_id=None):
3344 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
3344 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
3345 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
3345 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
3346 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
3346 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
3347 .filter(UserUserGroupToPerm.user_id == user_id)
3347 .filter(UserUserGroupToPerm.user_id == user_id)
3348 if user_group_id:
3348 if user_group_id:
3349 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
3349 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
3350 return q.all()
3350 return q.all()
3351
3351
3352 @classmethod
3352 @classmethod
3353 def get_default_user_group_perms_from_user_group(
3353 def get_default_user_group_perms_from_user_group(
3354 cls, user_id, user_group_id=None):
3354 cls, user_id, user_group_id=None):
3355 TargetUserGroup = aliased(UserGroup, name='target_user_group')
3355 TargetUserGroup = aliased(UserGroup, name='target_user_group')
3356 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
3356 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
3357 .join(
3357 .join(
3358 Permission,
3358 Permission,
3359 UserGroupUserGroupToPerm.permission_id ==
3359 UserGroupUserGroupToPerm.permission_id ==
3360 Permission.permission_id)\
3360 Permission.permission_id)\
3361 .join(
3361 .join(
3362 TargetUserGroup,
3362 TargetUserGroup,
3363 UserGroupUserGroupToPerm.target_user_group_id ==
3363 UserGroupUserGroupToPerm.target_user_group_id ==
3364 TargetUserGroup.users_group_id)\
3364 TargetUserGroup.users_group_id)\
3365 .join(
3365 .join(
3366 UserGroup,
3366 UserGroup,
3367 UserGroupUserGroupToPerm.user_group_id ==
3367 UserGroupUserGroupToPerm.user_group_id ==
3368 UserGroup.users_group_id)\
3368 UserGroup.users_group_id)\
3369 .join(
3369 .join(
3370 UserGroupMember,
3370 UserGroupMember,
3371 UserGroupUserGroupToPerm.user_group_id ==
3371 UserGroupUserGroupToPerm.user_group_id ==
3372 UserGroupMember.users_group_id)\
3372 UserGroupMember.users_group_id)\
3373 .filter(
3373 .filter(
3374 UserGroupMember.user_id == user_id,
3374 UserGroupMember.user_id == user_id,
3375 UserGroup.users_group_active == true())
3375 UserGroup.users_group_active == true())
3376 if user_group_id:
3376 if user_group_id:
3377 q = q.filter(
3377 q = q.filter(
3378 UserGroupUserGroupToPerm.user_group_id == user_group_id)
3378 UserGroupUserGroupToPerm.user_group_id == user_group_id)
3379
3379
3380 return q.all()
3380 return q.all()
3381
3381
3382
3382
3383 class UserRepoToPerm(Base, BaseModel):
3383 class UserRepoToPerm(Base, BaseModel):
3384 __tablename__ = 'repo_to_perm'
3384 __tablename__ = 'repo_to_perm'
3385 __table_args__ = (
3385 __table_args__ = (
3386 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
3386 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
3387 base_table_args
3387 base_table_args
3388 )
3388 )
3389
3389
3390 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3390 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3391 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3391 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3392 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3392 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3393 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
3393 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
3394
3394
3395 user = relationship('User')
3395 user = relationship('User')
3396 repository = relationship('Repository')
3396 repository = relationship('Repository')
3397 permission = relationship('Permission')
3397 permission = relationship('Permission')
3398
3398
3399 branch_perm_entry = relationship('UserToRepoBranchPermission', cascade="all, delete-orphan", lazy='joined')
3399 branch_perm_entry = relationship('UserToRepoBranchPermission', cascade="all, delete-orphan", lazy='joined')
3400
3400
3401 @classmethod
3401 @classmethod
3402 def create(cls, user, repository, permission):
3402 def create(cls, user, repository, permission):
3403 n = cls()
3403 n = cls()
3404 n.user = user
3404 n.user = user
3405 n.repository = repository
3405 n.repository = repository
3406 n.permission = permission
3406 n.permission = permission
3407 Session().add(n)
3407 Session().add(n)
3408 return n
3408 return n
3409
3409
3410 def __unicode__(self):
3410 def __unicode__(self):
3411 return u'<%s => %s >' % (self.user, self.repository)
3411 return u'<%s => %s >' % (self.user, self.repository)
3412
3412
3413
3413
3414 class UserUserGroupToPerm(Base, BaseModel):
3414 class UserUserGroupToPerm(Base, BaseModel):
3415 __tablename__ = 'user_user_group_to_perm'
3415 __tablename__ = 'user_user_group_to_perm'
3416 __table_args__ = (
3416 __table_args__ = (
3417 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
3417 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
3418 base_table_args
3418 base_table_args
3419 )
3419 )
3420
3420
3421 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3421 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3422 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3422 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3423 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3423 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3424 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3424 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3425
3425
3426 user = relationship('User')
3426 user = relationship('User')
3427 user_group = relationship('UserGroup')
3427 user_group = relationship('UserGroup')
3428 permission = relationship('Permission')
3428 permission = relationship('Permission')
3429
3429
3430 @classmethod
3430 @classmethod
3431 def create(cls, user, user_group, permission):
3431 def create(cls, user, user_group, permission):
3432 n = cls()
3432 n = cls()
3433 n.user = user
3433 n.user = user
3434 n.user_group = user_group
3434 n.user_group = user_group
3435 n.permission = permission
3435 n.permission = permission
3436 Session().add(n)
3436 Session().add(n)
3437 return n
3437 return n
3438
3438
3439 def __unicode__(self):
3439 def __unicode__(self):
3440 return u'<%s => %s >' % (self.user, self.user_group)
3440 return u'<%s => %s >' % (self.user, self.user_group)
3441
3441
3442
3442
3443 class UserToPerm(Base, BaseModel):
3443 class UserToPerm(Base, BaseModel):
3444 __tablename__ = 'user_to_perm'
3444 __tablename__ = 'user_to_perm'
3445 __table_args__ = (
3445 __table_args__ = (
3446 UniqueConstraint('user_id', 'permission_id'),
3446 UniqueConstraint('user_id', 'permission_id'),
3447 base_table_args
3447 base_table_args
3448 )
3448 )
3449
3449
3450 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3450 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3451 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3451 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3452 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3452 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3453
3453
3454 user = relationship('User')
3454 user = relationship('User')
3455 permission = relationship('Permission', lazy='joined')
3455 permission = relationship('Permission', lazy='joined')
3456
3456
3457 def __unicode__(self):
3457 def __unicode__(self):
3458 return u'<%s => %s >' % (self.user, self.permission)
3458 return u'<%s => %s >' % (self.user, self.permission)
3459
3459
3460
3460
3461 class UserGroupRepoToPerm(Base, BaseModel):
3461 class UserGroupRepoToPerm(Base, BaseModel):
3462 __tablename__ = 'users_group_repo_to_perm'
3462 __tablename__ = 'users_group_repo_to_perm'
3463 __table_args__ = (
3463 __table_args__ = (
3464 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
3464 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
3465 base_table_args
3465 base_table_args
3466 )
3466 )
3467
3467
3468 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3468 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3469 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3469 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3470 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3470 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3471 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
3471 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
3472
3472
3473 users_group = relationship('UserGroup')
3473 users_group = relationship('UserGroup')
3474 permission = relationship('Permission')
3474 permission = relationship('Permission')
3475 repository = relationship('Repository')
3475 repository = relationship('Repository')
3476 user_group_branch_perms = relationship('UserGroupToRepoBranchPermission', cascade='all')
3476 user_group_branch_perms = relationship('UserGroupToRepoBranchPermission', cascade='all')
3477
3477
3478 @classmethod
3478 @classmethod
3479 def create(cls, users_group, repository, permission):
3479 def create(cls, users_group, repository, permission):
3480 n = cls()
3480 n = cls()
3481 n.users_group = users_group
3481 n.users_group = users_group
3482 n.repository = repository
3482 n.repository = repository
3483 n.permission = permission
3483 n.permission = permission
3484 Session().add(n)
3484 Session().add(n)
3485 return n
3485 return n
3486
3486
3487 def __unicode__(self):
3487 def __unicode__(self):
3488 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
3488 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
3489
3489
3490
3490
3491 class UserGroupUserGroupToPerm(Base, BaseModel):
3491 class UserGroupUserGroupToPerm(Base, BaseModel):
3492 __tablename__ = 'user_group_user_group_to_perm'
3492 __tablename__ = 'user_group_user_group_to_perm'
3493 __table_args__ = (
3493 __table_args__ = (
3494 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
3494 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
3495 CheckConstraint('target_user_group_id != user_group_id'),
3495 CheckConstraint('target_user_group_id != user_group_id'),
3496 base_table_args
3496 base_table_args
3497 )
3497 )
3498
3498
3499 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3499 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3500 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3500 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3501 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3501 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3502 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3502 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3503
3503
3504 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
3504 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
3505 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
3505 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
3506 permission = relationship('Permission')
3506 permission = relationship('Permission')
3507
3507
3508 @classmethod
3508 @classmethod
3509 def create(cls, target_user_group, user_group, permission):
3509 def create(cls, target_user_group, user_group, permission):
3510 n = cls()
3510 n = cls()
3511 n.target_user_group = target_user_group
3511 n.target_user_group = target_user_group
3512 n.user_group = user_group
3512 n.user_group = user_group
3513 n.permission = permission
3513 n.permission = permission
3514 Session().add(n)
3514 Session().add(n)
3515 return n
3515 return n
3516
3516
3517 def __unicode__(self):
3517 def __unicode__(self):
3518 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3518 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3519
3519
3520
3520
3521 class UserGroupToPerm(Base, BaseModel):
3521 class UserGroupToPerm(Base, BaseModel):
3522 __tablename__ = 'users_group_to_perm'
3522 __tablename__ = 'users_group_to_perm'
3523 __table_args__ = (
3523 __table_args__ = (
3524 UniqueConstraint('users_group_id', 'permission_id',),
3524 UniqueConstraint('users_group_id', 'permission_id',),
3525 base_table_args
3525 base_table_args
3526 )
3526 )
3527
3527
3528 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3528 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3529 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3529 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3530 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3530 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3531
3531
3532 users_group = relationship('UserGroup')
3532 users_group = relationship('UserGroup')
3533 permission = relationship('Permission')
3533 permission = relationship('Permission')
3534
3534
3535
3535
3536 class UserRepoGroupToPerm(Base, BaseModel):
3536 class UserRepoGroupToPerm(Base, BaseModel):
3537 __tablename__ = 'user_repo_group_to_perm'
3537 __tablename__ = 'user_repo_group_to_perm'
3538 __table_args__ = (
3538 __table_args__ = (
3539 UniqueConstraint('user_id', 'group_id', 'permission_id'),
3539 UniqueConstraint('user_id', 'group_id', 'permission_id'),
3540 base_table_args
3540 base_table_args
3541 )
3541 )
3542
3542
3543 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3543 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3544 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3544 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3545 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3545 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3546 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3546 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3547
3547
3548 user = relationship('User')
3548 user = relationship('User')
3549 group = relationship('RepoGroup')
3549 group = relationship('RepoGroup')
3550 permission = relationship('Permission')
3550 permission = relationship('Permission')
3551
3551
3552 @classmethod
3552 @classmethod
3553 def create(cls, user, repository_group, permission):
3553 def create(cls, user, repository_group, permission):
3554 n = cls()
3554 n = cls()
3555 n.user = user
3555 n.user = user
3556 n.group = repository_group
3556 n.group = repository_group
3557 n.permission = permission
3557 n.permission = permission
3558 Session().add(n)
3558 Session().add(n)
3559 return n
3559 return n
3560
3560
3561
3561
3562 class UserGroupRepoGroupToPerm(Base, BaseModel):
3562 class UserGroupRepoGroupToPerm(Base, BaseModel):
3563 __tablename__ = 'users_group_repo_group_to_perm'
3563 __tablename__ = 'users_group_repo_group_to_perm'
3564 __table_args__ = (
3564 __table_args__ = (
3565 UniqueConstraint('users_group_id', 'group_id'),
3565 UniqueConstraint('users_group_id', 'group_id'),
3566 base_table_args
3566 base_table_args
3567 )
3567 )
3568
3568
3569 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3569 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3570 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3570 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3571 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3571 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3572 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3572 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3573
3573
3574 users_group = relationship('UserGroup')
3574 users_group = relationship('UserGroup')
3575 permission = relationship('Permission')
3575 permission = relationship('Permission')
3576 group = relationship('RepoGroup')
3576 group = relationship('RepoGroup')
3577
3577
3578 @classmethod
3578 @classmethod
3579 def create(cls, user_group, repository_group, permission):
3579 def create(cls, user_group, repository_group, permission):
3580 n = cls()
3580 n = cls()
3581 n.users_group = user_group
3581 n.users_group = user_group
3582 n.group = repository_group
3582 n.group = repository_group
3583 n.permission = permission
3583 n.permission = permission
3584 Session().add(n)
3584 Session().add(n)
3585 return n
3585 return n
3586
3586
3587 def __unicode__(self):
3587 def __unicode__(self):
3588 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3588 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3589
3589
3590
3590
3591 class Statistics(Base, BaseModel):
3591 class Statistics(Base, BaseModel):
3592 __tablename__ = 'statistics'
3592 __tablename__ = 'statistics'
3593 __table_args__ = (
3593 __table_args__ = (
3594 base_table_args
3594 base_table_args
3595 )
3595 )
3596
3596
3597 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3597 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3598 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
3598 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
3599 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
3599 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
3600 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
3600 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
3601 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
3601 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
3602 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
3602 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
3603
3603
3604 repository = relationship('Repository', single_parent=True)
3604 repository = relationship('Repository', single_parent=True)
3605
3605
3606
3606
3607 class UserFollowing(Base, BaseModel):
3607 class UserFollowing(Base, BaseModel):
3608 __tablename__ = 'user_followings'
3608 __tablename__ = 'user_followings'
3609 __table_args__ = (
3609 __table_args__ = (
3610 UniqueConstraint('user_id', 'follows_repository_id'),
3610 UniqueConstraint('user_id', 'follows_repository_id'),
3611 UniqueConstraint('user_id', 'follows_user_id'),
3611 UniqueConstraint('user_id', 'follows_user_id'),
3612 base_table_args
3612 base_table_args
3613 )
3613 )
3614
3614
3615 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3615 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3616 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3616 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3617 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
3617 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
3618 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
3618 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
3619 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
3619 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
3620
3620
3621 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
3621 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
3622
3622
3623 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
3623 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
3624 follows_repository = relationship('Repository', order_by='Repository.repo_name')
3624 follows_repository = relationship('Repository', order_by='Repository.repo_name')
3625
3625
3626 @classmethod
3626 @classmethod
3627 def get_repo_followers(cls, repo_id):
3627 def get_repo_followers(cls, repo_id):
3628 return cls.query().filter(cls.follows_repo_id == repo_id)
3628 return cls.query().filter(cls.follows_repo_id == repo_id)
3629
3629
3630
3630
3631 class CacheKey(Base, BaseModel):
3631 class CacheKey(Base, BaseModel):
3632 __tablename__ = 'cache_invalidation'
3632 __tablename__ = 'cache_invalidation'
3633 __table_args__ = (
3633 __table_args__ = (
3634 UniqueConstraint('cache_key'),
3634 UniqueConstraint('cache_key'),
3635 Index('key_idx', 'cache_key'),
3635 Index('key_idx', 'cache_key'),
3636 base_table_args,
3636 base_table_args,
3637 )
3637 )
3638
3638
3639 CACHE_TYPE_FEED = 'FEED'
3639 CACHE_TYPE_FEED = 'FEED'
3640
3640
3641 # namespaces used to register process/thread aware caches
3641 # namespaces used to register process/thread aware caches
3642 REPO_INVALIDATION_NAMESPACE = 'repo_cache:{repo_id}'
3642 REPO_INVALIDATION_NAMESPACE = 'repo_cache:{repo_id}'
3643 SETTINGS_INVALIDATION_NAMESPACE = 'system_settings'
3643 SETTINGS_INVALIDATION_NAMESPACE = 'system_settings'
3644
3644
3645 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3645 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3646 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
3646 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
3647 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
3647 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
3648 cache_state_uid = Column("cache_state_uid", String(255), nullable=True, unique=None, default=None)
3648 cache_state_uid = Column("cache_state_uid", String(255), nullable=True, unique=None, default=None)
3649 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
3649 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
3650
3650
3651 def __init__(self, cache_key, cache_args='', cache_state_uid=None):
3651 def __init__(self, cache_key, cache_args='', cache_state_uid=None):
3652 self.cache_key = cache_key
3652 self.cache_key = cache_key
3653 self.cache_args = cache_args
3653 self.cache_args = cache_args
3654 self.cache_active = False
3654 self.cache_active = False
3655 # first key should be same for all entries, since all workers should share it
3655 # first key should be same for all entries, since all workers should share it
3656 self.cache_state_uid = cache_state_uid or self.generate_new_state_uid()
3656 self.cache_state_uid = cache_state_uid or self.generate_new_state_uid()
3657
3657
3658 def __unicode__(self):
3658 def __unicode__(self):
3659 return u"<%s('%s:%s[%s]')>" % (
3659 return u"<%s('%s:%s[%s]')>" % (
3660 self.__class__.__name__,
3660 self.__class__.__name__,
3661 self.cache_id, self.cache_key, self.cache_active)
3661 self.cache_id, self.cache_key, self.cache_active)
3662
3662
3663 def _cache_key_partition(self):
3663 def _cache_key_partition(self):
3664 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
3664 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
3665 return prefix, repo_name, suffix
3665 return prefix, repo_name, suffix
3666
3666
3667 def get_prefix(self):
3667 def get_prefix(self):
3668 """
3668 """
3669 Try to extract prefix from existing cache key. The key could consist
3669 Try to extract prefix from existing cache key. The key could consist
3670 of prefix, repo_name, suffix
3670 of prefix, repo_name, suffix
3671 """
3671 """
3672 # this returns prefix, repo_name, suffix
3672 # this returns prefix, repo_name, suffix
3673 return self._cache_key_partition()[0]
3673 return self._cache_key_partition()[0]
3674
3674
3675 def get_suffix(self):
3675 def get_suffix(self):
3676 """
3676 """
3677 get suffix that might have been used in _get_cache_key to
3677 get suffix that might have been used in _get_cache_key to
3678 generate self.cache_key. Only used for informational purposes
3678 generate self.cache_key. Only used for informational purposes
3679 in repo_edit.mako.
3679 in repo_edit.mako.
3680 """
3680 """
3681 # prefix, repo_name, suffix
3681 # prefix, repo_name, suffix
3682 return self._cache_key_partition()[2]
3682 return self._cache_key_partition()[2]
3683
3683
3684 @classmethod
3684 @classmethod
3685 def generate_new_state_uid(cls, based_on=None):
3685 def generate_new_state_uid(cls, based_on=None):
3686 if based_on:
3686 if based_on:
3687 return str(uuid.uuid5(uuid.NAMESPACE_URL, safe_str(based_on)))
3687 return str(uuid.uuid5(uuid.NAMESPACE_URL, safe_str(based_on)))
3688 else:
3688 else:
3689 return str(uuid.uuid4())
3689 return str(uuid.uuid4())
3690
3690
3691 @classmethod
3691 @classmethod
3692 def delete_all_cache(cls):
3692 def delete_all_cache(cls):
3693 """
3693 """
3694 Delete all cache keys from database.
3694 Delete all cache keys from database.
3695 Should only be run when all instances are down and all entries
3695 Should only be run when all instances are down and all entries
3696 thus stale.
3696 thus stale.
3697 """
3697 """
3698 cls.query().delete()
3698 cls.query().delete()
3699 Session().commit()
3699 Session().commit()
3700
3700
3701 @classmethod
3701 @classmethod
3702 def set_invalidate(cls, cache_uid, delete=False):
3702 def set_invalidate(cls, cache_uid, delete=False):
3703 """
3703 """
3704 Mark all caches of a repo as invalid in the database.
3704 Mark all caches of a repo as invalid in the database.
3705 """
3705 """
3706
3706
3707 try:
3707 try:
3708 qry = Session().query(cls).filter(cls.cache_args == cache_uid)
3708 qry = Session().query(cls).filter(cls.cache_args == cache_uid)
3709 if delete:
3709 if delete:
3710 qry.delete()
3710 qry.delete()
3711 log.debug('cache objects deleted for cache args %s',
3711 log.debug('cache objects deleted for cache args %s',
3712 safe_str(cache_uid))
3712 safe_str(cache_uid))
3713 else:
3713 else:
3714 qry.update({"cache_active": False,
3714 qry.update({"cache_active": False,
3715 "cache_state_uid": cls.generate_new_state_uid()})
3715 "cache_state_uid": cls.generate_new_state_uid()})
3716 log.debug('cache objects marked as invalid for cache args %s',
3716 log.debug('cache objects marked as invalid for cache args %s',
3717 safe_str(cache_uid))
3717 safe_str(cache_uid))
3718
3718
3719 Session().commit()
3719 Session().commit()
3720 except Exception:
3720 except Exception:
3721 log.exception(
3721 log.exception(
3722 'Cache key invalidation failed for cache args %s',
3722 'Cache key invalidation failed for cache args %s',
3723 safe_str(cache_uid))
3723 safe_str(cache_uid))
3724 Session().rollback()
3724 Session().rollback()
3725
3725
3726 @classmethod
3726 @classmethod
3727 def get_active_cache(cls, cache_key):
3727 def get_active_cache(cls, cache_key):
3728 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
3728 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
3729 if inv_obj:
3729 if inv_obj:
3730 return inv_obj
3730 return inv_obj
3731 return None
3731 return None
3732
3732
3733 @classmethod
3733 @classmethod
3734 def get_namespace_map(cls, namespace):
3734 def get_namespace_map(cls, namespace):
3735 return {
3735 return {
3736 x.cache_key: x
3736 x.cache_key: x
3737 for x in cls.query().filter(cls.cache_args == namespace)}
3737 for x in cls.query().filter(cls.cache_args == namespace)}
3738
3738
3739
3739
3740 class ChangesetComment(Base, BaseModel):
3740 class ChangesetComment(Base, BaseModel):
3741 __tablename__ = 'changeset_comments'
3741 __tablename__ = 'changeset_comments'
3742 __table_args__ = (
3742 __table_args__ = (
3743 Index('cc_revision_idx', 'revision'),
3743 Index('cc_revision_idx', 'revision'),
3744 base_table_args,
3744 base_table_args,
3745 )
3745 )
3746
3746
3747 COMMENT_OUTDATED = u'comment_outdated'
3747 COMMENT_OUTDATED = u'comment_outdated'
3748 COMMENT_TYPE_NOTE = u'note'
3748 COMMENT_TYPE_NOTE = u'note'
3749 COMMENT_TYPE_TODO = u'todo'
3749 COMMENT_TYPE_TODO = u'todo'
3750 COMMENT_TYPES = [COMMENT_TYPE_NOTE, COMMENT_TYPE_TODO]
3750 COMMENT_TYPES = [COMMENT_TYPE_NOTE, COMMENT_TYPE_TODO]
3751
3751
3752 OP_IMMUTABLE = u'immutable'
3752 OP_IMMUTABLE = u'immutable'
3753 OP_CHANGEABLE = u'changeable'
3753 OP_CHANGEABLE = u'changeable'
3754
3754
3755 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
3755 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
3756 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3756 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3757 revision = Column('revision', String(40), nullable=True)
3757 revision = Column('revision', String(40), nullable=True)
3758 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3758 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3759 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
3759 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
3760 line_no = Column('line_no', Unicode(10), nullable=True)
3760 line_no = Column('line_no', Unicode(10), nullable=True)
3761 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
3761 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
3762 f_path = Column('f_path', Unicode(1000), nullable=True)
3762 f_path = Column('f_path', Unicode(1000), nullable=True)
3763 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3763 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3764 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3764 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3765 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3765 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3766 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3766 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3767 renderer = Column('renderer', Unicode(64), nullable=True)
3767 renderer = Column('renderer', Unicode(64), nullable=True)
3768 display_state = Column('display_state', Unicode(128), nullable=True)
3768 display_state = Column('display_state', Unicode(128), nullable=True)
3769 immutable_state = Column('immutable_state', Unicode(128), nullable=True, default=OP_CHANGEABLE)
3769 immutable_state = Column('immutable_state', Unicode(128), nullable=True, default=OP_CHANGEABLE)
3770 draft = Column('draft', Boolean(), nullable=True, default=False)
3770 draft = Column('draft', Boolean(), nullable=True, default=False)
3771
3771
3772 comment_type = Column('comment_type', Unicode(128), nullable=True, default=COMMENT_TYPE_NOTE)
3772 comment_type = Column('comment_type', Unicode(128), nullable=True, default=COMMENT_TYPE_NOTE)
3773 resolved_comment_id = Column('resolved_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=True)
3773 resolved_comment_id = Column('resolved_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=True)
3774
3774
3775 resolved_comment = relationship('ChangesetComment', remote_side=comment_id, back_populates='resolved_by')
3775 resolved_comment = relationship('ChangesetComment', remote_side=comment_id, back_populates='resolved_by')
3776 resolved_by = relationship('ChangesetComment', back_populates='resolved_comment')
3776 resolved_by = relationship('ChangesetComment', back_populates='resolved_comment')
3777
3777
3778 author = relationship('User', lazy='select')
3778 author = relationship('User', lazy='select')
3779 repo = relationship('Repository')
3779 repo = relationship('Repository')
3780 status_change = relationship('ChangesetStatus', cascade="all, delete-orphan", lazy='select')
3780 status_change = relationship('ChangesetStatus', cascade="all, delete-orphan", lazy='select')
3781 pull_request = relationship('PullRequest', lazy='select')
3781 pull_request = relationship('PullRequest', lazy='select')
3782 pull_request_version = relationship('PullRequestVersion', lazy='select')
3782 pull_request_version = relationship('PullRequestVersion', lazy='select')
3783 history = relationship('ChangesetCommentHistory', cascade='all, delete-orphan', lazy='select', order_by='ChangesetCommentHistory.version')
3783 history = relationship('ChangesetCommentHistory', cascade='all, delete-orphan', lazy='select', order_by='ChangesetCommentHistory.version')
3784
3784
3785 @classmethod
3785 @classmethod
3786 def get_users(cls, revision=None, pull_request_id=None):
3786 def get_users(cls, revision=None, pull_request_id=None):
3787 """
3787 """
3788 Returns user associated with this ChangesetComment. ie those
3788 Returns user associated with this ChangesetComment. ie those
3789 who actually commented
3789 who actually commented
3790
3790
3791 :param cls:
3791 :param cls:
3792 :param revision:
3792 :param revision:
3793 """
3793 """
3794 q = Session().query(User)\
3794 q = Session().query(User)\
3795 .join(ChangesetComment.author)
3795 .join(ChangesetComment.author)
3796 if revision:
3796 if revision:
3797 q = q.filter(cls.revision == revision)
3797 q = q.filter(cls.revision == revision)
3798 elif pull_request_id:
3798 elif pull_request_id:
3799 q = q.filter(cls.pull_request_id == pull_request_id)
3799 q = q.filter(cls.pull_request_id == pull_request_id)
3800 return q.all()
3800 return q.all()
3801
3801
3802 @classmethod
3802 @classmethod
3803 def get_index_from_version(cls, pr_version, versions):
3803 def get_index_from_version(cls, pr_version, versions):
3804 num_versions = [x.pull_request_version_id for x in versions]
3804 num_versions = [x.pull_request_version_id for x in versions]
3805 try:
3805 try:
3806 return num_versions.index(pr_version) + 1
3806 return num_versions.index(pr_version) + 1
3807 except (IndexError, ValueError):
3807 except (IndexError, ValueError):
3808 return
3808 return
3809
3809
3810 @property
3810 @property
3811 def outdated(self):
3811 def outdated(self):
3812 return self.display_state == self.COMMENT_OUTDATED
3812 return self.display_state == self.COMMENT_OUTDATED
3813
3813
3814 @property
3814 @property
3815 def outdated_js(self):
3815 def outdated_js(self):
3816 return json.dumps(self.display_state == self.COMMENT_OUTDATED)
3816 return json.dumps(self.display_state == self.COMMENT_OUTDATED)
3817
3817
3818 @property
3818 @property
3819 def immutable(self):
3819 def immutable(self):
3820 return self.immutable_state == self.OP_IMMUTABLE
3820 return self.immutable_state == self.OP_IMMUTABLE
3821
3821
3822 def outdated_at_version(self, version):
3822 def outdated_at_version(self, version):
3823 """
3823 """
3824 Checks if comment is outdated for given pull request version
3824 Checks if comment is outdated for given pull request version
3825 """
3825 """
3826 def version_check():
3826 def version_check():
3827 return self.pull_request_version_id and self.pull_request_version_id != version
3827 return self.pull_request_version_id and self.pull_request_version_id != version
3828
3828
3829 if self.is_inline:
3829 if self.is_inline:
3830 return self.outdated and version_check()
3830 return self.outdated and version_check()
3831 else:
3831 else:
3832 # general comments don't have .outdated set, also latest don't have a version
3832 # general comments don't have .outdated set, also latest don't have a version
3833 return version_check()
3833 return version_check()
3834
3834
3835 def outdated_at_version_js(self, version):
3835 def outdated_at_version_js(self, version):
3836 """
3836 """
3837 Checks if comment is outdated for given pull request version
3837 Checks if comment is outdated for given pull request version
3838 """
3838 """
3839 return json.dumps(self.outdated_at_version(version))
3839 return json.dumps(self.outdated_at_version(version))
3840
3840
3841 def older_than_version(self, version):
3841 def older_than_version(self, version):
3842 """
3842 """
3843 Checks if comment is made from previous version than given
3843 Checks if comment is made from previous version than given
3844 """
3844 """
3845 if version is None:
3845 if version is None:
3846 return self.pull_request_version != version
3846 return self.pull_request_version != version
3847
3847
3848 return self.pull_request_version < version
3848 return self.pull_request_version < version
3849
3849
3850 def older_than_version_js(self, version):
3850 def older_than_version_js(self, version):
3851 """
3851 """
3852 Checks if comment is made from previous version than given
3852 Checks if comment is made from previous version than given
3853 """
3853 """
3854 return json.dumps(self.older_than_version(version))
3854 return json.dumps(self.older_than_version(version))
3855
3855
3856 @property
3856 @property
3857 def commit_id(self):
3857 def commit_id(self):
3858 """New style naming to stop using .revision"""
3858 """New style naming to stop using .revision"""
3859 return self.revision
3859 return self.revision
3860
3860
3861 @property
3861 @property
3862 def resolved(self):
3862 def resolved(self):
3863 return self.resolved_by[0] if self.resolved_by else None
3863 return self.resolved_by[0] if self.resolved_by else None
3864
3864
3865 @property
3865 @property
3866 def is_todo(self):
3866 def is_todo(self):
3867 return self.comment_type == self.COMMENT_TYPE_TODO
3867 return self.comment_type == self.COMMENT_TYPE_TODO
3868
3868
3869 @property
3869 @property
3870 def is_inline(self):
3870 def is_inline(self):
3871 if self.line_no and self.f_path:
3871 if self.line_no and self.f_path:
3872 return True
3872 return True
3873 return False
3873 return False
3874
3874
3875 @property
3875 @property
3876 def last_version(self):
3876 def last_version(self):
3877 version = 0
3877 version = 0
3878 if self.history:
3878 if self.history:
3879 version = self.history[-1].version
3879 version = self.history[-1].version
3880 return version
3880 return version
3881
3881
3882 def get_index_version(self, versions):
3882 def get_index_version(self, versions):
3883 return self.get_index_from_version(
3883 return self.get_index_from_version(
3884 self.pull_request_version_id, versions)
3884 self.pull_request_version_id, versions)
3885
3885
3886 @property
3886 @property
3887 def review_status(self):
3887 def review_status(self):
3888 if self.status_change:
3888 if self.status_change:
3889 return self.status_change[0].status
3889 return self.status_change[0].status
3890
3890
3891 @property
3891 @property
3892 def review_status_lbl(self):
3892 def review_status_lbl(self):
3893 if self.status_change:
3893 if self.status_change:
3894 return self.status_change[0].status_lbl
3894 return self.status_change[0].status_lbl
3895
3895
3896 def __repr__(self):
3896 def __repr__(self):
3897 if self.comment_id:
3897 if self.comment_id:
3898 return '<DB:Comment #%s>' % self.comment_id
3898 return '<DB:Comment #%s>' % self.comment_id
3899 else:
3899 else:
3900 return '<DB:Comment at %#x>' % id(self)
3900 return '<DB:Comment at %#x>' % id(self)
3901
3901
3902 def get_api_data(self):
3902 def get_api_data(self):
3903 comment = self
3903 comment = self
3904
3904
3905 data = {
3905 data = {
3906 'comment_id': comment.comment_id,
3906 'comment_id': comment.comment_id,
3907 'comment_type': comment.comment_type,
3907 'comment_type': comment.comment_type,
3908 'comment_text': comment.text,
3908 'comment_text': comment.text,
3909 'comment_status': comment.status_change,
3909 'comment_status': comment.status_change,
3910 'comment_f_path': comment.f_path,
3910 'comment_f_path': comment.f_path,
3911 'comment_lineno': comment.line_no,
3911 'comment_lineno': comment.line_no,
3912 'comment_author': comment.author,
3912 'comment_author': comment.author,
3913 'comment_created_on': comment.created_on,
3913 'comment_created_on': comment.created_on,
3914 'comment_resolved_by': self.resolved,
3914 'comment_resolved_by': self.resolved,
3915 'comment_commit_id': comment.revision,
3915 'comment_commit_id': comment.revision,
3916 'comment_pull_request_id': comment.pull_request_id,
3916 'comment_pull_request_id': comment.pull_request_id,
3917 'comment_last_version': self.last_version
3917 'comment_last_version': self.last_version
3918 }
3918 }
3919 return data
3919 return data
3920
3920
3921 def __json__(self):
3921 def __json__(self):
3922 data = dict()
3922 data = dict()
3923 data.update(self.get_api_data())
3923 data.update(self.get_api_data())
3924 return data
3924 return data
3925
3925
3926
3926
3927 class ChangesetCommentHistory(Base, BaseModel):
3927 class ChangesetCommentHistory(Base, BaseModel):
3928 __tablename__ = 'changeset_comments_history'
3928 __tablename__ = 'changeset_comments_history'
3929 __table_args__ = (
3929 __table_args__ = (
3930 Index('cch_comment_id_idx', 'comment_id'),
3930 Index('cch_comment_id_idx', 'comment_id'),
3931 base_table_args,
3931 base_table_args,
3932 )
3932 )
3933
3933
3934 comment_history_id = Column('comment_history_id', Integer(), nullable=False, primary_key=True)
3934 comment_history_id = Column('comment_history_id', Integer(), nullable=False, primary_key=True)
3935 comment_id = Column('comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=False)
3935 comment_id = Column('comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=False)
3936 version = Column("version", Integer(), nullable=False, default=0)
3936 version = Column("version", Integer(), nullable=False, default=0)
3937 created_by_user_id = Column('created_by_user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3937 created_by_user_id = Column('created_by_user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3938 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3938 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3939 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3939 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3940 deleted = Column('deleted', Boolean(), default=False)
3940 deleted = Column('deleted', Boolean(), default=False)
3941
3941
3942 author = relationship('User', lazy='joined')
3942 author = relationship('User', lazy='joined')
3943 comment = relationship('ChangesetComment', cascade="all, delete")
3943 comment = relationship('ChangesetComment', cascade="all, delete")
3944
3944
3945 @classmethod
3945 @classmethod
3946 def get_version(cls, comment_id):
3946 def get_version(cls, comment_id):
3947 q = Session().query(ChangesetCommentHistory).filter(
3947 q = Session().query(ChangesetCommentHistory).filter(
3948 ChangesetCommentHistory.comment_id == comment_id).order_by(ChangesetCommentHistory.version.desc())
3948 ChangesetCommentHistory.comment_id == comment_id).order_by(ChangesetCommentHistory.version.desc())
3949 if q.count() == 0:
3949 if q.count() == 0:
3950 return 1
3950 return 1
3951 elif q.count() >= q[0].version:
3951 elif q.count() >= q[0].version:
3952 return q.count() + 1
3952 return q.count() + 1
3953 else:
3953 else:
3954 return q[0].version + 1
3954 return q[0].version + 1
3955
3955
3956
3956
3957 class ChangesetStatus(Base, BaseModel):
3957 class ChangesetStatus(Base, BaseModel):
3958 __tablename__ = 'changeset_statuses'
3958 __tablename__ = 'changeset_statuses'
3959 __table_args__ = (
3959 __table_args__ = (
3960 Index('cs_revision_idx', 'revision'),
3960 Index('cs_revision_idx', 'revision'),
3961 Index('cs_version_idx', 'version'),
3961 Index('cs_version_idx', 'version'),
3962 UniqueConstraint('repo_id', 'revision', 'version'),
3962 UniqueConstraint('repo_id', 'revision', 'version'),
3963 base_table_args
3963 base_table_args
3964 )
3964 )
3965
3965
3966 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
3966 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
3967 STATUS_APPROVED = 'approved'
3967 STATUS_APPROVED = 'approved'
3968 STATUS_REJECTED = 'rejected'
3968 STATUS_REJECTED = 'rejected'
3969 STATUS_UNDER_REVIEW = 'under_review'
3969 STATUS_UNDER_REVIEW = 'under_review'
3970
3970
3971 STATUSES = [
3971 STATUSES = [
3972 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
3972 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
3973 (STATUS_APPROVED, _("Approved")),
3973 (STATUS_APPROVED, _("Approved")),
3974 (STATUS_REJECTED, _("Rejected")),
3974 (STATUS_REJECTED, _("Rejected")),
3975 (STATUS_UNDER_REVIEW, _("Under Review")),
3975 (STATUS_UNDER_REVIEW, _("Under Review")),
3976 ]
3976 ]
3977
3977
3978 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
3978 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
3979 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3979 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3980 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
3980 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
3981 revision = Column('revision', String(40), nullable=False)
3981 revision = Column('revision', String(40), nullable=False)
3982 status = Column('status', String(128), nullable=False, default=DEFAULT)
3982 status = Column('status', String(128), nullable=False, default=DEFAULT)
3983 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
3983 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
3984 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
3984 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
3985 version = Column('version', Integer(), nullable=False, default=0)
3985 version = Column('version', Integer(), nullable=False, default=0)
3986 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3986 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3987
3987
3988 author = relationship('User', lazy='select')
3988 author = relationship('User', lazy='select')
3989 repo = relationship('Repository', lazy='select')
3989 repo = relationship('Repository', lazy='select')
3990 comment = relationship('ChangesetComment', lazy='select')
3990 comment = relationship('ChangesetComment', lazy='select')
3991 pull_request = relationship('PullRequest', lazy='select')
3991 pull_request = relationship('PullRequest', lazy='select')
3992
3992
3993 def __unicode__(self):
3993 def __unicode__(self):
3994 return u"<%s('%s[v%s]:%s')>" % (
3994 return u"<%s('%s[v%s]:%s')>" % (
3995 self.__class__.__name__,
3995 self.__class__.__name__,
3996 self.status, self.version, self.author
3996 self.status, self.version, self.author
3997 )
3997 )
3998
3998
3999 @classmethod
3999 @classmethod
4000 def get_status_lbl(cls, value):
4000 def get_status_lbl(cls, value):
4001 return dict(cls.STATUSES).get(value)
4001 return dict(cls.STATUSES).get(value)
4002
4002
4003 @property
4003 @property
4004 def status_lbl(self):
4004 def status_lbl(self):
4005 return ChangesetStatus.get_status_lbl(self.status)
4005 return ChangesetStatus.get_status_lbl(self.status)
4006
4006
4007 def get_api_data(self):
4007 def get_api_data(self):
4008 status = self
4008 status = self
4009 data = {
4009 data = {
4010 'status_id': status.changeset_status_id,
4010 'status_id': status.changeset_status_id,
4011 'status': status.status,
4011 'status': status.status,
4012 }
4012 }
4013 return data
4013 return data
4014
4014
4015 def __json__(self):
4015 def __json__(self):
4016 data = dict()
4016 data = dict()
4017 data.update(self.get_api_data())
4017 data.update(self.get_api_data())
4018 return data
4018 return data
4019
4019
4020
4020
4021 class _SetState(object):
4021 class _SetState(object):
4022 """
4022 """
4023 Context processor allowing changing state for sensitive operation such as
4023 Context processor allowing changing state for sensitive operation such as
4024 pull request update or merge
4024 pull request update or merge
4025 """
4025 """
4026
4026
4027 def __init__(self, pull_request, pr_state, back_state=None):
4027 def __init__(self, pull_request, pr_state, back_state=None):
4028 self._pr = pull_request
4028 self._pr = pull_request
4029 self._org_state = back_state or pull_request.pull_request_state
4029 self._org_state = back_state or pull_request.pull_request_state
4030 self._pr_state = pr_state
4030 self._pr_state = pr_state
4031 self._current_state = None
4031 self._current_state = None
4032
4032
4033 def __enter__(self):
4033 def __enter__(self):
4034 log.debug('StateLock: entering set state context of pr %s, setting state to: `%s`',
4034 log.debug('StateLock: entering set state context of pr %s, setting state to: `%s`',
4035 self._pr, self._pr_state)
4035 self._pr, self._pr_state)
4036 self.set_pr_state(self._pr_state)
4036 self.set_pr_state(self._pr_state)
4037 return self
4037 return self
4038
4038
4039 def __exit__(self, exc_type, exc_val, exc_tb):
4039 def __exit__(self, exc_type, exc_val, exc_tb):
4040 if exc_val is not None:
4040 if exc_val is not None:
4041 log.error(traceback.format_exc(exc_tb))
4041 log.error(traceback.format_exc(exc_tb))
4042 return None
4042 return None
4043
4043
4044 self.set_pr_state(self._org_state)
4044 self.set_pr_state(self._org_state)
4045 log.debug('StateLock: exiting set state context of pr %s, setting state to: `%s`',
4045 log.debug('StateLock: exiting set state context of pr %s, setting state to: `%s`',
4046 self._pr, self._org_state)
4046 self._pr, self._org_state)
4047
4047
4048 @property
4048 @property
4049 def state(self):
4049 def state(self):
4050 return self._current_state
4050 return self._current_state
4051
4051
4052 def set_pr_state(self, pr_state):
4052 def set_pr_state(self, pr_state):
4053 try:
4053 try:
4054 self._pr.pull_request_state = pr_state
4054 self._pr.pull_request_state = pr_state
4055 Session().add(self._pr)
4055 Session().add(self._pr)
4056 Session().commit()
4056 Session().commit()
4057 self._current_state = pr_state
4057 self._current_state = pr_state
4058 except Exception:
4058 except Exception:
4059 log.exception('Failed to set PullRequest %s state to %s', self._pr, pr_state)
4059 log.exception('Failed to set PullRequest %s state to %s', self._pr, pr_state)
4060 raise
4060 raise
4061
4061
4062
4062
4063 class _PullRequestBase(BaseModel):
4063 class _PullRequestBase(BaseModel):
4064 """
4064 """
4065 Common attributes of pull request and version entries.
4065 Common attributes of pull request and version entries.
4066 """
4066 """
4067
4067
4068 # .status values
4068 # .status values
4069 STATUS_NEW = u'new'
4069 STATUS_NEW = u'new'
4070 STATUS_OPEN = u'open'
4070 STATUS_OPEN = u'open'
4071 STATUS_CLOSED = u'closed'
4071 STATUS_CLOSED = u'closed'
4072
4072
4073 # available states
4073 # available states
4074 STATE_CREATING = u'creating'
4074 STATE_CREATING = u'creating'
4075 STATE_UPDATING = u'updating'
4075 STATE_UPDATING = u'updating'
4076 STATE_MERGING = u'merging'
4076 STATE_MERGING = u'merging'
4077 STATE_CREATED = u'created'
4077 STATE_CREATED = u'created'
4078
4078
4079 title = Column('title', Unicode(255), nullable=True)
4079 title = Column('title', Unicode(255), nullable=True)
4080 description = Column(
4080 description = Column(
4081 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
4081 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
4082 nullable=True)
4082 nullable=True)
4083 description_renderer = Column('description_renderer', Unicode(64), nullable=True)
4083 description_renderer = Column('description_renderer', Unicode(64), nullable=True)
4084
4084
4085 # new/open/closed status of pull request (not approve/reject/etc)
4085 # new/open/closed status of pull request (not approve/reject/etc)
4086 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
4086 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
4087 created_on = Column(
4087 created_on = Column(
4088 'created_on', DateTime(timezone=False), nullable=False,
4088 'created_on', DateTime(timezone=False), nullable=False,
4089 default=datetime.datetime.now)
4089 default=datetime.datetime.now)
4090 updated_on = Column(
4090 updated_on = Column(
4091 'updated_on', DateTime(timezone=False), nullable=False,
4091 'updated_on', DateTime(timezone=False), nullable=False,
4092 default=datetime.datetime.now)
4092 default=datetime.datetime.now)
4093
4093
4094 pull_request_state = Column("pull_request_state", String(255), nullable=True)
4094 pull_request_state = Column("pull_request_state", String(255), nullable=True)
4095
4095
4096 @declared_attr
4096 @declared_attr
4097 def user_id(cls):
4097 def user_id(cls):
4098 return Column(
4098 return Column(
4099 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
4099 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
4100 unique=None)
4100 unique=None)
4101
4101
4102 # 500 revisions max
4102 # 500 revisions max
4103 _revisions = Column(
4103 _revisions = Column(
4104 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
4104 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
4105
4105
4106 common_ancestor_id = Column('common_ancestor_id', Unicode(255), nullable=True)
4106 common_ancestor_id = Column('common_ancestor_id', Unicode(255), nullable=True)
4107
4107
4108 @declared_attr
4108 @declared_attr
4109 def source_repo_id(cls):
4109 def source_repo_id(cls):
4110 # TODO: dan: rename column to source_repo_id
4110 # TODO: dan: rename column to source_repo_id
4111 return Column(
4111 return Column(
4112 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
4112 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
4113 nullable=False)
4113 nullable=False)
4114
4114
4115 _source_ref = Column('org_ref', Unicode(255), nullable=False)
4115 _source_ref = Column('org_ref', Unicode(255), nullable=False)
4116
4116
4117 @hybrid_property
4117 @hybrid_property
4118 def source_ref(self):
4118 def source_ref(self):
4119 return self._source_ref
4119 return self._source_ref
4120
4120
4121 @source_ref.setter
4121 @source_ref.setter
4122 def source_ref(self, val):
4122 def source_ref(self, val):
4123 parts = (val or '').split(':')
4123 parts = (val or '').split(':')
4124 if len(parts) != 3:
4124 if len(parts) != 3:
4125 raise ValueError(
4125 raise ValueError(
4126 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4126 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4127 self._source_ref = safe_unicode(val)
4127 self._source_ref = safe_unicode(val)
4128
4128
4129 _target_ref = Column('other_ref', Unicode(255), nullable=False)
4129 _target_ref = Column('other_ref', Unicode(255), nullable=False)
4130
4130
4131 @hybrid_property
4131 @hybrid_property
4132 def target_ref(self):
4132 def target_ref(self):
4133 return self._target_ref
4133 return self._target_ref
4134
4134
4135 @target_ref.setter
4135 @target_ref.setter
4136 def target_ref(self, val):
4136 def target_ref(self, val):
4137 parts = (val or '').split(':')
4137 parts = (val or '').split(':')
4138 if len(parts) != 3:
4138 if len(parts) != 3:
4139 raise ValueError(
4139 raise ValueError(
4140 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4140 'Invalid reference format given: {}, expected X:Y:Z'.format(val))
4141 self._target_ref = safe_unicode(val)
4141 self._target_ref = safe_unicode(val)
4142
4142
4143 @declared_attr
4143 @declared_attr
4144 def target_repo_id(cls):
4144 def target_repo_id(cls):
4145 # TODO: dan: rename column to target_repo_id
4145 # TODO: dan: rename column to target_repo_id
4146 return Column(
4146 return Column(
4147 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
4147 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
4148 nullable=False)
4148 nullable=False)
4149
4149
4150 _shadow_merge_ref = Column('shadow_merge_ref', Unicode(255), nullable=True)
4150 _shadow_merge_ref = Column('shadow_merge_ref', Unicode(255), nullable=True)
4151
4151
4152 # TODO: dan: rename column to last_merge_source_rev
4152 # TODO: dan: rename column to last_merge_source_rev
4153 _last_merge_source_rev = Column(
4153 _last_merge_source_rev = Column(
4154 'last_merge_org_rev', String(40), nullable=True)
4154 'last_merge_org_rev', String(40), nullable=True)
4155 # TODO: dan: rename column to last_merge_target_rev
4155 # TODO: dan: rename column to last_merge_target_rev
4156 _last_merge_target_rev = Column(
4156 _last_merge_target_rev = Column(
4157 'last_merge_other_rev', String(40), nullable=True)
4157 'last_merge_other_rev', String(40), nullable=True)
4158 _last_merge_status = Column('merge_status', Integer(), nullable=True)
4158 _last_merge_status = Column('merge_status', Integer(), nullable=True)
4159 last_merge_metadata = Column(
4159 last_merge_metadata = Column(
4160 'last_merge_metadata', MutationObj.as_mutable(
4160 'last_merge_metadata', MutationObj.as_mutable(
4161 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4161 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4162
4162
4163 merge_rev = Column('merge_rev', String(40), nullable=True)
4163 merge_rev = Column('merge_rev', String(40), nullable=True)
4164
4164
4165 reviewer_data = Column(
4165 reviewer_data = Column(
4166 'reviewer_data_json', MutationObj.as_mutable(
4166 'reviewer_data_json', MutationObj.as_mutable(
4167 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4167 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4168
4168
4169 @property
4169 @property
4170 def reviewer_data_json(self):
4170 def reviewer_data_json(self):
4171 return json.dumps(self.reviewer_data)
4171 return json.dumps(self.reviewer_data)
4172
4172
4173 @property
4173 @property
4174 def last_merge_metadata_parsed(self):
4174 def last_merge_metadata_parsed(self):
4175 metadata = {}
4175 metadata = {}
4176 if not self.last_merge_metadata:
4176 if not self.last_merge_metadata:
4177 return metadata
4177 return metadata
4178
4178
4179 if hasattr(self.last_merge_metadata, 'de_coerce'):
4179 if hasattr(self.last_merge_metadata, 'de_coerce'):
4180 for k, v in self.last_merge_metadata.de_coerce().items():
4180 for k, v in self.last_merge_metadata.de_coerce().items():
4181 if k in ['target_ref', 'source_ref']:
4181 if k in ['target_ref', 'source_ref']:
4182 metadata[k] = Reference(v['type'], v['name'], v['commit_id'])
4182 metadata[k] = Reference(v['type'], v['name'], v['commit_id'])
4183 else:
4183 else:
4184 if hasattr(v, 'de_coerce'):
4184 if hasattr(v, 'de_coerce'):
4185 metadata[k] = v.de_coerce()
4185 metadata[k] = v.de_coerce()
4186 else:
4186 else:
4187 metadata[k] = v
4187 metadata[k] = v
4188 return metadata
4188 return metadata
4189
4189
4190 @property
4190 @property
4191 def work_in_progress(self):
4191 def work_in_progress(self):
4192 """checks if pull request is work in progress by checking the title"""
4192 """checks if pull request is work in progress by checking the title"""
4193 title = self.title.upper()
4193 title = self.title.upper()
4194 if re.match(r'^(\[WIP\]\s*|WIP:\s*|WIP\s+)', title):
4194 if re.match(r'^(\[WIP\]\s*|WIP:\s*|WIP\s+)', title):
4195 return True
4195 return True
4196 return False
4196 return False
4197
4197
4198 @hybrid_property
4198 @hybrid_property
4199 def description_safe(self):
4199 def description_safe(self):
4200 from rhodecode.lib import helpers as h
4200 from rhodecode.lib import helpers as h
4201 return h.escape(self.description)
4201 return h.escape(self.description)
4202
4202
4203 @hybrid_property
4203 @hybrid_property
4204 def revisions(self):
4204 def revisions(self):
4205 return self._revisions.split(':') if self._revisions else []
4205 return self._revisions.split(':') if self._revisions else []
4206
4206
4207 @revisions.setter
4207 @revisions.setter
4208 def revisions(self, val):
4208 def revisions(self, val):
4209 self._revisions = u':'.join(val)
4209 self._revisions = u':'.join(val)
4210
4210
4211 @hybrid_property
4211 @hybrid_property
4212 def last_merge_status(self):
4212 def last_merge_status(self):
4213 return safe_int(self._last_merge_status)
4213 return safe_int(self._last_merge_status)
4214
4214
4215 @last_merge_status.setter
4215 @last_merge_status.setter
4216 def last_merge_status(self, val):
4216 def last_merge_status(self, val):
4217 self._last_merge_status = val
4217 self._last_merge_status = val
4218
4218
4219 @declared_attr
4219 @declared_attr
4220 def author(cls):
4220 def author(cls):
4221 return relationship('User', lazy='joined')
4221 return relationship('User', lazy='joined')
4222
4222
4223 @declared_attr
4223 @declared_attr
4224 def source_repo(cls):
4224 def source_repo(cls):
4225 return relationship(
4225 return relationship(
4226 'Repository',
4226 'Repository',
4227 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
4227 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
4228
4228
4229 @property
4229 @property
4230 def source_ref_parts(self):
4230 def source_ref_parts(self):
4231 return self.unicode_to_reference(self.source_ref)
4231 return self.unicode_to_reference(self.source_ref)
4232
4232
4233 @declared_attr
4233 @declared_attr
4234 def target_repo(cls):
4234 def target_repo(cls):
4235 return relationship(
4235 return relationship(
4236 'Repository',
4236 'Repository',
4237 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
4237 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
4238
4238
4239 @property
4239 @property
4240 def target_ref_parts(self):
4240 def target_ref_parts(self):
4241 return self.unicode_to_reference(self.target_ref)
4241 return self.unicode_to_reference(self.target_ref)
4242
4242
4243 @property
4243 @property
4244 def shadow_merge_ref(self):
4244 def shadow_merge_ref(self):
4245 return self.unicode_to_reference(self._shadow_merge_ref)
4245 return self.unicode_to_reference(self._shadow_merge_ref)
4246
4246
4247 @shadow_merge_ref.setter
4247 @shadow_merge_ref.setter
4248 def shadow_merge_ref(self, ref):
4248 def shadow_merge_ref(self, ref):
4249 self._shadow_merge_ref = self.reference_to_unicode(ref)
4249 self._shadow_merge_ref = self.reference_to_unicode(ref)
4250
4250
4251 @staticmethod
4251 @staticmethod
4252 def unicode_to_reference(raw):
4252 def unicode_to_reference(raw):
4253 return unicode_to_reference(raw)
4253 return unicode_to_reference(raw)
4254
4254
4255 @staticmethod
4255 @staticmethod
4256 def reference_to_unicode(ref):
4256 def reference_to_unicode(ref):
4257 return reference_to_unicode(ref)
4257 return reference_to_unicode(ref)
4258
4258
4259 def get_api_data(self, with_merge_state=True):
4259 def get_api_data(self, with_merge_state=True):
4260 from rhodecode.model.pull_request import PullRequestModel
4260 from rhodecode.model.pull_request import PullRequestModel
4261
4261
4262 pull_request = self
4262 pull_request = self
4263 if with_merge_state:
4263 if with_merge_state:
4264 merge_response, merge_status, msg = \
4264 merge_response, merge_status, msg = \
4265 PullRequestModel().merge_status(pull_request)
4265 PullRequestModel().merge_status(pull_request)
4266 merge_state = {
4266 merge_state = {
4267 'status': merge_status,
4267 'status': merge_status,
4268 'message': safe_unicode(msg),
4268 'message': safe_unicode(msg),
4269 }
4269 }
4270 else:
4270 else:
4271 merge_state = {'status': 'not_available',
4271 merge_state = {'status': 'not_available',
4272 'message': 'not_available'}
4272 'message': 'not_available'}
4273
4273
4274 merge_data = {
4274 merge_data = {
4275 'clone_url': PullRequestModel().get_shadow_clone_url(pull_request),
4275 'clone_url': PullRequestModel().get_shadow_clone_url(pull_request),
4276 'reference': (
4276 'reference': (
4277 pull_request.shadow_merge_ref._asdict()
4277 pull_request.shadow_merge_ref._asdict()
4278 if pull_request.shadow_merge_ref else None),
4278 if pull_request.shadow_merge_ref else None),
4279 }
4279 }
4280
4280
4281 data = {
4281 data = {
4282 'pull_request_id': pull_request.pull_request_id,
4282 'pull_request_id': pull_request.pull_request_id,
4283 'url': PullRequestModel().get_url(pull_request),
4283 'url': PullRequestModel().get_url(pull_request),
4284 'title': pull_request.title,
4284 'title': pull_request.title,
4285 'description': pull_request.description,
4285 'description': pull_request.description,
4286 'status': pull_request.status,
4286 'status': pull_request.status,
4287 'state': pull_request.pull_request_state,
4287 'state': pull_request.pull_request_state,
4288 'created_on': pull_request.created_on,
4288 'created_on': pull_request.created_on,
4289 'updated_on': pull_request.updated_on,
4289 'updated_on': pull_request.updated_on,
4290 'commit_ids': pull_request.revisions,
4290 'commit_ids': pull_request.revisions,
4291 'review_status': pull_request.calculated_review_status(),
4291 'review_status': pull_request.calculated_review_status(),
4292 'mergeable': merge_state,
4292 'mergeable': merge_state,
4293 'source': {
4293 'source': {
4294 'clone_url': pull_request.source_repo.clone_url(),
4294 'clone_url': pull_request.source_repo.clone_url(),
4295 'repository': pull_request.source_repo.repo_name,
4295 'repository': pull_request.source_repo.repo_name,
4296 'reference': {
4296 'reference': {
4297 'name': pull_request.source_ref_parts.name,
4297 'name': pull_request.source_ref_parts.name,
4298 'type': pull_request.source_ref_parts.type,
4298 'type': pull_request.source_ref_parts.type,
4299 'commit_id': pull_request.source_ref_parts.commit_id,
4299 'commit_id': pull_request.source_ref_parts.commit_id,
4300 },
4300 },
4301 },
4301 },
4302 'target': {
4302 'target': {
4303 'clone_url': pull_request.target_repo.clone_url(),
4303 'clone_url': pull_request.target_repo.clone_url(),
4304 'repository': pull_request.target_repo.repo_name,
4304 'repository': pull_request.target_repo.repo_name,
4305 'reference': {
4305 'reference': {
4306 'name': pull_request.target_ref_parts.name,
4306 'name': pull_request.target_ref_parts.name,
4307 'type': pull_request.target_ref_parts.type,
4307 'type': pull_request.target_ref_parts.type,
4308 'commit_id': pull_request.target_ref_parts.commit_id,
4308 'commit_id': pull_request.target_ref_parts.commit_id,
4309 },
4309 },
4310 },
4310 },
4311 'merge': merge_data,
4311 'merge': merge_data,
4312 'author': pull_request.author.get_api_data(include_secrets=False,
4312 'author': pull_request.author.get_api_data(include_secrets=False,
4313 details='basic'),
4313 details='basic'),
4314 'reviewers': [
4314 'reviewers': [
4315 {
4315 {
4316 'user': reviewer.get_api_data(include_secrets=False,
4316 'user': reviewer.get_api_data(include_secrets=False,
4317 details='basic'),
4317 details='basic'),
4318 'reasons': reasons,
4318 'reasons': reasons,
4319 'review_status': st[0][1].status if st else 'not_reviewed',
4319 'review_status': st[0][1].status if st else 'not_reviewed',
4320 }
4320 }
4321 for obj, reviewer, reasons, mandatory, st in
4321 for obj, reviewer, reasons, mandatory, st in
4322 pull_request.reviewers_statuses()
4322 pull_request.reviewers_statuses()
4323 ]
4323 ]
4324 }
4324 }
4325
4325
4326 return data
4326 return data
4327
4327
4328 def set_state(self, pull_request_state, final_state=None):
4328 def set_state(self, pull_request_state, final_state=None):
4329 """
4329 """
4330 # goes from initial state to updating to initial state.
4330 # goes from initial state to updating to initial state.
4331 # initial state can be changed by specifying back_state=
4331 # initial state can be changed by specifying back_state=
4332 with pull_request_obj.set_state(PullRequest.STATE_UPDATING):
4332 with pull_request_obj.set_state(PullRequest.STATE_UPDATING):
4333 pull_request.merge()
4333 pull_request.merge()
4334
4334
4335 :param pull_request_state:
4335 :param pull_request_state:
4336 :param final_state:
4336 :param final_state:
4337
4337
4338 """
4338 """
4339
4339
4340 return _SetState(self, pull_request_state, back_state=final_state)
4340 return _SetState(self, pull_request_state, back_state=final_state)
4341
4341
4342
4342
4343 class PullRequest(Base, _PullRequestBase):
4343 class PullRequest(Base, _PullRequestBase):
4344 __tablename__ = 'pull_requests'
4344 __tablename__ = 'pull_requests'
4345 __table_args__ = (
4345 __table_args__ = (
4346 base_table_args,
4346 base_table_args,
4347 )
4347 )
4348 LATEST_VER = 'latest'
4348 LATEST_VER = 'latest'
4349
4349
4350 pull_request_id = Column(
4350 pull_request_id = Column(
4351 'pull_request_id', Integer(), nullable=False, primary_key=True)
4351 'pull_request_id', Integer(), nullable=False, primary_key=True)
4352
4352
4353 def __repr__(self):
4353 def __repr__(self):
4354 if self.pull_request_id:
4354 if self.pull_request_id:
4355 return '<DB:PullRequest #%s>' % self.pull_request_id
4355 return '<DB:PullRequest #%s>' % self.pull_request_id
4356 else:
4356 else:
4357 return '<DB:PullRequest at %#x>' % id(self)
4357 return '<DB:PullRequest at %#x>' % id(self)
4358
4358
4359 reviewers = relationship('PullRequestReviewers', cascade="all, delete-orphan")
4359 reviewers = relationship('PullRequestReviewers', cascade="all, delete-orphan")
4360 statuses = relationship('ChangesetStatus', cascade="all, delete-orphan")
4360 statuses = relationship('ChangesetStatus', cascade="all, delete-orphan")
4361 comments = relationship('ChangesetComment', cascade="all, delete-orphan")
4361 comments = relationship('ChangesetComment', cascade="all, delete-orphan")
4362 versions = relationship('PullRequestVersion', cascade="all, delete-orphan",
4362 versions = relationship('PullRequestVersion', cascade="all, delete-orphan",
4363 lazy='dynamic')
4363 lazy='dynamic')
4364
4364
4365 @classmethod
4365 @classmethod
4366 def get_pr_display_object(cls, pull_request_obj, org_pull_request_obj,
4366 def get_pr_display_object(cls, pull_request_obj, org_pull_request_obj,
4367 internal_methods=None):
4367 internal_methods=None):
4368
4368
4369 class PullRequestDisplay(object):
4369 class PullRequestDisplay(object):
4370 """
4370 """
4371 Special object wrapper for showing PullRequest data via Versions
4371 Special object wrapper for showing PullRequest data via Versions
4372 It mimics PR object as close as possible. This is read only object
4372 It mimics PR object as close as possible. This is read only object
4373 just for display
4373 just for display
4374 """
4374 """
4375
4375
4376 def __init__(self, attrs, internal=None):
4376 def __init__(self, attrs, internal=None):
4377 self.attrs = attrs
4377 self.attrs = attrs
4378 # internal have priority over the given ones via attrs
4378 # internal have priority over the given ones via attrs
4379 self.internal = internal or ['versions']
4379 self.internal = internal or ['versions']
4380
4380
4381 def __getattr__(self, item):
4381 def __getattr__(self, item):
4382 if item in self.internal:
4382 if item in self.internal:
4383 return getattr(self, item)
4383 return getattr(self, item)
4384 try:
4384 try:
4385 return self.attrs[item]
4385 return self.attrs[item]
4386 except KeyError:
4386 except KeyError:
4387 raise AttributeError(
4387 raise AttributeError(
4388 '%s object has no attribute %s' % (self, item))
4388 '%s object has no attribute %s' % (self, item))
4389
4389
4390 def __repr__(self):
4390 def __repr__(self):
4391 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
4391 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
4392
4392
4393 def versions(self):
4393 def versions(self):
4394 return pull_request_obj.versions.order_by(
4394 return pull_request_obj.versions.order_by(
4395 PullRequestVersion.pull_request_version_id).all()
4395 PullRequestVersion.pull_request_version_id).all()
4396
4396
4397 def is_closed(self):
4397 def is_closed(self):
4398 return pull_request_obj.is_closed()
4398 return pull_request_obj.is_closed()
4399
4399
4400 def is_state_changing(self):
4400 def is_state_changing(self):
4401 return pull_request_obj.is_state_changing()
4401 return pull_request_obj.is_state_changing()
4402
4402
4403 @property
4403 @property
4404 def pull_request_version_id(self):
4404 def pull_request_version_id(self):
4405 return getattr(pull_request_obj, 'pull_request_version_id', None)
4405 return getattr(pull_request_obj, 'pull_request_version_id', None)
4406
4406
4407 @property
4407 @property
4408 def pull_request_last_version(self):
4408 def pull_request_last_version(self):
4409 return pull_request_obj.pull_request_last_version
4409 return pull_request_obj.pull_request_last_version
4410
4410
4411 attrs = StrictAttributeDict(pull_request_obj.get_api_data(with_merge_state=False))
4411 attrs = StrictAttributeDict(pull_request_obj.get_api_data(with_merge_state=False))
4412
4412
4413 attrs.author = StrictAttributeDict(
4413 attrs.author = StrictAttributeDict(
4414 pull_request_obj.author.get_api_data())
4414 pull_request_obj.author.get_api_data())
4415 if pull_request_obj.target_repo:
4415 if pull_request_obj.target_repo:
4416 attrs.target_repo = StrictAttributeDict(
4416 attrs.target_repo = StrictAttributeDict(
4417 pull_request_obj.target_repo.get_api_data())
4417 pull_request_obj.target_repo.get_api_data())
4418 attrs.target_repo.clone_url = pull_request_obj.target_repo.clone_url
4418 attrs.target_repo.clone_url = pull_request_obj.target_repo.clone_url
4419
4419
4420 if pull_request_obj.source_repo:
4420 if pull_request_obj.source_repo:
4421 attrs.source_repo = StrictAttributeDict(
4421 attrs.source_repo = StrictAttributeDict(
4422 pull_request_obj.source_repo.get_api_data())
4422 pull_request_obj.source_repo.get_api_data())
4423 attrs.source_repo.clone_url = pull_request_obj.source_repo.clone_url
4423 attrs.source_repo.clone_url = pull_request_obj.source_repo.clone_url
4424
4424
4425 attrs.source_ref_parts = pull_request_obj.source_ref_parts
4425 attrs.source_ref_parts = pull_request_obj.source_ref_parts
4426 attrs.target_ref_parts = pull_request_obj.target_ref_parts
4426 attrs.target_ref_parts = pull_request_obj.target_ref_parts
4427 attrs.revisions = pull_request_obj.revisions
4427 attrs.revisions = pull_request_obj.revisions
4428 attrs.common_ancestor_id = pull_request_obj.common_ancestor_id
4428 attrs.common_ancestor_id = pull_request_obj.common_ancestor_id
4429 attrs.shadow_merge_ref = org_pull_request_obj.shadow_merge_ref
4429 attrs.shadow_merge_ref = org_pull_request_obj.shadow_merge_ref
4430 attrs.reviewer_data = org_pull_request_obj.reviewer_data
4430 attrs.reviewer_data = org_pull_request_obj.reviewer_data
4431 attrs.reviewer_data_json = org_pull_request_obj.reviewer_data_json
4431 attrs.reviewer_data_json = org_pull_request_obj.reviewer_data_json
4432
4432
4433 return PullRequestDisplay(attrs, internal=internal_methods)
4433 return PullRequestDisplay(attrs, internal=internal_methods)
4434
4434
4435 def is_closed(self):
4435 def is_closed(self):
4436 return self.status == self.STATUS_CLOSED
4436 return self.status == self.STATUS_CLOSED
4437
4437
4438 def is_state_changing(self):
4438 def is_state_changing(self):
4439 return self.pull_request_state != PullRequest.STATE_CREATED
4439 return self.pull_request_state != PullRequest.STATE_CREATED
4440
4440
4441 def __json__(self):
4441 def __json__(self):
4442 return {
4442 return {
4443 'revisions': self.revisions,
4443 'revisions': self.revisions,
4444 'versions': self.versions_count
4444 'versions': self.versions_count
4445 }
4445 }
4446
4446
4447 def calculated_review_status(self):
4447 def calculated_review_status(self):
4448 from rhodecode.model.changeset_status import ChangesetStatusModel
4448 from rhodecode.model.changeset_status import ChangesetStatusModel
4449 return ChangesetStatusModel().calculated_review_status(self)
4449 return ChangesetStatusModel().calculated_review_status(self)
4450
4450
4451 def reviewers_statuses(self):
4451 def reviewers_statuses(self):
4452 from rhodecode.model.changeset_status import ChangesetStatusModel
4452 from rhodecode.model.changeset_status import ChangesetStatusModel
4453 return ChangesetStatusModel().reviewers_statuses(self)
4453 return ChangesetStatusModel().reviewers_statuses(self)
4454
4454
4455 def get_pull_request_reviewers(self, role=None):
4455 def get_pull_request_reviewers(self, role=None):
4456 qry = PullRequestReviewers.query()\
4456 qry = PullRequestReviewers.query()\
4457 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)
4457 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)
4458 if role:
4458 if role:
4459 qry = qry.filter(PullRequestReviewers.role == role)
4459 qry = qry.filter(PullRequestReviewers.role == role)
4460
4460
4461 return qry.all()
4461 return qry.all()
4462
4462
4463 @property
4463 @property
4464 def reviewers_count(self):
4464 def reviewers_count(self):
4465 qry = PullRequestReviewers.query()\
4465 qry = PullRequestReviewers.query()\
4466 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4466 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4467 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_REVIEWER)
4467 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_REVIEWER)
4468 return qry.count()
4468 return qry.count()
4469
4469
4470 @property
4470 @property
4471 def observers_count(self):
4471 def observers_count(self):
4472 qry = PullRequestReviewers.query()\
4472 qry = PullRequestReviewers.query()\
4473 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4473 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4474 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_OBSERVER)
4474 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_OBSERVER)
4475 return qry.count()
4475 return qry.count()
4476
4476
4477 def observers(self):
4477 def observers(self):
4478 qry = PullRequestReviewers.query()\
4478 qry = PullRequestReviewers.query()\
4479 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4479 .filter(PullRequestReviewers.pull_request_id == self.pull_request_id)\
4480 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_OBSERVER)\
4480 .filter(PullRequestReviewers.role == PullRequestReviewers.ROLE_OBSERVER)\
4481 .all()
4481 .all()
4482
4482
4483 for entry in qry:
4483 for entry in qry:
4484 yield entry, entry.user
4484 yield entry, entry.user
4485
4485
4486 @property
4486 @property
4487 def workspace_id(self):
4487 def workspace_id(self):
4488 from rhodecode.model.pull_request import PullRequestModel
4488 from rhodecode.model.pull_request import PullRequestModel
4489 return PullRequestModel()._workspace_id(self)
4489 return PullRequestModel()._workspace_id(self)
4490
4490
4491 def get_shadow_repo(self):
4491 def get_shadow_repo(self):
4492 workspace_id = self.workspace_id
4492 workspace_id = self.workspace_id
4493 shadow_repository_path = self.target_repo.get_shadow_repository_path(workspace_id)
4493 shadow_repository_path = self.target_repo.get_shadow_repository_path(workspace_id)
4494 if os.path.isdir(shadow_repository_path):
4494 if os.path.isdir(shadow_repository_path):
4495 vcs_obj = self.target_repo.scm_instance()
4495 vcs_obj = self.target_repo.scm_instance()
4496 return vcs_obj.get_shadow_instance(shadow_repository_path)
4496 return vcs_obj.get_shadow_instance(shadow_repository_path)
4497
4497
4498 @property
4498 @property
4499 def versions_count(self):
4499 def versions_count(self):
4500 """
4500 """
4501 return number of versions this PR have, e.g a PR that once been
4501 return number of versions this PR have, e.g a PR that once been
4502 updated will have 2 versions
4502 updated will have 2 versions
4503 """
4503 """
4504 return self.versions.count() + 1
4504 return self.versions.count() + 1
4505
4505
4506 @property
4506 @property
4507 def pull_request_last_version(self):
4507 def pull_request_last_version(self):
4508 return self.versions_count
4508 return self.versions_count
4509
4509
4510
4510
4511 class PullRequestVersion(Base, _PullRequestBase):
4511 class PullRequestVersion(Base, _PullRequestBase):
4512 __tablename__ = 'pull_request_versions'
4512 __tablename__ = 'pull_request_versions'
4513 __table_args__ = (
4513 __table_args__ = (
4514 base_table_args,
4514 base_table_args,
4515 )
4515 )
4516
4516
4517 pull_request_version_id = Column(
4517 pull_request_version_id = Column(
4518 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
4518 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
4519 pull_request_id = Column(
4519 pull_request_id = Column(
4520 'pull_request_id', Integer(),
4520 'pull_request_id', Integer(),
4521 ForeignKey('pull_requests.pull_request_id'), nullable=False)
4521 ForeignKey('pull_requests.pull_request_id'), nullable=False)
4522 pull_request = relationship('PullRequest')
4522 pull_request = relationship('PullRequest')
4523
4523
4524 def __repr__(self):
4524 def __repr__(self):
4525 if self.pull_request_version_id:
4525 if self.pull_request_version_id:
4526 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
4526 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
4527 else:
4527 else:
4528 return '<DB:PullRequestVersion at %#x>' % id(self)
4528 return '<DB:PullRequestVersion at %#x>' % id(self)
4529
4529
4530 @property
4530 @property
4531 def reviewers(self):
4531 def reviewers(self):
4532 return self.pull_request.reviewers
4532 return self.pull_request.reviewers
4533 @property
4533 @property
4534 def reviewers(self):
4534 def reviewers(self):
4535 return self.pull_request.reviewers
4535 return self.pull_request.reviewers
4536
4536
4537 @property
4537 @property
4538 def versions(self):
4538 def versions(self):
4539 return self.pull_request.versions
4539 return self.pull_request.versions
4540
4540
4541 def is_closed(self):
4541 def is_closed(self):
4542 # calculate from original
4542 # calculate from original
4543 return self.pull_request.status == self.STATUS_CLOSED
4543 return self.pull_request.status == self.STATUS_CLOSED
4544
4544
4545 def is_state_changing(self):
4545 def is_state_changing(self):
4546 return self.pull_request.pull_request_state != PullRequest.STATE_CREATED
4546 return self.pull_request.pull_request_state != PullRequest.STATE_CREATED
4547
4547
4548 def calculated_review_status(self):
4548 def calculated_review_status(self):
4549 return self.pull_request.calculated_review_status()
4549 return self.pull_request.calculated_review_status()
4550
4550
4551 def reviewers_statuses(self):
4551 def reviewers_statuses(self):
4552 return self.pull_request.reviewers_statuses()
4552 return self.pull_request.reviewers_statuses()
4553
4553
4554 def observers(self):
4554 def observers(self):
4555 return self.pull_request.observers()
4555 return self.pull_request.observers()
4556
4556
4557
4557
4558 class PullRequestReviewers(Base, BaseModel):
4558 class PullRequestReviewers(Base, BaseModel):
4559 __tablename__ = 'pull_request_reviewers'
4559 __tablename__ = 'pull_request_reviewers'
4560 __table_args__ = (
4560 __table_args__ = (
4561 base_table_args,
4561 base_table_args,
4562 )
4562 )
4563 ROLE_REVIEWER = u'reviewer'
4563 ROLE_REVIEWER = u'reviewer'
4564 ROLE_OBSERVER = u'observer'
4564 ROLE_OBSERVER = u'observer'
4565 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
4565 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
4566
4566
4567 @hybrid_property
4567 @hybrid_property
4568 def reasons(self):
4568 def reasons(self):
4569 if not self._reasons:
4569 if not self._reasons:
4570 return []
4570 return []
4571 return self._reasons
4571 return self._reasons
4572
4572
4573 @reasons.setter
4573 @reasons.setter
4574 def reasons(self, val):
4574 def reasons(self, val):
4575 val = val or []
4575 val = val or []
4576 if any(not isinstance(x, compat.string_types) for x in val):
4576 if any(not isinstance(x, compat.string_types) for x in val):
4577 raise Exception('invalid reasons type, must be list of strings')
4577 raise Exception('invalid reasons type, must be list of strings')
4578 self._reasons = val
4578 self._reasons = val
4579
4579
4580 pull_requests_reviewers_id = Column(
4580 pull_requests_reviewers_id = Column(
4581 'pull_requests_reviewers_id', Integer(), nullable=False,
4581 'pull_requests_reviewers_id', Integer(), nullable=False,
4582 primary_key=True)
4582 primary_key=True)
4583 pull_request_id = Column(
4583 pull_request_id = Column(
4584 "pull_request_id", Integer(),
4584 "pull_request_id", Integer(),
4585 ForeignKey('pull_requests.pull_request_id'), nullable=False)
4585 ForeignKey('pull_requests.pull_request_id'), nullable=False)
4586 user_id = Column(
4586 user_id = Column(
4587 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
4587 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
4588 _reasons = Column(
4588 _reasons = Column(
4589 'reason', MutationList.as_mutable(
4589 'reason', MutationList.as_mutable(
4590 JsonType('list', dialect_map=dict(mysql=UnicodeText(16384)))))
4590 JsonType('list', dialect_map=dict(mysql=UnicodeText(16384)))))
4591
4591
4592 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4592 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4593 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
4593 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
4594
4594
4595 user = relationship('User')
4595 user = relationship('User')
4596 pull_request = relationship('PullRequest')
4596 pull_request = relationship('PullRequest')
4597
4597
4598 rule_data = Column(
4598 rule_data = Column(
4599 'rule_data_json',
4599 'rule_data_json',
4600 JsonType(dialect_map=dict(mysql=UnicodeText(16384))))
4600 JsonType(dialect_map=dict(mysql=UnicodeText(16384))))
4601
4601
4602 def rule_user_group_data(self):
4602 def rule_user_group_data(self):
4603 """
4603 """
4604 Returns the voting user group rule data for this reviewer
4604 Returns the voting user group rule data for this reviewer
4605 """
4605 """
4606
4606
4607 if self.rule_data and 'vote_rule' in self.rule_data:
4607 if self.rule_data and 'vote_rule' in self.rule_data:
4608 user_group_data = {}
4608 user_group_data = {}
4609 if 'rule_user_group_entry_id' in self.rule_data:
4609 if 'rule_user_group_entry_id' in self.rule_data:
4610 # means a group with voting rules !
4610 # means a group with voting rules !
4611 user_group_data['id'] = self.rule_data['rule_user_group_entry_id']
4611 user_group_data['id'] = self.rule_data['rule_user_group_entry_id']
4612 user_group_data['name'] = self.rule_data['rule_name']
4612 user_group_data['name'] = self.rule_data['rule_name']
4613 user_group_data['vote_rule'] = self.rule_data['vote_rule']
4613 user_group_data['vote_rule'] = self.rule_data['vote_rule']
4614
4614
4615 return user_group_data
4615 return user_group_data
4616
4616
4617 @classmethod
4617 @classmethod
4618 def get_pull_request_reviewers(cls, pull_request_id, role=None):
4618 def get_pull_request_reviewers(cls, pull_request_id, role=None):
4619 qry = PullRequestReviewers.query()\
4619 qry = PullRequestReviewers.query()\
4620 .filter(PullRequestReviewers.pull_request_id == pull_request_id)
4620 .filter(PullRequestReviewers.pull_request_id == pull_request_id)
4621 if role:
4621 if role:
4622 qry = qry.filter(PullRequestReviewers.role == role)
4622 qry = qry.filter(PullRequestReviewers.role == role)
4623
4623
4624 return qry.all()
4624 return qry.all()
4625
4625
4626 def __unicode__(self):
4626 def __unicode__(self):
4627 return u"<%s('id:%s')>" % (self.__class__.__name__,
4627 return u"<%s('id:%s')>" % (self.__class__.__name__,
4628 self.pull_requests_reviewers_id)
4628 self.pull_requests_reviewers_id)
4629
4629
4630
4630
4631 class Notification(Base, BaseModel):
4631 class Notification(Base, BaseModel):
4632 __tablename__ = 'notifications'
4632 __tablename__ = 'notifications'
4633 __table_args__ = (
4633 __table_args__ = (
4634 Index('notification_type_idx', 'type'),
4634 Index('notification_type_idx', 'type'),
4635 base_table_args,
4635 base_table_args,
4636 )
4636 )
4637
4637
4638 TYPE_CHANGESET_COMMENT = u'cs_comment'
4638 TYPE_CHANGESET_COMMENT = u'cs_comment'
4639 TYPE_MESSAGE = u'message'
4639 TYPE_MESSAGE = u'message'
4640 TYPE_MENTION = u'mention'
4640 TYPE_MENTION = u'mention'
4641 TYPE_REGISTRATION = u'registration'
4641 TYPE_REGISTRATION = u'registration'
4642 TYPE_PULL_REQUEST = u'pull_request'
4642 TYPE_PULL_REQUEST = u'pull_request'
4643 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
4643 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
4644 TYPE_PULL_REQUEST_UPDATE = u'pull_request_update'
4644 TYPE_PULL_REQUEST_UPDATE = u'pull_request_update'
4645
4645
4646 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
4646 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
4647 subject = Column('subject', Unicode(512), nullable=True)
4647 subject = Column('subject', Unicode(512), nullable=True)
4648 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
4648 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
4649 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
4649 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
4650 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4650 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4651 type_ = Column('type', Unicode(255))
4651 type_ = Column('type', Unicode(255))
4652
4652
4653 created_by_user = relationship('User')
4653 created_by_user = relationship('User')
4654 notifications_to_users = relationship('UserNotification', lazy='joined',
4654 notifications_to_users = relationship('UserNotification', lazy='joined',
4655 cascade="all, delete-orphan")
4655 cascade="all, delete-orphan")
4656
4656
4657 @property
4657 @property
4658 def recipients(self):
4658 def recipients(self):
4659 return [x.user for x in UserNotification.query()\
4659 return [x.user for x in UserNotification.query()\
4660 .filter(UserNotification.notification == self)\
4660 .filter(UserNotification.notification == self)\
4661 .order_by(UserNotification.user_id.asc()).all()]
4661 .order_by(UserNotification.user_id.asc()).all()]
4662
4662
4663 @classmethod
4663 @classmethod
4664 def create(cls, created_by, subject, body, recipients, type_=None):
4664 def create(cls, created_by, subject, body, recipients, type_=None):
4665 if type_ is None:
4665 if type_ is None:
4666 type_ = Notification.TYPE_MESSAGE
4666 type_ = Notification.TYPE_MESSAGE
4667
4667
4668 notification = cls()
4668 notification = cls()
4669 notification.created_by_user = created_by
4669 notification.created_by_user = created_by
4670 notification.subject = subject
4670 notification.subject = subject
4671 notification.body = body
4671 notification.body = body
4672 notification.type_ = type_
4672 notification.type_ = type_
4673 notification.created_on = datetime.datetime.now()
4673 notification.created_on = datetime.datetime.now()
4674
4674
4675 # For each recipient link the created notification to his account
4675 # For each recipient link the created notification to his account
4676 for u in recipients:
4676 for u in recipients:
4677 assoc = UserNotification()
4677 assoc = UserNotification()
4678 assoc.user_id = u.user_id
4678 assoc.user_id = u.user_id
4679 assoc.notification = notification
4679 assoc.notification = notification
4680
4680
4681 # if created_by is inside recipients mark his notification
4681 # if created_by is inside recipients mark his notification
4682 # as read
4682 # as read
4683 if u.user_id == created_by.user_id:
4683 if u.user_id == created_by.user_id:
4684 assoc.read = True
4684 assoc.read = True
4685 Session().add(assoc)
4685 Session().add(assoc)
4686
4686
4687 Session().add(notification)
4687 Session().add(notification)
4688
4688
4689 return notification
4689 return notification
4690
4690
4691
4691
4692 class UserNotification(Base, BaseModel):
4692 class UserNotification(Base, BaseModel):
4693 __tablename__ = 'user_to_notification'
4693 __tablename__ = 'user_to_notification'
4694 __table_args__ = (
4694 __table_args__ = (
4695 UniqueConstraint('user_id', 'notification_id'),
4695 UniqueConstraint('user_id', 'notification_id'),
4696 base_table_args
4696 base_table_args
4697 )
4697 )
4698
4698
4699 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
4699 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
4700 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
4700 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
4701 read = Column('read', Boolean, default=False)
4701 read = Column('read', Boolean, default=False)
4702 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
4702 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
4703
4703
4704 user = relationship('User', lazy="joined")
4704 user = relationship('User', lazy="joined")
4705 notification = relationship('Notification', lazy="joined",
4705 notification = relationship('Notification', lazy="joined",
4706 order_by=lambda: Notification.created_on.desc(),)
4706 order_by=lambda: Notification.created_on.desc(),)
4707
4707
4708 def mark_as_read(self):
4708 def mark_as_read(self):
4709 self.read = True
4709 self.read = True
4710 Session().add(self)
4710 Session().add(self)
4711
4711
4712
4712
4713 class UserNotice(Base, BaseModel):
4713 class UserNotice(Base, BaseModel):
4714 __tablename__ = 'user_notices'
4714 __tablename__ = 'user_notices'
4715 __table_args__ = (
4715 __table_args__ = (
4716 base_table_args
4716 base_table_args
4717 )
4717 )
4718
4718
4719 NOTIFICATION_TYPE_MESSAGE = 'message'
4719 NOTIFICATION_TYPE_MESSAGE = 'message'
4720 NOTIFICATION_TYPE_NOTICE = 'notice'
4720 NOTIFICATION_TYPE_NOTICE = 'notice'
4721
4721
4722 NOTIFICATION_LEVEL_INFO = 'info'
4722 NOTIFICATION_LEVEL_INFO = 'info'
4723 NOTIFICATION_LEVEL_WARNING = 'warning'
4723 NOTIFICATION_LEVEL_WARNING = 'warning'
4724 NOTIFICATION_LEVEL_ERROR = 'error'
4724 NOTIFICATION_LEVEL_ERROR = 'error'
4725
4725
4726 user_notice_id = Column('gist_id', Integer(), primary_key=True)
4726 user_notice_id = Column('gist_id', Integer(), primary_key=True)
4727
4727
4728 notice_subject = Column('notice_subject', Unicode(512), nullable=True)
4728 notice_subject = Column('notice_subject', Unicode(512), nullable=True)
4729 notice_body = Column('notice_body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
4729 notice_body = Column('notice_body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
4730
4730
4731 notice_read = Column('notice_read', Boolean, default=False)
4731 notice_read = Column('notice_read', Boolean, default=False)
4732
4732
4733 notification_level = Column('notification_level', String(1024), default=NOTIFICATION_LEVEL_INFO)
4733 notification_level = Column('notification_level', String(1024), default=NOTIFICATION_LEVEL_INFO)
4734 notification_type = Column('notification_type', String(1024), default=NOTIFICATION_TYPE_NOTICE)
4734 notification_type = Column('notification_type', String(1024), default=NOTIFICATION_TYPE_NOTICE)
4735
4735
4736 notice_created_by = Column('notice_created_by', Integer(), ForeignKey('users.user_id'), nullable=True)
4736 notice_created_by = Column('notice_created_by', Integer(), ForeignKey('users.user_id'), nullable=True)
4737 notice_created_on = Column('notice_created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4737 notice_created_on = Column('notice_created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4738
4738
4739 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'))
4739 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'))
4740 user = relationship('User', lazy="joined", primaryjoin='User.user_id==UserNotice.user_id')
4740 user = relationship('User', lazy="joined", primaryjoin='User.user_id==UserNotice.user_id')
4741
4741
4742 @classmethod
4742 @classmethod
4743 def create_for_user(cls, user, subject, body, notice_level=NOTIFICATION_LEVEL_INFO, allow_duplicate=False):
4743 def create_for_user(cls, user, subject, body, notice_level=NOTIFICATION_LEVEL_INFO, allow_duplicate=False):
4744
4744
4745 if notice_level not in [cls.NOTIFICATION_LEVEL_ERROR,
4745 if notice_level not in [cls.NOTIFICATION_LEVEL_ERROR,
4746 cls.NOTIFICATION_LEVEL_WARNING,
4746 cls.NOTIFICATION_LEVEL_WARNING,
4747 cls.NOTIFICATION_LEVEL_INFO]:
4747 cls.NOTIFICATION_LEVEL_INFO]:
4748 return
4748 return
4749
4749
4750 from rhodecode.model.user import UserModel
4750 from rhodecode.model.user import UserModel
4751 user = UserModel().get_user(user)
4751 user = UserModel().get_user(user)
4752
4752
4753 new_notice = UserNotice()
4753 new_notice = UserNotice()
4754 if not allow_duplicate:
4754 if not allow_duplicate:
4755 existing_msg = UserNotice().query() \
4755 existing_msg = UserNotice().query() \
4756 .filter(UserNotice.user == user) \
4756 .filter(UserNotice.user == user) \
4757 .filter(UserNotice.notice_body == body) \
4757 .filter(UserNotice.notice_body == body) \
4758 .filter(UserNotice.notice_read == false()) \
4758 .filter(UserNotice.notice_read == false()) \
4759 .scalar()
4759 .scalar()
4760 if existing_msg:
4760 if existing_msg:
4761 log.warning('Ignoring duplicate notice for user %s', user)
4761 log.warning('Ignoring duplicate notice for user %s', user)
4762 return
4762 return
4763
4763
4764 new_notice.user = user
4764 new_notice.user = user
4765 new_notice.notice_subject = subject
4765 new_notice.notice_subject = subject
4766 new_notice.notice_body = body
4766 new_notice.notice_body = body
4767 new_notice.notification_level = notice_level
4767 new_notice.notification_level = notice_level
4768 Session().add(new_notice)
4768 Session().add(new_notice)
4769 Session().commit()
4769 Session().commit()
4770
4770
4771
4771
4772 class Gist(Base, BaseModel):
4772 class Gist(Base, BaseModel):
4773 __tablename__ = 'gists'
4773 __tablename__ = 'gists'
4774 __table_args__ = (
4774 __table_args__ = (
4775 Index('g_gist_access_id_idx', 'gist_access_id'),
4775 Index('g_gist_access_id_idx', 'gist_access_id'),
4776 Index('g_created_on_idx', 'created_on'),
4776 Index('g_created_on_idx', 'created_on'),
4777 base_table_args
4777 base_table_args
4778 )
4778 )
4779
4779
4780 GIST_PUBLIC = u'public'
4780 GIST_PUBLIC = u'public'
4781 GIST_PRIVATE = u'private'
4781 GIST_PRIVATE = u'private'
4782 DEFAULT_FILENAME = u'gistfile1.txt'
4782 DEFAULT_FILENAME = u'gistfile1.txt'
4783
4783
4784 ACL_LEVEL_PUBLIC = u'acl_public'
4784 ACL_LEVEL_PUBLIC = u'acl_public'
4785 ACL_LEVEL_PRIVATE = u'acl_private'
4785 ACL_LEVEL_PRIVATE = u'acl_private'
4786
4786
4787 gist_id = Column('gist_id', Integer(), primary_key=True)
4787 gist_id = Column('gist_id', Integer(), primary_key=True)
4788 gist_access_id = Column('gist_access_id', Unicode(250))
4788 gist_access_id = Column('gist_access_id', Unicode(250))
4789 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
4789 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
4790 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
4790 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
4791 gist_expires = Column('gist_expires', Float(53), nullable=False)
4791 gist_expires = Column('gist_expires', Float(53), nullable=False)
4792 gist_type = Column('gist_type', Unicode(128), nullable=False)
4792 gist_type = Column('gist_type', Unicode(128), nullable=False)
4793 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4793 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4794 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4794 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4795 acl_level = Column('acl_level', Unicode(128), nullable=True)
4795 acl_level = Column('acl_level', Unicode(128), nullable=True)
4796
4796
4797 owner = relationship('User')
4797 owner = relationship('User')
4798
4798
4799 def __repr__(self):
4799 def __repr__(self):
4800 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
4800 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
4801
4801
4802 @hybrid_property
4802 @hybrid_property
4803 def description_safe(self):
4803 def description_safe(self):
4804 from rhodecode.lib import helpers as h
4804 from rhodecode.lib import helpers as h
4805 return h.escape(self.gist_description)
4805 return h.escape(self.gist_description)
4806
4806
4807 @classmethod
4807 @classmethod
4808 def get_or_404(cls, id_):
4808 def get_or_404(cls, id_):
4809 from pyramid.httpexceptions import HTTPNotFound
4809 from pyramid.httpexceptions import HTTPNotFound
4810
4810
4811 res = cls.query().filter(cls.gist_access_id == id_).scalar()
4811 res = cls.query().filter(cls.gist_access_id == id_).scalar()
4812 if not res:
4812 if not res:
4813 raise HTTPNotFound()
4813 raise HTTPNotFound()
4814 return res
4814 return res
4815
4815
4816 @classmethod
4816 @classmethod
4817 def get_by_access_id(cls, gist_access_id):
4817 def get_by_access_id(cls, gist_access_id):
4818 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
4818 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
4819
4819
4820 def gist_url(self):
4820 def gist_url(self):
4821 from rhodecode.model.gist import GistModel
4821 from rhodecode.model.gist import GistModel
4822 return GistModel().get_url(self)
4822 return GistModel().get_url(self)
4823
4823
4824 @classmethod
4824 @classmethod
4825 def base_path(cls):
4825 def base_path(cls):
4826 """
4826 """
4827 Returns base path when all gists are stored
4827 Returns base path when all gists are stored
4828
4828
4829 :param cls:
4829 :param cls:
4830 """
4830 """
4831 from rhodecode.model.gist import GIST_STORE_LOC
4831 from rhodecode.model.gist import GIST_STORE_LOC
4832 q = Session().query(RhodeCodeUi)\
4832 q = Session().query(RhodeCodeUi)\
4833 .filter(RhodeCodeUi.ui_key == URL_SEP)
4833 .filter(RhodeCodeUi.ui_key == URL_SEP)
4834 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
4834 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
4835 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
4835 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
4836
4836
4837 def get_api_data(self):
4837 def get_api_data(self):
4838 """
4838 """
4839 Common function for generating gist related data for API
4839 Common function for generating gist related data for API
4840 """
4840 """
4841 gist = self
4841 gist = self
4842 data = {
4842 data = {
4843 'gist_id': gist.gist_id,
4843 'gist_id': gist.gist_id,
4844 'type': gist.gist_type,
4844 'type': gist.gist_type,
4845 'access_id': gist.gist_access_id,
4845 'access_id': gist.gist_access_id,
4846 'description': gist.gist_description,
4846 'description': gist.gist_description,
4847 'url': gist.gist_url(),
4847 'url': gist.gist_url(),
4848 'expires': gist.gist_expires,
4848 'expires': gist.gist_expires,
4849 'created_on': gist.created_on,
4849 'created_on': gist.created_on,
4850 'modified_at': gist.modified_at,
4850 'modified_at': gist.modified_at,
4851 'content': None,
4851 'content': None,
4852 'acl_level': gist.acl_level,
4852 'acl_level': gist.acl_level,
4853 }
4853 }
4854 return data
4854 return data
4855
4855
4856 def __json__(self):
4856 def __json__(self):
4857 data = dict(
4857 data = dict(
4858 )
4858 )
4859 data.update(self.get_api_data())
4859 data.update(self.get_api_data())
4860 return data
4860 return data
4861 # SCM functions
4861 # SCM functions
4862
4862
4863 def scm_instance(self, **kwargs):
4863 def scm_instance(self, **kwargs):
4864 """
4864 """
4865 Get an instance of VCS Repository
4865 Get an instance of VCS Repository
4866
4866
4867 :param kwargs:
4867 :param kwargs:
4868 """
4868 """
4869 from rhodecode.model.gist import GistModel
4869 from rhodecode.model.gist import GistModel
4870 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4870 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4871 return get_vcs_instance(
4871 return get_vcs_instance(
4872 repo_path=safe_str(full_repo_path), create=False,
4872 repo_path=safe_str(full_repo_path), create=False,
4873 _vcs_alias=GistModel.vcs_backend)
4873 _vcs_alias=GistModel.vcs_backend)
4874
4874
4875
4875
4876 class ExternalIdentity(Base, BaseModel):
4876 class ExternalIdentity(Base, BaseModel):
4877 __tablename__ = 'external_identities'
4877 __tablename__ = 'external_identities'
4878 __table_args__ = (
4878 __table_args__ = (
4879 Index('local_user_id_idx', 'local_user_id'),
4879 Index('local_user_id_idx', 'local_user_id'),
4880 Index('external_id_idx', 'external_id'),
4880 Index('external_id_idx', 'external_id'),
4881 base_table_args
4881 base_table_args
4882 )
4882 )
4883
4883
4884 external_id = Column('external_id', Unicode(255), default=u'', primary_key=True)
4884 external_id = Column('external_id', Unicode(255), default=u'', primary_key=True)
4885 external_username = Column('external_username', Unicode(1024), default=u'')
4885 external_username = Column('external_username', Unicode(1024), default=u'')
4886 local_user_id = Column('local_user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
4886 local_user_id = Column('local_user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
4887 provider_name = Column('provider_name', Unicode(255), default=u'', primary_key=True)
4887 provider_name = Column('provider_name', Unicode(255), default=u'', primary_key=True)
4888 access_token = Column('access_token', String(1024), default=u'')
4888 access_token = Column('access_token', String(1024), default=u'')
4889 alt_token = Column('alt_token', String(1024), default=u'')
4889 alt_token = Column('alt_token', String(1024), default=u'')
4890 token_secret = Column('token_secret', String(1024), default=u'')
4890 token_secret = Column('token_secret', String(1024), default=u'')
4891
4891
4892 @classmethod
4892 @classmethod
4893 def by_external_id_and_provider(cls, external_id, provider_name, local_user_id=None):
4893 def by_external_id_and_provider(cls, external_id, provider_name, local_user_id=None):
4894 """
4894 """
4895 Returns ExternalIdentity instance based on search params
4895 Returns ExternalIdentity instance based on search params
4896
4896
4897 :param external_id:
4897 :param external_id:
4898 :param provider_name:
4898 :param provider_name:
4899 :return: ExternalIdentity
4899 :return: ExternalIdentity
4900 """
4900 """
4901 query = cls.query()
4901 query = cls.query()
4902 query = query.filter(cls.external_id == external_id)
4902 query = query.filter(cls.external_id == external_id)
4903 query = query.filter(cls.provider_name == provider_name)
4903 query = query.filter(cls.provider_name == provider_name)
4904 if local_user_id:
4904 if local_user_id:
4905 query = query.filter(cls.local_user_id == local_user_id)
4905 query = query.filter(cls.local_user_id == local_user_id)
4906 return query.first()
4906 return query.first()
4907
4907
4908 @classmethod
4908 @classmethod
4909 def user_by_external_id_and_provider(cls, external_id, provider_name):
4909 def user_by_external_id_and_provider(cls, external_id, provider_name):
4910 """
4910 """
4911 Returns User instance based on search params
4911 Returns User instance based on search params
4912
4912
4913 :param external_id:
4913 :param external_id:
4914 :param provider_name:
4914 :param provider_name:
4915 :return: User
4915 :return: User
4916 """
4916 """
4917 query = User.query()
4917 query = User.query()
4918 query = query.filter(cls.external_id == external_id)
4918 query = query.filter(cls.external_id == external_id)
4919 query = query.filter(cls.provider_name == provider_name)
4919 query = query.filter(cls.provider_name == provider_name)
4920 query = query.filter(User.user_id == cls.local_user_id)
4920 query = query.filter(User.user_id == cls.local_user_id)
4921 return query.first()
4921 return query.first()
4922
4922
4923 @classmethod
4923 @classmethod
4924 def by_local_user_id(cls, local_user_id):
4924 def by_local_user_id(cls, local_user_id):
4925 """
4925 """
4926 Returns all tokens for user
4926 Returns all tokens for user
4927
4927
4928 :param local_user_id:
4928 :param local_user_id:
4929 :return: ExternalIdentity
4929 :return: ExternalIdentity
4930 """
4930 """
4931 query = cls.query()
4931 query = cls.query()
4932 query = query.filter(cls.local_user_id == local_user_id)
4932 query = query.filter(cls.local_user_id == local_user_id)
4933 return query
4933 return query
4934
4934
4935 @classmethod
4935 @classmethod
4936 def load_provider_plugin(cls, plugin_id):
4936 def load_provider_plugin(cls, plugin_id):
4937 from rhodecode.authentication.base import loadplugin
4937 from rhodecode.authentication.base import loadplugin
4938 _plugin_id = 'egg:rhodecode-enterprise-ee#{}'.format(plugin_id)
4938 _plugin_id = 'egg:rhodecode-enterprise-ee#{}'.format(plugin_id)
4939 auth_plugin = loadplugin(_plugin_id)
4939 auth_plugin = loadplugin(_plugin_id)
4940 return auth_plugin
4940 return auth_plugin
4941
4941
4942
4942
4943 class Integration(Base, BaseModel):
4943 class Integration(Base, BaseModel):
4944 __tablename__ = 'integrations'
4944 __tablename__ = 'integrations'
4945 __table_args__ = (
4945 __table_args__ = (
4946 base_table_args
4946 base_table_args
4947 )
4947 )
4948
4948
4949 integration_id = Column('integration_id', Integer(), primary_key=True)
4949 integration_id = Column('integration_id', Integer(), primary_key=True)
4950 integration_type = Column('integration_type', String(255))
4950 integration_type = Column('integration_type', String(255))
4951 enabled = Column('enabled', Boolean(), nullable=False)
4951 enabled = Column('enabled', Boolean(), nullable=False)
4952 name = Column('name', String(255), nullable=False)
4952 name = Column('name', String(255), nullable=False)
4953 child_repos_only = Column('child_repos_only', Boolean(), nullable=False,
4953 child_repos_only = Column('child_repos_only', Boolean(), nullable=False,
4954 default=False)
4954 default=False)
4955
4955
4956 settings = Column(
4956 settings = Column(
4957 'settings_json', MutationObj.as_mutable(
4957 'settings_json', MutationObj.as_mutable(
4958 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4958 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4959 repo_id = Column(
4959 repo_id = Column(
4960 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
4960 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
4961 nullable=True, unique=None, default=None)
4961 nullable=True, unique=None, default=None)
4962 repo = relationship('Repository', lazy='joined')
4962 repo = relationship('Repository', lazy='joined')
4963
4963
4964 repo_group_id = Column(
4964 repo_group_id = Column(
4965 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
4965 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
4966 nullable=True, unique=None, default=None)
4966 nullable=True, unique=None, default=None)
4967 repo_group = relationship('RepoGroup', lazy='joined')
4967 repo_group = relationship('RepoGroup', lazy='joined')
4968
4968
4969 @property
4969 @property
4970 def scope(self):
4970 def scope(self):
4971 if self.repo:
4971 if self.repo:
4972 return repr(self.repo)
4972 return repr(self.repo)
4973 if self.repo_group:
4973 if self.repo_group:
4974 if self.child_repos_only:
4974 if self.child_repos_only:
4975 return repr(self.repo_group) + ' (child repos only)'
4975 return repr(self.repo_group) + ' (child repos only)'
4976 else:
4976 else:
4977 return repr(self.repo_group) + ' (recursive)'
4977 return repr(self.repo_group) + ' (recursive)'
4978 if self.child_repos_only:
4978 if self.child_repos_only:
4979 return 'root_repos'
4979 return 'root_repos'
4980 return 'global'
4980 return 'global'
4981
4981
4982 def __repr__(self):
4982 def __repr__(self):
4983 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
4983 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
4984
4984
4985
4985
4986 class RepoReviewRuleUser(Base, BaseModel):
4986 class RepoReviewRuleUser(Base, BaseModel):
4987 __tablename__ = 'repo_review_rules_users'
4987 __tablename__ = 'repo_review_rules_users'
4988 __table_args__ = (
4988 __table_args__ = (
4989 base_table_args
4989 base_table_args
4990 )
4990 )
4991 ROLE_REVIEWER = u'reviewer'
4991 ROLE_REVIEWER = u'reviewer'
4992 ROLE_OBSERVER = u'observer'
4992 ROLE_OBSERVER = u'observer'
4993 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
4993 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
4994
4994
4995 repo_review_rule_user_id = Column('repo_review_rule_user_id', Integer(), primary_key=True)
4995 repo_review_rule_user_id = Column('repo_review_rule_user_id', Integer(), primary_key=True)
4996 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4996 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4997 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False)
4997 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False)
4998 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4998 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4999 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
4999 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
5000 user = relationship('User')
5000 user = relationship('User')
5001
5001
5002 def rule_data(self):
5002 def rule_data(self):
5003 return {
5003 return {
5004 'mandatory': self.mandatory,
5004 'mandatory': self.mandatory,
5005 'role': self.role,
5005 'role': self.role,
5006 }
5006 }
5007
5007
5008
5008
5009 class RepoReviewRuleUserGroup(Base, BaseModel):
5009 class RepoReviewRuleUserGroup(Base, BaseModel):
5010 __tablename__ = 'repo_review_rules_users_groups'
5010 __tablename__ = 'repo_review_rules_users_groups'
5011 __table_args__ = (
5011 __table_args__ = (
5012 base_table_args
5012 base_table_args
5013 )
5013 )
5014
5014
5015 VOTE_RULE_ALL = -1
5015 VOTE_RULE_ALL = -1
5016 ROLE_REVIEWER = u'reviewer'
5016 ROLE_REVIEWER = u'reviewer'
5017 ROLE_OBSERVER = u'observer'
5017 ROLE_OBSERVER = u'observer'
5018 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
5018 ROLES = [ROLE_REVIEWER, ROLE_OBSERVER]
5019
5019
5020 repo_review_rule_users_group_id = Column('repo_review_rule_users_group_id', Integer(), primary_key=True)
5020 repo_review_rule_users_group_id = Column('repo_review_rule_users_group_id', Integer(), primary_key=True)
5021 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
5021 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
5022 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False)
5022 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False)
5023 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
5023 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
5024 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
5024 role = Column('role', Unicode(255), nullable=True, default=ROLE_REVIEWER)
5025 vote_rule = Column("vote_rule", Integer(), nullable=True, default=VOTE_RULE_ALL)
5025 vote_rule = Column("vote_rule", Integer(), nullable=True, default=VOTE_RULE_ALL)
5026 users_group = relationship('UserGroup')
5026 users_group = relationship('UserGroup')
5027
5027
5028 def rule_data(self):
5028 def rule_data(self):
5029 return {
5029 return {
5030 'mandatory': self.mandatory,
5030 'mandatory': self.mandatory,
5031 'role': self.role,
5031 'role': self.role,
5032 'vote_rule': self.vote_rule
5032 'vote_rule': self.vote_rule
5033 }
5033 }
5034
5034
5035 @property
5035 @property
5036 def vote_rule_label(self):
5036 def vote_rule_label(self):
5037 if not self.vote_rule or self.vote_rule == self.VOTE_RULE_ALL:
5037 if not self.vote_rule or self.vote_rule == self.VOTE_RULE_ALL:
5038 return 'all must vote'
5038 return 'all must vote'
5039 else:
5039 else:
5040 return 'min. vote {}'.format(self.vote_rule)
5040 return 'min. vote {}'.format(self.vote_rule)
5041
5041
5042
5042
5043 class RepoReviewRule(Base, BaseModel):
5043 class RepoReviewRule(Base, BaseModel):
5044 __tablename__ = 'repo_review_rules'
5044 __tablename__ = 'repo_review_rules'
5045 __table_args__ = (
5045 __table_args__ = (
5046 base_table_args
5046 base_table_args
5047 )
5047 )
5048
5048
5049 repo_review_rule_id = Column(
5049 repo_review_rule_id = Column(
5050 'repo_review_rule_id', Integer(), primary_key=True)
5050 'repo_review_rule_id', Integer(), primary_key=True)
5051 repo_id = Column(
5051 repo_id = Column(
5052 "repo_id", Integer(), ForeignKey('repositories.repo_id'))
5052 "repo_id", Integer(), ForeignKey('repositories.repo_id'))
5053 repo = relationship('Repository', backref='review_rules')
5053 repo = relationship('Repository', backref='review_rules')
5054
5054
5055 review_rule_name = Column('review_rule_name', String(255))
5055 review_rule_name = Column('review_rule_name', String(255))
5056 _branch_pattern = Column("branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5056 _branch_pattern = Column("branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5057 _target_branch_pattern = Column("target_branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5057 _target_branch_pattern = Column("target_branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5058 _file_pattern = Column("file_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5058 _file_pattern = Column("file_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
5059
5059
5060 use_authors_for_review = Column("use_authors_for_review", Boolean(), nullable=False, default=False)
5060 use_authors_for_review = Column("use_authors_for_review", Boolean(), nullable=False, default=False)
5061 forbid_author_to_review = Column("forbid_author_to_review", Boolean(), nullable=False, default=False)
5061
5062 forbid_commit_author_to_review = Column("forbid_commit_author_to_review", Boolean(), nullable=False, default=False)
5062 # Legacy fields, just for backward compat
5063 _forbid_author_to_review = Column("forbid_author_to_review", Boolean(), nullable=False, default=False)
5064 _forbid_commit_author_to_review = Column("forbid_commit_author_to_review", Boolean(), nullable=False, default=False)
5065
5066 pr_author = Column("pr_author", UnicodeText().with_variant(UnicodeText(255), 'mysql'), nullable=True)
5067 commit_author = Column("commit_author", UnicodeText().with_variant(UnicodeText(255), 'mysql'), nullable=True)
5068
5063 forbid_adding_reviewers = Column("forbid_adding_reviewers", Boolean(), nullable=False, default=False)
5069 forbid_adding_reviewers = Column("forbid_adding_reviewers", Boolean(), nullable=False, default=False)
5064
5070
5065 rule_users = relationship('RepoReviewRuleUser')
5071 rule_users = relationship('RepoReviewRuleUser')
5066 rule_user_groups = relationship('RepoReviewRuleUserGroup')
5072 rule_user_groups = relationship('RepoReviewRuleUserGroup')
5067
5073
5068 def _validate_pattern(self, value):
5074 def _validate_pattern(self, value):
5069 re.compile('^' + glob2re(value) + '$')
5075 re.compile('^' + glob2re(value) + '$')
5070
5076
5071 @hybrid_property
5077 @hybrid_property
5072 def source_branch_pattern(self):
5078 def source_branch_pattern(self):
5073 return self._branch_pattern or '*'
5079 return self._branch_pattern or '*'
5074
5080
5075 @source_branch_pattern.setter
5081 @source_branch_pattern.setter
5076 def source_branch_pattern(self, value):
5082 def source_branch_pattern(self, value):
5077 self._validate_pattern(value)
5083 self._validate_pattern(value)
5078 self._branch_pattern = value or '*'
5084 self._branch_pattern = value or '*'
5079
5085
5080 @hybrid_property
5086 @hybrid_property
5081 def target_branch_pattern(self):
5087 def target_branch_pattern(self):
5082 return self._target_branch_pattern or '*'
5088 return self._target_branch_pattern or '*'
5083
5089
5084 @target_branch_pattern.setter
5090 @target_branch_pattern.setter
5085 def target_branch_pattern(self, value):
5091 def target_branch_pattern(self, value):
5086 self._validate_pattern(value)
5092 self._validate_pattern(value)
5087 self._target_branch_pattern = value or '*'
5093 self._target_branch_pattern = value or '*'
5088
5094
5089 @hybrid_property
5095 @hybrid_property
5090 def file_pattern(self):
5096 def file_pattern(self):
5091 return self._file_pattern or '*'
5097 return self._file_pattern or '*'
5092
5098
5093 @file_pattern.setter
5099 @file_pattern.setter
5094 def file_pattern(self, value):
5100 def file_pattern(self, value):
5095 self._validate_pattern(value)
5101 self._validate_pattern(value)
5096 self._file_pattern = value or '*'
5102 self._file_pattern = value or '*'
5097
5103
5104 @hybrid_property
5105 def forbid_pr_author_to_review(self):
5106 return self.pr_author == 'forbid_pr_author'
5107
5108 @hybrid_property
5109 def include_pr_author_to_review(self):
5110 return self.pr_author == 'include_pr_author'
5111
5112 @hybrid_property
5113 def forbid_commit_author_to_review(self):
5114 return self.commit_author == 'forbid_commit_author'
5115
5116 @hybrid_property
5117 def include_commit_author_to_review(self):
5118 return self.commit_author == 'include_commit_author'
5119
5098 def matches(self, source_branch, target_branch, files_changed):
5120 def matches(self, source_branch, target_branch, files_changed):
5099 """
5121 """
5100 Check if this review rule matches a branch/files in a pull request
5122 Check if this review rule matches a branch/files in a pull request
5101
5123
5102 :param source_branch: source branch name for the commit
5124 :param source_branch: source branch name for the commit
5103 :param target_branch: target branch name for the commit
5125 :param target_branch: target branch name for the commit
5104 :param files_changed: list of file paths changed in the pull request
5126 :param files_changed: list of file paths changed in the pull request
5105 """
5127 """
5106
5128
5107 source_branch = source_branch or ''
5129 source_branch = source_branch or ''
5108 target_branch = target_branch or ''
5130 target_branch = target_branch or ''
5109 files_changed = files_changed or []
5131 files_changed = files_changed or []
5110
5132
5111 branch_matches = True
5133 branch_matches = True
5112 if source_branch or target_branch:
5134 if source_branch or target_branch:
5113 if self.source_branch_pattern == '*':
5135 if self.source_branch_pattern == '*':
5114 source_branch_match = True
5136 source_branch_match = True
5115 else:
5137 else:
5116 if self.source_branch_pattern.startswith('re:'):
5138 if self.source_branch_pattern.startswith('re:'):
5117 source_pattern = self.source_branch_pattern[3:]
5139 source_pattern = self.source_branch_pattern[3:]
5118 else:
5140 else:
5119 source_pattern = '^' + glob2re(self.source_branch_pattern) + '$'
5141 source_pattern = '^' + glob2re(self.source_branch_pattern) + '$'
5120 source_branch_regex = re.compile(source_pattern)
5142 source_branch_regex = re.compile(source_pattern)
5121 source_branch_match = bool(source_branch_regex.search(source_branch))
5143 source_branch_match = bool(source_branch_regex.search(source_branch))
5122 if self.target_branch_pattern == '*':
5144 if self.target_branch_pattern == '*':
5123 target_branch_match = True
5145 target_branch_match = True
5124 else:
5146 else:
5125 if self.target_branch_pattern.startswith('re:'):
5147 if self.target_branch_pattern.startswith('re:'):
5126 target_pattern = self.target_branch_pattern[3:]
5148 target_pattern = self.target_branch_pattern[3:]
5127 else:
5149 else:
5128 target_pattern = '^' + glob2re(self.target_branch_pattern) + '$'
5150 target_pattern = '^' + glob2re(self.target_branch_pattern) + '$'
5129 target_branch_regex = re.compile(target_pattern)
5151 target_branch_regex = re.compile(target_pattern)
5130 target_branch_match = bool(target_branch_regex.search(target_branch))
5152 target_branch_match = bool(target_branch_regex.search(target_branch))
5131
5153
5132 branch_matches = source_branch_match and target_branch_match
5154 branch_matches = source_branch_match and target_branch_match
5133
5155
5134 files_matches = True
5156 files_matches = True
5135 if self.file_pattern != '*':
5157 if self.file_pattern != '*':
5136 files_matches = False
5158 files_matches = False
5137 if self.file_pattern.startswith('re:'):
5159 if self.file_pattern.startswith('re:'):
5138 file_pattern = self.file_pattern[3:]
5160 file_pattern = self.file_pattern[3:]
5139 else:
5161 else:
5140 file_pattern = glob2re(self.file_pattern)
5162 file_pattern = glob2re(self.file_pattern)
5141 file_regex = re.compile(file_pattern)
5163 file_regex = re.compile(file_pattern)
5142 for file_data in files_changed:
5164 for file_data in files_changed:
5143 filename = file_data.get('filename')
5165 filename = file_data.get('filename')
5144
5166
5145 if file_regex.search(filename):
5167 if file_regex.search(filename):
5146 files_matches = True
5168 files_matches = True
5147 break
5169 break
5148
5170
5149 return branch_matches and files_matches
5171 return branch_matches and files_matches
5150
5172
5151 @property
5173 @property
5152 def review_users(self):
5174 def review_users(self):
5153 """ Returns the users which this rule applies to """
5175 """ Returns the users which this rule applies to """
5154
5176
5155 users = collections.OrderedDict()
5177 users = collections.OrderedDict()
5156
5178
5157 for rule_user in self.rule_users:
5179 for rule_user in self.rule_users:
5158 if rule_user.user.active:
5180 if rule_user.user.active:
5159 if rule_user.user not in users:
5181 if rule_user.user not in users:
5160 users[rule_user.user.username] = {
5182 users[rule_user.user.username] = {
5161 'user': rule_user.user,
5183 'user': rule_user.user,
5162 'source': 'user',
5184 'source': 'user',
5163 'source_data': {},
5185 'source_data': {},
5164 'data': rule_user.rule_data()
5186 'data': rule_user.rule_data()
5165 }
5187 }
5166
5188
5167 for rule_user_group in self.rule_user_groups:
5189 for rule_user_group in self.rule_user_groups:
5168 source_data = {
5190 source_data = {
5169 'user_group_id': rule_user_group.users_group.users_group_id,
5191 'user_group_id': rule_user_group.users_group.users_group_id,
5170 'name': rule_user_group.users_group.users_group_name,
5192 'name': rule_user_group.users_group.users_group_name,
5171 'members': len(rule_user_group.users_group.members)
5193 'members': len(rule_user_group.users_group.members)
5172 }
5194 }
5173 for member in rule_user_group.users_group.members:
5195 for member in rule_user_group.users_group.members:
5174 if member.user.active:
5196 if member.user.active:
5175 key = member.user.username
5197 key = member.user.username
5176 if key in users:
5198 if key in users:
5177 # skip this member as we have him already
5199 # skip this member as we have him already
5178 # this prevents from override the "first" matched
5200 # this prevents from override the "first" matched
5179 # users with duplicates in multiple groups
5201 # users with duplicates in multiple groups
5180 continue
5202 continue
5181
5203
5182 users[key] = {
5204 users[key] = {
5183 'user': member.user,
5205 'user': member.user,
5184 'source': 'user_group',
5206 'source': 'user_group',
5185 'source_data': source_data,
5207 'source_data': source_data,
5186 'data': rule_user_group.rule_data()
5208 'data': rule_user_group.rule_data()
5187 }
5209 }
5188
5210
5189 return users
5211 return users
5190
5212
5191 def user_group_vote_rule(self, user_id):
5213 def user_group_vote_rule(self, user_id):
5192
5214
5193 rules = []
5215 rules = []
5194 if not self.rule_user_groups:
5216 if not self.rule_user_groups:
5195 return rules
5217 return rules
5196
5218
5197 for user_group in self.rule_user_groups:
5219 for user_group in self.rule_user_groups:
5198 user_group_members = [x.user_id for x in user_group.users_group.members]
5220 user_group_members = [x.user_id for x in user_group.users_group.members]
5199 if user_id in user_group_members:
5221 if user_id in user_group_members:
5200 rules.append(user_group)
5222 rules.append(user_group)
5201 return rules
5223 return rules
5202
5224
5203 def __repr__(self):
5225 def __repr__(self):
5204 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
5226 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
5205 self.repo_review_rule_id, self.repo)
5227 self.repo_review_rule_id, self.repo)
5206
5228
5207
5229
5208 class ScheduleEntry(Base, BaseModel):
5230 class ScheduleEntry(Base, BaseModel):
5209 __tablename__ = 'schedule_entries'
5231 __tablename__ = 'schedule_entries'
5210 __table_args__ = (
5232 __table_args__ = (
5211 UniqueConstraint('schedule_name', name='s_schedule_name_idx'),
5233 UniqueConstraint('schedule_name', name='s_schedule_name_idx'),
5212 UniqueConstraint('task_uid', name='s_task_uid_idx'),
5234 UniqueConstraint('task_uid', name='s_task_uid_idx'),
5213 base_table_args,
5235 base_table_args,
5214 )
5236 )
5215
5237
5216 schedule_types = ['crontab', 'timedelta', 'integer']
5238 schedule_types = ['crontab', 'timedelta', 'integer']
5217 schedule_entry_id = Column('schedule_entry_id', Integer(), primary_key=True)
5239 schedule_entry_id = Column('schedule_entry_id', Integer(), primary_key=True)
5218
5240
5219 schedule_name = Column("schedule_name", String(255), nullable=False, unique=None, default=None)
5241 schedule_name = Column("schedule_name", String(255), nullable=False, unique=None, default=None)
5220 schedule_description = Column("schedule_description", String(10000), nullable=True, unique=None, default=None)
5242 schedule_description = Column("schedule_description", String(10000), nullable=True, unique=None, default=None)
5221 schedule_enabled = Column("schedule_enabled", Boolean(), nullable=False, unique=None, default=True)
5243 schedule_enabled = Column("schedule_enabled", Boolean(), nullable=False, unique=None, default=True)
5222
5244
5223 _schedule_type = Column("schedule_type", String(255), nullable=False, unique=None, default=None)
5245 _schedule_type = Column("schedule_type", String(255), nullable=False, unique=None, default=None)
5224 schedule_definition = Column('schedule_definition_json', MutationObj.as_mutable(JsonType(default=lambda: "", dialect_map=dict(mysql=LONGTEXT()))))
5246 schedule_definition = Column('schedule_definition_json', MutationObj.as_mutable(JsonType(default=lambda: "", dialect_map=dict(mysql=LONGTEXT()))))
5225
5247
5226 schedule_last_run = Column('schedule_last_run', DateTime(timezone=False), nullable=True, unique=None, default=None)
5248 schedule_last_run = Column('schedule_last_run', DateTime(timezone=False), nullable=True, unique=None, default=None)
5227 schedule_total_run_count = Column('schedule_total_run_count', Integer(), nullable=True, unique=None, default=0)
5249 schedule_total_run_count = Column('schedule_total_run_count', Integer(), nullable=True, unique=None, default=0)
5228
5250
5229 # task
5251 # task
5230 task_uid = Column("task_uid", String(255), nullable=False, unique=None, default=None)
5252 task_uid = Column("task_uid", String(255), nullable=False, unique=None, default=None)
5231 task_dot_notation = Column("task_dot_notation", String(4096), nullable=False, unique=None, default=None)
5253 task_dot_notation = Column("task_dot_notation", String(4096), nullable=False, unique=None, default=None)
5232 task_args = Column('task_args_json', MutationObj.as_mutable(JsonType(default=list, dialect_map=dict(mysql=LONGTEXT()))))
5254 task_args = Column('task_args_json', MutationObj.as_mutable(JsonType(default=list, dialect_map=dict(mysql=LONGTEXT()))))
5233 task_kwargs = Column('task_kwargs_json', MutationObj.as_mutable(JsonType(default=dict, dialect_map=dict(mysql=LONGTEXT()))))
5255 task_kwargs = Column('task_kwargs_json', MutationObj.as_mutable(JsonType(default=dict, dialect_map=dict(mysql=LONGTEXT()))))
5234
5256
5235 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5257 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5236 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=None)
5258 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=None)
5237
5259
5238 @hybrid_property
5260 @hybrid_property
5239 def schedule_type(self):
5261 def schedule_type(self):
5240 return self._schedule_type
5262 return self._schedule_type
5241
5263
5242 @schedule_type.setter
5264 @schedule_type.setter
5243 def schedule_type(self, val):
5265 def schedule_type(self, val):
5244 if val not in self.schedule_types:
5266 if val not in self.schedule_types:
5245 raise ValueError('Value must be on of `{}` and got `{}`'.format(
5267 raise ValueError('Value must be on of `{}` and got `{}`'.format(
5246 val, self.schedule_type))
5268 val, self.schedule_type))
5247
5269
5248 self._schedule_type = val
5270 self._schedule_type = val
5249
5271
5250 @classmethod
5272 @classmethod
5251 def get_uid(cls, obj):
5273 def get_uid(cls, obj):
5252 args = obj.task_args
5274 args = obj.task_args
5253 kwargs = obj.task_kwargs
5275 kwargs = obj.task_kwargs
5254 if isinstance(args, JsonRaw):
5276 if isinstance(args, JsonRaw):
5255 try:
5277 try:
5256 args = json.loads(args)
5278 args = json.loads(args)
5257 except ValueError:
5279 except ValueError:
5258 args = tuple()
5280 args = tuple()
5259
5281
5260 if isinstance(kwargs, JsonRaw):
5282 if isinstance(kwargs, JsonRaw):
5261 try:
5283 try:
5262 kwargs = json.loads(kwargs)
5284 kwargs = json.loads(kwargs)
5263 except ValueError:
5285 except ValueError:
5264 kwargs = dict()
5286 kwargs = dict()
5265
5287
5266 dot_notation = obj.task_dot_notation
5288 dot_notation = obj.task_dot_notation
5267 val = '.'.join(map(safe_str, [
5289 val = '.'.join(map(safe_str, [
5268 sorted(dot_notation), args, sorted(kwargs.items())]))
5290 sorted(dot_notation), args, sorted(kwargs.items())]))
5269 return hashlib.sha1(val).hexdigest()
5291 return hashlib.sha1(val).hexdigest()
5270
5292
5271 @classmethod
5293 @classmethod
5272 def get_by_schedule_name(cls, schedule_name):
5294 def get_by_schedule_name(cls, schedule_name):
5273 return cls.query().filter(cls.schedule_name == schedule_name).scalar()
5295 return cls.query().filter(cls.schedule_name == schedule_name).scalar()
5274
5296
5275 @classmethod
5297 @classmethod
5276 def get_by_schedule_id(cls, schedule_id):
5298 def get_by_schedule_id(cls, schedule_id):
5277 return cls.query().filter(cls.schedule_entry_id == schedule_id).scalar()
5299 return cls.query().filter(cls.schedule_entry_id == schedule_id).scalar()
5278
5300
5279 @property
5301 @property
5280 def task(self):
5302 def task(self):
5281 return self.task_dot_notation
5303 return self.task_dot_notation
5282
5304
5283 @property
5305 @property
5284 def schedule(self):
5306 def schedule(self):
5285 from rhodecode.lib.celerylib.utils import raw_2_schedule
5307 from rhodecode.lib.celerylib.utils import raw_2_schedule
5286 schedule = raw_2_schedule(self.schedule_definition, self.schedule_type)
5308 schedule = raw_2_schedule(self.schedule_definition, self.schedule_type)
5287 return schedule
5309 return schedule
5288
5310
5289 @property
5311 @property
5290 def args(self):
5312 def args(self):
5291 try:
5313 try:
5292 return list(self.task_args or [])
5314 return list(self.task_args or [])
5293 except ValueError:
5315 except ValueError:
5294 return list()
5316 return list()
5295
5317
5296 @property
5318 @property
5297 def kwargs(self):
5319 def kwargs(self):
5298 try:
5320 try:
5299 return dict(self.task_kwargs or {})
5321 return dict(self.task_kwargs or {})
5300 except ValueError:
5322 except ValueError:
5301 return dict()
5323 return dict()
5302
5324
5303 def _as_raw(self, val):
5325 def _as_raw(self, val):
5304 if hasattr(val, 'de_coerce'):
5326 if hasattr(val, 'de_coerce'):
5305 val = val.de_coerce()
5327 val = val.de_coerce()
5306 if val:
5328 if val:
5307 val = json.dumps(val)
5329 val = json.dumps(val)
5308
5330
5309 return val
5331 return val
5310
5332
5311 @property
5333 @property
5312 def schedule_definition_raw(self):
5334 def schedule_definition_raw(self):
5313 return self._as_raw(self.schedule_definition)
5335 return self._as_raw(self.schedule_definition)
5314
5336
5315 @property
5337 @property
5316 def args_raw(self):
5338 def args_raw(self):
5317 return self._as_raw(self.task_args)
5339 return self._as_raw(self.task_args)
5318
5340
5319 @property
5341 @property
5320 def kwargs_raw(self):
5342 def kwargs_raw(self):
5321 return self._as_raw(self.task_kwargs)
5343 return self._as_raw(self.task_kwargs)
5322
5344
5323 def __repr__(self):
5345 def __repr__(self):
5324 return '<DB:ScheduleEntry({}:{})>'.format(
5346 return '<DB:ScheduleEntry({}:{})>'.format(
5325 self.schedule_entry_id, self.schedule_name)
5347 self.schedule_entry_id, self.schedule_name)
5326
5348
5327
5349
5328 @event.listens_for(ScheduleEntry, 'before_update')
5350 @event.listens_for(ScheduleEntry, 'before_update')
5329 def update_task_uid(mapper, connection, target):
5351 def update_task_uid(mapper, connection, target):
5330 target.task_uid = ScheduleEntry.get_uid(target)
5352 target.task_uid = ScheduleEntry.get_uid(target)
5331
5353
5332
5354
5333 @event.listens_for(ScheduleEntry, 'before_insert')
5355 @event.listens_for(ScheduleEntry, 'before_insert')
5334 def set_task_uid(mapper, connection, target):
5356 def set_task_uid(mapper, connection, target):
5335 target.task_uid = ScheduleEntry.get_uid(target)
5357 target.task_uid = ScheduleEntry.get_uid(target)
5336
5358
5337
5359
5338 class _BaseBranchPerms(BaseModel):
5360 class _BaseBranchPerms(BaseModel):
5339 @classmethod
5361 @classmethod
5340 def compute_hash(cls, value):
5362 def compute_hash(cls, value):
5341 return sha1_safe(value)
5363 return sha1_safe(value)
5342
5364
5343 @hybrid_property
5365 @hybrid_property
5344 def branch_pattern(self):
5366 def branch_pattern(self):
5345 return self._branch_pattern or '*'
5367 return self._branch_pattern or '*'
5346
5368
5347 @hybrid_property
5369 @hybrid_property
5348 def branch_hash(self):
5370 def branch_hash(self):
5349 return self._branch_hash
5371 return self._branch_hash
5350
5372
5351 def _validate_glob(self, value):
5373 def _validate_glob(self, value):
5352 re.compile('^' + glob2re(value) + '$')
5374 re.compile('^' + glob2re(value) + '$')
5353
5375
5354 @branch_pattern.setter
5376 @branch_pattern.setter
5355 def branch_pattern(self, value):
5377 def branch_pattern(self, value):
5356 self._validate_glob(value)
5378 self._validate_glob(value)
5357 self._branch_pattern = value or '*'
5379 self._branch_pattern = value or '*'
5358 # set the Hash when setting the branch pattern
5380 # set the Hash when setting the branch pattern
5359 self._branch_hash = self.compute_hash(self._branch_pattern)
5381 self._branch_hash = self.compute_hash(self._branch_pattern)
5360
5382
5361 def matches(self, branch):
5383 def matches(self, branch):
5362 """
5384 """
5363 Check if this the branch matches entry
5385 Check if this the branch matches entry
5364
5386
5365 :param branch: branch name for the commit
5387 :param branch: branch name for the commit
5366 """
5388 """
5367
5389
5368 branch = branch or ''
5390 branch = branch or ''
5369
5391
5370 branch_matches = True
5392 branch_matches = True
5371 if branch:
5393 if branch:
5372 branch_regex = re.compile('^' + glob2re(self.branch_pattern) + '$')
5394 branch_regex = re.compile('^' + glob2re(self.branch_pattern) + '$')
5373 branch_matches = bool(branch_regex.search(branch))
5395 branch_matches = bool(branch_regex.search(branch))
5374
5396
5375 return branch_matches
5397 return branch_matches
5376
5398
5377
5399
5378 class UserToRepoBranchPermission(Base, _BaseBranchPerms):
5400 class UserToRepoBranchPermission(Base, _BaseBranchPerms):
5379 __tablename__ = 'user_to_repo_branch_permissions'
5401 __tablename__ = 'user_to_repo_branch_permissions'
5380 __table_args__ = (
5402 __table_args__ = (
5381 base_table_args
5403 base_table_args
5382 )
5404 )
5383
5405
5384 branch_rule_id = Column('branch_rule_id', Integer(), primary_key=True)
5406 branch_rule_id = Column('branch_rule_id', Integer(), primary_key=True)
5385
5407
5386 repository_id = Column('repository_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
5408 repository_id = Column('repository_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
5387 repo = relationship('Repository', backref='user_branch_perms')
5409 repo = relationship('Repository', backref='user_branch_perms')
5388
5410
5389 permission_id = Column('permission_id', Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
5411 permission_id = Column('permission_id', Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
5390 permission = relationship('Permission')
5412 permission = relationship('Permission')
5391
5413
5392 rule_to_perm_id = Column('rule_to_perm_id', Integer(), ForeignKey('repo_to_perm.repo_to_perm_id'), nullable=False, unique=None, default=None)
5414 rule_to_perm_id = Column('rule_to_perm_id', Integer(), ForeignKey('repo_to_perm.repo_to_perm_id'), nullable=False, unique=None, default=None)
5393 user_repo_to_perm = relationship('UserRepoToPerm')
5415 user_repo_to_perm = relationship('UserRepoToPerm')
5394
5416
5395 rule_order = Column('rule_order', Integer(), nullable=False)
5417 rule_order = Column('rule_order', Integer(), nullable=False)
5396 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5418 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5397 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5419 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5398
5420
5399 def __unicode__(self):
5421 def __unicode__(self):
5400 return u'<UserBranchPermission(%s => %r)>' % (
5422 return u'<UserBranchPermission(%s => %r)>' % (
5401 self.user_repo_to_perm, self.branch_pattern)
5423 self.user_repo_to_perm, self.branch_pattern)
5402
5424
5403
5425
5404 class UserGroupToRepoBranchPermission(Base, _BaseBranchPerms):
5426 class UserGroupToRepoBranchPermission(Base, _BaseBranchPerms):
5405 __tablename__ = 'user_group_to_repo_branch_permissions'
5427 __tablename__ = 'user_group_to_repo_branch_permissions'
5406 __table_args__ = (
5428 __table_args__ = (
5407 base_table_args
5429 base_table_args
5408 )
5430 )
5409
5431
5410 branch_rule_id = Column('branch_rule_id', Integer(), primary_key=True)
5432 branch_rule_id = Column('branch_rule_id', Integer(), primary_key=True)
5411
5433
5412 repository_id = Column('repository_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
5434 repository_id = Column('repository_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
5413 repo = relationship('Repository', backref='user_group_branch_perms')
5435 repo = relationship('Repository', backref='user_group_branch_perms')
5414
5436
5415 permission_id = Column('permission_id', Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
5437 permission_id = Column('permission_id', Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
5416 permission = relationship('Permission')
5438 permission = relationship('Permission')
5417
5439
5418 rule_to_perm_id = Column('rule_to_perm_id', Integer(), ForeignKey('users_group_repo_to_perm.users_group_to_perm_id'), nullable=False, unique=None, default=None)
5440 rule_to_perm_id = Column('rule_to_perm_id', Integer(), ForeignKey('users_group_repo_to_perm.users_group_to_perm_id'), nullable=False, unique=None, default=None)
5419 user_group_repo_to_perm = relationship('UserGroupRepoToPerm')
5441 user_group_repo_to_perm = relationship('UserGroupRepoToPerm')
5420
5442
5421 rule_order = Column('rule_order', Integer(), nullable=False)
5443 rule_order = Column('rule_order', Integer(), nullable=False)
5422 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5444 _branch_pattern = Column('branch_pattern', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), default=u'*') # glob
5423 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5445 _branch_hash = Column('branch_hash', UnicodeText().with_variant(UnicodeText(2048), 'mysql'))
5424
5446
5425 def __unicode__(self):
5447 def __unicode__(self):
5426 return u'<UserBranchPermission(%s => %r)>' % (
5448 return u'<UserBranchPermission(%s => %r)>' % (
5427 self.user_group_repo_to_perm, self.branch_pattern)
5449 self.user_group_repo_to_perm, self.branch_pattern)
5428
5450
5429
5451
5430 class UserBookmark(Base, BaseModel):
5452 class UserBookmark(Base, BaseModel):
5431 __tablename__ = 'user_bookmarks'
5453 __tablename__ = 'user_bookmarks'
5432 __table_args__ = (
5454 __table_args__ = (
5433 UniqueConstraint('user_id', 'bookmark_repo_id'),
5455 UniqueConstraint('user_id', 'bookmark_repo_id'),
5434 UniqueConstraint('user_id', 'bookmark_repo_group_id'),
5456 UniqueConstraint('user_id', 'bookmark_repo_group_id'),
5435 UniqueConstraint('user_id', 'bookmark_position'),
5457 UniqueConstraint('user_id', 'bookmark_position'),
5436 base_table_args
5458 base_table_args
5437 )
5459 )
5438
5460
5439 user_bookmark_id = Column("user_bookmark_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
5461 user_bookmark_id = Column("user_bookmark_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
5440 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
5462 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
5441 position = Column("bookmark_position", Integer(), nullable=False)
5463 position = Column("bookmark_position", Integer(), nullable=False)
5442 title = Column("bookmark_title", String(255), nullable=True, unique=None, default=None)
5464 title = Column("bookmark_title", String(255), nullable=True, unique=None, default=None)
5443 redirect_url = Column("bookmark_redirect_url", String(10240), nullable=True, unique=None, default=None)
5465 redirect_url = Column("bookmark_redirect_url", String(10240), nullable=True, unique=None, default=None)
5444 created_on = Column("created_on", DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5466 created_on = Column("created_on", DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5445
5467
5446 bookmark_repo_id = Column("bookmark_repo_id", Integer(), ForeignKey("repositories.repo_id"), nullable=True, unique=None, default=None)
5468 bookmark_repo_id = Column("bookmark_repo_id", Integer(), ForeignKey("repositories.repo_id"), nullable=True, unique=None, default=None)
5447 bookmark_repo_group_id = Column("bookmark_repo_group_id", Integer(), ForeignKey("groups.group_id"), nullable=True, unique=None, default=None)
5469 bookmark_repo_group_id = Column("bookmark_repo_group_id", Integer(), ForeignKey("groups.group_id"), nullable=True, unique=None, default=None)
5448
5470
5449 user = relationship("User")
5471 user = relationship("User")
5450
5472
5451 repository = relationship("Repository")
5473 repository = relationship("Repository")
5452 repository_group = relationship("RepoGroup")
5474 repository_group = relationship("RepoGroup")
5453
5475
5454 @classmethod
5476 @classmethod
5455 def get_by_position_for_user(cls, position, user_id):
5477 def get_by_position_for_user(cls, position, user_id):
5456 return cls.query() \
5478 return cls.query() \
5457 .filter(UserBookmark.user_id == user_id) \
5479 .filter(UserBookmark.user_id == user_id) \
5458 .filter(UserBookmark.position == position).scalar()
5480 .filter(UserBookmark.position == position).scalar()
5459
5481
5460 @classmethod
5482 @classmethod
5461 def get_bookmarks_for_user(cls, user_id, cache=True):
5483 def get_bookmarks_for_user(cls, user_id, cache=True):
5462 bookmarks = cls.query() \
5484 bookmarks = cls.query() \
5463 .filter(UserBookmark.user_id == user_id) \
5485 .filter(UserBookmark.user_id == user_id) \
5464 .options(joinedload(UserBookmark.repository)) \
5486 .options(joinedload(UserBookmark.repository)) \
5465 .options(joinedload(UserBookmark.repository_group)) \
5487 .options(joinedload(UserBookmark.repository_group)) \
5466 .order_by(UserBookmark.position.asc())
5488 .order_by(UserBookmark.position.asc())
5467
5489
5468 if cache:
5490 if cache:
5469 bookmarks = bookmarks.options(
5491 bookmarks = bookmarks.options(
5470 FromCache("sql_cache_short", "get_user_{}_bookmarks".format(user_id))
5492 FromCache("sql_cache_short", "get_user_{}_bookmarks".format(user_id))
5471 )
5493 )
5472
5494
5473 return bookmarks.all()
5495 return bookmarks.all()
5474
5496
5475 def __unicode__(self):
5497 def __unicode__(self):
5476 return u'<UserBookmark(%s @ %r)>' % (self.position, self.redirect_url)
5498 return u'<UserBookmark(%s @ %r)>' % (self.position, self.redirect_url)
5477
5499
5478
5500
5479 class FileStore(Base, BaseModel):
5501 class FileStore(Base, BaseModel):
5480 __tablename__ = 'file_store'
5502 __tablename__ = 'file_store'
5481 __table_args__ = (
5503 __table_args__ = (
5482 base_table_args
5504 base_table_args
5483 )
5505 )
5484
5506
5485 file_store_id = Column('file_store_id', Integer(), primary_key=True)
5507 file_store_id = Column('file_store_id', Integer(), primary_key=True)
5486 file_uid = Column('file_uid', String(1024), nullable=False)
5508 file_uid = Column('file_uid', String(1024), nullable=False)
5487 file_display_name = Column('file_display_name', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), nullable=True)
5509 file_display_name = Column('file_display_name', UnicodeText().with_variant(UnicodeText(2048), 'mysql'), nullable=True)
5488 file_description = Column('file_description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'), nullable=True)
5510 file_description = Column('file_description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'), nullable=True)
5489 file_org_name = Column('file_org_name', UnicodeText().with_variant(UnicodeText(10240), 'mysql'), nullable=False)
5511 file_org_name = Column('file_org_name', UnicodeText().with_variant(UnicodeText(10240), 'mysql'), nullable=False)
5490
5512
5491 # sha256 hash
5513 # sha256 hash
5492 file_hash = Column('file_hash', String(512), nullable=False)
5514 file_hash = Column('file_hash', String(512), nullable=False)
5493 file_size = Column('file_size', BigInteger(), nullable=False)
5515 file_size = Column('file_size', BigInteger(), nullable=False)
5494
5516
5495 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5517 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
5496 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True)
5518 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True)
5497 accessed_count = Column('accessed_count', Integer(), default=0)
5519 accessed_count = Column('accessed_count', Integer(), default=0)
5498
5520
5499 enabled = Column('enabled', Boolean(), nullable=False, default=True)
5521 enabled = Column('enabled', Boolean(), nullable=False, default=True)
5500
5522
5501 # if repo/repo_group reference is set, check for permissions
5523 # if repo/repo_group reference is set, check for permissions
5502 check_acl = Column('check_acl', Boolean(), nullable=False, default=True)
5524 check_acl = Column('check_acl', Boolean(), nullable=False, default=True)
5503
5525
5504 # hidden defines an attachment that should be hidden from showing in artifact listing
5526 # hidden defines an attachment that should be hidden from showing in artifact listing
5505 hidden = Column('hidden', Boolean(), nullable=False, default=False)
5527 hidden = Column('hidden', Boolean(), nullable=False, default=False)
5506
5528
5507 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
5529 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
5508 upload_user = relationship('User', lazy='joined', primaryjoin='User.user_id==FileStore.user_id')
5530 upload_user = relationship('User', lazy='joined', primaryjoin='User.user_id==FileStore.user_id')
5509
5531
5510 file_metadata = relationship('FileStoreMetadata', lazy='joined')
5532 file_metadata = relationship('FileStoreMetadata', lazy='joined')
5511
5533
5512 # scope limited to user, which requester have access to
5534 # scope limited to user, which requester have access to
5513 scope_user_id = Column(
5535 scope_user_id = Column(
5514 'scope_user_id', Integer(), ForeignKey('users.user_id'),
5536 'scope_user_id', Integer(), ForeignKey('users.user_id'),
5515 nullable=True, unique=None, default=None)
5537 nullable=True, unique=None, default=None)
5516 user = relationship('User', lazy='joined', primaryjoin='User.user_id==FileStore.scope_user_id')
5538 user = relationship('User', lazy='joined', primaryjoin='User.user_id==FileStore.scope_user_id')
5517
5539
5518 # scope limited to user group, which requester have access to
5540 # scope limited to user group, which requester have access to
5519 scope_user_group_id = Column(
5541 scope_user_group_id = Column(
5520 'scope_user_group_id', Integer(), ForeignKey('users_groups.users_group_id'),
5542 'scope_user_group_id', Integer(), ForeignKey('users_groups.users_group_id'),
5521 nullable=True, unique=None, default=None)
5543 nullable=True, unique=None, default=None)
5522 user_group = relationship('UserGroup', lazy='joined')
5544 user_group = relationship('UserGroup', lazy='joined')
5523
5545
5524 # scope limited to repo, which requester have access to
5546 # scope limited to repo, which requester have access to
5525 scope_repo_id = Column(
5547 scope_repo_id = Column(
5526 'scope_repo_id', Integer(), ForeignKey('repositories.repo_id'),
5548 'scope_repo_id', Integer(), ForeignKey('repositories.repo_id'),
5527 nullable=True, unique=None, default=None)
5549 nullable=True, unique=None, default=None)
5528 repo = relationship('Repository', lazy='joined')
5550 repo = relationship('Repository', lazy='joined')
5529
5551
5530 # scope limited to repo group, which requester have access to
5552 # scope limited to repo group, which requester have access to
5531 scope_repo_group_id = Column(
5553 scope_repo_group_id = Column(
5532 'scope_repo_group_id', Integer(), ForeignKey('groups.group_id'),
5554 'scope_repo_group_id', Integer(), ForeignKey('groups.group_id'),
5533 nullable=True, unique=None, default=None)
5555 nullable=True, unique=None, default=None)
5534 repo_group = relationship('RepoGroup', lazy='joined')
5556 repo_group = relationship('RepoGroup', lazy='joined')
5535
5557
5536 @classmethod
5558 @classmethod
5537 def get_by_store_uid(cls, file_store_uid, safe=False):
5559 def get_by_store_uid(cls, file_store_uid, safe=False):
5538 if safe:
5560 if safe:
5539 return FileStore.query().filter(FileStore.file_uid == file_store_uid).first()
5561 return FileStore.query().filter(FileStore.file_uid == file_store_uid).first()
5540 else:
5562 else:
5541 return FileStore.query().filter(FileStore.file_uid == file_store_uid).scalar()
5563 return FileStore.query().filter(FileStore.file_uid == file_store_uid).scalar()
5542
5564
5543 @classmethod
5565 @classmethod
5544 def create(cls, file_uid, filename, file_hash, file_size, file_display_name='',
5566 def create(cls, file_uid, filename, file_hash, file_size, file_display_name='',
5545 file_description='', enabled=True, hidden=False, check_acl=True,
5567 file_description='', enabled=True, hidden=False, check_acl=True,
5546 user_id=None, scope_user_id=None, scope_repo_id=None, scope_repo_group_id=None):
5568 user_id=None, scope_user_id=None, scope_repo_id=None, scope_repo_group_id=None):
5547
5569
5548 store_entry = FileStore()
5570 store_entry = FileStore()
5549 store_entry.file_uid = file_uid
5571 store_entry.file_uid = file_uid
5550 store_entry.file_display_name = file_display_name
5572 store_entry.file_display_name = file_display_name
5551 store_entry.file_org_name = filename
5573 store_entry.file_org_name = filename
5552 store_entry.file_size = file_size
5574 store_entry.file_size = file_size
5553 store_entry.file_hash = file_hash
5575 store_entry.file_hash = file_hash
5554 store_entry.file_description = file_description
5576 store_entry.file_description = file_description
5555
5577
5556 store_entry.check_acl = check_acl
5578 store_entry.check_acl = check_acl
5557 store_entry.enabled = enabled
5579 store_entry.enabled = enabled
5558 store_entry.hidden = hidden
5580 store_entry.hidden = hidden
5559
5581
5560 store_entry.user_id = user_id
5582 store_entry.user_id = user_id
5561 store_entry.scope_user_id = scope_user_id
5583 store_entry.scope_user_id = scope_user_id
5562 store_entry.scope_repo_id = scope_repo_id
5584 store_entry.scope_repo_id = scope_repo_id
5563 store_entry.scope_repo_group_id = scope_repo_group_id
5585 store_entry.scope_repo_group_id = scope_repo_group_id
5564
5586
5565 return store_entry
5587 return store_entry
5566
5588
5567 @classmethod
5589 @classmethod
5568 def store_metadata(cls, file_store_id, args, commit=True):
5590 def store_metadata(cls, file_store_id, args, commit=True):
5569 file_store = FileStore.get(file_store_id)
5591 file_store = FileStore.get(file_store_id)
5570 if file_store is None:
5592 if file_store is None:
5571 return
5593 return
5572
5594
5573 for section, key, value, value_type in args:
5595 for section, key, value, value_type in args:
5574 has_key = FileStoreMetadata().query() \
5596 has_key = FileStoreMetadata().query() \
5575 .filter(FileStoreMetadata.file_store_id == file_store.file_store_id) \
5597 .filter(FileStoreMetadata.file_store_id == file_store.file_store_id) \
5576 .filter(FileStoreMetadata.file_store_meta_section == section) \
5598 .filter(FileStoreMetadata.file_store_meta_section == section) \
5577 .filter(FileStoreMetadata.file_store_meta_key == key) \
5599 .filter(FileStoreMetadata.file_store_meta_key == key) \
5578 .scalar()
5600 .scalar()
5579 if has_key:
5601 if has_key:
5580 msg = 'key `{}` already defined under section `{}` for this file.'\
5602 msg = 'key `{}` already defined under section `{}` for this file.'\
5581 .format(key, section)
5603 .format(key, section)
5582 raise ArtifactMetadataDuplicate(msg, err_section=section, err_key=key)
5604 raise ArtifactMetadataDuplicate(msg, err_section=section, err_key=key)
5583
5605
5584 # NOTE(marcink): raises ArtifactMetadataBadValueType
5606 # NOTE(marcink): raises ArtifactMetadataBadValueType
5585 FileStoreMetadata.valid_value_type(value_type)
5607 FileStoreMetadata.valid_value_type(value_type)
5586
5608
5587 meta_entry = FileStoreMetadata()
5609 meta_entry = FileStoreMetadata()
5588 meta_entry.file_store = file_store
5610 meta_entry.file_store = file_store
5589 meta_entry.file_store_meta_section = section
5611 meta_entry.file_store_meta_section = section
5590 meta_entry.file_store_meta_key = key
5612 meta_entry.file_store_meta_key = key
5591 meta_entry.file_store_meta_value_type = value_type
5613 meta_entry.file_store_meta_value_type = value_type
5592 meta_entry.file_store_meta_value = value
5614 meta_entry.file_store_meta_value = value
5593
5615
5594 Session().add(meta_entry)
5616 Session().add(meta_entry)
5595
5617
5596 try:
5618 try:
5597 if commit:
5619 if commit:
5598 Session().commit()
5620 Session().commit()
5599 except IntegrityError:
5621 except IntegrityError:
5600 Session().rollback()
5622 Session().rollback()
5601 raise ArtifactMetadataDuplicate('Duplicate section/key found for this file.')
5623 raise ArtifactMetadataDuplicate('Duplicate section/key found for this file.')
5602
5624
5603 @classmethod
5625 @classmethod
5604 def bump_access_counter(cls, file_uid, commit=True):
5626 def bump_access_counter(cls, file_uid, commit=True):
5605 FileStore().query()\
5627 FileStore().query()\
5606 .filter(FileStore.file_uid == file_uid)\
5628 .filter(FileStore.file_uid == file_uid)\
5607 .update({FileStore.accessed_count: (FileStore.accessed_count + 1),
5629 .update({FileStore.accessed_count: (FileStore.accessed_count + 1),
5608 FileStore.accessed_on: datetime.datetime.now()})
5630 FileStore.accessed_on: datetime.datetime.now()})
5609 if commit:
5631 if commit:
5610 Session().commit()
5632 Session().commit()
5611
5633
5612 def __json__(self):
5634 def __json__(self):
5613 data = {
5635 data = {
5614 'filename': self.file_display_name,
5636 'filename': self.file_display_name,
5615 'filename_org': self.file_org_name,
5637 'filename_org': self.file_org_name,
5616 'file_uid': self.file_uid,
5638 'file_uid': self.file_uid,
5617 'description': self.file_description,
5639 'description': self.file_description,
5618 'hidden': self.hidden,
5640 'hidden': self.hidden,
5619 'size': self.file_size,
5641 'size': self.file_size,
5620 'created_on': self.created_on,
5642 'created_on': self.created_on,
5621 'uploaded_by': self.upload_user.get_api_data(details='basic'),
5643 'uploaded_by': self.upload_user.get_api_data(details='basic'),
5622 'downloaded_times': self.accessed_count,
5644 'downloaded_times': self.accessed_count,
5623 'sha256': self.file_hash,
5645 'sha256': self.file_hash,
5624 'metadata': self.file_metadata,
5646 'metadata': self.file_metadata,
5625 }
5647 }
5626
5648
5627 return data
5649 return data
5628
5650
5629 def __repr__(self):
5651 def __repr__(self):
5630 return '<FileStore({})>'.format(self.file_store_id)
5652 return '<FileStore({})>'.format(self.file_store_id)
5631
5653
5632
5654
5633 class FileStoreMetadata(Base, BaseModel):
5655 class FileStoreMetadata(Base, BaseModel):
5634 __tablename__ = 'file_store_metadata'
5656 __tablename__ = 'file_store_metadata'
5635 __table_args__ = (
5657 __table_args__ = (
5636 UniqueConstraint('file_store_id', 'file_store_meta_section_hash', 'file_store_meta_key_hash'),
5658 UniqueConstraint('file_store_id', 'file_store_meta_section_hash', 'file_store_meta_key_hash'),
5637 Index('file_store_meta_section_idx', 'file_store_meta_section', mysql_length=255),
5659 Index('file_store_meta_section_idx', 'file_store_meta_section', mysql_length=255),
5638 Index('file_store_meta_key_idx', 'file_store_meta_key', mysql_length=255),
5660 Index('file_store_meta_key_idx', 'file_store_meta_key', mysql_length=255),
5639 base_table_args
5661 base_table_args
5640 )
5662 )
5641 SETTINGS_TYPES = {
5663 SETTINGS_TYPES = {
5642 'str': safe_str,
5664 'str': safe_str,
5643 'int': safe_int,
5665 'int': safe_int,
5644 'unicode': safe_unicode,
5666 'unicode': safe_unicode,
5645 'bool': str2bool,
5667 'bool': str2bool,
5646 'list': functools.partial(aslist, sep=',')
5668 'list': functools.partial(aslist, sep=',')
5647 }
5669 }
5648
5670
5649 file_store_meta_id = Column(
5671 file_store_meta_id = Column(
5650 "file_store_meta_id", Integer(), nullable=False, unique=True, default=None,
5672 "file_store_meta_id", Integer(), nullable=False, unique=True, default=None,
5651 primary_key=True)
5673 primary_key=True)
5652 _file_store_meta_section = Column(
5674 _file_store_meta_section = Column(
5653 "file_store_meta_section", UnicodeText().with_variant(UnicodeText(1024), 'mysql'),
5675 "file_store_meta_section", UnicodeText().with_variant(UnicodeText(1024), 'mysql'),
5654 nullable=True, unique=None, default=None)
5676 nullable=True, unique=None, default=None)
5655 _file_store_meta_section_hash = Column(
5677 _file_store_meta_section_hash = Column(
5656 "file_store_meta_section_hash", String(255),
5678 "file_store_meta_section_hash", String(255),
5657 nullable=True, unique=None, default=None)
5679 nullable=True, unique=None, default=None)
5658 _file_store_meta_key = Column(
5680 _file_store_meta_key = Column(
5659 "file_store_meta_key", UnicodeText().with_variant(UnicodeText(1024), 'mysql'),
5681 "file_store_meta_key", UnicodeText().with_variant(UnicodeText(1024), 'mysql'),
5660 nullable=True, unique=None, default=None)
5682 nullable=True, unique=None, default=None)
5661 _file_store_meta_key_hash = Column(
5683 _file_store_meta_key_hash = Column(
5662 "file_store_meta_key_hash", String(255), nullable=True, unique=None, default=None)
5684 "file_store_meta_key_hash", String(255), nullable=True, unique=None, default=None)
5663 _file_store_meta_value = Column(
5685 _file_store_meta_value = Column(
5664 "file_store_meta_value", UnicodeText().with_variant(UnicodeText(20480), 'mysql'),
5686 "file_store_meta_value", UnicodeText().with_variant(UnicodeText(20480), 'mysql'),
5665 nullable=True, unique=None, default=None)
5687 nullable=True, unique=None, default=None)
5666 _file_store_meta_value_type = Column(
5688 _file_store_meta_value_type = Column(
5667 "file_store_meta_value_type", String(255), nullable=True, unique=None,
5689 "file_store_meta_value_type", String(255), nullable=True, unique=None,
5668 default='unicode')
5690 default='unicode')
5669
5691
5670 file_store_id = Column(
5692 file_store_id = Column(
5671 'file_store_id', Integer(), ForeignKey('file_store.file_store_id'),
5693 'file_store_id', Integer(), ForeignKey('file_store.file_store_id'),
5672 nullable=True, unique=None, default=None)
5694 nullable=True, unique=None, default=None)
5673
5695
5674 file_store = relationship('FileStore', lazy='joined')
5696 file_store = relationship('FileStore', lazy='joined')
5675
5697
5676 @classmethod
5698 @classmethod
5677 def valid_value_type(cls, value):
5699 def valid_value_type(cls, value):
5678 if value.split('.')[0] not in cls.SETTINGS_TYPES:
5700 if value.split('.')[0] not in cls.SETTINGS_TYPES:
5679 raise ArtifactMetadataBadValueType(
5701 raise ArtifactMetadataBadValueType(
5680 'value_type must be one of %s got %s' % (cls.SETTINGS_TYPES.keys(), value))
5702 'value_type must be one of %s got %s' % (cls.SETTINGS_TYPES.keys(), value))
5681
5703
5682 @hybrid_property
5704 @hybrid_property
5683 def file_store_meta_section(self):
5705 def file_store_meta_section(self):
5684 return self._file_store_meta_section
5706 return self._file_store_meta_section
5685
5707
5686 @file_store_meta_section.setter
5708 @file_store_meta_section.setter
5687 def file_store_meta_section(self, value):
5709 def file_store_meta_section(self, value):
5688 self._file_store_meta_section = value
5710 self._file_store_meta_section = value
5689 self._file_store_meta_section_hash = _hash_key(value)
5711 self._file_store_meta_section_hash = _hash_key(value)
5690
5712
5691 @hybrid_property
5713 @hybrid_property
5692 def file_store_meta_key(self):
5714 def file_store_meta_key(self):
5693 return self._file_store_meta_key
5715 return self._file_store_meta_key
5694
5716
5695 @file_store_meta_key.setter
5717 @file_store_meta_key.setter
5696 def file_store_meta_key(self, value):
5718 def file_store_meta_key(self, value):
5697 self._file_store_meta_key = value
5719 self._file_store_meta_key = value
5698 self._file_store_meta_key_hash = _hash_key(value)
5720 self._file_store_meta_key_hash = _hash_key(value)
5699
5721
5700 @hybrid_property
5722 @hybrid_property
5701 def file_store_meta_value(self):
5723 def file_store_meta_value(self):
5702 val = self._file_store_meta_value
5724 val = self._file_store_meta_value
5703
5725
5704 if self._file_store_meta_value_type:
5726 if self._file_store_meta_value_type:
5705 # e.g unicode.encrypted == unicode
5727 # e.g unicode.encrypted == unicode
5706 _type = self._file_store_meta_value_type.split('.')[0]
5728 _type = self._file_store_meta_value_type.split('.')[0]
5707 # decode the encrypted value if it's encrypted field type
5729 # decode the encrypted value if it's encrypted field type
5708 if '.encrypted' in self._file_store_meta_value_type:
5730 if '.encrypted' in self._file_store_meta_value_type:
5709 cipher = EncryptedTextValue()
5731 cipher = EncryptedTextValue()
5710 val = safe_unicode(cipher.process_result_value(val, None))
5732 val = safe_unicode(cipher.process_result_value(val, None))
5711 # do final type conversion
5733 # do final type conversion
5712 converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode']
5734 converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode']
5713 val = converter(val)
5735 val = converter(val)
5714
5736
5715 return val
5737 return val
5716
5738
5717 @file_store_meta_value.setter
5739 @file_store_meta_value.setter
5718 def file_store_meta_value(self, val):
5740 def file_store_meta_value(self, val):
5719 val = safe_unicode(val)
5741 val = safe_unicode(val)
5720 # encode the encrypted value
5742 # encode the encrypted value
5721 if '.encrypted' in self.file_store_meta_value_type:
5743 if '.encrypted' in self.file_store_meta_value_type:
5722 cipher = EncryptedTextValue()
5744 cipher = EncryptedTextValue()
5723 val = safe_unicode(cipher.process_bind_param(val, None))
5745 val = safe_unicode(cipher.process_bind_param(val, None))
5724 self._file_store_meta_value = val
5746 self._file_store_meta_value = val
5725
5747
5726 @hybrid_property
5748 @hybrid_property
5727 def file_store_meta_value_type(self):
5749 def file_store_meta_value_type(self):
5728 return self._file_store_meta_value_type
5750 return self._file_store_meta_value_type
5729
5751
5730 @file_store_meta_value_type.setter
5752 @file_store_meta_value_type.setter
5731 def file_store_meta_value_type(self, val):
5753 def file_store_meta_value_type(self, val):
5732 # e.g unicode.encrypted
5754 # e.g unicode.encrypted
5733 self.valid_value_type(val)
5755 self.valid_value_type(val)
5734 self._file_store_meta_value_type = val
5756 self._file_store_meta_value_type = val
5735
5757
5736 def __json__(self):
5758 def __json__(self):
5737 data = {
5759 data = {
5738 'artifact': self.file_store.file_uid,
5760 'artifact': self.file_store.file_uid,
5739 'section': self.file_store_meta_section,
5761 'section': self.file_store_meta_section,
5740 'key': self.file_store_meta_key,
5762 'key': self.file_store_meta_key,
5741 'value': self.file_store_meta_value,
5763 'value': self.file_store_meta_value,
5742 }
5764 }
5743
5765
5744 return data
5766 return data
5745
5767
5746 def __repr__(self):
5768 def __repr__(self):
5747 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section,
5769 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.file_store_meta_section,
5748 self.file_store_meta_key, self.file_store_meta_value)
5770 self.file_store_meta_key, self.file_store_meta_value)
5749
5771
5750
5772
5751 class DbMigrateVersion(Base, BaseModel):
5773 class DbMigrateVersion(Base, BaseModel):
5752 __tablename__ = 'db_migrate_version'
5774 __tablename__ = 'db_migrate_version'
5753 __table_args__ = (
5775 __table_args__ = (
5754 base_table_args,
5776 base_table_args,
5755 )
5777 )
5756
5778
5757 repository_id = Column('repository_id', String(250), primary_key=True)
5779 repository_id = Column('repository_id', String(250), primary_key=True)
5758 repository_path = Column('repository_path', Text)
5780 repository_path = Column('repository_path', Text)
5759 version = Column('version', Integer)
5781 version = Column('version', Integer)
5760
5782
5761 @classmethod
5783 @classmethod
5762 def set_version(cls, version):
5784 def set_version(cls, version):
5763 """
5785 """
5764 Helper for forcing a different version, usually for debugging purposes via ishell.
5786 Helper for forcing a different version, usually for debugging purposes via ishell.
5765 """
5787 """
5766 ver = DbMigrateVersion.query().first()
5788 ver = DbMigrateVersion.query().first()
5767 ver.version = version
5789 ver.version = version
5768 Session().commit()
5790 Session().commit()
5769
5791
5770
5792
5771 class DbSession(Base, BaseModel):
5793 class DbSession(Base, BaseModel):
5772 __tablename__ = 'db_session'
5794 __tablename__ = 'db_session'
5773 __table_args__ = (
5795 __table_args__ = (
5774 base_table_args,
5796 base_table_args,
5775 )
5797 )
5776
5798
5777 def __repr__(self):
5799 def __repr__(self):
5778 return '<DB:DbSession({})>'.format(self.id)
5800 return '<DB:DbSession({})>'.format(self.id)
5779
5801
5780 id = Column('id', Integer())
5802 id = Column('id', Integer())
5781 namespace = Column('namespace', String(255), primary_key=True)
5803 namespace = Column('namespace', String(255), primary_key=True)
5782 accessed = Column('accessed', DateTime, nullable=False)
5804 accessed = Column('accessed', DateTime, nullable=False)
5783 created = Column('created', DateTime, nullable=False)
5805 created = Column('created', DateTime, nullable=False)
5784 data = Column('data', PickleType, nullable=False)
5806 data = Column('data', PickleType, nullable=False)
@@ -1,1195 +1,1146 b''
1 // # Copyright (C) 2010-2020 RhodeCode GmbH
1 // # Copyright (C) 2010-2020 RhodeCode GmbH
2 // #
2 // #
3 // # This program is free software: you can redistribute it and/or modify
3 // # This program is free software: you can redistribute it and/or modify
4 // # it under the terms of the GNU Affero General Public License, version 3
4 // # it under the terms of the GNU Affero General Public License, version 3
5 // # (only), as published by the Free Software Foundation.
5 // # (only), as published by the Free Software Foundation.
6 // #
6 // #
7 // # This program is distributed in the hope that it will be useful,
7 // # This program is distributed in the hope that it will be useful,
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // # GNU General Public License for more details.
10 // # GNU General Public License for more details.
11 // #
11 // #
12 // # You should have received a copy of the GNU Affero General Public License
12 // # You should have received a copy of the GNU Affero General Public License
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 // #
14 // #
15 // # This program is dual-licensed. If you wish to learn more about the
15 // # This program is dual-licensed. If you wish to learn more about the
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
18
18
19
19
20 var prButtonLockChecks = {
20 var prButtonLockChecks = {
21 'compare': false,
21 'compare': false,
22 'reviewers': false
22 'reviewers': false
23 };
23 };
24
24
25 /**
25 /**
26 * lock button until all checks and loads are made. E.g reviewer calculation
26 * lock button until all checks and loads are made. E.g reviewer calculation
27 * should prevent from submitting a PR
27 * should prevent from submitting a PR
28 * @param lockEnabled
28 * @param lockEnabled
29 * @param msg
29 * @param msg
30 * @param scope
30 * @param scope
31 */
31 */
32 var prButtonLock = function(lockEnabled, msg, scope) {
32 var prButtonLock = function(lockEnabled, msg, scope) {
33 scope = scope || 'all';
33 scope = scope || 'all';
34 if (scope == 'all'){
34 if (scope == 'all'){
35 prButtonLockChecks['compare'] = !lockEnabled;
35 prButtonLockChecks['compare'] = !lockEnabled;
36 prButtonLockChecks['reviewers'] = !lockEnabled;
36 prButtonLockChecks['reviewers'] = !lockEnabled;
37 } else if (scope == 'compare') {
37 } else if (scope == 'compare') {
38 prButtonLockChecks['compare'] = !lockEnabled;
38 prButtonLockChecks['compare'] = !lockEnabled;
39 } else if (scope == 'reviewers'){
39 } else if (scope == 'reviewers'){
40 prButtonLockChecks['reviewers'] = !lockEnabled;
40 prButtonLockChecks['reviewers'] = !lockEnabled;
41 }
41 }
42 var checksMeet = prButtonLockChecks.compare && prButtonLockChecks.reviewers;
42 var checksMeet = prButtonLockChecks.compare && prButtonLockChecks.reviewers;
43 if (lockEnabled) {
43 if (lockEnabled) {
44 $('#pr_submit').attr('disabled', 'disabled');
44 $('#pr_submit').attr('disabled', 'disabled');
45 }
45 }
46 else if (checksMeet) {
46 else if (checksMeet) {
47 $('#pr_submit').removeAttr('disabled');
47 $('#pr_submit').removeAttr('disabled');
48 }
48 }
49
49
50 if (msg) {
50 if (msg) {
51 $('#pr_open_message').html(msg);
51 $('#pr_open_message').html(msg);
52 }
52 }
53 };
53 };
54
54
55
55
56 /**
56 /**
57 Generate Title and Description for a PullRequest.
57 Generate Title and Description for a PullRequest.
58 In case of 1 commits, the title and description is that one commit
58 In case of 1 commits, the title and description is that one commit
59 in case of multiple commits, we iterate on them with max N number of commits,
59 in case of multiple commits, we iterate on them with max N number of commits,
60 and build description in a form
60 and build description in a form
61 - commitN
61 - commitN
62 - commitN+1
62 - commitN+1
63 ...
63 ...
64
64
65 Title is then constructed from branch names, or other references,
65 Title is then constructed from branch names, or other references,
66 replacing '-' and '_' into spaces
66 replacing '-' and '_' into spaces
67
67
68 * @param sourceRef
68 * @param sourceRef
69 * @param elements
69 * @param elements
70 * @param limit
70 * @param limit
71 * @returns {*[]}
71 * @returns {*[]}
72 */
72 */
73 var getTitleAndDescription = function(sourceRefType, sourceRef, elements, limit) {
73 var getTitleAndDescription = function(sourceRefType, sourceRef, elements, limit) {
74 var title = '';
74 var title = '';
75 var desc = '';
75 var desc = '';
76
76
77 $.each($(elements).get().reverse().slice(0, limit), function(idx, value) {
77 $.each($(elements).get().reverse().slice(0, limit), function(idx, value) {
78 var rawMessage = value['message'];
78 var rawMessage = value['message'];
79 desc += '- ' + rawMessage.split('\n')[0].replace(/\n+$/, "") + '\n';
79 desc += '- ' + rawMessage.split('\n')[0].replace(/\n+$/, "") + '\n';
80 });
80 });
81 // only 1 commit, use commit message as title
81 // only 1 commit, use commit message as title
82 if (elements.length === 1) {
82 if (elements.length === 1) {
83 var rawMessage = elements[0]['message'];
83 var rawMessage = elements[0]['message'];
84 title = rawMessage.split('\n')[0];
84 title = rawMessage.split('\n')[0];
85 }
85 }
86 else {
86 else {
87 // use reference name
87 // use reference name
88 var normalizedRef = sourceRef.replace(/-/g, ' ').replace(/_/g, ' ').capitalizeFirstLetter()
88 var normalizedRef = sourceRef.replace(/-/g, ' ').replace(/_/g, ' ').capitalizeFirstLetter()
89 var refType = sourceRefType;
89 var refType = sourceRefType;
90 title = 'Changes from {0}: {1}'.format(refType, normalizedRef);
90 title = 'Changes from {0}: {1}'.format(refType, normalizedRef);
91 }
91 }
92
92
93 return [title, desc]
93 return [title, desc]
94 };
94 };
95
95
96
96
97 window.ReviewersController = function () {
97 window.ReviewersController = function () {
98 var self = this;
98 var self = this;
99 this.$loadingIndicator = $('.calculate-reviewers');
99 this.$loadingIndicator = $('.calculate-reviewers');
100 this.$reviewRulesContainer = $('#review_rules');
100 this.$reviewRulesContainer = $('#review_rules');
101 this.$rulesList = this.$reviewRulesContainer.find('.pr-reviewer-rules');
101 this.$rulesList = this.$reviewRulesContainer.find('.pr-reviewer-rules');
102 this.$userRule = $('.pr-user-rule-container');
102 this.$userRule = $('.pr-user-rule-container');
103 this.$reviewMembers = $('#review_members');
103 this.$reviewMembers = $('#review_members');
104 this.$observerMembers = $('#observer_members');
104 this.$observerMembers = $('#observer_members');
105
105
106 this.currentRequest = null;
106 this.currentRequest = null;
107 this.diffData = null;
107 this.diffData = null;
108 this.enabledRules = [];
108 this.enabledRules = [];
109 // sync with db.py entries
109 // sync with db.py entries
110 this.ROLE_REVIEWER = 'reviewer';
110 this.ROLE_REVIEWER = 'reviewer';
111 this.ROLE_OBSERVER = 'observer'
111 this.ROLE_OBSERVER = 'observer'
112
112
113 //dummy handler, we might register our own later
113 //dummy handler, we might register our own later
114 this.diffDataHandler = function (data) {};
114 this.diffDataHandler = function (data) {};
115
115
116 this.defaultForbidUsers = function () {
116 this.defaultForbidUsers = function () {
117 return [
117 return [
118 {
118 {
119 'username': 'default',
119 'username': 'default',
120 'user_id': templateContext.default_user.user_id
120 'user_id': templateContext.default_user.user_id
121 }
121 }
122 ];
122 ];
123 };
123 };
124
124
125 // init default forbidden users
125 // init default forbidden users
126 this.forbidUsers = this.defaultForbidUsers();
126 this.forbidUsers = this.defaultForbidUsers();
127
127
128 this.hideReviewRules = function () {
128 this.hideReviewRules = function () {
129 self.$reviewRulesContainer.hide();
129 self.$reviewRulesContainer.hide();
130 $(self.$userRule.selector).hide();
130 $(self.$userRule.selector).hide();
131 };
131 };
132
132
133 this.showReviewRules = function () {
133 this.showReviewRules = function () {
134 self.$reviewRulesContainer.show();
134 self.$reviewRulesContainer.show();
135 $(self.$userRule.selector).show();
135 $(self.$userRule.selector).show();
136 };
136 };
137
137
138 this.addRule = function (ruleText) {
138 this.addRule = function (ruleText) {
139 self.showReviewRules();
139 self.showReviewRules();
140 self.enabledRules.push(ruleText);
140 self.enabledRules.push(ruleText);
141 return '<div>- {0}</div>'.format(ruleText)
141 return '<div>- {0}</div>'.format(ruleText)
142 };
142 };
143
143
144 this.increaseCounter = function(role) {
144 this.increaseCounter = function(role) {
145 if (role === self.ROLE_REVIEWER) {
145 if (role === self.ROLE_REVIEWER) {
146 var $elem = $('#reviewers-cnt')
146 var $elem = $('#reviewers-cnt')
147 var cnt = parseInt($elem.data('count') || 0)
147 var cnt = parseInt($elem.data('count') || 0)
148 cnt +=1
148 cnt +=1
149 $elem.html(cnt);
149 $elem.html(cnt);
150 $elem.data('count', cnt);
150 $elem.data('count', cnt);
151 }
151 }
152 else if (role === self.ROLE_OBSERVER) {
152 else if (role === self.ROLE_OBSERVER) {
153 var $elem = $('#observers-cnt');
153 var $elem = $('#observers-cnt');
154 var cnt = parseInt($elem.data('count') || 0)
154 var cnt = parseInt($elem.data('count') || 0)
155 cnt +=1
155 cnt +=1
156 $elem.html(cnt);
156 $elem.html(cnt);
157 $elem.data('count', cnt);
157 $elem.data('count', cnt);
158 }
158 }
159 }
159 }
160
160
161 this.resetCounter = function () {
161 this.resetCounter = function () {
162 var $elem = $('#reviewers-cnt');
162 var $elem = $('#reviewers-cnt');
163
163
164 $elem.data('count', 0);
164 $elem.data('count', 0);
165 $elem.html(0);
165 $elem.html(0);
166
166
167 var $elem = $('#observers-cnt');
167 var $elem = $('#observers-cnt');
168
168
169 $elem.data('count', 0);
169 $elem.data('count', 0);
170 $elem.html(0);
170 $elem.html(0);
171 }
171 }
172
172
173 this.loadReviewRules = function (data) {
173 this.loadReviewRules = function (data) {
174 self.diffData = data;
174 self.diffData = data;
175
175
176 // reset forbidden Users
176 // reset forbidden Users
177 this.forbidUsers = self.defaultForbidUsers();
177 this.forbidUsers = self.defaultForbidUsers();
178
178
179 // reset state of review rules
179 // reset state of review rules
180 self.$rulesList.html('');
180 self.$rulesList.html('');
181
181
182 if (!data || data.rules === undefined || $.isEmptyObject(data.rules)) {
182 if (!data || data.rules === undefined || $.isEmptyObject(data.rules)) {
183 // default rule, case for older repo that don't have any rules stored
183 // default rule, case for older repo that don't have any rules stored
184 self.$rulesList.append(
184 self.$rulesList.append(
185 self.addRule(
185 self.addRule(_gettext('All reviewers must vote.'))
186 _gettext('All reviewers must vote.'))
187 );
186 );
188 return self.forbidUsers
187 return self.forbidUsers
189 }
188 }
190
189
191 if (data.rules.voting !== undefined) {
190 if (data.rules.forbid_adding_reviewers) {
192 if (data.rules.voting < 0) {
191 $('#add_reviewer_input').remove();
193 self.$rulesList.append(
194 self.addRule(
195 _gettext('All individual reviewers must vote.'))
196 )
197 } else if (data.rules.voting === 1) {
198 self.$rulesList.append(
199 self.addRule(
200 _gettext('At least {0} reviewer must vote.').format(data.rules.voting))
201 )
202
203 } else {
204 self.$rulesList.append(
205 self.addRule(
206 _gettext('At least {0} reviewers must vote.').format(data.rules.voting))
207 )
208 }
209 }
192 }
210
193
211 if (data.rules.voting_groups !== undefined) {
194 if (data.rules_data !== undefined && data.rules_data.forbidden_users !== undefined) {
212 $.each(data.rules.voting_groups, function (index, rule_data) {
195 $.each(data.rules_data.forbidden_users, function(idx, val){
213 self.$rulesList.append(
196 self.forbidUsers.push(val)
214 self.addRule(rule_data.text)
197 })
215 )
216 });
217 }
218
219 if (data.rules.use_code_authors_for_review) {
220 self.$rulesList.append(
221 self.addRule(
222 _gettext('Reviewers picked from source code changes.'))
223 )
224 }
198 }
225
199
226 if (data.rules.forbid_adding_reviewers) {
200 if (data.rules_humanized !== undefined && data.rules_humanized.length > 0) {
227 $('#add_reviewer_input').remove();
201 $.each(data.rules_humanized, function(idx, val) {
228 self.$rulesList.append(
202 self.$rulesList.append(
229 self.addRule(
203 self.addRule(val)
230 _gettext('Adding new reviewers is forbidden.'))
231 )
232 }
233
234 if (data.rules.forbid_author_to_review) {
235 self.forbidUsers.push(data.rules_data.pr_author);
236 self.$rulesList.append(
237 self.addRule(
238 _gettext('Author is not allowed to be a reviewer.'))
239 )
204 )
240 }
205 })
241
206 } else {
242 if (data.rules.forbid_commit_author_to_review) {
207 // we don't have any rules set, so we inform users about it
243
244 if (data.rules_data.forbidden_users) {
245 $.each(data.rules_data.forbidden_users, function (index, member_data) {
246 self.forbidUsers.push(member_data)
247 });
248 }
249
250 self.$rulesList.append(
208 self.$rulesList.append(
251 self.addRule(
209 self.addRule(_gettext('No additional review rules set.'))
252 _gettext('Commit Authors are not allowed to be a reviewer.'))
253 )
210 )
254 }
211 }
255
212
256 // we don't have any rules set, so we inform users about it
257 if (self.enabledRules.length === 0) {
258 self.addRule(
259 _gettext('No review rules set.'))
260 }
261
262 return self.forbidUsers
213 return self.forbidUsers
263 };
214 };
264
215
265 this.emptyTables = function () {
216 this.emptyTables = function () {
266 self.emptyReviewersTable();
217 self.emptyReviewersTable();
267 self.emptyObserversTable();
218 self.emptyObserversTable();
268
219
269 // Also reset counters.
220 // Also reset counters.
270 self.resetCounter();
221 self.resetCounter();
271 }
222 }
272
223
273 this.emptyReviewersTable = function (withText) {
224 this.emptyReviewersTable = function (withText) {
274 self.$reviewMembers.empty();
225 self.$reviewMembers.empty();
275 if (withText !== undefined) {
226 if (withText !== undefined) {
276 self.$reviewMembers.html(withText)
227 self.$reviewMembers.html(withText)
277 }
228 }
278 };
229 };
279
230
280 this.emptyObserversTable = function (withText) {
231 this.emptyObserversTable = function (withText) {
281 self.$observerMembers.empty();
232 self.$observerMembers.empty();
282 if (withText !== undefined) {
233 if (withText !== undefined) {
283 self.$observerMembers.html(withText)
234 self.$observerMembers.html(withText)
284 }
235 }
285 }
236 }
286
237
287 this.loadDefaultReviewers = function (sourceRepo, sourceRef, targetRepo, targetRef) {
238 this.loadDefaultReviewers = function (sourceRepo, sourceRef, targetRepo, targetRef) {
288
239
289 if (self.currentRequest) {
240 if (self.currentRequest) {
290 // make sure we cleanup old running requests before triggering this again
241 // make sure we cleanup old running requests before triggering this again
291 self.currentRequest.abort();
242 self.currentRequest.abort();
292 }
243 }
293
244
294 self.$loadingIndicator.show();
245 self.$loadingIndicator.show();
295
246
296 // reset reviewer/observe members
247 // reset reviewer/observe members
297 self.emptyTables();
248 self.emptyTables();
298
249
299 prButtonLock(true, null, 'reviewers');
250 prButtonLock(true, null, 'reviewers');
300 $('#user').hide(); // hide user autocomplete before load
251 $('#user').hide(); // hide user autocomplete before load
301 $('#observer').hide(); //hide observer autocomplete before load
252 $('#observer').hide(); //hide observer autocomplete before load
302
253
303 // lock PR button, so we cannot send PR before it's calculated
254 // lock PR button, so we cannot send PR before it's calculated
304 prButtonLock(true, _gettext('Loading diff ...'), 'compare');
255 prButtonLock(true, _gettext('Loading diff ...'), 'compare');
305
256
306 if (sourceRef.length !== 3 || targetRef.length !== 3) {
257 if (sourceRef.length !== 3 || targetRef.length !== 3) {
307 // don't load defaults in case we're missing some refs...
258 // don't load defaults in case we're missing some refs...
308 self.$loadingIndicator.hide();
259 self.$loadingIndicator.hide();
309 return
260 return
310 }
261 }
311
262
312 var url = pyroutes.url('repo_default_reviewers_data',
263 var url = pyroutes.url('repo_default_reviewers_data',
313 {
264 {
314 'repo_name': templateContext.repo_name,
265 'repo_name': templateContext.repo_name,
315 'source_repo': sourceRepo,
266 'source_repo': sourceRepo,
316 'source_ref_type': sourceRef[0],
267 'source_ref_type': sourceRef[0],
317 'source_ref_name': sourceRef[1],
268 'source_ref_name': sourceRef[1],
318 'source_ref': sourceRef[2],
269 'source_ref': sourceRef[2],
319 'target_repo': targetRepo,
270 'target_repo': targetRepo,
320 'target_ref': targetRef[2],
271 'target_ref': targetRef[2],
321 'target_ref_type': sourceRef[0],
272 'target_ref_type': sourceRef[0],
322 'target_ref_name': sourceRef[1]
273 'target_ref_name': sourceRef[1]
323 });
274 });
324
275
325 self.currentRequest = $.ajax({
276 self.currentRequest = $.ajax({
326 url: url,
277 url: url,
327 headers: {'X-PARTIAL-XHR': true},
278 headers: {'X-PARTIAL-XHR': true},
328 type: 'GET',
279 type: 'GET',
329 success: function (data) {
280 success: function (data) {
330
281
331 self.currentRequest = null;
282 self.currentRequest = null;
332
283
333 // review rules
284 // review rules
334 self.loadReviewRules(data);
285 self.loadReviewRules(data);
335 var diffHandled = self.handleDiffData(data["diff_info"]);
286 var diffHandled = self.handleDiffData(data["diff_info"]);
336 if (diffHandled === false) {
287 if (diffHandled === false) {
337 return
288 return
338 }
289 }
339
290
340 for (var i = 0; i < data.reviewers.length; i++) {
291 for (var i = 0; i < data.reviewers.length; i++) {
341 var reviewer = data.reviewers[i];
292 var reviewer = data.reviewers[i];
342 // load reviewer rules from the repo data
293 // load reviewer rules from the repo data
343 self.addMember(reviewer, reviewer.reasons, reviewer.mandatory, reviewer.role);
294 self.addMember(reviewer, reviewer.reasons, reviewer.mandatory, reviewer.role);
344 }
295 }
345
296
346
297
347 self.$loadingIndicator.hide();
298 self.$loadingIndicator.hide();
348 prButtonLock(false, null, 'reviewers');
299 prButtonLock(false, null, 'reviewers');
349
300
350 $('#user').show(); // show user autocomplete before load
301 $('#user').show(); // show user autocomplete before load
351 $('#observer').show(); // show observer autocomplete before load
302 $('#observer').show(); // show observer autocomplete before load
352
303
353 var commitElements = data["diff_info"]['commits'];
304 var commitElements = data["diff_info"]['commits'];
354
305
355 if (commitElements.length === 0) {
306 if (commitElements.length === 0) {
356 var noCommitsMsg = '<span class="alert-text-warning">{0}</span>'.format(
307 var noCommitsMsg = '<span class="alert-text-warning">{0}</span>'.format(
357 _gettext('There are no commits to merge.'));
308 _gettext('There are no commits to merge.'));
358 prButtonLock(true, noCommitsMsg, 'all');
309 prButtonLock(true, noCommitsMsg, 'all');
359
310
360 } else {
311 } else {
361 // un-lock PR button, so we cannot send PR before it's calculated
312 // un-lock PR button, so we cannot send PR before it's calculated
362 prButtonLock(false, null, 'compare');
313 prButtonLock(false, null, 'compare');
363 }
314 }
364
315
365 },
316 },
366 error: function (jqXHR, textStatus, errorThrown) {
317 error: function (jqXHR, textStatus, errorThrown) {
367 var prefix = "Loading diff and reviewers/observers failed\n"
318 var prefix = "Loading diff and reviewers/observers failed\n"
368 var message = formatErrorMessage(jqXHR, textStatus, errorThrown, prefix);
319 var message = formatErrorMessage(jqXHR, textStatus, errorThrown, prefix);
369 ajaxErrorSwal(message);
320 ajaxErrorSwal(message);
370 }
321 }
371 });
322 });
372
323
373 };
324 };
374
325
375 // check those, refactor
326 // check those, refactor
376 this.removeMember = function (reviewer_id, mark_delete) {
327 this.removeMember = function (reviewer_id, mark_delete) {
377 var reviewer = $('#reviewer_{0}'.format(reviewer_id));
328 var reviewer = $('#reviewer_{0}'.format(reviewer_id));
378
329
379 if (typeof (mark_delete) === undefined) {
330 if (typeof (mark_delete) === undefined) {
380 mark_delete = false;
331 mark_delete = false;
381 }
332 }
382
333
383 if (mark_delete === true) {
334 if (mark_delete === true) {
384 if (reviewer) {
335 if (reviewer) {
385 // now delete the input
336 // now delete the input
386 $('#reviewer_{0} input'.format(reviewer_id)).remove();
337 $('#reviewer_{0} input'.format(reviewer_id)).remove();
387 $('#reviewer_{0}_rules input'.format(reviewer_id)).remove();
338 $('#reviewer_{0}_rules input'.format(reviewer_id)).remove();
388 // mark as to-delete
339 // mark as to-delete
389 var obj = $('#reviewer_{0}_name'.format(reviewer_id));
340 var obj = $('#reviewer_{0}_name'.format(reviewer_id));
390 obj.addClass('to-delete');
341 obj.addClass('to-delete');
391 obj.css({"text-decoration": "line-through", "opacity": 0.5});
342 obj.css({"text-decoration": "line-through", "opacity": 0.5});
392 }
343 }
393 } else {
344 } else {
394 $('#reviewer_{0}'.format(reviewer_id)).remove();
345 $('#reviewer_{0}'.format(reviewer_id)).remove();
395 }
346 }
396 };
347 };
397
348
398 this.addMember = function (reviewer_obj, reasons, mandatory, role) {
349 this.addMember = function (reviewer_obj, reasons, mandatory, role) {
399
350
400 var id = reviewer_obj.user_id;
351 var id = reviewer_obj.user_id;
401 var username = reviewer_obj.username;
352 var username = reviewer_obj.username;
402
353
403 reasons = reasons || [];
354 reasons = reasons || [];
404 mandatory = mandatory || false;
355 mandatory = mandatory || false;
405 role = role || self.ROLE_REVIEWER
356 role = role || self.ROLE_REVIEWER
406
357
407 // register current set IDS to check if we don't have this ID already in
358 // register current set IDS to check if we don't have this ID already in
408 // and prevent duplicates
359 // and prevent duplicates
409 var currentIds = [];
360 var currentIds = [];
410
361
411 $.each($('.reviewer_entry'), function (index, value) {
362 $.each($('.reviewer_entry'), function (index, value) {
412 currentIds.push($(value).data('reviewerUserId'))
363 currentIds.push($(value).data('reviewerUserId'))
413 })
364 })
414
365
415 var userAllowedReview = function (userId) {
366 var userAllowedReview = function (userId) {
416 var allowed = true;
367 var allowed = true;
417 $.each(self.forbidUsers, function (index, member_data) {
368 $.each(self.forbidUsers, function (index, member_data) {
418 if (parseInt(userId) === member_data['user_id']) {
369 if (parseInt(userId) === member_data['user_id']) {
419 allowed = false;
370 allowed = false;
420 return false // breaks the loop
371 return false // breaks the loop
421 }
372 }
422 });
373 });
423 return allowed
374 return allowed
424 };
375 };
425
376
426 var userAllowed = userAllowedReview(id);
377 var userAllowed = userAllowedReview(id);
427
378
428 if (!userAllowed) {
379 if (!userAllowed) {
429 alert(_gettext('User `{0}` not allowed to be a reviewer').format(username));
380 alert(_gettext('User `{0}` not allowed to be a reviewer').format(username));
430 } else {
381 } else {
431 // only add if it's not there
382 // only add if it's not there
432 var alreadyReviewer = currentIds.indexOf(id) != -1;
383 var alreadyReviewer = currentIds.indexOf(id) != -1;
433
384
434 if (alreadyReviewer) {
385 if (alreadyReviewer) {
435 alert(_gettext('User `{0}` already in reviewers/observers').format(username));
386 alert(_gettext('User `{0}` already in reviewers/observers').format(username));
436 } else {
387 } else {
437
388
438 var reviewerEntry = renderTemplate('reviewMemberEntry', {
389 var reviewerEntry = renderTemplate('reviewMemberEntry', {
439 'member': reviewer_obj,
390 'member': reviewer_obj,
440 'mandatory': mandatory,
391 'mandatory': mandatory,
441 'role': role,
392 'role': role,
442 'reasons': reasons,
393 'reasons': reasons,
443 'allowed_to_update': true,
394 'allowed_to_update': true,
444 'review_status': 'not_reviewed',
395 'review_status': 'not_reviewed',
445 'review_status_label': _gettext('Not Reviewed'),
396 'review_status_label': _gettext('Not Reviewed'),
446 'user_group': reviewer_obj.user_group,
397 'user_group': reviewer_obj.user_group,
447 'create': true,
398 'create': true,
448 'rule_show': true,
399 'rule_show': true,
449 })
400 })
450
401
451 if (role === self.ROLE_REVIEWER) {
402 if (role === self.ROLE_REVIEWER) {
452 $(self.$reviewMembers.selector).append(reviewerEntry);
403 $(self.$reviewMembers.selector).append(reviewerEntry);
453 self.increaseCounter(self.ROLE_REVIEWER);
404 self.increaseCounter(self.ROLE_REVIEWER);
454 $('#reviewer-empty-msg').remove()
405 $('#reviewer-empty-msg').remove()
455 }
406 }
456 else if (role === self.ROLE_OBSERVER) {
407 else if (role === self.ROLE_OBSERVER) {
457 $(self.$observerMembers.selector).append(reviewerEntry);
408 $(self.$observerMembers.selector).append(reviewerEntry);
458 self.increaseCounter(self.ROLE_OBSERVER);
409 self.increaseCounter(self.ROLE_OBSERVER);
459 $('#observer-empty-msg').remove();
410 $('#observer-empty-msg').remove();
460 }
411 }
461
412
462 tooltipActivate();
413 tooltipActivate();
463 }
414 }
464 }
415 }
465
416
466 };
417 };
467
418
468 this.updateReviewers = function (repo_name, pull_request_id, role) {
419 this.updateReviewers = function (repo_name, pull_request_id, role) {
469 if (role === 'reviewer') {
420 if (role === 'reviewer') {
470 var postData = $('#reviewers input').serialize();
421 var postData = $('#reviewers input').serialize();
471 _updatePullRequest(repo_name, pull_request_id, postData);
422 _updatePullRequest(repo_name, pull_request_id, postData);
472 } else if (role === 'observer') {
423 } else if (role === 'observer') {
473 var postData = $('#observers input').serialize();
424 var postData = $('#observers input').serialize();
474 _updatePullRequest(repo_name, pull_request_id, postData);
425 _updatePullRequest(repo_name, pull_request_id, postData);
475 }
426 }
476 };
427 };
477
428
478 this.handleDiffData = function (data) {
429 this.handleDiffData = function (data) {
479 return self.diffDataHandler(data)
430 return self.diffDataHandler(data)
480 }
431 }
481 };
432 };
482
433
483
434
484 var _updatePullRequest = function(repo_name, pull_request_id, postData) {
435 var _updatePullRequest = function(repo_name, pull_request_id, postData) {
485 var url = pyroutes.url(
436 var url = pyroutes.url(
486 'pullrequest_update',
437 'pullrequest_update',
487 {"repo_name": repo_name, "pull_request_id": pull_request_id});
438 {"repo_name": repo_name, "pull_request_id": pull_request_id});
488 if (typeof postData === 'string' ) {
439 if (typeof postData === 'string' ) {
489 postData += '&csrf_token=' + CSRF_TOKEN;
440 postData += '&csrf_token=' + CSRF_TOKEN;
490 } else {
441 } else {
491 postData.csrf_token = CSRF_TOKEN;
442 postData.csrf_token = CSRF_TOKEN;
492 }
443 }
493
444
494 var success = function(o) {
445 var success = function(o) {
495 var redirectUrl = o['redirect_url'];
446 var redirectUrl = o['redirect_url'];
496 if (redirectUrl !== undefined && redirectUrl !== null && redirectUrl !== '') {
447 if (redirectUrl !== undefined && redirectUrl !== null && redirectUrl !== '') {
497 window.location = redirectUrl;
448 window.location = redirectUrl;
498 } else {
449 } else {
499 window.location.reload();
450 window.location.reload();
500 }
451 }
501 };
452 };
502
453
503 ajaxPOST(url, postData, success);
454 ajaxPOST(url, postData, success);
504 };
455 };
505
456
506 /**
457 /**
507 * PULL REQUEST update commits
458 * PULL REQUEST update commits
508 */
459 */
509 var updateCommits = function(repo_name, pull_request_id, force) {
460 var updateCommits = function(repo_name, pull_request_id, force) {
510 var postData = {
461 var postData = {
511 'update_commits': true
462 'update_commits': true
512 };
463 };
513 if (force !== undefined && force === true) {
464 if (force !== undefined && force === true) {
514 postData['force_refresh'] = true
465 postData['force_refresh'] = true
515 }
466 }
516 _updatePullRequest(repo_name, pull_request_id, postData);
467 _updatePullRequest(repo_name, pull_request_id, postData);
517 };
468 };
518
469
519
470
520 /**
471 /**
521 * PULL REQUEST edit info
472 * PULL REQUEST edit info
522 */
473 */
523 var editPullRequest = function(repo_name, pull_request_id, title, description, renderer) {
474 var editPullRequest = function(repo_name, pull_request_id, title, description, renderer) {
524 var url = pyroutes.url(
475 var url = pyroutes.url(
525 'pullrequest_update',
476 'pullrequest_update',
526 {"repo_name": repo_name, "pull_request_id": pull_request_id});
477 {"repo_name": repo_name, "pull_request_id": pull_request_id});
527
478
528 var postData = {
479 var postData = {
529 'title': title,
480 'title': title,
530 'description': description,
481 'description': description,
531 'description_renderer': renderer,
482 'description_renderer': renderer,
532 'edit_pull_request': true,
483 'edit_pull_request': true,
533 'csrf_token': CSRF_TOKEN
484 'csrf_token': CSRF_TOKEN
534 };
485 };
535 var success = function(o) {
486 var success = function(o) {
536 window.location.reload();
487 window.location.reload();
537 };
488 };
538 ajaxPOST(url, postData, success);
489 ajaxPOST(url, postData, success);
539 };
490 };
540
491
541
492
542 /**
493 /**
543 * autocomplete handler for reviewers/observers
494 * autocomplete handler for reviewers/observers
544 */
495 */
545 var autoCompleteHandler = function (inputId, controller, role) {
496 var autoCompleteHandler = function (inputId, controller, role) {
546
497
547 return function (element, data) {
498 return function (element, data) {
548 var mandatory = false;
499 var mandatory = false;
549 var reasons = [_gettext('added manually by "{0}"').format(
500 var reasons = [_gettext('added manually by "{0}"').format(
550 templateContext.rhodecode_user.username)];
501 templateContext.rhodecode_user.username)];
551
502
552 // add whole user groups
503 // add whole user groups
553 if (data.value_type == 'user_group') {
504 if (data.value_type == 'user_group') {
554 reasons.push(_gettext('member of "{0}"').format(data.value_display));
505 reasons.push(_gettext('member of "{0}"').format(data.value_display));
555
506
556 $.each(data.members, function (index, member_data) {
507 $.each(data.members, function (index, member_data) {
557 var reviewer = member_data;
508 var reviewer = member_data;
558 reviewer['user_id'] = member_data['id'];
509 reviewer['user_id'] = member_data['id'];
559 reviewer['gravatar_link'] = member_data['icon_link'];
510 reviewer['gravatar_link'] = member_data['icon_link'];
560 reviewer['user_link'] = member_data['profile_link'];
511 reviewer['user_link'] = member_data['profile_link'];
561 reviewer['rules'] = [];
512 reviewer['rules'] = [];
562 controller.addMember(reviewer, reasons, mandatory, role);
513 controller.addMember(reviewer, reasons, mandatory, role);
563 })
514 })
564 }
515 }
565 // add single user
516 // add single user
566 else {
517 else {
567 var reviewer = data;
518 var reviewer = data;
568 reviewer['user_id'] = data['id'];
519 reviewer['user_id'] = data['id'];
569 reviewer['gravatar_link'] = data['icon_link'];
520 reviewer['gravatar_link'] = data['icon_link'];
570 reviewer['user_link'] = data['profile_link'];
521 reviewer['user_link'] = data['profile_link'];
571 reviewer['rules'] = [];
522 reviewer['rules'] = [];
572 controller.addMember(reviewer, reasons, mandatory, role);
523 controller.addMember(reviewer, reasons, mandatory, role);
573 }
524 }
574
525
575 $(inputId).val('');
526 $(inputId).val('');
576 }
527 }
577 }
528 }
578
529
579 /**
530 /**
580 * Reviewer autocomplete
531 * Reviewer autocomplete
581 */
532 */
582 var ReviewerAutoComplete = function (inputId, controller) {
533 var ReviewerAutoComplete = function (inputId, controller) {
583 var self = this;
534 var self = this;
584 self.controller = controller;
535 self.controller = controller;
585 self.inputId = inputId;
536 self.inputId = inputId;
586 var handler = autoCompleteHandler(inputId, controller, controller.ROLE_REVIEWER);
537 var handler = autoCompleteHandler(inputId, controller, controller.ROLE_REVIEWER);
587
538
588 $(inputId).autocomplete({
539 $(inputId).autocomplete({
589 serviceUrl: pyroutes.url('user_autocomplete_data'),
540 serviceUrl: pyroutes.url('user_autocomplete_data'),
590 minChars: 2,
541 minChars: 2,
591 maxHeight: 400,
542 maxHeight: 400,
592 deferRequestBy: 300, //miliseconds
543 deferRequestBy: 300, //miliseconds
593 showNoSuggestionNotice: true,
544 showNoSuggestionNotice: true,
594 tabDisabled: true,
545 tabDisabled: true,
595 autoSelectFirst: true,
546 autoSelectFirst: true,
596 params: {
547 params: {
597 user_id: templateContext.rhodecode_user.user_id,
548 user_id: templateContext.rhodecode_user.user_id,
598 user_groups: true,
549 user_groups: true,
599 user_groups_expand: true,
550 user_groups_expand: true,
600 skip_default_user: true
551 skip_default_user: true
601 },
552 },
602 formatResult: autocompleteFormatResult,
553 formatResult: autocompleteFormatResult,
603 lookupFilter: autocompleteFilterResult,
554 lookupFilter: autocompleteFilterResult,
604 onSelect: handler
555 onSelect: handler
605 });
556 });
606 };
557 };
607
558
608 /**
559 /**
609 * Observers autocomplete
560 * Observers autocomplete
610 */
561 */
611 var ObserverAutoComplete = function(inputId, controller) {
562 var ObserverAutoComplete = function(inputId, controller) {
612 var self = this;
563 var self = this;
613 self.controller = controller;
564 self.controller = controller;
614 self.inputId = inputId;
565 self.inputId = inputId;
615 var handler = autoCompleteHandler(inputId, controller, controller.ROLE_OBSERVER);
566 var handler = autoCompleteHandler(inputId, controller, controller.ROLE_OBSERVER);
616
567
617 $(inputId).autocomplete({
568 $(inputId).autocomplete({
618 serviceUrl: pyroutes.url('user_autocomplete_data'),
569 serviceUrl: pyroutes.url('user_autocomplete_data'),
619 minChars: 2,
570 minChars: 2,
620 maxHeight: 400,
571 maxHeight: 400,
621 deferRequestBy: 300, //miliseconds
572 deferRequestBy: 300, //miliseconds
622 showNoSuggestionNotice: true,
573 showNoSuggestionNotice: true,
623 tabDisabled: true,
574 tabDisabled: true,
624 autoSelectFirst: true,
575 autoSelectFirst: true,
625 params: {
576 params: {
626 user_id: templateContext.rhodecode_user.user_id,
577 user_id: templateContext.rhodecode_user.user_id,
627 user_groups: true,
578 user_groups: true,
628 user_groups_expand: true,
579 user_groups_expand: true,
629 skip_default_user: true
580 skip_default_user: true
630 },
581 },
631 formatResult: autocompleteFormatResult,
582 formatResult: autocompleteFormatResult,
632 lookupFilter: autocompleteFilterResult,
583 lookupFilter: autocompleteFilterResult,
633 onSelect: handler
584 onSelect: handler
634 });
585 });
635 }
586 }
636
587
637
588
638 window.VersionController = function () {
589 window.VersionController = function () {
639 var self = this;
590 var self = this;
640 this.$verSource = $('input[name=ver_source]');
591 this.$verSource = $('input[name=ver_source]');
641 this.$verTarget = $('input[name=ver_target]');
592 this.$verTarget = $('input[name=ver_target]');
642 this.$showVersionDiff = $('#show-version-diff');
593 this.$showVersionDiff = $('#show-version-diff');
643
594
644 this.adjustRadioSelectors = function (curNode) {
595 this.adjustRadioSelectors = function (curNode) {
645 var getVal = function (item) {
596 var getVal = function (item) {
646 if (item === 'latest') {
597 if (item === 'latest') {
647 return Number.MAX_SAFE_INTEGER
598 return Number.MAX_SAFE_INTEGER
648 }
599 }
649 else {
600 else {
650 return parseInt(item)
601 return parseInt(item)
651 }
602 }
652 };
603 };
653
604
654 var curVal = getVal($(curNode).val());
605 var curVal = getVal($(curNode).val());
655 var cleared = false;
606 var cleared = false;
656
607
657 $.each(self.$verSource, function (index, value) {
608 $.each(self.$verSource, function (index, value) {
658 var elVal = getVal($(value).val());
609 var elVal = getVal($(value).val());
659
610
660 if (elVal > curVal) {
611 if (elVal > curVal) {
661 if ($(value).is(':checked')) {
612 if ($(value).is(':checked')) {
662 cleared = true;
613 cleared = true;
663 }
614 }
664 $(value).attr('disabled', 'disabled');
615 $(value).attr('disabled', 'disabled');
665 $(value).removeAttr('checked');
616 $(value).removeAttr('checked');
666 $(value).css({'opacity': 0.1});
617 $(value).css({'opacity': 0.1});
667 }
618 }
668 else {
619 else {
669 $(value).css({'opacity': 1});
620 $(value).css({'opacity': 1});
670 $(value).removeAttr('disabled');
621 $(value).removeAttr('disabled');
671 }
622 }
672 });
623 });
673
624
674 if (cleared) {
625 if (cleared) {
675 // if we unchecked an active, set the next one to same loc.
626 // if we unchecked an active, set the next one to same loc.
676 $(this.$verSource).filter('[value={0}]'.format(
627 $(this.$verSource).filter('[value={0}]'.format(
677 curVal)).attr('checked', 'checked');
628 curVal)).attr('checked', 'checked');
678 }
629 }
679
630
680 self.setLockAction(false,
631 self.setLockAction(false,
681 $(curNode).data('verPos'),
632 $(curNode).data('verPos'),
682 $(this.$verSource).filter(':checked').data('verPos')
633 $(this.$verSource).filter(':checked').data('verPos')
683 );
634 );
684 };
635 };
685
636
686
637
687 this.attachVersionListener = function () {
638 this.attachVersionListener = function () {
688 self.$verTarget.change(function (e) {
639 self.$verTarget.change(function (e) {
689 self.adjustRadioSelectors(this)
640 self.adjustRadioSelectors(this)
690 });
641 });
691 self.$verSource.change(function (e) {
642 self.$verSource.change(function (e) {
692 self.adjustRadioSelectors(self.$verTarget.filter(':checked'))
643 self.adjustRadioSelectors(self.$verTarget.filter(':checked'))
693 });
644 });
694 };
645 };
695
646
696 this.init = function () {
647 this.init = function () {
697
648
698 var curNode = self.$verTarget.filter(':checked');
649 var curNode = self.$verTarget.filter(':checked');
699 self.adjustRadioSelectors(curNode);
650 self.adjustRadioSelectors(curNode);
700 self.setLockAction(true);
651 self.setLockAction(true);
701 self.attachVersionListener();
652 self.attachVersionListener();
702
653
703 };
654 };
704
655
705 this.setLockAction = function (state, selectedVersion, otherVersion) {
656 this.setLockAction = function (state, selectedVersion, otherVersion) {
706 var $showVersionDiff = this.$showVersionDiff;
657 var $showVersionDiff = this.$showVersionDiff;
707
658
708 if (state) {
659 if (state) {
709 $showVersionDiff.attr('disabled', 'disabled');
660 $showVersionDiff.attr('disabled', 'disabled');
710 $showVersionDiff.addClass('disabled');
661 $showVersionDiff.addClass('disabled');
711 $showVersionDiff.html($showVersionDiff.data('labelTextLocked'));
662 $showVersionDiff.html($showVersionDiff.data('labelTextLocked'));
712 }
663 }
713 else {
664 else {
714 $showVersionDiff.removeAttr('disabled');
665 $showVersionDiff.removeAttr('disabled');
715 $showVersionDiff.removeClass('disabled');
666 $showVersionDiff.removeClass('disabled');
716
667
717 if (selectedVersion == otherVersion) {
668 if (selectedVersion == otherVersion) {
718 $showVersionDiff.html($showVersionDiff.data('labelTextShow'));
669 $showVersionDiff.html($showVersionDiff.data('labelTextShow'));
719 } else {
670 } else {
720 $showVersionDiff.html($showVersionDiff.data('labelTextDiff'));
671 $showVersionDiff.html($showVersionDiff.data('labelTextDiff'));
721 }
672 }
722 }
673 }
723
674
724 };
675 };
725
676
726 this.showVersionDiff = function () {
677 this.showVersionDiff = function () {
727 var target = self.$verTarget.filter(':checked');
678 var target = self.$verTarget.filter(':checked');
728 var source = self.$verSource.filter(':checked');
679 var source = self.$verSource.filter(':checked');
729
680
730 if (target.val() && source.val()) {
681 if (target.val() && source.val()) {
731 var params = {
682 var params = {
732 'pull_request_id': templateContext.pull_request_data.pull_request_id,
683 'pull_request_id': templateContext.pull_request_data.pull_request_id,
733 'repo_name': templateContext.repo_name,
684 'repo_name': templateContext.repo_name,
734 'version': target.val(),
685 'version': target.val(),
735 'from_version': source.val()
686 'from_version': source.val()
736 };
687 };
737 window.location = pyroutes.url('pullrequest_show', params)
688 window.location = pyroutes.url('pullrequest_show', params)
738 }
689 }
739
690
740 return false;
691 return false;
741 };
692 };
742
693
743 this.toggleVersionView = function (elem) {
694 this.toggleVersionView = function (elem) {
744
695
745 if (this.$showVersionDiff.is(':visible')) {
696 if (this.$showVersionDiff.is(':visible')) {
746 $('.version-pr').hide();
697 $('.version-pr').hide();
747 this.$showVersionDiff.hide();
698 this.$showVersionDiff.hide();
748 $(elem).html($(elem).data('toggleOn'))
699 $(elem).html($(elem).data('toggleOn'))
749 } else {
700 } else {
750 $('.version-pr').show();
701 $('.version-pr').show();
751 this.$showVersionDiff.show();
702 this.$showVersionDiff.show();
752 $(elem).html($(elem).data('toggleOff'))
703 $(elem).html($(elem).data('toggleOff'))
753 }
704 }
754
705
755 return false
706 return false
756 };
707 };
757
708
758 };
709 };
759
710
760
711
761 window.UpdatePrController = function () {
712 window.UpdatePrController = function () {
762 var self = this;
713 var self = this;
763 this.$updateCommits = $('#update_commits');
714 this.$updateCommits = $('#update_commits');
764 this.$updateCommitsSwitcher = $('#update_commits_switcher');
715 this.$updateCommitsSwitcher = $('#update_commits_switcher');
765
716
766 this.lockUpdateButton = function (label) {
717 this.lockUpdateButton = function (label) {
767 self.$updateCommits.attr('disabled', 'disabled');
718 self.$updateCommits.attr('disabled', 'disabled');
768 self.$updateCommitsSwitcher.attr('disabled', 'disabled');
719 self.$updateCommitsSwitcher.attr('disabled', 'disabled');
769
720
770 self.$updateCommits.addClass('disabled');
721 self.$updateCommits.addClass('disabled');
771 self.$updateCommitsSwitcher.addClass('disabled');
722 self.$updateCommitsSwitcher.addClass('disabled');
772
723
773 self.$updateCommits.removeClass('btn-primary');
724 self.$updateCommits.removeClass('btn-primary');
774 self.$updateCommitsSwitcher.removeClass('btn-primary');
725 self.$updateCommitsSwitcher.removeClass('btn-primary');
775
726
776 self.$updateCommits.text(_gettext(label));
727 self.$updateCommits.text(_gettext(label));
777 };
728 };
778
729
779 this.isUpdateLocked = function () {
730 this.isUpdateLocked = function () {
780 return self.$updateCommits.attr('disabled') !== undefined;
731 return self.$updateCommits.attr('disabled') !== undefined;
781 };
732 };
782
733
783 this.updateCommits = function (curNode) {
734 this.updateCommits = function (curNode) {
784 if (self.isUpdateLocked()) {
735 if (self.isUpdateLocked()) {
785 return
736 return
786 }
737 }
787 self.lockUpdateButton(_gettext('Updating...'));
738 self.lockUpdateButton(_gettext('Updating...'));
788 updateCommits(
739 updateCommits(
789 templateContext.repo_name,
740 templateContext.repo_name,
790 templateContext.pull_request_data.pull_request_id);
741 templateContext.pull_request_data.pull_request_id);
791 };
742 };
792
743
793 this.forceUpdateCommits = function () {
744 this.forceUpdateCommits = function () {
794 if (self.isUpdateLocked()) {
745 if (self.isUpdateLocked()) {
795 return
746 return
796 }
747 }
797 self.lockUpdateButton(_gettext('Force updating...'));
748 self.lockUpdateButton(_gettext('Force updating...'));
798 var force = true;
749 var force = true;
799 updateCommits(
750 updateCommits(
800 templateContext.repo_name,
751 templateContext.repo_name,
801 templateContext.pull_request_data.pull_request_id, force);
752 templateContext.pull_request_data.pull_request_id, force);
802 };
753 };
803 };
754 };
804
755
805
756
806 /**
757 /**
807 * Reviewer display panel
758 * Reviewer display panel
808 */
759 */
809 window.ReviewersPanel = {
760 window.ReviewersPanel = {
810 editButton: null,
761 editButton: null,
811 closeButton: null,
762 closeButton: null,
812 addButton: null,
763 addButton: null,
813 removeButtons: null,
764 removeButtons: null,
814 reviewRules: null,
765 reviewRules: null,
815 setReviewers: null,
766 setReviewers: null,
816 controller: null,
767 controller: null,
817
768
818 setSelectors: function () {
769 setSelectors: function () {
819 var self = this;
770 var self = this;
820 self.editButton = $('#open_edit_reviewers');
771 self.editButton = $('#open_edit_reviewers');
821 self.closeButton =$('#close_edit_reviewers');
772 self.closeButton =$('#close_edit_reviewers');
822 self.addButton = $('#add_reviewer');
773 self.addButton = $('#add_reviewer');
823 self.removeButtons = $('.reviewer_member_remove,.reviewer_member_mandatory_remove');
774 self.removeButtons = $('.reviewer_member_remove,.reviewer_member_mandatory_remove');
824 },
775 },
825
776
826 init: function (controller, reviewRules, setReviewers) {
777 init: function (controller, reviewRules, setReviewers) {
827 var self = this;
778 var self = this;
828 self.setSelectors();
779 self.setSelectors();
829
780
830 self.controller = controller;
781 self.controller = controller;
831 self.reviewRules = reviewRules;
782 self.reviewRules = reviewRules;
832 self.setReviewers = setReviewers;
783 self.setReviewers = setReviewers;
833
784
834 self.editButton.on('click', function (e) {
785 self.editButton.on('click', function (e) {
835 self.edit();
786 self.edit();
836 });
787 });
837 self.closeButton.on('click', function (e) {
788 self.closeButton.on('click', function (e) {
838 self.close();
789 self.close();
839 self.renderReviewers();
790 self.renderReviewers();
840 });
791 });
841
792
842 self.renderReviewers();
793 self.renderReviewers();
843
794
844 },
795 },
845
796
846 renderReviewers: function () {
797 renderReviewers: function () {
847 var self = this;
798 var self = this;
848
799
849 if (self.setReviewers.reviewers === undefined) {
800 if (self.setReviewers.reviewers === undefined) {
850 return
801 return
851 }
802 }
852 if (self.setReviewers.reviewers.length === 0) {
803 if (self.setReviewers.reviewers.length === 0) {
853 self.controller.emptyReviewersTable('<tr id="reviewer-empty-msg"><td colspan="6">No reviewers</td></tr>');
804 self.controller.emptyReviewersTable('<tr id="reviewer-empty-msg"><td colspan="6">No reviewers</td></tr>');
854 return
805 return
855 }
806 }
856
807
857 self.controller.emptyReviewersTable();
808 self.controller.emptyReviewersTable();
858
809
859 $.each(self.setReviewers.reviewers, function (key, val) {
810 $.each(self.setReviewers.reviewers, function (key, val) {
860
811
861 var member = val;
812 var member = val;
862 if (member.role === self.controller.ROLE_REVIEWER) {
813 if (member.role === self.controller.ROLE_REVIEWER) {
863 var entry = renderTemplate('reviewMemberEntry', {
814 var entry = renderTemplate('reviewMemberEntry', {
864 'member': member,
815 'member': member,
865 'mandatory': member.mandatory,
816 'mandatory': member.mandatory,
866 'role': member.role,
817 'role': member.role,
867 'reasons': member.reasons,
818 'reasons': member.reasons,
868 'allowed_to_update': member.allowed_to_update,
819 'allowed_to_update': member.allowed_to_update,
869 'review_status': member.review_status,
820 'review_status': member.review_status,
870 'review_status_label': member.review_status_label,
821 'review_status_label': member.review_status_label,
871 'user_group': member.user_group,
822 'user_group': member.user_group,
872 'create': false
823 'create': false
873 });
824 });
874
825
875 $(self.controller.$reviewMembers.selector).append(entry)
826 $(self.controller.$reviewMembers.selector).append(entry)
876 }
827 }
877 });
828 });
878
829
879 tooltipActivate();
830 tooltipActivate();
880 },
831 },
881
832
882 edit: function (event) {
833 edit: function (event) {
883 var self = this;
834 var self = this;
884 self.editButton.hide();
835 self.editButton.hide();
885 self.closeButton.show();
836 self.closeButton.show();
886 self.addButton.show();
837 self.addButton.show();
887 $(self.removeButtons.selector).css('visibility', 'visible');
838 $(self.removeButtons.selector).css('visibility', 'visible');
888 // review rules
839 // review rules
889 self.controller.loadReviewRules(this.reviewRules);
840 self.controller.loadReviewRules(this.reviewRules);
890 },
841 },
891
842
892 close: function (event) {
843 close: function (event) {
893 var self = this;
844 var self = this;
894 this.editButton.show();
845 this.editButton.show();
895 this.closeButton.hide();
846 this.closeButton.hide();
896 this.addButton.hide();
847 this.addButton.hide();
897 $(this.removeButtons.selector).css('visibility', 'hidden');
848 $(this.removeButtons.selector).css('visibility', 'hidden');
898 // hide review rules
849 // hide review rules
899 self.controller.hideReviewRules();
850 self.controller.hideReviewRules();
900 }
851 }
901 };
852 };
902
853
903 /**
854 /**
904 * Reviewer display panel
855 * Reviewer display panel
905 */
856 */
906 window.ObserversPanel = {
857 window.ObserversPanel = {
907 editButton: null,
858 editButton: null,
908 closeButton: null,
859 closeButton: null,
909 addButton: null,
860 addButton: null,
910 removeButtons: null,
861 removeButtons: null,
911 reviewRules: null,
862 reviewRules: null,
912 setReviewers: null,
863 setReviewers: null,
913 controller: null,
864 controller: null,
914
865
915 setSelectors: function () {
866 setSelectors: function () {
916 var self = this;
867 var self = this;
917 self.editButton = $('#open_edit_observers');
868 self.editButton = $('#open_edit_observers');
918 self.closeButton =$('#close_edit_observers');
869 self.closeButton =$('#close_edit_observers');
919 self.addButton = $('#add_observer');
870 self.addButton = $('#add_observer');
920 self.removeButtons = $('.observer_member_remove,.observer_member_mandatory_remove');
871 self.removeButtons = $('.observer_member_remove,.observer_member_mandatory_remove');
921 },
872 },
922
873
923 init: function (controller, reviewRules, setReviewers) {
874 init: function (controller, reviewRules, setReviewers) {
924 var self = this;
875 var self = this;
925 self.setSelectors();
876 self.setSelectors();
926
877
927 self.controller = controller;
878 self.controller = controller;
928 self.reviewRules = reviewRules;
879 self.reviewRules = reviewRules;
929 self.setReviewers = setReviewers;
880 self.setReviewers = setReviewers;
930
881
931 self.editButton.on('click', function (e) {
882 self.editButton.on('click', function (e) {
932 self.edit();
883 self.edit();
933 });
884 });
934 self.closeButton.on('click', function (e) {
885 self.closeButton.on('click', function (e) {
935 self.close();
886 self.close();
936 self.renderObservers();
887 self.renderObservers();
937 });
888 });
938
889
939 self.renderObservers();
890 self.renderObservers();
940
891
941 },
892 },
942
893
943 renderObservers: function () {
894 renderObservers: function () {
944 var self = this;
895 var self = this;
945 if (self.setReviewers.observers === undefined) {
896 if (self.setReviewers.observers === undefined) {
946 return
897 return
947 }
898 }
948 if (self.setReviewers.observers.length === 0) {
899 if (self.setReviewers.observers.length === 0) {
949 self.controller.emptyObserversTable('<tr id="observer-empty-msg"><td colspan="6">No observers</td></tr>');
900 self.controller.emptyObserversTable('<tr id="observer-empty-msg"><td colspan="6">No observers</td></tr>');
950 return
901 return
951 }
902 }
952
903
953 self.controller.emptyObserversTable();
904 self.controller.emptyObserversTable();
954
905
955 $.each(self.setReviewers.observers, function (key, val) {
906 $.each(self.setReviewers.observers, function (key, val) {
956 var member = val;
907 var member = val;
957 if (member.role === self.controller.ROLE_OBSERVER) {
908 if (member.role === self.controller.ROLE_OBSERVER) {
958 var entry = renderTemplate('reviewMemberEntry', {
909 var entry = renderTemplate('reviewMemberEntry', {
959 'member': member,
910 'member': member,
960 'mandatory': member.mandatory,
911 'mandatory': member.mandatory,
961 'role': member.role,
912 'role': member.role,
962 'reasons': member.reasons,
913 'reasons': member.reasons,
963 'allowed_to_update': member.allowed_to_update,
914 'allowed_to_update': member.allowed_to_update,
964 'review_status': member.review_status,
915 'review_status': member.review_status,
965 'review_status_label': member.review_status_label,
916 'review_status_label': member.review_status_label,
966 'user_group': member.user_group,
917 'user_group': member.user_group,
967 'create': false
918 'create': false
968 });
919 });
969
920
970 $(self.controller.$observerMembers.selector).append(entry)
921 $(self.controller.$observerMembers.selector).append(entry)
971 }
922 }
972 });
923 });
973
924
974 tooltipActivate();
925 tooltipActivate();
975 },
926 },
976
927
977 edit: function (event) {
928 edit: function (event) {
978 this.editButton.hide();
929 this.editButton.hide();
979 this.closeButton.show();
930 this.closeButton.show();
980 this.addButton.show();
931 this.addButton.show();
981 $(this.removeButtons.selector).css('visibility', 'visible');
932 $(this.removeButtons.selector).css('visibility', 'visible');
982 },
933 },
983
934
984 close: function (event) {
935 close: function (event) {
985 this.editButton.show();
936 this.editButton.show();
986 this.closeButton.hide();
937 this.closeButton.hide();
987 this.addButton.hide();
938 this.addButton.hide();
988 $(this.removeButtons.selector).css('visibility', 'hidden');
939 $(this.removeButtons.selector).css('visibility', 'hidden');
989 }
940 }
990
941
991 };
942 };
992
943
993 window.PRDetails = {
944 window.PRDetails = {
994 editButton: null,
945 editButton: null,
995 closeButton: null,
946 closeButton: null,
996 deleteButton: null,
947 deleteButton: null,
997 viewFields: null,
948 viewFields: null,
998 editFields: null,
949 editFields: null,
999
950
1000 setSelectors: function () {
951 setSelectors: function () {
1001 var self = this;
952 var self = this;
1002 self.editButton = $('#open_edit_pullrequest')
953 self.editButton = $('#open_edit_pullrequest')
1003 self.closeButton = $('#close_edit_pullrequest')
954 self.closeButton = $('#close_edit_pullrequest')
1004 self.deleteButton = $('#delete_pullrequest')
955 self.deleteButton = $('#delete_pullrequest')
1005 self.viewFields = $('#pr-desc, #pr-title')
956 self.viewFields = $('#pr-desc, #pr-title')
1006 self.editFields = $('#pr-desc-edit, #pr-title-edit, .pr-save')
957 self.editFields = $('#pr-desc-edit, #pr-title-edit, .pr-save')
1007 },
958 },
1008
959
1009 init: function () {
960 init: function () {
1010 var self = this;
961 var self = this;
1011 self.setSelectors();
962 self.setSelectors();
1012 self.editButton.on('click', function (e) {
963 self.editButton.on('click', function (e) {
1013 self.edit();
964 self.edit();
1014 });
965 });
1015 self.closeButton.on('click', function (e) {
966 self.closeButton.on('click', function (e) {
1016 self.view();
967 self.view();
1017 });
968 });
1018 },
969 },
1019
970
1020 edit: function (event) {
971 edit: function (event) {
1021 var cmInstance = $('#pr-description-input').get(0).MarkupForm.cm;
972 var cmInstance = $('#pr-description-input').get(0).MarkupForm.cm;
1022 this.viewFields.hide();
973 this.viewFields.hide();
1023 this.editButton.hide();
974 this.editButton.hide();
1024 this.deleteButton.hide();
975 this.deleteButton.hide();
1025 this.closeButton.show();
976 this.closeButton.show();
1026 this.editFields.show();
977 this.editFields.show();
1027 cmInstance.refresh();
978 cmInstance.refresh();
1028 },
979 },
1029
980
1030 view: function (event) {
981 view: function (event) {
1031 this.editButton.show();
982 this.editButton.show();
1032 this.deleteButton.show();
983 this.deleteButton.show();
1033 this.editFields.hide();
984 this.editFields.hide();
1034 this.closeButton.hide();
985 this.closeButton.hide();
1035 this.viewFields.show();
986 this.viewFields.show();
1036 }
987 }
1037 };
988 };
1038
989
1039 /**
990 /**
1040 * OnLine presence using channelstream
991 * OnLine presence using channelstream
1041 */
992 */
1042 window.ReviewerPresenceController = function (channel) {
993 window.ReviewerPresenceController = function (channel) {
1043 var self = this;
994 var self = this;
1044 this.channel = channel;
995 this.channel = channel;
1045 this.users = {};
996 this.users = {};
1046
997
1047 this.storeUsers = function (users) {
998 this.storeUsers = function (users) {
1048 self.users = {}
999 self.users = {}
1049 $.each(users, function (index, value) {
1000 $.each(users, function (index, value) {
1050 var userId = value.state.id;
1001 var userId = value.state.id;
1051 self.users[userId] = value.state;
1002 self.users[userId] = value.state;
1052 })
1003 })
1053 }
1004 }
1054
1005
1055 this.render = function () {
1006 this.render = function () {
1056 $.each($('.reviewer_entry'), function (index, value) {
1007 $.each($('.reviewer_entry'), function (index, value) {
1057 var userData = $(value).data();
1008 var userData = $(value).data();
1058 if (self.users[userData.reviewerUserId] !== undefined) {
1009 if (self.users[userData.reviewerUserId] !== undefined) {
1059 $(value).find('.presence-state').show();
1010 $(value).find('.presence-state').show();
1060 } else {
1011 } else {
1061 $(value).find('.presence-state').hide();
1012 $(value).find('.presence-state').hide();
1062 }
1013 }
1063 })
1014 })
1064 };
1015 };
1065
1016
1066 this.handlePresence = function (data) {
1017 this.handlePresence = function (data) {
1067 if (data.type == 'presence' && data.channel === self.channel) {
1018 if (data.type == 'presence' && data.channel === self.channel) {
1068 this.storeUsers(data.users);
1019 this.storeUsers(data.users);
1069 this.render();
1020 this.render();
1070 }
1021 }
1071 };
1022 };
1072
1023
1073 this.handleChannelUpdate = function (data) {
1024 this.handleChannelUpdate = function (data) {
1074 if (data.channel === this.channel) {
1025 if (data.channel === this.channel) {
1075 this.storeUsers(data.state.users);
1026 this.storeUsers(data.state.users);
1076 this.render();
1027 this.render();
1077 }
1028 }
1078
1029
1079 };
1030 };
1080
1031
1081 /* subscribe to the current presence */
1032 /* subscribe to the current presence */
1082 $.Topic('/connection_controller/presence').subscribe(this.handlePresence.bind(this));
1033 $.Topic('/connection_controller/presence').subscribe(this.handlePresence.bind(this));
1083 /* subscribe to updates e.g connect/disconnect */
1034 /* subscribe to updates e.g connect/disconnect */
1084 $.Topic('/connection_controller/channel_update').subscribe(this.handleChannelUpdate.bind(this));
1035 $.Topic('/connection_controller/channel_update').subscribe(this.handleChannelUpdate.bind(this));
1085
1036
1086 };
1037 };
1087
1038
1088 window.refreshComments = function (version) {
1039 window.refreshComments = function (version) {
1089 version = version || templateContext.pull_request_data.pull_request_version || '';
1040 version = version || templateContext.pull_request_data.pull_request_version || '';
1090
1041
1091 // Pull request case
1042 // Pull request case
1092 if (templateContext.pull_request_data.pull_request_id !== null) {
1043 if (templateContext.pull_request_data.pull_request_id !== null) {
1093 var params = {
1044 var params = {
1094 'pull_request_id': templateContext.pull_request_data.pull_request_id,
1045 'pull_request_id': templateContext.pull_request_data.pull_request_id,
1095 'repo_name': templateContext.repo_name,
1046 'repo_name': templateContext.repo_name,
1096 'version': version,
1047 'version': version,
1097 };
1048 };
1098 var loadUrl = pyroutes.url('pullrequest_comments', params);
1049 var loadUrl = pyroutes.url('pullrequest_comments', params);
1099 } // commit case
1050 } // commit case
1100 else {
1051 else {
1101 return
1052 return
1102 }
1053 }
1103
1054
1104 var currentIDs = []
1055 var currentIDs = []
1105 $.each($('.comment'), function (idx, element) {
1056 $.each($('.comment'), function (idx, element) {
1106 currentIDs.push($(element).data('commentId'));
1057 currentIDs.push($(element).data('commentId'));
1107 });
1058 });
1108 var data = {"comments": currentIDs};
1059 var data = {"comments": currentIDs};
1109
1060
1110 var $targetElem = $('.comments-content-table');
1061 var $targetElem = $('.comments-content-table');
1111 $targetElem.css('opacity', 0.3);
1062 $targetElem.css('opacity', 0.3);
1112
1063
1113 var success = function (data) {
1064 var success = function (data) {
1114 var $counterElem = $('#comments-count');
1065 var $counterElem = $('#comments-count');
1115 var newCount = $(data).data('counter');
1066 var newCount = $(data).data('counter');
1116 if (newCount !== undefined) {
1067 if (newCount !== undefined) {
1117 var callback = function () {
1068 var callback = function () {
1118 $counterElem.animate({'opacity': 1.00}, 200)
1069 $counterElem.animate({'opacity': 1.00}, 200)
1119 $counterElem.html(newCount);
1070 $counterElem.html(newCount);
1120 };
1071 };
1121 $counterElem.animate({'opacity': 0.15}, 200, callback);
1072 $counterElem.animate({'opacity': 0.15}, 200, callback);
1122 }
1073 }
1123
1074
1124 $targetElem.css('opacity', 1);
1075 $targetElem.css('opacity', 1);
1125 $targetElem.html(data);
1076 $targetElem.html(data);
1126 tooltipActivate();
1077 tooltipActivate();
1127 }
1078 }
1128
1079
1129 ajaxPOST(loadUrl, data, success, null, {})
1080 ajaxPOST(loadUrl, data, success, null, {})
1130
1081
1131 }
1082 }
1132
1083
1133 window.refreshTODOs = function (version) {
1084 window.refreshTODOs = function (version) {
1134 version = version || templateContext.pull_request_data.pull_request_version || '';
1085 version = version || templateContext.pull_request_data.pull_request_version || '';
1135 // Pull request case
1086 // Pull request case
1136 if (templateContext.pull_request_data.pull_request_id !== null) {
1087 if (templateContext.pull_request_data.pull_request_id !== null) {
1137 var params = {
1088 var params = {
1138 'pull_request_id': templateContext.pull_request_data.pull_request_id,
1089 'pull_request_id': templateContext.pull_request_data.pull_request_id,
1139 'repo_name': templateContext.repo_name,
1090 'repo_name': templateContext.repo_name,
1140 'version': version,
1091 'version': version,
1141 };
1092 };
1142 var loadUrl = pyroutes.url('pullrequest_todos', params);
1093 var loadUrl = pyroutes.url('pullrequest_todos', params);
1143 } // commit case
1094 } // commit case
1144 else {
1095 else {
1145 return
1096 return
1146 }
1097 }
1147
1098
1148 var currentIDs = []
1099 var currentIDs = []
1149 $.each($('.comment'), function (idx, element) {
1100 $.each($('.comment'), function (idx, element) {
1150 currentIDs.push($(element).data('commentId'));
1101 currentIDs.push($(element).data('commentId'));
1151 });
1102 });
1152
1103
1153 var data = {"comments": currentIDs};
1104 var data = {"comments": currentIDs};
1154 var $targetElem = $('.todos-content-table');
1105 var $targetElem = $('.todos-content-table');
1155 $targetElem.css('opacity', 0.3);
1106 $targetElem.css('opacity', 0.3);
1156
1107
1157 var success = function (data) {
1108 var success = function (data) {
1158 var $counterElem = $('#todos-count')
1109 var $counterElem = $('#todos-count')
1159 var newCount = $(data).data('counter');
1110 var newCount = $(data).data('counter');
1160 if (newCount !== undefined) {
1111 if (newCount !== undefined) {
1161 var callback = function () {
1112 var callback = function () {
1162 $counterElem.animate({'opacity': 1.00}, 200)
1113 $counterElem.animate({'opacity': 1.00}, 200)
1163 $counterElem.html(newCount);
1114 $counterElem.html(newCount);
1164 };
1115 };
1165 $counterElem.animate({'opacity': 0.15}, 200, callback);
1116 $counterElem.animate({'opacity': 0.15}, 200, callback);
1166 }
1117 }
1167
1118
1168 $targetElem.css('opacity', 1);
1119 $targetElem.css('opacity', 1);
1169 $targetElem.html(data);
1120 $targetElem.html(data);
1170 tooltipActivate();
1121 tooltipActivate();
1171 }
1122 }
1172
1123
1173 ajaxPOST(loadUrl, data, success, null, {})
1124 ajaxPOST(loadUrl, data, success, null, {})
1174
1125
1175 }
1126 }
1176
1127
1177 window.refreshAllComments = function (version) {
1128 window.refreshAllComments = function (version) {
1178 version = version || templateContext.pull_request_data.pull_request_version || '';
1129 version = version || templateContext.pull_request_data.pull_request_version || '';
1179
1130
1180 refreshComments(version);
1131 refreshComments(version);
1181 refreshTODOs(version);
1132 refreshTODOs(version);
1182 };
1133 };
1183
1134
1184 window.refreshDraftComments = function () {
1135 window.refreshDraftComments = function () {
1185 alert('TODO: refresh Draft Comments needs implementation')
1136 alert('TODO: refresh Draft Comments needs implementation')
1186 };
1137 };
1187
1138
1188 window.sidebarComment = function (commentId) {
1139 window.sidebarComment = function (commentId) {
1189 var jsonData = $('#commentHovercard{0}'.format(commentId)).data('commentJsonB64');
1140 var jsonData = $('#commentHovercard{0}'.format(commentId)).data('commentJsonB64');
1190 if (!jsonData) {
1141 if (!jsonData) {
1191 return 'Failed to load comment {0}'.format(commentId)
1142 return 'Failed to load comment {0}'.format(commentId)
1192 }
1143 }
1193 var funcData = JSON.parse(atob(jsonData));
1144 var funcData = JSON.parse(atob(jsonData));
1194 return renderTemplate('sideBarCommentHovercard', funcData)
1145 return renderTemplate('sideBarCommentHovercard', funcData)
1195 };
1146 };
General Comments 0
You need to be logged in to leave comments. Login now