Show More
@@ -1,207 +1,205 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import pytest |
|
22 | 22 | |
|
23 | 23 | from rhodecode.lib.vcs.nodes import FileNode |
|
24 | 24 | from rhodecode.model.db import User |
|
25 | 25 | from rhodecode.model.pull_request import PullRequestModel |
|
26 | 26 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN |
|
27 |
from rhodecode.api.tests.utils import ( |
|
|
27 | from rhodecode.api.tests.utils import ( | |
|
28 | build_data, api_call, assert_ok, assert_error) | |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | @pytest.mark.usefixtures("testuser_api", "app") |
|
31 | 32 | class TestUpdatePullRequest(object): |
|
32 | 33 | |
|
33 | 34 | @pytest.mark.backends("git", "hg") |
|
34 | 35 | def test_api_update_pull_request_title_or_description( |
|
35 | 36 | self, pr_util, silence_action_logger, no_notifications): |
|
36 | 37 | pull_request = pr_util.create_pull_request() |
|
37 | 38 | |
|
38 | 39 | id_, params = build_data( |
|
39 | 40 | self.apikey, 'update_pull_request', |
|
40 | 41 | repoid=pull_request.target_repo.repo_name, |
|
41 | 42 | pullrequestid=pull_request.pull_request_id, |
|
42 | 43 | title='New TITLE OF A PR', |
|
43 | 44 | description='New DESC OF A PR', |
|
44 | 45 | ) |
|
45 | 46 | response = api_call(self.app, params) |
|
46 | 47 | |
|
47 | 48 | expected = { |
|
48 | 49 | "msg": "Updated pull request `{}`".format( |
|
49 | 50 | pull_request.pull_request_id), |
|
50 | 51 | "pull_request": response.json['result']['pull_request'], |
|
51 | 52 | "updated_commits": {"added": [], "common": [], "removed": []}, |
|
52 | 53 | "updated_reviewers": {"added": [], "removed": []}, |
|
53 | 54 | } |
|
54 | 55 | |
|
55 | 56 | response_json = response.json['result'] |
|
56 | 57 | assert response_json == expected |
|
57 | 58 | pr = response_json['pull_request'] |
|
58 | 59 | assert pr['title'] == 'New TITLE OF A PR' |
|
59 | 60 | assert pr['description'] == 'New DESC OF A PR' |
|
60 | 61 | |
|
61 | 62 | @pytest.mark.backends("git", "hg") |
|
62 | 63 | def test_api_try_update_closed_pull_request( |
|
63 | 64 | self, pr_util, silence_action_logger, no_notifications): |
|
64 | 65 | pull_request = pr_util.create_pull_request() |
|
65 | 66 | PullRequestModel().close_pull_request( |
|
66 | 67 | pull_request, TEST_USER_ADMIN_LOGIN) |
|
67 | 68 | |
|
68 | 69 | id_, params = build_data( |
|
69 | 70 | self.apikey, 'update_pull_request', |
|
70 | 71 | repoid=pull_request.target_repo.repo_name, |
|
71 | 72 | pullrequestid=pull_request.pull_request_id) |
|
72 | 73 | response = api_call(self.app, params) |
|
73 | 74 | |
|
74 | 75 | expected = 'pull request `{}` update failed, pull request ' \ |
|
75 | 76 | 'is closed'.format(pull_request.pull_request_id) |
|
76 | 77 | |
|
77 | response_json = response.json['error'] | |
|
78 | assert response_json == expected | |
|
78 | assert_error(id_, expected, response.body) | |
|
79 | 79 | |
|
80 | 80 | @pytest.mark.backends("git", "hg") |
|
81 | 81 | def test_api_update_update_commits( |
|
82 | 82 | self, pr_util, silence_action_logger, no_notifications): |
|
83 | 83 | commits = [ |
|
84 | 84 | {'message': 'a'}, |
|
85 | 85 | {'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]}, |
|
86 | 86 | {'message': 'c', 'added': [FileNode('file_c', 'test_content\n')]}, |
|
87 | 87 | ] |
|
88 | 88 | pull_request = pr_util.create_pull_request( |
|
89 | 89 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
90 | 90 | pr_util.update_source_repository(head='c') |
|
91 | 91 | repo = pull_request.source_repo.scm_instance() |
|
92 | 92 | commits = [x for x in repo.get_commits()] |
|
93 | print commits | |
|
93 | 94 | |
|
94 | 95 | added_commit_id = commits[-1].raw_id # c commit |
|
95 |
common_commit |
|
|
96 | common_commit_id = commits[1].raw_id # b commit is common ancestor | |
|
97 | total_commits = [added_commit_id, common_commit_id] | |
|
96 | 98 | |
|
97 | 99 | id_, params = build_data( |
|
98 | 100 | self.apikey, 'update_pull_request', |
|
99 | 101 | repoid=pull_request.target_repo.repo_name, |
|
100 | 102 | pullrequestid=pull_request.pull_request_id, |
|
101 | 103 | update_commits=True |
|
102 | 104 | ) |
|
103 | 105 | response = api_call(self.app, params) |
|
104 | 106 | |
|
105 | 107 | expected = { |
|
106 | 108 | "msg": "Updated pull request `{}`".format( |
|
107 | 109 | pull_request.pull_request_id), |
|
108 | 110 | "pull_request": response.json['result']['pull_request'], |
|
109 | 111 | "updated_commits": {"added": [added_commit_id], |
|
110 |
"common": [common_commit |
|
|
112 | "common": [common_commit_id], | |
|
113 | "total": total_commits, | |
|
114 | "removed": []}, | |
|
111 | 115 | "updated_reviewers": {"added": [], "removed": []}, |
|
112 | 116 | } |
|
113 | 117 | |
|
114 | response_json = response.json['result'] | |
|
115 | assert response_json == expected | |
|
118 | assert_ok(id_, expected, response.body) | |
|
116 | 119 | |
|
117 | 120 | @pytest.mark.backends("git", "hg") |
|
118 | 121 | def test_api_update_change_reviewers( |
|
119 | 122 | self, pr_util, silence_action_logger, no_notifications): |
|
120 | 123 | |
|
121 | 124 | users = [x.username for x in User.get_all()] |
|
122 | 125 | new = [users.pop(0)] |
|
123 | 126 | removed = sorted(new) |
|
124 | 127 | added = sorted(users) |
|
125 | 128 | |
|
126 | 129 | pull_request = pr_util.create_pull_request(reviewers=new) |
|
127 | 130 | |
|
128 | 131 | id_, params = build_data( |
|
129 | 132 | self.apikey, 'update_pull_request', |
|
130 | 133 | repoid=pull_request.target_repo.repo_name, |
|
131 | 134 | pullrequestid=pull_request.pull_request_id, |
|
132 | 135 | reviewers=added) |
|
133 | 136 | response = api_call(self.app, params) |
|
134 | 137 | expected = { |
|
135 | 138 | "msg": "Updated pull request `{}`".format( |
|
136 | 139 | pull_request.pull_request_id), |
|
137 | 140 | "pull_request": response.json['result']['pull_request'], |
|
138 | 141 | "updated_commits": {"added": [], "common": [], "removed": []}, |
|
139 | 142 | "updated_reviewers": {"added": added, "removed": removed}, |
|
140 | 143 | } |
|
141 | 144 | |
|
142 | response_json = response.json['result'] | |
|
143 | assert response_json == expected | |
|
145 | assert_ok(id_, expected, response.body) | |
|
144 | 146 | |
|
145 | 147 | @pytest.mark.backends("git", "hg") |
|
146 | 148 | def test_api_update_bad_user_in_reviewers(self, pr_util): |
|
147 | 149 | pull_request = pr_util.create_pull_request() |
|
148 | 150 | |
|
149 | 151 | id_, params = build_data( |
|
150 | 152 | self.apikey, 'update_pull_request', |
|
151 | 153 | repoid=pull_request.target_repo.repo_name, |
|
152 | 154 | pullrequestid=pull_request.pull_request_id, |
|
153 | 155 | reviewers=['bad_name']) |
|
154 | 156 | response = api_call(self.app, params) |
|
155 | 157 | |
|
156 | 158 | expected = 'user `bad_name` does not exist' |
|
157 | 159 | |
|
158 | response_json = response.json['error'] | |
|
159 | assert response_json == expected | |
|
160 | assert_error(id_, expected, response.body) | |
|
160 | 161 | |
|
161 | 162 | @pytest.mark.backends("git", "hg") |
|
162 | 163 | def test_api_update_repo_error(self, pr_util): |
|
163 | 164 | id_, params = build_data( |
|
164 | 165 | self.apikey, 'update_pull_request', |
|
165 | 166 | repoid='fake', |
|
166 | 167 | pullrequestid='fake', |
|
167 | 168 | reviewers=['bad_name']) |
|
168 | 169 | response = api_call(self.app, params) |
|
169 | 170 | |
|
170 | 171 | expected = 'repository `fake` does not exist' |
|
171 | 172 | |
|
172 | 173 | response_json = response.json['error'] |
|
173 | 174 | assert response_json == expected |
|
174 | 175 | |
|
175 | 176 | @pytest.mark.backends("git", "hg") |
|
176 | 177 | def test_api_update_pull_request_error(self, pr_util): |
|
177 | 178 | pull_request = pr_util.create_pull_request() |
|
178 | 179 | |
|
179 | 180 | id_, params = build_data( |
|
180 | 181 | self.apikey, 'update_pull_request', |
|
181 | 182 | repoid=pull_request.target_repo.repo_name, |
|
182 | 183 | pullrequestid=999999, |
|
183 | 184 | reviewers=['bad_name']) |
|
184 | 185 | response = api_call(self.app, params) |
|
185 | 186 | |
|
186 | 187 | expected = 'pull request `999999` does not exist' |
|
187 | ||
|
188 | response_json = response.json['error'] | |
|
189 | assert response_json == expected | |
|
188 | assert_error(id_, expected, response.body) | |
|
190 | 189 | |
|
191 | 190 | @pytest.mark.backends("git", "hg") |
|
192 | 191 | def test_api_update_pull_request_no_perms_to_update( |
|
193 | 192 | self, user_util, pr_util): |
|
194 | 193 | user = user_util.create_user() |
|
195 | 194 | pull_request = pr_util.create_pull_request() |
|
196 | 195 | |
|
197 | 196 | id_, params = build_data( |
|
198 | 197 | user.api_key, 'update_pull_request', |
|
199 | 198 | repoid=pull_request.target_repo.repo_name, |
|
200 | 199 | pullrequestid=pull_request.pull_request_id,) |
|
201 | 200 | response = api_call(self.app, params) |
|
202 | 201 | |
|
203 | 202 | expected = ('pull request `%s` update failed, ' |
|
204 | 203 | 'no permission to update.') % pull_request.pull_request_id |
|
205 | 204 | |
|
206 | response_json = response.json['error'] | |
|
207 | assert response_json == expected | |
|
205 | assert_error(id_, expected, response.body) |
@@ -1,846 +1,852 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | import textwrap |
|
24 | 24 | |
|
25 | 25 | import rhodecode |
|
26 | 26 | from rhodecode.lib.utils2 import safe_unicode |
|
27 | 27 | from rhodecode.lib.vcs.backends import get_backend |
|
28 | 28 | from rhodecode.lib.vcs.backends.base import ( |
|
29 | 29 | MergeResponse, MergeFailureReason, Reference) |
|
30 | 30 | from rhodecode.lib.vcs.exceptions import RepositoryError |
|
31 | 31 | from rhodecode.lib.vcs.nodes import FileNode |
|
32 | 32 | from rhodecode.model.comment import CommentsModel |
|
33 | 33 | from rhodecode.model.db import PullRequest, Session |
|
34 | 34 | from rhodecode.model.pull_request import PullRequestModel |
|
35 | 35 | from rhodecode.model.user import UserModel |
|
36 | 36 | from rhodecode.tests import TEST_USER_ADMIN_LOGIN |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | pytestmark = [ |
|
40 | 40 | pytest.mark.backends("git", "hg"), |
|
41 | 41 | ] |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | class TestPullRequestModel: |
|
45 | 45 | |
|
46 | 46 | @pytest.fixture |
|
47 | 47 | def pull_request(self, request, backend, pr_util): |
|
48 | 48 | """ |
|
49 | 49 | A pull request combined with multiples patches. |
|
50 | 50 | """ |
|
51 | 51 | BackendClass = get_backend(backend.alias) |
|
52 | 52 | self.merge_patcher = mock.patch.object(BackendClass, 'merge') |
|
53 | 53 | self.workspace_remove_patcher = mock.patch.object( |
|
54 | 54 | BackendClass, 'cleanup_merge_workspace') |
|
55 | 55 | |
|
56 | 56 | self.workspace_remove_mock = self.workspace_remove_patcher.start() |
|
57 | 57 | self.merge_mock = self.merge_patcher.start() |
|
58 | 58 | self.comment_patcher = mock.patch( |
|
59 | 59 | 'rhodecode.model.changeset_status.ChangesetStatusModel.set_status') |
|
60 | 60 | self.comment_patcher.start() |
|
61 | 61 | self.notification_patcher = mock.patch( |
|
62 | 62 | 'rhodecode.model.notification.NotificationModel.create') |
|
63 | 63 | self.notification_patcher.start() |
|
64 | 64 | self.helper_patcher = mock.patch( |
|
65 | 65 | 'rhodecode.lib.helpers.url') |
|
66 | 66 | self.helper_patcher.start() |
|
67 | 67 | |
|
68 | 68 | self.hook_patcher = mock.patch.object(PullRequestModel, |
|
69 | 69 | '_trigger_pull_request_hook') |
|
70 | 70 | self.hook_mock = self.hook_patcher.start() |
|
71 | 71 | |
|
72 | 72 | self.invalidation_patcher = mock.patch( |
|
73 | 73 | 'rhodecode.model.pull_request.ScmModel.mark_for_invalidation') |
|
74 | 74 | self.invalidation_mock = self.invalidation_patcher.start() |
|
75 | 75 | |
|
76 | 76 | self.pull_request = pr_util.create_pull_request( |
|
77 | 77 | mergeable=True, name_suffix=u'Δ Δ') |
|
78 | 78 | self.source_commit = self.pull_request.source_ref_parts.commit_id |
|
79 | 79 | self.target_commit = self.pull_request.target_ref_parts.commit_id |
|
80 | 80 | self.workspace_id = 'pr-%s' % self.pull_request.pull_request_id |
|
81 | 81 | |
|
82 | 82 | @request.addfinalizer |
|
83 | 83 | def cleanup_pull_request(): |
|
84 | 84 | calls = [mock.call( |
|
85 | 85 | self.pull_request, self.pull_request.author, 'create')] |
|
86 | 86 | self.hook_mock.assert_has_calls(calls) |
|
87 | 87 | |
|
88 | 88 | self.workspace_remove_patcher.stop() |
|
89 | 89 | self.merge_patcher.stop() |
|
90 | 90 | self.comment_patcher.stop() |
|
91 | 91 | self.notification_patcher.stop() |
|
92 | 92 | self.helper_patcher.stop() |
|
93 | 93 | self.hook_patcher.stop() |
|
94 | 94 | self.invalidation_patcher.stop() |
|
95 | 95 | |
|
96 | 96 | return self.pull_request |
|
97 | 97 | |
|
98 | 98 | def test_get_all(self, pull_request): |
|
99 | 99 | prs = PullRequestModel().get_all(pull_request.target_repo) |
|
100 | 100 | assert isinstance(prs, list) |
|
101 | 101 | assert len(prs) == 1 |
|
102 | 102 | |
|
103 | 103 | def test_count_all(self, pull_request): |
|
104 | 104 | pr_count = PullRequestModel().count_all(pull_request.target_repo) |
|
105 | 105 | assert pr_count == 1 |
|
106 | 106 | |
|
107 | 107 | def test_get_awaiting_review(self, pull_request): |
|
108 | 108 | prs = PullRequestModel().get_awaiting_review(pull_request.target_repo) |
|
109 | 109 | assert isinstance(prs, list) |
|
110 | 110 | assert len(prs) == 1 |
|
111 | 111 | |
|
112 | 112 | def test_count_awaiting_review(self, pull_request): |
|
113 | 113 | pr_count = PullRequestModel().count_awaiting_review( |
|
114 | 114 | pull_request.target_repo) |
|
115 | 115 | assert pr_count == 1 |
|
116 | 116 | |
|
117 | 117 | def test_get_awaiting_my_review(self, pull_request): |
|
118 | 118 | PullRequestModel().update_reviewers( |
|
119 | 119 | pull_request, [(pull_request.author, ['author'])]) |
|
120 | 120 | prs = PullRequestModel().get_awaiting_my_review( |
|
121 | 121 | pull_request.target_repo, user_id=pull_request.author.user_id) |
|
122 | 122 | assert isinstance(prs, list) |
|
123 | 123 | assert len(prs) == 1 |
|
124 | 124 | |
|
125 | 125 | def test_count_awaiting_my_review(self, pull_request): |
|
126 | 126 | PullRequestModel().update_reviewers( |
|
127 | 127 | pull_request, [(pull_request.author, ['author'])]) |
|
128 | 128 | pr_count = PullRequestModel().count_awaiting_my_review( |
|
129 | 129 | pull_request.target_repo, user_id=pull_request.author.user_id) |
|
130 | 130 | assert pr_count == 1 |
|
131 | 131 | |
|
132 | 132 | def test_delete_calls_cleanup_merge(self, pull_request): |
|
133 | 133 | PullRequestModel().delete(pull_request) |
|
134 | 134 | |
|
135 | 135 | self.workspace_remove_mock.assert_called_once_with( |
|
136 | 136 | self.workspace_id) |
|
137 | 137 | |
|
138 | 138 | def test_close_calls_cleanup_and_hook(self, pull_request): |
|
139 | 139 | PullRequestModel().close_pull_request( |
|
140 | 140 | pull_request, pull_request.author) |
|
141 | 141 | |
|
142 | 142 | self.workspace_remove_mock.assert_called_once_with( |
|
143 | 143 | self.workspace_id) |
|
144 | 144 | self.hook_mock.assert_called_with( |
|
145 | 145 | self.pull_request, self.pull_request.author, 'close') |
|
146 | 146 | |
|
147 | 147 | def test_merge_status(self, pull_request): |
|
148 | 148 | self.merge_mock.return_value = MergeResponse( |
|
149 | 149 | True, False, None, MergeFailureReason.NONE) |
|
150 | 150 | |
|
151 | 151 | assert pull_request._last_merge_source_rev is None |
|
152 | 152 | assert pull_request._last_merge_target_rev is None |
|
153 | 153 | assert pull_request._last_merge_status is None |
|
154 | 154 | |
|
155 | 155 | status, msg = PullRequestModel().merge_status(pull_request) |
|
156 | 156 | assert status is True |
|
157 | 157 | assert msg.eval() == 'This pull request can be automatically merged.' |
|
158 | 158 | self.merge_mock.assert_called_once_with( |
|
159 | 159 | pull_request.target_ref_parts, |
|
160 | 160 | pull_request.source_repo.scm_instance(), |
|
161 | 161 | pull_request.source_ref_parts, self.workspace_id, dry_run=True, |
|
162 | 162 | use_rebase=False) |
|
163 | 163 | |
|
164 | 164 | assert pull_request._last_merge_source_rev == self.source_commit |
|
165 | 165 | assert pull_request._last_merge_target_rev == self.target_commit |
|
166 | 166 | assert pull_request._last_merge_status is MergeFailureReason.NONE |
|
167 | 167 | |
|
168 | 168 | self.merge_mock.reset_mock() |
|
169 | 169 | status, msg = PullRequestModel().merge_status(pull_request) |
|
170 | 170 | assert status is True |
|
171 | 171 | assert msg.eval() == 'This pull request can be automatically merged.' |
|
172 | 172 | assert self.merge_mock.called is False |
|
173 | 173 | |
|
174 | 174 | def test_merge_status_known_failure(self, pull_request): |
|
175 | 175 | self.merge_mock.return_value = MergeResponse( |
|
176 | 176 | False, False, None, MergeFailureReason.MERGE_FAILED) |
|
177 | 177 | |
|
178 | 178 | assert pull_request._last_merge_source_rev is None |
|
179 | 179 | assert pull_request._last_merge_target_rev is None |
|
180 | 180 | assert pull_request._last_merge_status is None |
|
181 | 181 | |
|
182 | 182 | status, msg = PullRequestModel().merge_status(pull_request) |
|
183 | 183 | assert status is False |
|
184 | 184 | assert ( |
|
185 | 185 | msg.eval() == |
|
186 | 186 | 'This pull request cannot be merged because of merge conflicts.') |
|
187 | 187 | self.merge_mock.assert_called_once_with( |
|
188 | 188 | pull_request.target_ref_parts, |
|
189 | 189 | pull_request.source_repo.scm_instance(), |
|
190 | 190 | pull_request.source_ref_parts, self.workspace_id, dry_run=True, |
|
191 | 191 | use_rebase=False) |
|
192 | 192 | |
|
193 | 193 | assert pull_request._last_merge_source_rev == self.source_commit |
|
194 | 194 | assert pull_request._last_merge_target_rev == self.target_commit |
|
195 | 195 | assert ( |
|
196 | 196 | pull_request._last_merge_status is MergeFailureReason.MERGE_FAILED) |
|
197 | 197 | |
|
198 | 198 | self.merge_mock.reset_mock() |
|
199 | 199 | status, msg = PullRequestModel().merge_status(pull_request) |
|
200 | 200 | assert status is False |
|
201 | 201 | assert ( |
|
202 | 202 | msg.eval() == |
|
203 | 203 | 'This pull request cannot be merged because of merge conflicts.') |
|
204 | 204 | assert self.merge_mock.called is False |
|
205 | 205 | |
|
206 | 206 | def test_merge_status_unknown_failure(self, pull_request): |
|
207 | 207 | self.merge_mock.return_value = MergeResponse( |
|
208 | 208 | False, False, None, MergeFailureReason.UNKNOWN) |
|
209 | 209 | |
|
210 | 210 | assert pull_request._last_merge_source_rev is None |
|
211 | 211 | assert pull_request._last_merge_target_rev is None |
|
212 | 212 | assert pull_request._last_merge_status is None |
|
213 | 213 | |
|
214 | 214 | status, msg = PullRequestModel().merge_status(pull_request) |
|
215 | 215 | assert status is False |
|
216 | 216 | assert msg.eval() == ( |
|
217 | 217 | 'This pull request cannot be merged because of an unhandled' |
|
218 | 218 | ' exception.') |
|
219 | 219 | self.merge_mock.assert_called_once_with( |
|
220 | 220 | pull_request.target_ref_parts, |
|
221 | 221 | pull_request.source_repo.scm_instance(), |
|
222 | 222 | pull_request.source_ref_parts, self.workspace_id, dry_run=True, |
|
223 | 223 | use_rebase=False) |
|
224 | 224 | |
|
225 | 225 | assert pull_request._last_merge_source_rev is None |
|
226 | 226 | assert pull_request._last_merge_target_rev is None |
|
227 | 227 | assert pull_request._last_merge_status is None |
|
228 | 228 | |
|
229 | 229 | self.merge_mock.reset_mock() |
|
230 | 230 | status, msg = PullRequestModel().merge_status(pull_request) |
|
231 | 231 | assert status is False |
|
232 | 232 | assert msg.eval() == ( |
|
233 | 233 | 'This pull request cannot be merged because of an unhandled' |
|
234 | 234 | ' exception.') |
|
235 | 235 | assert self.merge_mock.called is True |
|
236 | 236 | |
|
237 | 237 | def test_merge_status_when_target_is_locked(self, pull_request): |
|
238 | 238 | pull_request.target_repo.locked = [1, u'12345.50', 'lock_web'] |
|
239 | 239 | status, msg = PullRequestModel().merge_status(pull_request) |
|
240 | 240 | assert status is False |
|
241 | 241 | assert msg.eval() == ( |
|
242 | 242 | 'This pull request cannot be merged because the target repository' |
|
243 | 243 | ' is locked.') |
|
244 | 244 | |
|
245 | 245 | def test_merge_status_requirements_check_target(self, pull_request): |
|
246 | 246 | |
|
247 | 247 | def has_largefiles(self, repo): |
|
248 | 248 | return repo == pull_request.source_repo |
|
249 | 249 | |
|
250 | 250 | patcher = mock.patch.object( |
|
251 | 251 | PullRequestModel, '_has_largefiles', has_largefiles) |
|
252 | 252 | with patcher: |
|
253 | 253 | status, msg = PullRequestModel().merge_status(pull_request) |
|
254 | 254 | |
|
255 | 255 | assert status is False |
|
256 | 256 | assert msg == 'Target repository large files support is disabled.' |
|
257 | 257 | |
|
258 | 258 | def test_merge_status_requirements_check_source(self, pull_request): |
|
259 | 259 | |
|
260 | 260 | def has_largefiles(self, repo): |
|
261 | 261 | return repo == pull_request.target_repo |
|
262 | 262 | |
|
263 | 263 | patcher = mock.patch.object( |
|
264 | 264 | PullRequestModel, '_has_largefiles', has_largefiles) |
|
265 | 265 | with patcher: |
|
266 | 266 | status, msg = PullRequestModel().merge_status(pull_request) |
|
267 | 267 | |
|
268 | 268 | assert status is False |
|
269 | 269 | assert msg == 'Source repository large files support is disabled.' |
|
270 | 270 | |
|
271 | 271 | def test_merge(self, pull_request, merge_extras): |
|
272 | 272 | user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
273 | 273 | merge_ref = Reference( |
|
274 | 274 | 'type', 'name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6') |
|
275 | 275 | self.merge_mock.return_value = MergeResponse( |
|
276 | 276 | True, True, merge_ref, MergeFailureReason.NONE) |
|
277 | 277 | |
|
278 | 278 | merge_extras['repository'] = pull_request.target_repo.repo_name |
|
279 | 279 | PullRequestModel().merge( |
|
280 | 280 | pull_request, pull_request.author, extras=merge_extras) |
|
281 | 281 | |
|
282 | 282 | message = ( |
|
283 | 283 | u'Merge pull request #{pr_id} from {source_repo} {source_ref_name}' |
|
284 | 284 | u'\n\n {pr_title}'.format( |
|
285 | 285 | pr_id=pull_request.pull_request_id, |
|
286 | 286 | source_repo=safe_unicode( |
|
287 | 287 | pull_request.source_repo.scm_instance().name), |
|
288 | 288 | source_ref_name=pull_request.source_ref_parts.name, |
|
289 | 289 | pr_title=safe_unicode(pull_request.title) |
|
290 | 290 | ) |
|
291 | 291 | ) |
|
292 | 292 | self.merge_mock.assert_called_once_with( |
|
293 | 293 | pull_request.target_ref_parts, |
|
294 | 294 | pull_request.source_repo.scm_instance(), |
|
295 | 295 | pull_request.source_ref_parts, self.workspace_id, |
|
296 | 296 | user_name=user.username, user_email=user.email, message=message, |
|
297 | 297 | use_rebase=False |
|
298 | 298 | ) |
|
299 | 299 | self.invalidation_mock.assert_called_once_with( |
|
300 | 300 | pull_request.target_repo.repo_name) |
|
301 | 301 | |
|
302 | 302 | self.hook_mock.assert_called_with( |
|
303 | 303 | self.pull_request, self.pull_request.author, 'merge') |
|
304 | 304 | |
|
305 | 305 | pull_request = PullRequest.get(pull_request.pull_request_id) |
|
306 | 306 | assert ( |
|
307 | 307 | pull_request.merge_rev == |
|
308 | 308 | '6126b7bfcc82ad2d3deaee22af926b082ce54cc6') |
|
309 | 309 | |
|
310 | 310 | def test_merge_failed(self, pull_request, merge_extras): |
|
311 | 311 | user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
|
312 | 312 | merge_ref = Reference( |
|
313 | 313 | 'type', 'name', '6126b7bfcc82ad2d3deaee22af926b082ce54cc6') |
|
314 | 314 | self.merge_mock.return_value = MergeResponse( |
|
315 | 315 | False, False, merge_ref, MergeFailureReason.MERGE_FAILED) |
|
316 | 316 | |
|
317 | 317 | merge_extras['repository'] = pull_request.target_repo.repo_name |
|
318 | 318 | PullRequestModel().merge( |
|
319 | 319 | pull_request, pull_request.author, extras=merge_extras) |
|
320 | 320 | |
|
321 | 321 | message = ( |
|
322 | 322 | u'Merge pull request #{pr_id} from {source_repo} {source_ref_name}' |
|
323 | 323 | u'\n\n {pr_title}'.format( |
|
324 | 324 | pr_id=pull_request.pull_request_id, |
|
325 | 325 | source_repo=safe_unicode( |
|
326 | 326 | pull_request.source_repo.scm_instance().name), |
|
327 | 327 | source_ref_name=pull_request.source_ref_parts.name, |
|
328 | 328 | pr_title=safe_unicode(pull_request.title) |
|
329 | 329 | ) |
|
330 | 330 | ) |
|
331 | 331 | self.merge_mock.assert_called_once_with( |
|
332 | 332 | pull_request.target_ref_parts, |
|
333 | 333 | pull_request.source_repo.scm_instance(), |
|
334 | 334 | pull_request.source_ref_parts, self.workspace_id, |
|
335 | 335 | user_name=user.username, user_email=user.email, message=message, |
|
336 | 336 | use_rebase=False |
|
337 | 337 | ) |
|
338 | 338 | |
|
339 | 339 | pull_request = PullRequest.get(pull_request.pull_request_id) |
|
340 | 340 | assert self.invalidation_mock.called is False |
|
341 | 341 | assert pull_request.merge_rev is None |
|
342 | 342 | |
|
343 | 343 | def test_get_commit_ids(self, pull_request): |
|
344 | 344 | # The PR has been not merget yet, so expect an exception |
|
345 | 345 | with pytest.raises(ValueError): |
|
346 | 346 | PullRequestModel()._get_commit_ids(pull_request) |
|
347 | 347 | |
|
348 | 348 | # Merge revision is in the revisions list |
|
349 | 349 | pull_request.merge_rev = pull_request.revisions[0] |
|
350 | 350 | commit_ids = PullRequestModel()._get_commit_ids(pull_request) |
|
351 | 351 | assert commit_ids == pull_request.revisions |
|
352 | 352 | |
|
353 | 353 | # Merge revision is not in the revisions list |
|
354 | 354 | pull_request.merge_rev = 'f000' * 10 |
|
355 | 355 | commit_ids = PullRequestModel()._get_commit_ids(pull_request) |
|
356 | 356 | assert commit_ids == pull_request.revisions + [pull_request.merge_rev] |
|
357 | 357 | |
|
358 | 358 | def test_get_diff_from_pr_version(self, pull_request): |
|
359 | source_repo = pull_request.source_repo | |
|
360 | source_ref_id = pull_request.source_ref_parts.commit_id | |
|
361 | target_ref_id = pull_request.target_ref_parts.commit_id | |
|
359 | 362 | diff = PullRequestModel()._get_diff_from_pr_or_version( |
|
360 |
|
|
|
363 | source_repo, source_ref_id, target_ref_id, context=6) | |
|
361 | 364 | assert 'file_1' in diff.raw |
|
362 | 365 | |
|
363 | 366 | def test_generate_title_returns_unicode(self): |
|
364 | 367 | title = PullRequestModel().generate_pullrequest_title( |
|
365 | 368 | source='source-dummy', |
|
366 | 369 | source_ref='source-ref-dummy', |
|
367 | 370 | target='target-dummy', |
|
368 | 371 | ) |
|
369 | 372 | assert type(title) == unicode |
|
370 | 373 | |
|
371 | 374 | |
|
372 | 375 | class TestIntegrationMerge(object): |
|
373 | 376 | @pytest.mark.parametrize('extra_config', ( |
|
374 | 377 | {'vcs.hooks.protocol': 'http', 'vcs.hooks.direct_calls': False}, |
|
375 | 378 | {'vcs.hooks.protocol': 'Pyro4', 'vcs.hooks.direct_calls': False}, |
|
376 | 379 | )) |
|
377 | 380 | def test_merge_triggers_push_hooks( |
|
378 | 381 | self, pr_util, user_admin, capture_rcextensions, merge_extras, |
|
379 | 382 | extra_config): |
|
380 | 383 | pull_request = pr_util.create_pull_request( |
|
381 | 384 | approved=True, mergeable=True) |
|
382 | 385 | # TODO: johbo: Needed for sqlite, try to find an automatic way for it |
|
383 | 386 | merge_extras['repository'] = pull_request.target_repo.repo_name |
|
384 | 387 | Session().commit() |
|
385 | 388 | |
|
386 | 389 | with mock.patch.dict(rhodecode.CONFIG, extra_config, clear=False): |
|
387 | 390 | merge_state = PullRequestModel().merge( |
|
388 | 391 | pull_request, user_admin, extras=merge_extras) |
|
389 | 392 | |
|
390 | 393 | assert merge_state.executed |
|
391 | 394 | assert 'pre_push' in capture_rcextensions |
|
392 | 395 | assert 'post_push' in capture_rcextensions |
|
393 | 396 | |
|
394 | 397 | def test_merge_can_be_rejected_by_pre_push_hook( |
|
395 | 398 | self, pr_util, user_admin, capture_rcextensions, merge_extras): |
|
396 | 399 | pull_request = pr_util.create_pull_request( |
|
397 | 400 | approved=True, mergeable=True) |
|
398 | 401 | # TODO: johbo: Needed for sqlite, try to find an automatic way for it |
|
399 | 402 | merge_extras['repository'] = pull_request.target_repo.repo_name |
|
400 | 403 | Session().commit() |
|
401 | 404 | |
|
402 | 405 | with mock.patch('rhodecode.EXTENSIONS.PRE_PUSH_HOOK') as pre_pull: |
|
403 | 406 | pre_pull.side_effect = RepositoryError("Disallow push!") |
|
404 | 407 | merge_status = PullRequestModel().merge( |
|
405 | 408 | pull_request, user_admin, extras=merge_extras) |
|
406 | 409 | |
|
407 | 410 | assert not merge_status.executed |
|
408 | 411 | assert 'pre_push' not in capture_rcextensions |
|
409 | 412 | assert 'post_push' not in capture_rcextensions |
|
410 | 413 | |
|
411 | 414 | def test_merge_fails_if_target_is_locked( |
|
412 | 415 | self, pr_util, user_regular, merge_extras): |
|
413 | 416 | pull_request = pr_util.create_pull_request( |
|
414 | 417 | approved=True, mergeable=True) |
|
415 | 418 | locked_by = [user_regular.user_id + 1, 12345.50, 'lock_web'] |
|
416 | 419 | pull_request.target_repo.locked = locked_by |
|
417 | 420 | # TODO: johbo: Check if this can work based on the database, currently |
|
418 | 421 | # all data is pre-computed, that's why just updating the DB is not |
|
419 | 422 | # enough. |
|
420 | 423 | merge_extras['locked_by'] = locked_by |
|
421 | 424 | merge_extras['repository'] = pull_request.target_repo.repo_name |
|
422 | 425 | # TODO: johbo: Needed for sqlite, try to find an automatic way for it |
|
423 | 426 | Session().commit() |
|
424 | 427 | merge_status = PullRequestModel().merge( |
|
425 | 428 | pull_request, user_regular, extras=merge_extras) |
|
426 | 429 | assert not merge_status.executed |
|
427 | 430 | |
|
428 | 431 | |
|
429 | 432 | @pytest.mark.parametrize('use_outdated, inlines_count, outdated_count', [ |
|
430 | 433 | (False, 1, 0), |
|
431 | 434 | (True, 0, 1), |
|
432 | 435 | ]) |
|
433 | 436 | def test_outdated_comments( |
|
434 | 437 | pr_util, use_outdated, inlines_count, outdated_count): |
|
435 | 438 | pull_request = pr_util.create_pull_request() |
|
436 | 439 | pr_util.create_inline_comment(file_path='not_in_updated_diff') |
|
437 | 440 | |
|
438 | 441 | with outdated_comments_patcher(use_outdated) as outdated_comment_mock: |
|
439 | 442 | pr_util.add_one_commit() |
|
440 | 443 | assert_inline_comments( |
|
441 | 444 | pull_request, visible=inlines_count, outdated=outdated_count) |
|
442 | 445 | outdated_comment_mock.assert_called_with(pull_request) |
|
443 | 446 | |
|
444 | 447 | |
|
445 | 448 | @pytest.fixture |
|
446 | 449 | def merge_extras(user_regular): |
|
447 | 450 | """ |
|
448 | 451 | Context for the vcs operation when running a merge. |
|
449 | 452 | """ |
|
450 | 453 | extras = { |
|
451 | 454 | 'ip': '127.0.0.1', |
|
452 | 455 | 'username': user_regular.username, |
|
453 | 456 | 'action': 'push', |
|
454 | 457 | 'repository': 'fake_target_repo_name', |
|
455 | 458 | 'scm': 'git', |
|
456 | 459 | 'config': 'fake_config_ini_path', |
|
457 | 460 | 'make_lock': None, |
|
458 | 461 | 'locked_by': [None, None, None], |
|
459 | 462 | 'server_url': 'http://test.example.com:5000', |
|
460 | 463 | 'hooks': ['push', 'pull'], |
|
461 | 464 | 'is_shadow_repo': False, |
|
462 | 465 | } |
|
463 | 466 | return extras |
|
464 | 467 | |
|
465 | 468 | |
|
466 | 469 | class TestUpdateCommentHandling(object): |
|
467 | 470 | |
|
468 | 471 | @pytest.fixture(autouse=True, scope='class') |
|
469 | 472 | def enable_outdated_comments(self, request, pylonsapp): |
|
470 | 473 | config_patch = mock.patch.dict( |
|
471 | 474 | 'rhodecode.CONFIG', {'rhodecode_use_outdated_comments': True}) |
|
472 | 475 | config_patch.start() |
|
473 | 476 | |
|
474 | 477 | @request.addfinalizer |
|
475 | 478 | def cleanup(): |
|
476 | 479 | config_patch.stop() |
|
477 | 480 | |
|
478 | 481 | def test_comment_stays_unflagged_on_unchanged_diff(self, pr_util): |
|
479 | 482 | commits = [ |
|
480 | 483 | {'message': 'a'}, |
|
481 | 484 | {'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]}, |
|
482 | 485 | {'message': 'c', 'added': [FileNode('file_c', 'test_content\n')]}, |
|
483 | 486 | ] |
|
484 | 487 | pull_request = pr_util.create_pull_request( |
|
485 | 488 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
486 | 489 | pr_util.create_inline_comment(file_path='file_b') |
|
487 | 490 | pr_util.add_one_commit(head='c') |
|
488 | 491 | |
|
489 | 492 | assert_inline_comments(pull_request, visible=1, outdated=0) |
|
490 | 493 | |
|
491 | 494 | def test_comment_stays_unflagged_on_change_above(self, pr_util): |
|
492 | 495 | original_content = ''.join( |
|
493 | 496 | ['line {}\n'.format(x) for x in range(1, 11)]) |
|
494 | 497 | updated_content = 'new_line_at_top\n' + original_content |
|
495 | 498 | commits = [ |
|
496 | 499 | {'message': 'a'}, |
|
497 | 500 | {'message': 'b', 'added': [FileNode('file_b', original_content)]}, |
|
498 | 501 | {'message': 'c', 'changed': [FileNode('file_b', updated_content)]}, |
|
499 | 502 | ] |
|
500 | 503 | pull_request = pr_util.create_pull_request( |
|
501 | 504 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
502 | 505 | |
|
503 | 506 | with outdated_comments_patcher(): |
|
504 | 507 | comment = pr_util.create_inline_comment( |
|
505 | 508 | line_no=u'n8', file_path='file_b') |
|
506 | 509 | pr_util.add_one_commit(head='c') |
|
507 | 510 | |
|
508 | 511 | assert_inline_comments(pull_request, visible=1, outdated=0) |
|
509 | 512 | assert comment.line_no == u'n9' |
|
510 | 513 | |
|
511 | 514 | def test_comment_stays_unflagged_on_change_below(self, pr_util): |
|
512 | 515 | original_content = ''.join(['line {}\n'.format(x) for x in range(10)]) |
|
513 | 516 | updated_content = original_content + 'new_line_at_end\n' |
|
514 | 517 | commits = [ |
|
515 | 518 | {'message': 'a'}, |
|
516 | 519 | {'message': 'b', 'added': [FileNode('file_b', original_content)]}, |
|
517 | 520 | {'message': 'c', 'changed': [FileNode('file_b', updated_content)]}, |
|
518 | 521 | ] |
|
519 | 522 | pull_request = pr_util.create_pull_request( |
|
520 | 523 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
521 | 524 | pr_util.create_inline_comment(file_path='file_b') |
|
522 | 525 | pr_util.add_one_commit(head='c') |
|
523 | 526 | |
|
524 | 527 | assert_inline_comments(pull_request, visible=1, outdated=0) |
|
525 | 528 | |
|
526 | 529 | @pytest.mark.parametrize('line_no', ['n4', 'o4', 'n10', 'o9']) |
|
527 | 530 | def test_comment_flagged_on_change_around_context(self, pr_util, line_no): |
|
528 | 531 | base_lines = ['line {}\n'.format(x) for x in range(1, 13)] |
|
529 | 532 | change_lines = list(base_lines) |
|
530 | 533 | change_lines.insert(6, 'line 6a added\n') |
|
531 | 534 | |
|
532 | 535 | # Changes on the last line of sight |
|
533 | 536 | update_lines = list(change_lines) |
|
534 | 537 | update_lines[0] = 'line 1 changed\n' |
|
535 | 538 | update_lines[-1] = 'line 12 changed\n' |
|
536 | 539 | |
|
537 | 540 | def file_b(lines): |
|
538 | 541 | return FileNode('file_b', ''.join(lines)) |
|
539 | 542 | |
|
540 | 543 | commits = [ |
|
541 | 544 | {'message': 'a', 'added': [file_b(base_lines)]}, |
|
542 | 545 | {'message': 'b', 'changed': [file_b(change_lines)]}, |
|
543 | 546 | {'message': 'c', 'changed': [file_b(update_lines)]}, |
|
544 | 547 | ] |
|
545 | 548 | |
|
546 | 549 | pull_request = pr_util.create_pull_request( |
|
547 | 550 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
548 | 551 | pr_util.create_inline_comment(line_no=line_no, file_path='file_b') |
|
549 | 552 | |
|
550 | 553 | with outdated_comments_patcher(): |
|
551 | 554 | pr_util.add_one_commit(head='c') |
|
552 | 555 | assert_inline_comments(pull_request, visible=0, outdated=1) |
|
553 | 556 | |
|
554 | 557 | @pytest.mark.parametrize("change, content", [ |
|
555 | 558 | ('changed', 'changed\n'), |
|
556 | 559 | ('removed', ''), |
|
557 | 560 | ], ids=['changed', 'removed']) |
|
558 | 561 | def test_comment_flagged_on_change(self, pr_util, change, content): |
|
559 | 562 | commits = [ |
|
560 | 563 | {'message': 'a'}, |
|
561 | 564 | {'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]}, |
|
562 | 565 | {'message': 'c', change: [FileNode('file_b', content)]}, |
|
563 | 566 | ] |
|
564 | 567 | pull_request = pr_util.create_pull_request( |
|
565 | 568 | commits=commits, target_head='a', source_head='b', revisions=['b']) |
|
566 | 569 | pr_util.create_inline_comment(file_path='file_b') |
|
567 | 570 | |
|
568 | 571 | with outdated_comments_patcher(): |
|
569 | 572 | pr_util.add_one_commit(head='c') |
|
570 | 573 | assert_inline_comments(pull_request, visible=0, outdated=1) |
|
571 | 574 | |
|
572 | 575 | |
|
573 | 576 | class TestUpdateChangedFiles(object): |
|
574 | 577 | |
|
575 | 578 | def test_no_changes_on_unchanged_diff(self, pr_util): |
|
576 | 579 | commits = [ |
|
577 | 580 | {'message': 'a'}, |
|
578 | 581 | {'message': 'b', |
|
579 | 582 | 'added': [FileNode('file_b', 'test_content b\n')]}, |
|
580 | 583 | {'message': 'c', |
|
581 | 584 | 'added': [FileNode('file_c', 'test_content c\n')]}, |
|
582 | 585 | ] |
|
583 | 586 | # open a PR from a to b, adding file_b |
|
584 | 587 | pull_request = pr_util.create_pull_request( |
|
585 | 588 | commits=commits, target_head='a', source_head='b', revisions=['b'], |
|
586 | 589 | name_suffix='per-file-review') |
|
587 | 590 | |
|
588 | 591 | # modify PR adding new file file_c |
|
589 | 592 | pr_util.add_one_commit(head='c') |
|
590 | 593 | |
|
591 | 594 | assert_pr_file_changes( |
|
592 | 595 | pull_request, |
|
593 | 596 | added=['file_c'], |
|
594 | 597 | modified=[], |
|
595 | 598 | removed=[]) |
|
596 | 599 | |
|
597 | 600 | def test_modify_and_undo_modification_diff(self, pr_util): |
|
598 | 601 | commits = [ |
|
599 | 602 | {'message': 'a'}, |
|
600 | 603 | {'message': 'b', |
|
601 | 604 | 'added': [FileNode('file_b', 'test_content b\n')]}, |
|
602 | 605 | {'message': 'c', |
|
603 | 606 | 'changed': [FileNode('file_b', 'test_content b modified\n')]}, |
|
604 | 607 | {'message': 'd', |
|
605 | 608 | 'changed': [FileNode('file_b', 'test_content b\n')]}, |
|
606 | 609 | ] |
|
607 | 610 | # open a PR from a to b, adding file_b |
|
608 | 611 | pull_request = pr_util.create_pull_request( |
|
609 | 612 | commits=commits, target_head='a', source_head='b', revisions=['b'], |
|
610 | 613 | name_suffix='per-file-review') |
|
611 | 614 | |
|
612 | 615 | # modify PR modifying file file_b |
|
613 | 616 | pr_util.add_one_commit(head='c') |
|
614 | 617 | |
|
615 | 618 | assert_pr_file_changes( |
|
616 | 619 | pull_request, |
|
617 | 620 | added=[], |
|
618 | 621 | modified=['file_b'], |
|
619 | 622 | removed=[]) |
|
620 | 623 | |
|
621 | 624 | # move the head again to d, which rollbacks change, |
|
622 | 625 | # meaning we should indicate no changes |
|
623 | 626 | pr_util.add_one_commit(head='d') |
|
624 | 627 | |
|
625 | 628 | assert_pr_file_changes( |
|
626 | 629 | pull_request, |
|
627 | 630 | added=[], |
|
628 | 631 | modified=[], |
|
629 | 632 | removed=[]) |
|
630 | 633 | |
|
631 | 634 | def test_updated_all_files_in_pr(self, pr_util): |
|
632 | 635 | commits = [ |
|
633 | 636 | {'message': 'a'}, |
|
634 | 637 | {'message': 'b', 'added': [ |
|
635 | 638 | FileNode('file_a', 'test_content a\n'), |
|
636 | 639 | FileNode('file_b', 'test_content b\n'), |
|
637 | 640 | FileNode('file_c', 'test_content c\n')]}, |
|
638 | 641 | {'message': 'c', 'changed': [ |
|
639 | 642 | FileNode('file_a', 'test_content a changed\n'), |
|
640 | 643 | FileNode('file_b', 'test_content b changed\n'), |
|
641 | 644 | FileNode('file_c', 'test_content c changed\n')]}, |
|
642 | 645 | ] |
|
643 | 646 | # open a PR from a to b, changing 3 files |
|
644 | 647 | pull_request = pr_util.create_pull_request( |
|
645 | 648 | commits=commits, target_head='a', source_head='b', revisions=['b'], |
|
646 | 649 | name_suffix='per-file-review') |
|
647 | 650 | |
|
648 | 651 | pr_util.add_one_commit(head='c') |
|
649 | 652 | |
|
650 | 653 | assert_pr_file_changes( |
|
651 | 654 | pull_request, |
|
652 | 655 | added=[], |
|
653 | 656 | modified=['file_a', 'file_b', 'file_c'], |
|
654 | 657 | removed=[]) |
|
655 | 658 | |
|
656 | 659 | def test_updated_and_removed_all_files_in_pr(self, pr_util): |
|
657 | 660 | commits = [ |
|
658 | 661 | {'message': 'a'}, |
|
659 | 662 | {'message': 'b', 'added': [ |
|
660 | 663 | FileNode('file_a', 'test_content a\n'), |
|
661 | 664 | FileNode('file_b', 'test_content b\n'), |
|
662 | 665 | FileNode('file_c', 'test_content c\n')]}, |
|
663 | 666 | {'message': 'c', 'removed': [ |
|
664 | 667 | FileNode('file_a', 'test_content a changed\n'), |
|
665 | 668 | FileNode('file_b', 'test_content b changed\n'), |
|
666 | 669 | FileNode('file_c', 'test_content c changed\n')]}, |
|
667 | 670 | ] |
|
668 | 671 | # open a PR from a to b, removing 3 files |
|
669 | 672 | pull_request = pr_util.create_pull_request( |
|
670 | 673 | commits=commits, target_head='a', source_head='b', revisions=['b'], |
|
671 | 674 | name_suffix='per-file-review') |
|
672 | 675 | |
|
673 | 676 | pr_util.add_one_commit(head='c') |
|
674 | 677 | |
|
675 | 678 | assert_pr_file_changes( |
|
676 | 679 | pull_request, |
|
677 | 680 | added=[], |
|
678 | 681 | modified=[], |
|
679 | 682 | removed=['file_a', 'file_b', 'file_c']) |
|
680 | 683 | |
|
681 | 684 | |
|
682 | 685 | def test_update_writes_snapshot_into_pull_request_version(pr_util): |
|
683 | 686 | model = PullRequestModel() |
|
684 | 687 | pull_request = pr_util.create_pull_request() |
|
685 | 688 | pr_util.update_source_repository() |
|
686 | 689 | |
|
687 | 690 | model.update_commits(pull_request) |
|
688 | 691 | |
|
689 | 692 | # Expect that it has a version entry now |
|
690 | 693 | assert len(model.get_versions(pull_request)) == 1 |
|
691 | 694 | |
|
692 | 695 | |
|
693 | 696 | def test_update_skips_new_version_if_unchanged(pr_util): |
|
694 | 697 | pull_request = pr_util.create_pull_request() |
|
695 | 698 | model = PullRequestModel() |
|
696 | 699 | model.update_commits(pull_request) |
|
697 | 700 | |
|
698 | 701 | # Expect that it still has no versions |
|
699 | 702 | assert len(model.get_versions(pull_request)) == 0 |
|
700 | 703 | |
|
701 | 704 | |
|
702 | 705 | def test_update_assigns_comments_to_the_new_version(pr_util): |
|
703 | 706 | model = PullRequestModel() |
|
704 | 707 | pull_request = pr_util.create_pull_request() |
|
705 | 708 | comment = pr_util.create_comment() |
|
706 | 709 | pr_util.update_source_repository() |
|
707 | 710 | |
|
708 | 711 | model.update_commits(pull_request) |
|
709 | 712 | |
|
710 | 713 | # Expect that the comment is linked to the pr version now |
|
711 | 714 | assert comment.pull_request_version == model.get_versions(pull_request)[0] |
|
712 | 715 | |
|
713 | 716 | |
|
714 | 717 | def test_update_adds_a_comment_to_the_pull_request_about_the_change(pr_util): |
|
715 | 718 | model = PullRequestModel() |
|
716 | 719 | pull_request = pr_util.create_pull_request() |
|
717 | 720 | pr_util.update_source_repository() |
|
718 | 721 | pr_util.update_source_repository() |
|
719 | 722 | |
|
720 | 723 | model.update_commits(pull_request) |
|
721 | 724 | |
|
722 | 725 | # Expect to find a new comment about the change |
|
723 | 726 | expected_message = textwrap.dedent( |
|
724 | 727 | """\ |
|
725 | 728 | Pull request updated. Auto status change to |under_review| |
|
726 | 729 | |
|
727 | 730 | .. role:: added |
|
728 | 731 | .. role:: removed |
|
729 | 732 | .. parsed-literal:: |
|
730 | 733 | |
|
731 | 734 | Changed commits: |
|
732 | 735 | * :added:`1 added` |
|
733 | 736 | * :removed:`0 removed` |
|
734 | 737 | |
|
735 | 738 | Changed files: |
|
736 | 739 | * `A file_2 <#a_c--92ed3b5f07b4>`_ |
|
737 | 740 | |
|
738 | 741 | .. |under_review| replace:: *"Under Review"*""" |
|
739 | 742 | ) |
|
740 | 743 | pull_request_comments = sorted( |
|
741 | 744 | pull_request.comments, key=lambda c: c.modified_at) |
|
742 | 745 | update_comment = pull_request_comments[-1] |
|
743 | 746 | assert update_comment.text == expected_message |
|
744 | 747 | |
|
745 | 748 | |
|
746 | 749 | def test_create_version_from_snapshot_updates_attributes(pr_util): |
|
747 | 750 | pull_request = pr_util.create_pull_request() |
|
748 | 751 | |
|
749 | 752 | # Avoiding default values |
|
750 | 753 | pull_request.status = PullRequest.STATUS_CLOSED |
|
751 | 754 | pull_request._last_merge_source_rev = "0" * 40 |
|
752 | 755 | pull_request._last_merge_target_rev = "1" * 40 |
|
753 | 756 | pull_request._last_merge_status = 1 |
|
754 | 757 | pull_request.merge_rev = "2" * 40 |
|
755 | 758 | |
|
756 | 759 | # Remember automatic values |
|
757 | 760 | created_on = pull_request.created_on |
|
758 | 761 | updated_on = pull_request.updated_on |
|
759 | 762 | |
|
760 | 763 | # Create a new version of the pull request |
|
761 | 764 | version = PullRequestModel()._create_version_from_snapshot(pull_request) |
|
762 | 765 | |
|
763 | 766 | # Check attributes |
|
764 | 767 | assert version.title == pr_util.create_parameters['title'] |
|
765 | 768 | assert version.description == pr_util.create_parameters['description'] |
|
766 | 769 | assert version.status == PullRequest.STATUS_CLOSED |
|
767 | 770 | |
|
768 | 771 | # versions get updated created_on |
|
769 | 772 | assert version.created_on != created_on |
|
770 | 773 | |
|
771 | 774 | assert version.updated_on == updated_on |
|
772 | 775 | assert version.user_id == pull_request.user_id |
|
773 | 776 | assert version.revisions == pr_util.create_parameters['revisions'] |
|
774 | 777 | assert version.source_repo == pr_util.source_repository |
|
775 | 778 | assert version.source_ref == pr_util.create_parameters['source_ref'] |
|
776 | 779 | assert version.target_repo == pr_util.target_repository |
|
777 | 780 | assert version.target_ref == pr_util.create_parameters['target_ref'] |
|
778 | 781 | assert version._last_merge_source_rev == pull_request._last_merge_source_rev |
|
779 | 782 | assert version._last_merge_target_rev == pull_request._last_merge_target_rev |
|
780 | 783 | assert version._last_merge_status == pull_request._last_merge_status |
|
781 | 784 | assert version.merge_rev == pull_request.merge_rev |
|
782 | 785 | assert version.pull_request == pull_request |
|
783 | 786 | |
|
784 | 787 | |
|
785 | 788 | def test_link_comments_to_version_only_updates_unlinked_comments(pr_util): |
|
786 | 789 | version1 = pr_util.create_version_of_pull_request() |
|
787 | 790 | comment_linked = pr_util.create_comment(linked_to=version1) |
|
788 | 791 | comment_unlinked = pr_util.create_comment() |
|
789 | 792 | version2 = pr_util.create_version_of_pull_request() |
|
790 | 793 | |
|
791 | 794 | PullRequestModel()._link_comments_to_version(version2) |
|
792 | 795 | |
|
793 | 796 | # Expect that only the new comment is linked to version2 |
|
794 | 797 | assert ( |
|
795 | 798 | comment_unlinked.pull_request_version_id == |
|
796 | 799 | version2.pull_request_version_id) |
|
797 | 800 | assert ( |
|
798 | 801 | comment_linked.pull_request_version_id == |
|
799 | 802 | version1.pull_request_version_id) |
|
800 | 803 | assert ( |
|
801 | 804 | comment_unlinked.pull_request_version_id != |
|
802 | 805 | comment_linked.pull_request_version_id) |
|
803 | 806 | |
|
804 | 807 | |
|
805 | 808 | def test_calculate_commits(): |
|
806 | change = PullRequestModel()._calculate_commit_id_changes( | |
|
807 | set([1, 2, 3]), set([1, 3, 4, 5])) | |
|
808 | assert (set([4, 5]), set([1, 3]), set([2])) == ( | |
|
809 | change.added, change.common, change.removed) | |
|
809 | old_ids = [1, 2, 3] | |
|
810 | new_ids = [1, 3, 4, 5] | |
|
811 | change = PullRequestModel()._calculate_commit_id_changes(old_ids, new_ids) | |
|
812 | assert change.added == [4, 5] | |
|
813 | assert change.common == [1, 3] | |
|
814 | assert change.removed == [2] | |
|
815 | assert change.total == [1, 3, 4, 5] | |
|
810 | 816 | |
|
811 | 817 | |
|
812 | 818 | def assert_inline_comments(pull_request, visible=None, outdated=None): |
|
813 | 819 | if visible is not None: |
|
814 | 820 | inline_comments = CommentsModel().get_inline_comments( |
|
815 | 821 | pull_request.target_repo.repo_id, pull_request=pull_request) |
|
816 | 822 | inline_cnt = CommentsModel().get_inline_comments_count( |
|
817 | 823 | inline_comments) |
|
818 | 824 | assert inline_cnt == visible |
|
819 | 825 | if outdated is not None: |
|
820 | 826 | outdated_comments = CommentsModel().get_outdated_comments( |
|
821 | 827 | pull_request.target_repo.repo_id, pull_request) |
|
822 | 828 | assert len(outdated_comments) == outdated |
|
823 | 829 | |
|
824 | 830 | |
|
825 | 831 | def assert_pr_file_changes( |
|
826 | 832 | pull_request, added=None, modified=None, removed=None): |
|
827 | 833 | pr_versions = PullRequestModel().get_versions(pull_request) |
|
828 | 834 | # always use first version, ie original PR to calculate changes |
|
829 | 835 | pull_request_version = pr_versions[0] |
|
830 | 836 | old_diff_data, new_diff_data = PullRequestModel()._generate_update_diffs( |
|
831 | 837 | pull_request, pull_request_version) |
|
832 | 838 | file_changes = PullRequestModel()._calculate_file_changes( |
|
833 | 839 | old_diff_data, new_diff_data) |
|
834 | 840 | |
|
835 | 841 | assert added == file_changes.added, \ |
|
836 | 842 | 'expected added:%s vs value:%s' % (added, file_changes.added) |
|
837 | 843 | assert modified == file_changes.modified, \ |
|
838 | 844 | 'expected modified:%s vs value:%s' % (modified, file_changes.modified) |
|
839 | 845 | assert removed == file_changes.removed, \ |
|
840 | 846 | 'expected removed:%s vs value:%s' % (removed, file_changes.removed) |
|
841 | 847 | |
|
842 | 848 | |
|
843 | 849 | def outdated_comments_patcher(use_outdated=True): |
|
844 | 850 | return mock.patch.object( |
|
845 | 851 | CommentsModel, 'use_outdated_comments', |
|
846 | 852 | return_value=use_outdated) |
@@ -1,66 +1,69 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import pytest |
|
22 | 22 | |
|
23 | 23 | from rhodecode.lib.vcs.nodes import FileNode |
|
24 | 24 | from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError |
|
25 | 25 | from rhodecode.model.pull_request import PullRequestModel |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | @pytest.mark.backends('git') |
|
29 | 29 | class TestGetDiffForPrOrVersion(object): |
|
30 | 30 | |
|
31 | 31 | def test_works_for_missing_git_references(self, pr_util): |
|
32 | 32 | pull_request = self._prepare_pull_request(pr_util) |
|
33 | 33 | removed_commit_id = pr_util.remove_one_commit() |
|
34 | 34 | self.assert_commit_cannot_be_accessed(removed_commit_id, pull_request) |
|
35 | 35 | |
|
36 | 36 | self.assert_diff_can_be_fetched(pull_request) |
|
37 | 37 | |
|
38 | 38 | def test_works_for_missing_git_references_during_update(self, pr_util): |
|
39 | 39 | pull_request = self._prepare_pull_request(pr_util) |
|
40 | 40 | removed_commit_id = pr_util.remove_one_commit() |
|
41 | 41 | self.assert_commit_cannot_be_accessed(removed_commit_id, pull_request) |
|
42 | 42 | |
|
43 | 43 | pr_version = PullRequestModel().get_versions(pull_request)[0] |
|
44 | 44 | self.assert_diff_can_be_fetched(pr_version) |
|
45 | 45 | |
|
46 | 46 | def _prepare_pull_request(self, pr_util): |
|
47 | 47 | commits = [ |
|
48 | 48 | {'message': 'a'}, |
|
49 | 49 | {'message': 'b', 'added': [FileNode('file_b', 'test_content\n')]}, |
|
50 | 50 | {'message': 'c', 'added': [FileNode('file_c', 'test_content\n')]}, |
|
51 | 51 | ] |
|
52 | 52 | pull_request = pr_util.create_pull_request( |
|
53 | 53 | commits=commits, target_head='a', source_head='c', |
|
54 | 54 | revisions=['b', 'c']) |
|
55 | 55 | return pull_request |
|
56 | 56 | |
|
57 | 57 | def assert_diff_can_be_fetched(self, pr_or_version): |
|
58 | source_repo = pr_or_version.source_repo | |
|
59 | source_ref_id = pr_or_version.source_ref_parts.commit_id | |
|
60 | target_ref_id = pr_or_version.target_ref_parts.commit_id | |
|
58 | 61 | diff = PullRequestModel()._get_diff_from_pr_or_version( |
|
59 |
|
|
|
62 | source_repo, source_ref_id, target_ref_id, context=6) | |
|
60 | 63 | assert 'file_b' in diff.raw |
|
61 | 64 | |
|
62 | 65 | def assert_commit_cannot_be_accessed( |
|
63 | 66 | self, removed_commit_id, pull_request): |
|
64 | 67 | source_vcs = pull_request.source_repo.scm_instance() |
|
65 | 68 | with pytest.raises(CommitDoesNotExistError): |
|
66 | 69 | source_vcs.get_commit(commit_id=removed_commit_id) |
General Comments 0
You need to be logged in to leave comments.
Login now