annotate kallithea/model/changeset_status.py @ 5397:7d0727d11104

cleanup: remove unused imports Found with pyflakes.
author Mads Kiilerich <madski@unity3d.com>
date Mon, 17 Aug 2015 01:11:42 +0200
parents 0ad053c172fa
children edb24bc0f71a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5376
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
2 # This program is free software: you can redistribute it and/or modify
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
3 # it under the terms of the GNU General Public License as published by
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
4 # the Free Software Foundation, either version 3 of the License, or
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
5 # (at your option) any later version.
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
6 #
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
7 # This program is distributed in the hope that it will be useful,
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
10 # GNU General Public License for more details.
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
11 #
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
12 # You should have received a copy of the GNU General Public License
0ad053c172fa cleanup: make module self-naming consistent
Mads Kiilerich <madski@unity3d.com>
parents: 5047
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 """
4186
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
15 kallithea.model.changeset_status
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4097
diff changeset
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17
4919
494b04a84a49 model: trivial typo fixes
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4750
diff changeset
18 Changeset status controller
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19
4211
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
20 This file was forked by the Kallithea project in July 2014.
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
21 Original author and date, and relevant copyright and licensing information is below:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4097
diff changeset
22 :created_on: Apr 30, 2012
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4097
diff changeset
23 :author: marcink
4211
1948ede028ef RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4208
diff changeset
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
4208
ad38f9f93b3b Correct licensing information in individual files.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4187
diff changeset
25 :license: GPLv3, see LICENSE.md for more details.
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 """
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import logging
4750
3752cf4972a7 status: explicitly use joinedload of authors instead of relying on lazy=join
Mads Kiilerich <madski@unity3d.com>
parents: 4526
diff changeset
29 from sqlalchemy.orm import joinedload
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30
4186
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
31 from kallithea.model import BaseModel
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
32 from kallithea.model.db import ChangesetStatus, PullRequest
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
33 from kallithea.lib.exceptions import StatusChangeOnClosedPullRequestError
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 log = logging.getLogger(__name__)
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 class ChangesetStatusModel(BaseModel):
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39
2522
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2481
diff changeset
40 cls = ChangesetStatus
17893d61792a Added associated classes into child models
Marcin Kuzminski <marcin@python-works.com>
parents: 2481
diff changeset
41
2216
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 def __get_changeset_status(self, changeset_status):
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 return self._get_instance(ChangesetStatus, changeset_status)
a2987fa580d9 dummy ChangesetStatus model
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44
2440
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
45 def __get_pull_request(self, pull_request):
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
46 return self._get_instance(PullRequest, pull_request)
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
47
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
48 def _get_status_query(self, repo, revision, pull_request,
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
49 with_revisions=False):
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
50 repo = self._get_repo(repo)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
51
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
52 q = ChangesetStatus.query()\
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
53 .filter(ChangesetStatus.repo == repo)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
54 if not with_revisions:
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
55 q = q.filter(ChangesetStatus.version == 0)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
56
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
57 if revision:
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
58 q = q.filter(ChangesetStatus.revision == revision)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
59 elif pull_request:
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
60 pull_request = self.__get_pull_request(pull_request)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
61 q = q.filter(ChangesetStatus.pull_request == pull_request)
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
62 else:
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
63 raise Exception('Please specify revision or pull_request')
4355
ab38e825d303 pull requests: fix sorting of votes - the result of the method call was not used like other method calls are
Mads Kiilerich <madski@unity3d.com>
parents: 4329
diff changeset
64 q = q.order_by(ChangesetStatus.version.asc())
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
65 return q
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
66
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
67 def _calculate_status(self, statuses):
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
68 """
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
69 Given a list of statuses, calculate the resulting status, according to
5041
6b1e1437c026 changeset status: set status to rejected if at least one reviewer rejected
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5040
diff changeset
70 the policy: approve if consensus, reject when at least one reject.
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
71 """
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
72
5040
930caa6a4e5f changeset status: simplify status calculation logic
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5039
diff changeset
73 if not statuses:
930caa6a4e5f changeset status: simplify status calculation logic
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5039
diff changeset
74 return ChangesetStatus.STATUS_UNDER_REVIEW
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
75
5047
99f090563f5f changeset status: fix common case where a reviewer did not yet review anything
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5041
diff changeset
76 if all(st and st.status == ChangesetStatus.STATUS_APPROVED for st in statuses):
5040
930caa6a4e5f changeset status: simplify status calculation logic
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5039
diff changeset
77 return ChangesetStatus.STATUS_APPROVED
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
78
5047
99f090563f5f changeset status: fix common case where a reviewer did not yet review anything
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5041
diff changeset
79 if any(st and st.status == ChangesetStatus.STATUS_REJECTED for st in statuses):
5041
6b1e1437c026 changeset status: set status to rejected if at least one reviewer rejected
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5040
diff changeset
80 return ChangesetStatus.STATUS_REJECTED
6b1e1437c026 changeset status: set status to rejected if at least one reviewer rejected
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5040
diff changeset
81
5040
930caa6a4e5f changeset status: simplify status calculation logic
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 5039
diff changeset
82 return ChangesetStatus.STATUS_UNDER_REVIEW
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
83
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
84 def calculate_pull_request_result(self, pull_request):
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
85 """
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
86 Return a tuple (reviewers, pending reviewers, pull request status)
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
87 Only approve and reject counts as valid votes.
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
88 """
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
89
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
90 # collect latest votes from all voters
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
91 cs_statuses = dict()
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
92 for st in reversed(self.get_statuses(pull_request.org_repo,
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
93 pull_request=pull_request,
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
94 with_revisions=True)):
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
95 cs_statuses[st.author.username] = st
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
96
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
97 # collect votes from official reviewers
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
98 pull_request_reviewers = []
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
99 pull_request_pending_reviewers = []
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
100 relevant_statuses = []
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
101 for r in pull_request.reviewers:
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
102 st = cs_statuses.get(r.user.username)
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
103 relevant_statuses.append(st)
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
104 if not st or st.status in (ChangesetStatus.STATUS_NOT_REVIEWED,
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
105 ChangesetStatus.STATUS_UNDER_REVIEW):
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
106 st = None
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
107 pull_request_pending_reviewers.append(r.user)
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
108 pull_request_reviewers.append((r.user, st))
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
109
5039
36d81185efe4 changeset_status: add unit tests for calculation of overall status
Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
parents: 4919
diff changeset
110 result = self._calculate_status(relevant_statuses)
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
111
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
112 return (pull_request_reviewers,
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
113 pull_request_pending_reviewers,
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
114 result)
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
115
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
116 def get_statuses(self, repo, revision=None, pull_request=None,
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
117 with_revisions=False):
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
118 q = self._get_status_query(repo, revision, pull_request,
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
119 with_revisions)
4750
3752cf4972a7 status: explicitly use joinedload of authors instead of relying on lazy=join
Mads Kiilerich <madski@unity3d.com>
parents: 4526
diff changeset
120 q = q.options(joinedload('author'))
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
121 return q.all()
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
122
3176
7ec5f9c1df45 show comments from pull requests into associated changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 3175
diff changeset
123 def get_status(self, repo, revision=None, pull_request=None, as_str=True):
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
124 """
2440
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
125 Returns latest status of changeset for given revision or for given
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
126 pull request. Statuses are versioned inside a table itself and
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
127 version == 0 is always the current one
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
128
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
129 :param repo:
2440
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
130 :param revision: 40char hash or None
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
131 :param pull_request: pull_request reference
3176
7ec5f9c1df45 show comments from pull requests into associated changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 3175
diff changeset
132 :param as_str: return status as string not object
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
133 """
2481
4d3032431d4f Adde pull request voting recalculation
Marcin Kuzminski <marcin@python-works.com>
parents: 2478
diff changeset
134 q = self._get_status_query(repo, revision, pull_request)
2440
1bc579bcd67a - pull request generates overview based on it's params
Marcin Kuzminski <marcin@python-works.com>
parents: 2432
diff changeset
135
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
136 # need to use first here since there can be multiple statuses
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
137 # returned from pull_request
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
138 status = q.first()
3176
7ec5f9c1df45 show comments from pull requests into associated changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 3175
diff changeset
139 if as_str:
4318
afbd43437237 pull requests: cleanup of messages and code
Mads Kiilerich <madski@unity3d.com>
parents: 4211
diff changeset
140 return str(status.status) if status else ChangesetStatus.DEFAULT
3176
7ec5f9c1df45 show comments from pull requests into associated changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 3175
diff changeset
141 return status
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
142
4329
f54dca244402 changeset: refactor set_status so a comment always must be specified
Mads Kiilerich <madski@unity3d.com>
parents: 4318
diff changeset
143 def set_status(self, repo, status, user, comment, revision=None,
2677
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
144 pull_request=None, dont_allow_on_closed_pull_request=False):
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
145 """
2287
8447d35b674e Added simple versioning for changeset status
Marcin Kuzminski <marcin@python-works.com>
parents: 2286
diff changeset
146 Creates new status for changeset or updates the old ones bumping their
4526
1f94dd1e2757 Fix readability issues in comments
Na'Tosha Bard <natosha@unity3d.com>
parents: 4360
diff changeset
147 version, leaving the current status at the value of 'status'.
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
148
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
149 :param repo:
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
150 :param status:
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
151 :param user:
2286
9265958e33bb Show changes of status inside comments
Marcin Kuzminski <marcin@python-works.com>
parents: 2217
diff changeset
152 :param comment:
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
153 :param revision:
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
154 :param pull_request:
2677
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
155 :param dont_allow_on_closed_pull_request: don't allow a status change
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
156 if last status was for pull request and it's closed. We shouldn't
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
157 mess around this manually
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
158 """
2432
d3ac7491a5c8 Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents: 2287
diff changeset
159 repo = self._get_repo(repo)
2217
76947224bf27 Implemented initial code-review status of changesets
Marcin Kuzminski <marcin@python-works.com>
parents: 2216
diff changeset
160
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
161 q = ChangesetStatus.query()
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
162 if revision is not None:
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
163 assert pull_request is None
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
164 q = q.filter(ChangesetStatus.repo == repo)
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
165 q = q.filter(ChangesetStatus.revision == revision)
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
166 revisions = [revision]
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
167 else:
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
168 assert pull_request is not None
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
169 pull_request = self.__get_pull_request(pull_request)
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
170 repo = pull_request.org_repo
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
171 q = q.filter(ChangesetStatus.repo == repo)
3175
5d1d25c1c700 set the status of changesets initially on pull request, and make sure we care of version collisions.
Marcin Kuzminski <marcin@python-works.com>
parents: 2803
diff changeset
172 q = q.filter(ChangesetStatus.revision.in_(pull_request.revisions))
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
173 revisions = pull_request.revisions
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
174 cur_statuses = q.all()
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
175
2677
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
176 #if statuses exists and last is associated with a closed pull request
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
177 # we need to check if we can allow this status change
2771
9640c1c6399b really check if last status is from pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2677
diff changeset
178 if (dont_allow_on_closed_pull_request and cur_statuses
9640c1c6399b really check if last status is from pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2677
diff changeset
179 and getattr(cur_statuses[0].pull_request, 'status', '')
9640c1c6399b really check if last status is from pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2677
diff changeset
180 == PullRequest.STATUS_CLOSED):
2677
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
181 raise StatusChangeOnClosedPullRequestError(
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
182 'Changing status on closed pull request is not allowed'
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
183 )
4fbbc65e8cd5 Forbid changing changset status when it is associated with a closed pull request
Marcin Kuzminski <marcin@python-works.com>
parents: 2522
diff changeset
184
3175
5d1d25c1c700 set the status of changesets initially on pull request, and make sure we care of version collisions.
Marcin Kuzminski <marcin@python-works.com>
parents: 2803
diff changeset
185 #update all current statuses with older version
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
186 for st in cur_statuses:
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
187 st.version += 1
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
188 self.sa.add(st)
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
189
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
190 new_statuses = []
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
191 for rev in revisions:
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
192 new_status = ChangesetStatus()
4359
cfd9115db2a5 pull requests: cleanup of PR status voting calculation
Mads Kiilerich <madski@unity3d.com>
parents: 4355
diff changeset
193 new_status.version = 0 # default
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
194 new_status.author = self._get_user(user)
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
195 new_status.repo = self._get_repo(repo)
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
196 new_status.status = status
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
197 new_status.comment = comment
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
198 new_status.revision = rev
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
199 new_status.pull_request = pull_request
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
200 new_statuses.append(new_status)
2443
fd0a822481ec - added commenting to pull requests
Marcin Kuzminski <marcin@python-works.com>
parents: 2440
diff changeset
201 self.sa.add(new_status)
4360
05af189da2ae pull requests: refactor ChangesetStatus set_status and ChangesetComment create
Mads Kiilerich <madski@unity3d.com>
parents: 4359
diff changeset
202 return new_statuses