Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn off Telemetry in CI. #50

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@ 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:
raise RuntimeError('The first instantiation of the Telemetry should be done with the '
'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:
Expand Down
1 change: 1 addition & 0 deletions src/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 22 additions & 2 deletions src/utils/opt_in_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading