• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chiefonboarding / ChiefOnboarding / 18481185920

13 Oct 2025 11:53PM UTC coverage: 89.613% (-0.006%) from 89.619%
18481185920

Pull #572

github

web-flow
Merge f5b8970dc into f01e5ecbf
Pull Request #572: Add permissions based on department

7057 of 7875 relevant lines covered (89.61%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.07
back/admin/integrations/models.py
1
import base64
1✔
2
import io
1✔
3
import json
1✔
4
import time
1✔
5
import uuid
1✔
6
from datetime import timedelta
1✔
7
from json.decoder import JSONDecodeError as NativeJSONDecodeError
1✔
8

9
import requests
1✔
10
from django.conf import settings
1✔
11
from django.core.exceptions import ValidationError
1✔
12
from django.db import models
1✔
13
from django.db.models.signals import post_delete
1✔
14
from django.dispatch import receiver
1✔
15
from django.template import Context, Template
1✔
16
from django.template.loader import render_to_string
1✔
17
from django.urls import reverse, reverse_lazy
1✔
18
from django.utils import timezone
1✔
19
from django.utils.crypto import get_random_string
1✔
20
from django.utils.translation import gettext_lazy as _
1✔
21
from django_q.models import Schedule
1✔
22
from django_q.tasks import schedule
1✔
23
from requests.exceptions import (
1✔
24
    HTTPError,
25
    InvalidHeader,
26
    InvalidJSONError,
27
    InvalidSchema,
28
    InvalidURL,
29
    JSONDecodeError,
30
    MissingSchema,
31
    SSLError,
32
    Timeout,
33
    TooManyRedirects,
34
    URLRequired,
35
)
36
from twilio.rest import Client
1✔
37

38
from admin.integrations.serializers import (
1✔
39
    SyncUsersManifestSerializer,
40
    WebhookManifestSerializer,
41
)
42
from admin.integrations.utils import get_value_from_notation
1✔
43
from misc.fernet_fields import EncryptedTextField
1✔
44
from misc.fields import EncryptedJSONField
1✔
45
from organization.models import FilteredForManagerQuerySet, Notification
1✔
46
from organization.utils import has_manager_or_buddy_tags, send_email_with_notification
1✔
47

48

49
class IntegrationTracker(models.Model):
1✔
50
    """Model to track the integrations that ran. Gives insights into error messages"""
51

52
    class Category(models.IntegerChoices):
1✔
53
        EXECUTE = 0, _("Run the execute part")
1✔
54
        EXISTS = 1, _("Check if user exists")
1✔
55
        REVOKE = 2, _("Revoke user")
1✔
56

57
    integration = models.ForeignKey(
1✔
58
        "integrations.Integration", on_delete=models.CASCADE, null=True
59
    )
60
    category = models.IntegerField(choices=Category.choices)
1✔
61
    for_user = models.ForeignKey("users.User", on_delete=models.CASCADE, null=True)
1✔
62
    ran_at = models.DateTimeField(auto_now_add=True)
1✔
63

64
    @property
1✔
65
    def ran_execute_block(self):
1✔
66
        return self.category == IntegrationTracker.Category.EXECUTE
×
67

68
    @property
1✔
69
    def ran_exists_block(self):
1✔
70
        return self.category == IntegrationTracker.Category.EXISTS
×
71

72
    @property
1✔
73
    def ran_revoke_block(self):
1✔
74
        return self.category == IntegrationTracker.Category.REVOKE
×
75

76

77
class IntegrationTrackerStep(models.Model):
1✔
78
    tracker = models.ForeignKey(
1✔
79
        "integrations.IntegrationTracker",
80
        on_delete=models.CASCADE,
81
        related_name="steps",
82
    )
83
    status_code = models.IntegerField()
1✔
84
    json_response = models.JSONField()
1✔
85
    text_response = models.TextField()
1✔
86
    url = models.TextField()
1✔
87
    method = models.TextField()
1✔
88
    post_data = models.JSONField()
1✔
89
    headers = models.JSONField()
1✔
90
    expected = models.TextField()
1✔
91
    error = models.TextField()
1✔
92

93
    @property
1✔
94
    def has_succeeded(self):
1✔
95
        return self.status_code >= 200 and self.status_code < 300
×
96

97
    @property
1✔
98
    def found_expected(self):
1✔
99
        if self.expected == "":
1✔
100
            return True
×
101

102
        if self.text_response != "":
1✔
103
            return self.expected in self.text_response
×
104
        if len(self.json_response):
1✔
105
            return self.expected in json.dumps(self.json_response)
1✔
106
        return False
×
107

108
    @property
1✔
109
    def pretty_json_response(self):
1✔
110
        return json.dumps(self.json_response, indent=4)
1✔
111

112
    @property
1✔
113
    def pretty_headers(self):
1✔
114
        return json.dumps(self.headers, indent=4)
1✔
115

116
    @property
1✔
117
    def pretty_post_data(self):
1✔
118
        return json.dumps(self.post_data, indent=4)
1✔
119

120

121
class IntegrationManager(models.Manager):
1✔
122
    def get_queryset(self):
1✔
123
        return FilteredForManagerQuerySet(self.model, using=self._db)
1✔
124

125
    def for_user(self, user):
1✔
126
        return self.get_queryset().for_user(user)
1✔
127

128
    def sequence_integration_options(self, user):
1✔
129
        # any webhooks and account provisioning
130
        return (
1✔
131
            self.get_queryset()
132
            .for_user(user=user)
133
            .filter(
134
                integration=Integration.Type.CUSTOM,
135
                manifest_type__in=[
136
                    Integration.ManifestType.WEBHOOK,
137
                    Integration.ManifestType.MANUAL_USER_PROVISIONING,
138
                ],
139
            )
140
        )
141

142
    def account_provision_options(self, user):
1✔
143
        # only account provisioning (no general webhooks)
144
        return self.get_queryset().for_user(user=user).filter(
1✔
145
            integration=Integration.Type.CUSTOM,
146
            manifest_type=Integration.ManifestType.WEBHOOK,
147
            manifest__exists__isnull=False,
148
        ) | self.get_queryset().filter(
149
            integration=Integration.Type.CUSTOM,
150
            manifest_type=Integration.ManifestType.MANUAL_USER_PROVISIONING,
151
        )
152

153
    def import_users_options(self):
1✔
154
        # only import user items - admin function
155
        return (
1✔
156
            self.get_queryset()
157
            .filter(
158
                integration=Integration.Type.CUSTOM,
159
                manifest_type=Integration.ManifestType.SYNC_USERS,
160
            )
161
            .exclude(manifest__schedule__isnull=False)
162
        )
163

164

165
class IntegrationInactiveManager(IntegrationManager):
1✔
166
    def get_queryset(self):
1✔
167
        return super().get_queryset().filter(is_active=False)
1✔
168

169

170
class Integration(models.Model):
1✔
171
    class Type(models.IntegerChoices):
1✔
172
        SLACK_BOT = 0, _("Slack bot")
1✔
173
        CUSTOM = 10, _("Custom")
1✔
174

175
    class ManifestType(models.IntegerChoices):
1✔
176
        WEBHOOK = 0, _("Provision user accounts or trigger webhooks")
1✔
177
        SYNC_USERS = 1, _("Sync users")
1✔
178
        MANUAL_USER_PROVISIONING = (
1✔
179
            3,
180
            _("Manual user account provisioning, no manifest required"),
181
        )
182

183
    name = models.CharField(max_length=300, default="", blank=True)
1✔
184
    is_active = models.BooleanField(
1✔
185
        default=True, help_text="If inactive, it's a test/debug integration"
186
    )
187
    integration = models.IntegerField(choices=Type.choices)
1✔
188
    departments = models.ManyToManyField(
1✔
189
        "users.Department",
190
        blank=True,
191
        help_text=_("Leave empty to make it available to all managers"),
192
    )
193
    manifest_type = models.IntegerField(
1✔
194
        choices=ManifestType.choices, null=True, blank=True
195
    )
196
    token = EncryptedTextField(max_length=10000, default="", blank=True)
1✔
197
    refresh_token = EncryptedTextField(max_length=10000, default="", blank=True)
1✔
198
    base_url = models.CharField(max_length=22300, default="", blank=True)
1✔
199
    redirect_url = models.CharField(max_length=22300, default="", blank=True)
1✔
200
    account_id = models.CharField(max_length=22300, default="", blank=True)
1✔
201
    active = models.BooleanField(default=True)  # legacy?
1✔
202
    ttl = models.IntegerField(null=True, blank=True)
1✔
203
    expiring = models.DateTimeField(auto_now_add=True, blank=True)
1✔
204
    one_time_auth_code = models.UUIDField(
1✔
205
        default=uuid.uuid4, editable=False, unique=True
206
    )
207

208
    manifest = models.JSONField(default=dict, null=True, blank=True)
1✔
209
    extra_args = EncryptedJSONField(default=dict)
1✔
210
    enabled_oauth = models.BooleanField(default=False)
1✔
211

212
    # Slack
213
    app_id = models.CharField(max_length=100, default="")
1✔
214
    client_id = models.CharField(max_length=100, default="")
1✔
215
    client_secret = models.CharField(max_length=100, default="")
1✔
216
    signing_secret = models.CharField(max_length=100, default="")
1✔
217
    verification_token = models.CharField(max_length=100, default="")
1✔
218
    bot_token = EncryptedTextField(max_length=10000, default="", blank=True)
1✔
219
    bot_id = models.CharField(max_length=100, default="")
1✔
220

221
    @property
1✔
222
    def skip_user_provisioning(self):
1✔
223
        return self.manifest_type == Integration.ManifestType.MANUAL_USER_PROVISIONING
1✔
224

225
    @property
1✔
226
    def is_sync_users_integration(self):
1✔
227
        return self.manifest_type == Integration.ManifestType.SYNC_USERS
×
228

229
    @property
1✔
230
    def can_revoke_access(self):
1✔
231
        return len(self.manifest.get("revoke", []))
1✔
232

233
    @property
1✔
234
    def update_url(self):
1✔
235
        return reverse("integrations:update", args=[self.id])
1✔
236

237
    def get_icon_template(self):
1✔
238
        return render_to_string("_integration_config.html")
1✔
239

240
    @property
1✔
241
    def schedule_name(self):
1✔
242
        return f"User sync for integration: {self.id}"
1✔
243

244
    @property
1✔
245
    def secret_values(self):
1✔
246
        return [
1✔
247
            item
248
            for item in self.manifest["initial_data_form"]
249
            if item.get("secret", False) and item.get("name") != "generate"
250
        ]
251

252
    @property
1✔
253
    def missing_secret_values(self):
1✔
254
        return [
×
255
            item for item in self.secret_values if item["id"] not in self.extra_args
256
        ]
257

258
    @property
1✔
259
    def filled_secret_values(self):
1✔
260
        return [item for item in self.secret_values if item["id"] in self.extra_args]
1✔
261

262
    @property
1✔
263
    def requires_assigned_manager_or_buddy(self):
1✔
264
        # returns manager, buddy
265
        return has_manager_or_buddy_tags(self.manifest)
1✔
266

267
    def clean(self):
1✔
268
        if not self.manifest or self.skip_user_provisioning:
1✔
269
            # ignore field if form doesn't have it or no manifest is necessary
270
            return
1✔
271

272
        if self.manifest_type == Integration.ManifestType.WEBHOOK:
1✔
273
            manifest_serializer = WebhookManifestSerializer(data=self.manifest)
1✔
274
        else:
275
            manifest_serializer = SyncUsersManifestSerializer(data=self.manifest)
1✔
276
        if not manifest_serializer.is_valid():
1✔
277
            raise ValidationError({"manifest": json.dumps(manifest_serializer.errors)})
×
278

279
    def save(self, *args, **kwargs):
1✔
280
        super().save(*args, **kwargs)
1✔
281

282
        # skip if it's not a sync user integration (no background jobs for the others)
283
        if self.manifest_type != Integration.ManifestType.SYNC_USERS:
1✔
284
            return
1✔
285

286
        # update the background job based on the manifest
287
        schedule_cron = self.manifest.get("schedule")
1✔
288

289
        try:
1✔
290
            schedule_obj = Schedule.objects.get(name=self.schedule_name)
1✔
291
        except Schedule.DoesNotExist:
1✔
292
            # Schedule does not exist yet, so create it if specified
293
            if schedule_cron:
1✔
294
                schedule(
1✔
295
                    "admin.integrations.tasks.sync_user_info",
296
                    self.id,
297
                    schedule_type=Schedule.CRON,
298
                    cron=schedule_cron,
299
                    name=self.schedule_name,
300
                )
301
            return
1✔
302

303
        # delete if cron was removed
304
        if schedule_cron is None:
1✔
305
            schedule_obj.delete()
1✔
306
            return
1✔
307

308
        # if schedule changed, then update
309
        if schedule_obj.cron != schedule_cron:
×
310
            schedule_obj.cron = schedule_cron
×
311
            schedule_obj.save()
×
312

313
    def register_manual_integration_run(self, user):
1✔
314
        from users.models import IntegrationUser
1✔
315

316
        IntegrationUser.objects.update_or_create(
1✔
317
            user=user,
318
            integration=self,
319
            defaults={"revoked": user.is_offboarding},
320
        )
321

322
    def cast_to_json(self, value):
1✔
323
        try:
1✔
324
            value = json.loads(value)
1✔
325
        except Exception:
1✔
326
            pass
1✔
327

328
        return value
1✔
329

330
    def run_request(self, data):
1✔
331
        url = self._replace_vars(data["url"])
1✔
332
        if "data" in data:
1✔
333
            post_data = self._replace_vars(json.dumps(data["data"]))
1✔
334
        else:
335
            post_data = {}
1✔
336
        if data.get("cast_data_to_json", True):
1✔
337
            post_data = self.cast_to_json(post_data)
1✔
338

339
        error = ""
1✔
340

341
        # extract files from locally saved files and send them with the request
342
        files_to_send = {}
1✔
343
        for field_name, file_name in data.get("files", {}).items():
1✔
344
            try:
1✔
345
                files_to_send[field_name] = (file_name, self.params["files"][file_name])
1✔
346
            except KeyError:
1✔
347
                error = f"{file_name} could not be found in the locally saved files"
1✔
348
                if hasattr(self, "tracker"):
1✔
349
                    IntegrationTrackerStep.objects.create(
1✔
350
                        status_code=0,
351
                        tracker=self.tracker,
352
                        json_response={},
353
                        text_response=error,
354
                        url=self.clean_response(url),
355
                        method=data.get("method", "POST"),
356
                        post_data=json.loads(
357
                            self.clean_response(self.cast_to_json(post_data))
358
                        ),
359
                        headers=json.loads(
360
                            self.clean_response(self.headers(data.get("headers", {})))
361
                        ),
362
                        error=error,
363
                    )
364
                return False, error
1✔
365

366
        response = None
1✔
367
        try:
1✔
368
            response = requests.request(
1✔
369
                data.get("method", "POST"),
370
                url,
371
                headers=self.headers(data.get("headers", {})),
372
                data=post_data,
373
                files=files_to_send,
374
                timeout=120,
375
            )
376
        except (InvalidJSONError, JSONDecodeError):
1✔
377
            error = "JSON is invalid"
×
378

379
        except HTTPError:
1✔
380
            error = "An HTTP error occurred"
×
381

382
        except SSLError:
1✔
383
            error = "An SSL error occurred"
×
384

385
        except Timeout:
1✔
386
            error = "The request timed out"
×
387

388
        except (URLRequired, MissingSchema, InvalidSchema, InvalidURL):
1✔
389
            error = "The url is invalid"
1✔
390

391
        except TooManyRedirects:
×
392
            error = "There are too many redirects"
×
393

394
        except InvalidHeader:
×
395
            error = "The header is invalid"
×
396

397
        except:  # noqa E722
×
398
            error = "There was an unexpected error with the request"
×
399

400
        if response is not None and error == "":
1✔
401
            if len(data.get("status_code", [])) and str(
1✔
402
                response.status_code
403
            ) not in data.get("status_code", []):
404
                error = f"Status code ({response.status_code}) not in allowed list ({data.get('status_code')})"
×
405

406
        try:
1✔
407
            json_response = response.json()
1✔
408
            text_response = ""
1✔
409
        except:  # noqa E722
1✔
410
            json_response = {}
1✔
411
            if error:
1✔
412
                text_response = error
1✔
413
            else:
414
                text_response = response.text
1✔
415

416
        if hasattr(self, "tracker"):
1✔
417
            # TODO: JSON needs to be refactored
418
            try:
1✔
419
                json_payload = json.loads(self.clean_response(json_response))
1✔
420
            except (NativeJSONDecodeError, TypeError):
×
421
                json_payload = self.clean_response(json_response)
×
422

423
            try:
1✔
424
                json_post_payload = json.loads(
1✔
425
                    self.clean_response(self.cast_to_json(post_data))
426
                )
427
            except (NativeJSONDecodeError, TypeError):
×
428
                json_post_payload = self.clean_response(self.cast_to_json(post_data))
×
429

430
            try:
1✔
431
                json_headers_payload = json.loads(
1✔
432
                    self.clean_response(self.headers(data.get("headers", {})))
433
                )
434
            except (NativeJSONDecodeError, TypeError):
×
435
                json_headers_payload = self.clean_response(
×
436
                    self.headers(data.get("headers", {}))
437
                )
438

439
            IntegrationTrackerStep.objects.create(
1✔
440
                status_code=0 if response is None else response.status_code,
441
                tracker=self.tracker,
442
                json_response=json_payload,
443
                text_response=(
444
                    "Cannot display, could be file"
445
                    if data.get("save_as_file", False)
446
                    else self.clean_response(text_response)
447
                ),
448
                url=self.clean_response(url),
449
                method=data.get("method", "POST"),
450
                post_data=json_post_payload,
451
                headers=json_headers_payload,
452
                expected=self._replace_vars(data.get("expected", "")),
453
                error=self.clean_response(error),
454
            )
455

456
        if error:
1✔
457
            return False, error
1✔
458

459
        return True, response
1✔
460

461
    def _replace_vars(self, text):
1✔
462
        params = {} if not hasattr(self, "params") else self.params
1✔
463
        if self.pk is not None:
1✔
464
            params["redirect_url"] = settings.BASE_URL + reverse_lazy(
1✔
465
                "integrations:oauth-callback", args=[self.id]
466
            )
467
        if hasattr(self, "new_hire") and self.new_hire is not None:
1✔
468
            text = self.new_hire.personalize(text, self.extra_args | params)
1✔
469
            return text
1✔
470
        t = Template(text)
1✔
471
        context = Context(self.extra_args | params)
1✔
472
        text = t.render(context)
1✔
473
        return text
1✔
474

475
    @property
1✔
476
    def has_oauth(self):
1✔
477
        return "oauth" in self.manifest and len(self.manifest.get("oauth", {}))
1✔
478

479
    def headers(self, headers=None):
1✔
480
        if headers is None:
1✔
481
            headers = {}
1✔
482

483
        headers = (
1✔
484
            self.manifest.get("headers", {}).items()
485
            if len(headers) == 0
486
            else headers.items()
487
        )
488
        new_headers = {}
1✔
489
        for key, value in headers:
1✔
490
            # If Basic authentication then swap to base64
491
            if key == "Authorization" and value.startswith("Basic"):
1✔
492
                auth_details = self._replace_vars(value.split(" ", 1)[1])
1✔
493
                value = "Basic " + base64.b64encode(
1✔
494
                    auth_details.encode("ascii")
495
                ).decode("ascii")
496

497
            # Adding an empty string to force to return a string instead of a
498
            # safestring. Ref: https://github.com/psf/requests/issues/6159
499
            new_headers[self._replace_vars(key) + ""] = self._replace_vars(value) + ""
1✔
500
        return new_headers
1✔
501

502
    def user_exists(self, new_hire, save_result=True):
1✔
503
        from users.models import IntegrationUser
1✔
504

505
        if not len(self.manifest.get("exists", [])):
1✔
506
            return None
×
507

508
        # check if user has been created manually
509
        if self.skip_user_provisioning:
1✔
510
            try:
×
511
                user_integration = IntegrationUser.objects.get(
×
512
                    user=new_hire, integration=self
513
                )
514
            except IntegrationUser.DoesNotExist:
×
515
                return False
×
516

517
            return not user_integration.revoked
×
518

519
        self.tracker = IntegrationTracker.objects.create(
1✔
520
            category=IntegrationTracker.Category.EXISTS,
521
            integration=self,
522
            for_user=new_hire,
523
        )
524

525
        self.new_hire = new_hire
1✔
526
        self.has_user_context = new_hire is not None
1✔
527

528
        # Renew token if necessary
529
        if not self.renew_key():
1✔
530
            return
×
531

532
        success, response = self.run_request(self.manifest["exists"])
1✔
533

534
        if not success:
1✔
535
            return None
×
536

537
        user_exists = self.tracker.steps.last().found_expected
1✔
538

539
        if save_result:
1✔
540
            IntegrationUser.objects.update_or_create(
1✔
541
                integration=self, user=new_hire, defaults={"revoked": not user_exists}
542
            )
543

544
        return user_exists
1✔
545

546
    def needs_user_info(self, user):
1✔
547
        if self.skip_user_provisioning:
1✔
548
            return False
1✔
549

550
        # form created from the manifest, this info is always needed to create a new
551
        # account. Check if there is anything that needs to be filled
552
        form = self.manifest.get("form", [])
1✔
553

554
        # extra items that are needed from the integration (often prefilled by admin)
555
        extra_user_info = self.manifest.get("extra_user_info", [])
1✔
556
        needs_more_info = any(
1✔
557
            item["id"] not in user.extra_fields.keys() for item in extra_user_info
558
        )
559

560
        return len(form) > 0 or needs_more_info
1✔
561

562
    def revoke_user(self, user):
1✔
563
        if self.skip_user_provisioning:
×
564
            # should never be triggered
565
            return False, "Cannot revoke manual integration"
×
566

567
        self.new_hire = user
×
568
        self.has_user_context = True
×
569

570
        # Renew token if necessary
571
        if not self.renew_key():
×
572
            return False, "Couldn't renew key"
×
573

574
        revoke_manifest = self.manifest.get("revoke", [])
×
575

576
        # add extra fields directly to params
577
        self.params = self.new_hire.extra_fields
×
578
        self.tracker = IntegrationTracker.objects.create(
×
579
            category=IntegrationTracker.Category.REVOKE,
580
            integration=self if self.pk is not None else None,
581
            for_user=self.new_hire,
582
        )
583

584
        for item in revoke_manifest:
×
585
            success, response = self.run_request(item)
×
586

587
            if not success or not self.tracker.steps.last().found_expected:
×
588
                return False, self.clean_response(response)
×
589

590
        return True, ""
×
591

592
    def renew_key(self):
1✔
593
        # Oauth2 refreshing access token if needed
594
        success = True
1✔
595
        if (
1✔
596
            self.has_oauth
597
            and "expires_in" in self.extra_args.get("oauth", {})
598
            and self.expiring < timezone.now()
599
        ):
600
            success, response = self.run_request(self.manifest["oauth"]["refresh"])
1✔
601

602
            if not success:
1✔
603
                user = self.new_hire if self.has_user_context else None
1✔
604
                Notification.objects.create(
1✔
605
                    notification_type=Notification.Type.FAILED_INTEGRATION,
606
                    extra_text=self.name,
607
                    created_for=user,
608
                    description="Refresh url: " + str(response),
609
                )
610
                return success
1✔
611

612
            self.extra_args["oauth"] |= response.json()
1✔
613
            if "expires_in" in response.json():
1✔
614
                self.expiring = timezone.now() + timedelta(
1✔
615
                    seconds=response.json()["expires_in"]
616
                )
617
            self.save(update_fields=["expiring", "extra_args"])
1✔
618
            if hasattr(self, "tracker"):
1✔
619
                # we need to clean the last step as we now probably got new secret keys
620
                # that need to be masked
621
                last_step = self.tracker.steps.last()
×
622
                last_step.json_response = self.clean_response(last_step.json_response)
×
623
                last_step.save()
×
624

625
        return success
1✔
626

627
    def _check_condition(self, response, condition):
1✔
628
        value = self._replace_vars(condition.get("value"))
1✔
629
        try:
1✔
630
            # first argument will be taken from the response
631
            response_value = get_value_from_notation(
1✔
632
                condition.get("response_notation"), response.json()
633
            )
634
        except KeyError:
×
635
            # we know that the result might not be in the response yet, as we are
636
            # waiting for the correct response, so just respond with an empty string
637
            response_value = ""
×
638
        return value == response_value
1✔
639

640
    def _polling(self, item, response):
1✔
641
        polling = item.get("polling")
1✔
642
        continue_if = item.get("continue_if")
1✔
643
        interval = polling.get("interval")
1✔
644
        amount = polling.get("amount")
1✔
645

646
        got_expected_result = self._check_condition(response, continue_if)
1✔
647
        if got_expected_result:
1✔
648
            return True, response
×
649

650
        tried = 1
1✔
651
        while amount > tried:
1✔
652
            time.sleep(interval)
1✔
653
            success, response = self.run_request(item)
1✔
654
            got_expected_result = self._check_condition(response, continue_if)
1✔
655
            if got_expected_result:
1✔
656
                return True, response
1✔
657
            tried += 1
1✔
658
        # if exceeding the max amounts, then fail
659
        return False, response
1✔
660

661
    def execute(self, new_hire=None, params=None, retry_on_failure=False):
1✔
662
        self.params = params or {}
1✔
663
        self.params["responses"] = []
1✔
664
        self.params["files"] = {}
1✔
665
        self.new_hire = new_hire
1✔
666
        self.has_user_context = new_hire is not None
1✔
667

668
        self.tracker = IntegrationTracker.objects.create(
1✔
669
            category=IntegrationTracker.Category.EXECUTE,
670
            integration=self if self.pk is not None else None,
671
            for_user=self.new_hire,
672
        )
673

674
        if self.has_user_context:
1✔
675
            self.params |= new_hire.extra_fields
1✔
676
            self.new_hire = new_hire
1✔
677

678
        # Renew token if necessary
679
        if not self.renew_key():
1✔
680
            return False, None
×
681

682
        # Add generated secrets
683
        for item in self.manifest.get("initial_data_form", []):
1✔
684
            if "name" in item and item["name"] == "generate":
1✔
685
                self.extra_args[item["id"]] = get_random_string(length=10)
1✔
686

687
        response = None
1✔
688
        # Run all requests
689
        for item in self.manifest["execute"]:
1✔
690
            success, response = self.run_request(item)
1✔
691

692
            # check if we need to poll before continuing
693
            if polling := item.get("polling", False):
1✔
694
                success, response = self._polling(item, response)
1✔
695

696
            # check if we need to block this integration based on condition
697
            if continue_if := item.get("continue_if", False):
1✔
698
                got_expected_result = self._check_condition(response, continue_if)
1✔
699
                if not got_expected_result:
1✔
700
                    response = self.clean_response(response=response)
1✔
701
                    Notification.objects.create(
1✔
702
                        notification_type=Notification.Type.BLOCKED_INTEGRATION,
703
                        extra_text=self.name,
704
                        created_for=new_hire,
705
                        description=f"Execute url ({item['url']}): {response}",
706
                    )
707
                    return False, response
1✔
708

709
            # No need to retry or log when we are importing users
710
            if not success:
1✔
711
                if self.has_user_context:
1✔
712
                    response = self.clean_response(response=response)
1✔
713
                    if polling:
1✔
714
                        response = "Polling timed out: " + response
×
715
                    Notification.objects.create(
1✔
716
                        notification_type=Notification.Type.FAILED_INTEGRATION,
717
                        extra_text=self.name,
718
                        created_for=new_hire,
719
                        description=f"Execute url ({item['url']}): {response}",
720
                    )
721
                if retry_on_failure:
1✔
722
                    # Retry url in one hour
723
                    schedule(
×
724
                        "admin.integrations.tasks.retry_integration",
725
                        new_hire.id,
726
                        self.id,
727
                        params,
728
                        name=(
729
                            f"Retrying integration {self.id} for new hire {new_hire.id}"
730
                        ),
731
                        next_run=timezone.now() + timedelta(hours=1),
732
                        schedule_type=Schedule.ONCE,
733
                    )
734
                return False, response
1✔
735

736
            # save if file, so we can reuse later
737
            save_as_file = item.get("save_as_file")
1✔
738
            if save_as_file is not None:
1✔
739
                self.params["files"][save_as_file] = io.BytesIO(response.content)
1✔
740

741
            # save json response temporarily to be reused in other parts
742
            try:
1✔
743
                self.params["responses"].append(response.json())
1✔
744
            except:  # noqa E722
1✔
745
                # if we save a file, then just append an empty dict
746
                self.params["responses"].append({})
1✔
747

748
            # store data coming back from response to the user, so we can reuse in other
749
            # integrations
750
            if store_data := item.get("store_data", {}):
1✔
751
                for new_hire_prop, notation_for_response in store_data.items():
1✔
752
                    try:
1✔
753
                        value = get_value_from_notation(
1✔
754
                            notation_for_response, response.json()
755
                        )
756
                    except KeyError:
1✔
757
                        return (
1✔
758
                            False,
759
                            f"Could not store data to new hire: {notation_for_response}"
760
                            f" not found in {self.clean_response(response.json())}",
761
                        )
762

763
                    # save to new hire and to temp var `params` on this model for use in
764
                    # the same integration
765
                    new_hire.extra_fields[new_hire_prop] = value
1✔
766
                    self.params[new_hire_prop] = value
1✔
767
                new_hire.save()
1✔
768

769
        # Run all post requests (notifications)
770
        for item in self.manifest.get("post_execute_notification", []):
1✔
771
            if item["type"] == "email":
1✔
772
                send_email_with_notification(
1✔
773
                    subject=self._replace_vars(item["subject"]),
774
                    message=self._replace_vars(item["message"]),
775
                    to=self._replace_vars(item["to"]),
776
                    notification_type=(
777
                        Notification.Type.SENT_EMAIL_INTEGRATION_NOTIFICATION
778
                    ),
779
                )
780
                return True, None
1✔
781
            else:
782
                try:
×
783
                    client = Client(
×
784
                        settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN
785
                    )
786
                    client.messages.create(
×
787
                        to=new_hire.phone,
788
                        from_=settings.TWILIO_FROM_NUMBER,
789
                        body=self._replace_vars(item["message"]),
790
                    )
791
                except Exception:
×
792
                    Notification.objects.create(
×
793
                        notification_type=(
794
                            Notification.Type.FAILED_TEXT_INTEGRATION_NOTIFICATION
795
                        ),
796
                        extra_text=self.name,
797
                        created_for=new_hire,
798
                    )
799
                    return True, None
×
800

801
        # Succesfully ran integration, add notification only when we are provisioning
802
        # access
803
        if self.has_user_context:
1✔
804
            Notification.objects.create(
1✔
805
                notification_type=Notification.Type.RAN_INTEGRATION,
806
                extra_text=self.name,
807
                created_for=new_hire,
808
            )
809
        return True, response
1✔
810

811
    def config_form(self, data=None):
1✔
812
        if self.skip_user_provisioning:
1✔
813
            from .forms import ManualIntegrationConfigForm
1✔
814

815
            return ManualIntegrationConfigForm(data=data)
1✔
816

817
        from .forms import IntegrationConfigForm
1✔
818

819
        return IntegrationConfigForm(instance=self, data=data)
1✔
820

821
    def clean_response(self, response) -> str:
1✔
822
        if isinstance(response, dict):
1✔
823
            try:
1✔
824
                response = json.dumps(response)
1✔
825
            except ValueError:
×
826
                response = str(response)
×
827

828
        for name, value in self.extra_args.items():
1✔
829
            if isinstance(value, dict):
1✔
830
                for inner_name, inner_value in value.items():
×
831
                    response = response.replace(
×
832
                        str(inner_value),
833
                        _("***Secret value for %(name)s***")
834
                        % {"name": name + "." + inner_name},
835
                    )
836
            elif value != "":
1✔
837
                response = response.replace(
1✔
838
                    str(value), _("***Secret value for %(name)s***") % {"name": name}
839
                )
840

841
            if name == "Authorization" and value.startswith("Basic"):
1✔
842
                response.replace(
×
843
                    base64.b64encode(value.split(" ", 1)[1].encode("ascii")).decode(
844
                        "ascii"
845
                    ),
846
                    "BASE64 ENCODED SECRET",
847
                )
848

849
        return response
1✔
850

851
    objects = IntegrationManager()
1✔
852
    inactive = IntegrationInactiveManager()
1✔
853

854

855
@receiver(post_delete, sender=Integration)
1✔
856
def delete_schedule(sender, instance, **kwargs):
1✔
857
    Schedule.objects.filter(name=instance.schedule_name).delete()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc