##// END OF EJS Templates
comments: added new events for comment editing to handle them in integrations.
marcink -
r4444:912182ba default
parent child Browse files
Show More
@@ -48,6 +48,7 b' from rhodecode.model.db import ('
48 48 Session, ChangesetStatus, RepositoryField, Repository, RepoGroup,
49 49 ChangesetComment)
50 50 from rhodecode.model.permission import PermissionModel
51 from rhodecode.model.pull_request import PullRequestModel
51 52 from rhodecode.model.repo import RepoModel
52 53 from rhodecode.model.scm import ScmModel, RepoList
53 54 from rhodecode.model.settings import SettingsModel, VcsSettingsModel
@@ -1869,6 +1870,20 b' def edit_comment(request, apiuser, messa'
1869 1870 raise JSONRPCError(
1870 1871 "comment ({}) can't be changed with empty string".format(comment_id)
1871 1872 )
1873
1874 if comment.pull_request:
1875 pull_request = comment.pull_request
1876 PullRequestModel().trigger_pull_request_hook(
1877 pull_request, apiuser, 'comment_edit',
1878 data={'comment': comment})
1879 else:
1880 db_repo = comment.repo
1881 commit_id = comment.revision
1882 commit = db_repo.get_commit(commit_id)
1883 CommentsModel().trigger_commit_comment_hook(
1884 db_repo, apiuser, 'edit',
1885 data={'comment': comment, 'commit': commit})
1886
1872 1887 data = {
1873 1888 'comment': comment,
1874 1889 'version': comment_history.version if comment_history else None,
@@ -649,6 +649,12 b' class RepoCommitsView(RepoAppView):'
649 649 if not comment_history:
650 650 raise HTTPNotFound()
651 651
652 commit_id = self.request.matchdict['commit_id']
653 commit = self.db_repo.get_commit(commit_id)
654 CommentsModel().trigger_commit_comment_hook(
655 self.db_repo, self._rhodecode_user, 'edit',
656 data={'comment': comment, 'commit': commit})
657
652 658 Session().commit()
653 659 return {
654 660 'comment_history_id': comment_history.comment_history_id,
@@ -1611,6 +1611,11 b' class RepoPullRequestsView(RepoAppView, '
1611 1611 raise HTTPNotFound()
1612 1612
1613 1613 Session().commit()
1614
1615 PullRequestModel().trigger_pull_request_hook(
1616 pull_request, self._rhodecode_user, 'comment_edit',
1617 data={'comment': comment})
1618
1614 1619 return {
1615 1620 'comment_history_id': comment_history.comment_history_id,
1616 1621 'comment_id': comment.comment_id,
@@ -53,7 +53,8 b' from rhodecode.events.user import ( # p'
53 53 )
54 54
55 55 from rhodecode.events.repo import ( # pragma: no cover
56 RepoEvent, RepoCommitCommentEvent,
56 RepoEvent,
57 RepoCommitCommentEvent, RepoCommitCommentEditEvent,
57 58 RepoPreCreateEvent, RepoCreateEvent,
58 59 RepoPreDeleteEvent, RepoDeleteEvent,
59 60 RepoPrePushEvent, RepoPushEvent,
@@ -72,8 +73,8 b' from rhodecode.events.pullrequest import'
72 73 PullRequestCreateEvent,
73 74 PullRequestUpdateEvent,
74 75 PullRequestCommentEvent,
76 PullRequestCommentEditEvent,
75 77 PullRequestReviewEvent,
76 78 PullRequestMergeEvent,
77 79 PullRequestCloseEvent,
78 PullRequestCommentEvent,
79 80 )
@@ -19,8 +19,7 b''
19 19 import logging
20 20
21 21 from rhodecode.translation import lazy_ugettext
22 from rhodecode.events.repo import (
23 RepoEvent, _commits_as_dict, _issues_as_dict)
22 from rhodecode.events.repo import (RepoEvent, _commits_as_dict, _issues_as_dict)
24 23
25 24 log = logging.getLogger(__name__)
26 25
@@ -155,6 +154,7 b' class PullRequestCommentEvent(PullReques'
155 154 'type': self.comment.comment_type,
156 155 'file': self.comment.f_path,
157 156 'line': self.comment.line_no,
157 'version': self.comment.last_version,
158 158 'url': CommentsModel().get_url(
159 159 self.comment, request=self.request),
160 160 'permalink_url': CommentsModel().get_url(
@@ -162,3 +162,42 b' class PullRequestCommentEvent(PullReques'
162 162 }
163 163 })
164 164 return data
165
166
167 class PullRequestCommentEditEvent(PullRequestEvent):
168 """
169 An instance of this class is emitted as an :term:`event` after a pull
170 request comment is edited.
171 """
172 name = 'pullrequest-comment-edit'
173 display_name = lazy_ugettext('pullrequest comment edited')
174 description = lazy_ugettext('Event triggered after a comment was edited on a code '
175 'in the pull request')
176
177 def __init__(self, pullrequest, comment):
178 super(PullRequestCommentEditEvent, self).__init__(pullrequest)
179 self.comment = comment
180
181 def as_dict(self):
182 from rhodecode.model.comment import CommentsModel
183 data = super(PullRequestCommentEditEvent, self).as_dict()
184
185 status = None
186 if self.comment.status_change:
187 status = self.comment.status_change[0].status
188
189 data.update({
190 'comment': {
191 'status': status,
192 'text': self.comment.text,
193 'type': self.comment.comment_type,
194 'file': self.comment.f_path,
195 'line': self.comment.line_no,
196 'version': self.comment.last_version,
197 'url': CommentsModel().get_url(
198 self.comment, request=self.request),
199 'permalink_url': CommentsModel().get_url(
200 self.comment, request=self.request, permalink=True),
201 }
202 })
203 return data
@@ -211,6 +211,42 b' class RepoCommitCommentEvent(RepoEvent):'
211 211 'comment_type': self.comment.comment_type,
212 212 'comment_f_path': self.comment.f_path,
213 213 'comment_line_no': self.comment.line_no,
214 'comment_version': self.comment.last_version,
215 }
216 return data
217
218
219 class RepoCommitCommentEditEvent(RepoEvent):
220 """
221 An instance of this class is emitted as an :term:`event` after a comment is edited
222 on repository commit.
223 """
224
225 name = 'repo-commit-edit-comment'
226 display_name = lazy_ugettext('repository commit edit comment')
227 description = lazy_ugettext('Event triggered after a comment was edited '
228 'on commit inside a repository')
229
230 def __init__(self, repo, commit, comment):
231 super(RepoCommitCommentEditEvent, self).__init__(repo)
232 self.commit = commit
233 self.comment = comment
234
235 def as_dict(self):
236 data = super(RepoCommitCommentEditEvent, self).as_dict()
237 data['commit'] = {
238 'commit_id': self.commit.raw_id,
239 'commit_message': self.commit.message,
240 'commit_branch': self.commit.branch,
241 }
242
243 data['comment'] = {
244 'comment_id': self.comment.comment_id,
245 'comment_text': self.comment.text,
246 'comment_type': self.comment.comment_type,
247 'comment_f_path': self.comment.f_path,
248 'comment_line_no': self.comment.line_no,
249 'comment_version': self.comment.last_version,
214 250 }
215 251 return data
216 252
@@ -331,6 +331,26 b' class WebhookDataHandler(CommitParsingDa'
331 331
332 332 return [(url, self.headers, data)]
333 333
334 def repo_commit_comment_edit_handler(self, event, data):
335 url = self.get_base_parsed_template(data)
336 log.debug('register %s call(%s) to url %s', self.name, event, url)
337 comment_vars = [
338 ('commit_comment_id', data['comment']['comment_id']),
339 ('commit_comment_text', data['comment']['comment_text']),
340 ('commit_comment_type', data['comment']['comment_type']),
341
342 ('commit_comment_f_path', data['comment']['comment_f_path']),
343 ('commit_comment_line_no', data['comment']['comment_line_no']),
344
345 ('commit_comment_commit_id', data['commit']['commit_id']),
346 ('commit_comment_commit_branch', data['commit']['commit_branch']),
347 ('commit_comment_commit_message', data['commit']['commit_message']),
348 ]
349 for k, v in comment_vars:
350 url = UrlTmpl(url).safe_substitute(**{k: v})
351
352 return [(url, self.headers, data)]
353
334 354 def repo_create_event_handler(self, event, data):
335 355 url = self.get_base_parsed_template(data)
336 356 log.debug('register %s call(%s) to url %s', self.name, event, url)
@@ -360,6 +380,8 b' class WebhookDataHandler(CommitParsingDa'
360 380 return self.repo_create_event_handler(event, data)
361 381 elif isinstance(event, events.RepoCommitCommentEvent):
362 382 return self.repo_commit_comment_handler(event, data)
383 elif isinstance(event, events.RepoCommitCommentEditEvent):
384 return self.repo_commit_comment_edit_handler(event, data)
363 385 elif isinstance(event, events.PullRequestEvent):
364 386 return self.pull_request_event_handler(event, data)
365 387 else:
@@ -133,6 +133,8 b' class HipchatIntegrationType(Integration'
133 133
134 134 if isinstance(event, events.PullRequestCommentEvent):
135 135 text = self.format_pull_request_comment_event(event, data)
136 elif isinstance(event, events.PullRequestCommentEditEvent):
137 text = self.format_pull_request_comment_event(event, data)
136 138 elif isinstance(event, events.PullRequestReviewEvent):
137 139 text = self.format_pull_request_review_event(event, data)
138 140 elif isinstance(event, events.PullRequestEvent):
@@ -157,6 +157,9 b' class SlackIntegrationType(IntegrationTy'
157 157 if isinstance(event, events.PullRequestCommentEvent):
158 158 (title, text, fields, overrides) \
159 159 = self.format_pull_request_comment_event(event, data)
160 elif isinstance(event, events.PullRequestCommentEditEvent):
161 (title, text, fields, overrides) \
162 = self.format_pull_request_comment_event(event, data)
160 163 elif isinstance(event, events.PullRequestReviewEvent):
161 164 title, text = self.format_pull_request_review_event(event, data)
162 165 elif isinstance(event, events.PullRequestEvent):
@@ -144,11 +144,13 b' class WebhookIntegrationType(Integration'
144 144 events.PullRequestMergeEvent,
145 145 events.PullRequestUpdateEvent,
146 146 events.PullRequestCommentEvent,
147 events.PullRequestCommentEditEvent,
147 148 events.PullRequestReviewEvent,
148 149 events.PullRequestCreateEvent,
149 150 events.RepoPushEvent,
150 151 events.RepoCreateEvent,
151 152 events.RepoCommitCommentEvent,
153 events.RepoCommitCommentEditEvent,
152 154 ]
153 155
154 156 def settings_schema(self):
@@ -97,6 +97,34 b' def trigger_comment_commit_hooks(usernam'
97 97 hooks_base.log_comment_commit_repository(**extras)
98 98
99 99
100 def trigger_comment_commit_edit_hooks(username, repo_name, repo_type, repo, data=None):
101 """
102 Triggers when a comment is edited on a commit
103
104 :param username: username who edits the comment
105 :param repo_name: name of target repo
106 :param repo_type: the type of SCM target repo
107 :param repo: the repo object we trigger the event for
108 :param data: extra data for specific events e.g {'comment': comment_obj, 'commit': commit_obj}
109 """
110 if not _supports_repo_type(repo_type):
111 return
112
113 extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_commit')
114
115 comment = data['comment']
116 commit = data['commit']
117
118 events.trigger(events.RepoCommitCommentEditEvent(repo, commit, comment))
119 extras.update(repo.get_dict())
120
121 extras.commit = commit.serialize()
122 extras.comment = comment.get_api_data()
123 extras.created_by = username
124 # TODO(marcink): rcextensions handlers ??
125 hooks_base.log_comment_commit_repository(**extras)
126
127
100 128 def trigger_create_pull_request_hook(username, repo_name, repo_type, pull_request, data=None):
101 129 """
102 130 Triggers create pull request action hooks
@@ -196,6 +224,29 b' def trigger_comment_pull_request_hook(us'
196 224 hooks_base.log_comment_pull_request(**extras)
197 225
198 226
227 def trigger_comment_pull_request_edit_hook(username, repo_name, repo_type, pull_request, data=None):
228 """
229 Triggers when a comment was edited on a pull request
230
231 :param username: username who made the edit
232 :param repo_name: name of target repo
233 :param repo_type: the type of SCM target repo
234 :param pull_request: the pull request that comment was made on
235 :param data: extra data for specific events e.g {'comment': comment_obj}
236 """
237 if not _supports_repo_type(repo_type):
238 return
239
240 extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_pull_request')
241
242 comment = data['comment']
243 events.trigger(events.PullRequestCommentEditEvent(pull_request, comment))
244 extras.update(pull_request.get_api_data())
245 extras.comment = comment.get_api_data()
246 # TODO(marcink): handle rcextensions...
247 hooks_base.log_comment_pull_request(**extras)
248
249
199 250 def trigger_update_pull_request_hook(username, repo_name, repo_type, pull_request, data=None):
200 251 """
201 252 Triggers update pull request action hooks
@@ -789,8 +789,7 b' class CommentsModel(BaseModel):'
789 789 if action == 'create':
790 790 trigger_hook = hooks_utils.trigger_comment_commit_hooks
791 791 elif action == 'edit':
792 # TODO(dan): when this is supported we trigger edit hook too
793 return
792 trigger_hook = hooks_utils.trigger_comment_commit_edit_hooks
794 793 else:
795 794 return
796 795
@@ -703,6 +703,8 b' class PullRequestModel(BaseModel):'
703 703 trigger_hook = hooks_utils.trigger_update_pull_request_hook
704 704 elif action == 'comment':
705 705 trigger_hook = hooks_utils.trigger_comment_pull_request_hook
706 elif action == 'comment_edit':
707 trigger_hook = hooks_utils.trigger_comment_pull_request_edit_hook
706 708 else:
707 709 return
708 710
@@ -28,6 +28,7 b' from rhodecode.events import ('
28 28 PullRequestCreateEvent,
29 29 PullRequestUpdateEvent,
30 30 PullRequestCommentEvent,
31 PullRequestCommentEditEvent,
31 32 PullRequestReviewEvent,
32 33 PullRequestMergeEvent,
33 34 PullRequestCloseEvent,
@@ -80,6 +81,21 b' def test_pullrequest_comment_events_seri'
80 81
81 82
82 83 @pytest.mark.backends("git", "hg")
84 def test_pullrequest_comment_edit_events_serialized(pr_util, config_stub):
85 pr = pr_util.create_pull_request()
86 comment = CommentsModel().get_comments(
87 pr.target_repo.repo_id, pull_request=pr)[0]
88 event = PullRequestCommentEditEvent(pr, comment)
89 data = event.as_dict()
90 assert data['name'] == PullRequestCommentEditEvent.name
91 assert data['repo']['repo_name'] == pr.target_repo.repo_name
92 assert data['pullrequest']['pull_request_id'] == pr.pull_request_id
93 assert data['pullrequest']['url']
94 assert data['pullrequest']['permalink_url']
95 assert data['comment']['text'] == comment.text
96
97
98 @pytest.mark.backends("git", "hg")
83 99 def test_close_pull_request_events(pr_util, user_admin, config_stub):
84 100 pr = pr_util.create_pull_request()
85 101
@@ -29,7 +29,8 b' from rhodecode.events.repo import ('
29 29 RepoPrePullEvent, RepoPullEvent,
30 30 RepoPrePushEvent, RepoPushEvent,
31 31 RepoPreCreateEvent, RepoCreateEvent,
32 RepoPreDeleteEvent, RepoDeleteEvent, RepoCommitCommentEvent,
32 RepoPreDeleteEvent, RepoDeleteEvent,
33 RepoCommitCommentEvent, RepoCommitCommentEditEvent
33 34 )
34 35
35 36
@@ -138,8 +139,32 b' def test_repo_commit_event(config_stub, '
138 139 'comment_type': 'comment_type',
139 140 'f_path': 'f_path',
140 141 'line_no': 'line_no',
142 'last_version': 0,
141 143 })
142 144 event = EventClass(repo=repo_stub, commit=commit, comment=comment)
143 145 data = event.as_dict()
144 146 assert data['commit']['commit_id']
145 147 assert data['comment']['comment_id']
148
149
150 @pytest.mark.parametrize('EventClass', [RepoCommitCommentEditEvent])
151 def test_repo_commit_edit_event(config_stub, repo_stub, EventClass):
152
153 commit = StrictAttributeDict({
154 'raw_id': 'raw_id',
155 'message': 'message',
156 'branch': 'branch',
157 })
158
159 comment = StrictAttributeDict({
160 'comment_id': 'comment_id',
161 'text': 'text',
162 'comment_type': 'comment_type',
163 'f_path': 'f_path',
164 'line_no': 'line_no',
165 'last_version': 0,
166 })
167 event = EventClass(repo=repo_stub, commit=commit, comment=comment)
168 data = event.as_dict()
169 assert data['commit']['commit_id']
170 assert data['comment']['comment_id']
General Comments 0
You need to be logged in to leave comments. Login now