From 69eeae5606c118254c6c4827217c8d5d817ea99f Mon Sep 17 00:00:00 2001 From: Nicholas Albion Date: Fri, 8 Sep 2023 03:03:38 +1000 Subject: [PATCH] attempt to get email from ~/.gitconfig --- pilot/utils/arguments.py | 26 +++++++++++++++++++++++--- pilot/utils/test_arguments.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 pilot/utils/test_arguments.py diff --git a/pilot/utils/arguments.py b/pilot/utils/arguments.py index 8590373..b1f9b92 100644 --- a/pilot/utils/arguments.py +++ b/pilot/utils/arguments.py @@ -1,3 +1,5 @@ +import os +import re import sys import uuid @@ -39,9 +41,7 @@ def get_arguments(): arguments['user_id'] = str(uuid.uuid4()) if 'email' not in arguments: - # todo change email so its not uuid4 but make sure to fix storing of development steps where - # 1 user can have multiple apps. In that case each app should have its own development steps - arguments['email'] = str(uuid.uuid4()) + arguments['email'] = get_email() if 'password' not in arguments: arguments['password'] = 'password' @@ -54,3 +54,23 @@ def get_arguments(): print(colored(f'python main.py app_id={arguments["app_id"]}', 'green', attrs=['bold'])) print(colored('--------------------------------------------------------------\n', 'green', attrs=['bold'])) return arguments + + +def get_email(): + # Attempt to get email from .gitconfig + gitconfig_path = os.path.expanduser('~/.gitconfig') + + if os.path.exists(gitconfig_path): + with open(gitconfig_path, 'r') as file: + content = file.read() + + # Use regex to search for email address + email_match = re.search(r'email\s*=\s*([\w\.-]+@[\w\.-]+)', content) + + if email_match: + return email_match.group(1) + + # If not found, return a UUID + # todo change email so its not uuid4 but make sure to fix storing of development steps where + # 1 user can have multiple apps. In that case each app should have its own development steps + return str(uuid.uuid4()) diff --git a/pilot/utils/test_arguments.py b/pilot/utils/test_arguments.py new file mode 100644 index 0000000..3992826 --- /dev/null +++ b/pilot/utils/test_arguments.py @@ -0,0 +1,34 @@ +import pytest +from unittest.mock import patch, mock_open +import uuid +from .arguments import get_email + + +def test_email_found_in_gitconfig(): + mock_file_content = """ + [user] + name = test_user + email = test@example.com + """ + with patch('builtins.open', mock_open(read_data=mock_file_content)): + assert get_email() == "test@example.com" + + +def test_email_not_found_in_gitconfig(): + mock_file_content = """ + [user] + name = test_user + """ + mock_uuid = "12345678-1234-5678-1234-567812345678" + + with patch('builtins.open', mock_open(read_data=mock_file_content)): + with patch.object(uuid, "uuid4", return_value=mock_uuid): + assert get_email() == mock_uuid + + +def test_gitconfig_not_present(): + mock_uuid = "12345678-1234-5678-1234-567812345678" + + with patch('os.path.exists', return_value=False): + with patch.object(uuid, "uuid4", return_value=mock_uuid): + assert get_email() == mock_uuid