Bin
2025-12-17 dcf780a91c16b6be28635b6e2e0e702060ee19f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import logging
from functools import wraps
 
import requests
from core.feature_flags import flag_set
from core.redis import start_job_async_or_sync
from core.utils.common import load_func
from django.conf import settings
from django.db.models import Q
from django.db.models.query import QuerySet
 
from .models import Webhook, WebhookAction
 
logger = logging.getLogger(__name__)
 
 
def get_active_webhooks(organization, project, action):
    """Return all active webhooks for organization or project by action.
 
    If project is None - function return only organization hooks
    else project is not None - function return project and organization hooks
    Organization hooks are global hooks.
    """
    action_meta = WebhookAction.ACTIONS[action]
    if project and action_meta.get('organization-only'):
        raise ValueError('There is no project webhooks for organization-only action')
 
    return Webhook.objects.filter(
        Q(organization=organization)
        & (Q(project=project) | Q(project=None))
        & Q(is_active=True)
        & (
            Q(send_for_all_actions=True)
            | Q(
                id__in=WebhookAction.objects.filter(webhook__organization=organization, action=action).values_list(
                    'webhook_id', flat=True
                )
            )
        )
    ).distinct()
 
 
def run_webhook_sync(webhook, action, payload=None):
    """Run one webhook for action.
 
    This function must not raise any exceptions.
    """
    data = {
        'action': action,
    }
    if webhook.send_payload and payload:
        data.update(payload)
    try:
        logging.debug('Run webhook %s for action %s', webhook.id, action)
        return requests.post(
            webhook.url,
            headers=webhook.headers,
            json=data,
            timeout=settings.WEBHOOK_TIMEOUT,
        )
    except requests.RequestException as exc:
        logging.error(exc, exc_info=True)
        return
 
 
def emit_webhooks_sync(organization, project, action, payload):
    """
    Run all active webhooks for the action.
    """
    webhooks = get_active_webhooks(organization, project, action)
    if project and payload and webhooks.filter(send_payload=True).exists():
        payload['project'] = load_func(settings.WEBHOOK_SERIALIZERS['project'])(instance=project).data
    for wh in webhooks:
        run_webhook_sync(wh, action, payload)
 
 
def _process_webhook_batch(webhooks, project, action, batch, action_meta):
    """Process a single batch of instances for webhooks.
 
    Args:
        webhooks: Active webhooks to send
        project: Project instance (optional)
        action: Action name
        batch: Batch of instances to process
        action_meta: Action metadata from WebhookAction.ACTIONS
    """
    payload = {}
 
    if batch and webhooks.filter(send_payload=True).exists():
        serializer_class = action_meta.get('serializer')
        if serializer_class:
            payload[action_meta['key']] = serializer_class(instance=batch, many=action_meta['many']).data
        if project and payload:
            payload['project'] = load_func(settings.WEBHOOK_SERIALIZERS['project'])(instance=project).data
        if payload and 'nested-fields' in action_meta:
            for key, value in action_meta['nested-fields'].items():
                payload[key] = value['serializer'](
                    instance=get_nested_field(batch, value['field']), many=value['many']
                ).data
 
    for wh in webhooks:
        run_webhook_sync(wh, action, payload)
 
 
def emit_webhooks_for_instance_sync(organization, project, action, instance=None):
    """Run all active webhooks for the action using instances as payload.
 
    Be sure WebhookAction.ACTIONS contains all required fields.
    """
    webhooks = get_active_webhooks(organization, project, action)
    if not webhooks.exists():
        return
 
    action_meta = WebhookAction.ACTIONS[action]
 
    # Convert list of IDs to queryset
    if instance and isinstance(instance, list) and isinstance(instance[0], int):
        instance = action_meta['model'].objects.filter(id__in=instance)
 
    # Check if batching is needed
    is_batch_collection = isinstance(instance, (list, QuerySet))
    use_batching = is_batch_collection and flag_set('fflag_fix_back_plt_843_webhook_memory_improvement_12082025_short')
 
    if use_batching:
        # Process in batches
        batch_size = settings.WEBHOOK_BATCH_SIZE
 
        if isinstance(instance, QuerySet):
            # For QuerySets, use iterator with chunk_size
            total_count = instance.count()
            logger.debug(f'Processing webhook for {total_count} instances in batches of {batch_size}')
            for i in range(0, total_count, batch_size):
                batch = instance[i : i + batch_size]
                logger.debug(f'Processing batch {i // batch_size + 1} with {batch.count()} instances')
                _process_webhook_batch(webhooks, project, action, batch, action_meta)
        else:
            # For lists, slice directly
            total_count = len(instance)
            logger.debug(f'Processing webhook for {total_count} instances in batches of {batch_size}')
            for i in range(0, len(instance), batch_size):
                batch = instance[i : i + batch_size]
                logger.debug(f'Processing batch {i // batch_size + 1} with {len(batch)} instances')
                _process_webhook_batch(webhooks, project, action, batch, action_meta)
    else:
        # Original behavior - process all at once
        _process_webhook_batch(webhooks, project, action, instance, action_meta)
 
 
def run_webhook(webhook, action, payload=None):
    """Run one webhook for action.
 
    This function must not raise any exceptions.
 
    Will run a webhook in an RQ worker.
    """
    if flag_set('fflag_fix_back_lsdv_4604_excess_sql_queries_in_api_short'):
        start_job_async_or_sync(
            run_webhook_sync,
            webhook,
            action,
            payload,
            queue_name='high',
        )
    else:
        run_webhook_sync(webhook, action, payload)
 
 
def emit_webhooks_for_instance(organization, project, action, instance=None):
    """Run all active webhooks for the action using instances as payload.
 
    Be sure WebhookAction.ACTIONS contains all required fields.
 
    Will run all selected webhooks in an RQ worker.
    """
    if flag_set('fflag_fix_back_lsdv_4604_excess_sql_queries_in_api_short'):
        start_job_async_or_sync(emit_webhooks_for_instance_sync, organization, project, action, instance)
    else:
        emit_webhooks_for_instance_sync(organization, project, action, instance)
 
 
def emit_webhooks(organization, project, action, payload):
    """
    Run all active webhooks for the action.
 
    Will run all selected webhooks in an RQ worker.
    """
    if flag_set('fflag_fix_back_lsdv_4604_excess_sql_queries_in_api_short'):
        start_job_async_or_sync(emit_webhooks_sync, organization, project, action, payload)
    else:
        emit_webhooks_sync(organization, project, action, payload)
 
 
def api_webhook(action):
    """Decorator emit webhooks for APIView methods: post, put, patch.
 
    Used for simple Create/Update methods.
    The decorator expects authorized request and response with 'id' key in data.
 
    Example:
        ```
        @api_webhook(WebhookAction.PROJECT_UPDATED)
        def put(self, request, *args, **kwargs):
            return super(ProjectAPI, self).put(request, *args, **kwargs)
        ```
    """
 
    def decorator(func):
        @wraps(func)
        def wrap(self, request, *args, **kwargs):
            response = func(self, request, *args, **kwargs)
 
            action_meta = WebhookAction.ACTIONS[action]
            many = action_meta['many']
            instance = action_meta['model'].objects.get(id=response.data.get('id'))
            if many:
                instance = [instance]
            project = None
            if 'project-field' in action_meta:
                project = get_nested_field(instance, action_meta['project-field'])
            emit_webhooks_for_instance(
                request.user.active_organization,
                project,
                action,
                instance,
            )
            return response
 
        return wrap
 
    return decorator
 
 
def api_webhook_for_delete(action):
    """Decorator emit webhooks for APIView delete method.
 
    The decorator expects authorized request and use get_object() method
    before delete.
 
    Example:
        ```
        @extend_schema(tags=['Annotations'])
        @api_webhook_for_delete(WebhookAction.ANNOTATIONS_DELETED)
        def delete(self, request, *args, **kwargs):
            return super(AnnotationAPI, self).delete(request, *args, **kwargs)
        ```
    """
 
    def decorator(func):
        @wraps(func)
        def wrap(self, request, *args, **kwargs):
            instance = self.get_object()
            action_meta = WebhookAction.ACTIONS[action]
            many = action_meta['many']
            project = None
            if 'project-field' in action_meta:
                project = get_nested_field(instance, action_meta['project-field'])
 
            obj = {'id': instance.pk}
            if many:
                obj = [obj]
 
            response = func(self, request, *args, **kwargs)
 
            emit_webhooks_for_instance(request.user.active_organization, project, action, obj)
            return response
 
        return wrap
 
    return decorator
 
 
def get_nested_field(value, field):
    """
    Get nested field from list of objects or single instance
    :param value: Single instance or list to look up field
    :param field: Field to lookup
    :return: List or single instance of looked up field
    """
    if field == '__self__':
        return value
    fields = field.split('__')
    for fld in fields:
        if isinstance(value, list):
            value = [getattr(v, fld) for v in value]
        else:
            value = getattr(value, fld)
    return value