From 36e12b784cedabe2c09b0ccdf61acbc0b4d869e3 Mon Sep 17 00:00:00 2001 From: Anastasiia Pnevskaia Date: Wed, 18 Oct 2023 15:01:54 +0200 Subject: [PATCH] Turn off Telemetry in CI. (#50) * Added check if CI env vars are set. * Minor correction. * Test fix. * Flag to turn off telemetry in CI. * Minor correction. * Minor corrections. * Check CI true. * Added comment. --- src/main.py | 9 +++++---- src/main_test.py | 1 + src/utils/opt_in_checker.py | 24 ++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main.py b/src/main.py index 2fee5cd..8181d7b 100644 --- a/src/main.py +++ b/src/main.py @@ -42,9 +42,10 @@ class Telemetry(metaclass=SingletonMetaClass): no telemetry is sent until user accepts telemetry with dialog. If enable_opt_in_dialog=False, telemetry is sent without opt-in dialog, unless user explicitly turned it off with opt_in_out script. + :param disable_in_ci: Turn off telemetry for CI jobs. """ def __init__(self, app_name: str = None, app_version: str = None, tid: str = None, - backend: [str, None] = 'ga', enable_opt_in_dialog=True): + backend: [str, None] = 'ga', enable_opt_in_dialog=True, disable_in_ci=False): # The case when instance is already configured if app_name is None: if not hasattr(self, 'sender') or self.sender is None: @@ -52,12 +53,12 @@ def __init__(self, app_name: str = None, app_version: str = None, tid: str = Non 'application name, version and TID.') return - self.init(app_name, app_version, tid, backend, enable_opt_in_dialog) + self.init(app_name, app_version, tid, backend, enable_opt_in_dialog, disable_in_ci) def init(self, app_name: str = None, app_version: str = None, tid: str = None, - backend: [str, None] = 'ga', enable_opt_in_dialog=True): + backend: [str, None] = 'ga', enable_opt_in_dialog=True, disable_in_ci=False): opt_in_checker = OptInChecker() - opt_in_check_result = opt_in_checker.check(enable_opt_in_dialog) + opt_in_check_result = opt_in_checker.check(enable_opt_in_dialog, disable_in_ci) if enable_opt_in_dialog: self.consent = opt_in_check_result == ConsentCheckResult.ACCEPTED else: diff --git a/src/main_test.py b/src/main_test.py index a17c1a3..54d0be1 100644 --- a/src/main_test.py +++ b/src/main_test.py @@ -56,6 +56,7 @@ class GeneralTelemetryTest(unittest.TestCase): def init_backend(self, test_dir, test_subdir): self.backend = BackendRegistry.get_backend('ga4')("test_backend", "NONE") OptInChecker.consent_file_base_dir = MagicMock(return_value=test_dir) + OptInChecker._run_in_ci = MagicMock(return_value=False) self.cid_path = os.path.join(test_subdir, self.backend.cid_filename) OptInChecker.consent_file_subdirectory = MagicMock(return_value=os.path.basename(test_subdir)) _ = Telemetry("a", "b", "c") diff --git a/src/utils/opt_in_checker.py b/src/utils/opt_in_checker.py index 335e72c..060423e 100644 --- a/src/utils/opt_in_checker.py +++ b/src/utils/opt_in_checker.py @@ -275,11 +275,31 @@ def _check_run_in_notebook(): pass return False - def check(self, enable_opt_in_dialog): + @staticmethod + def _run_in_ci(): + """ + Checks that script is executed in CI job. + :return: True if script is executed in CI job, otherwise False + """ + if "CI" in os.environ and os.environ["CI"].lower() == "true": + return True + + if "TF_BUILD" in os.environ and len(os.environ["TF_BUILD"]): + return True + + if "JENKINS_URL" in os.environ and len(os.environ["JENKINS_URL"]): + return True + + return False + + def check(self, enable_opt_in_dialog, disable_in_ci=False): """ Checks if user has accepted the collection of the information by checking the consent file. - :return: opt-in dialog result + :return: consent check result """ + if disable_in_ci and self._run_in_ci(): + return ConsentCheckResult.DECLINED + if not os.path.exists(self.consent_file()): if enable_opt_in_dialog: if not self._check_main_process():