Repository: kedro-org/kedro
Pull request: Experiment Tracking: Store username in the session store
Signed-off-by: SajidAlamQB <90610031+SajidAlamQB@users.noreply.github.com>

## Description
<!-- Why was this PR created? -->
https://github.com/kedro-org/kedro/issues/1298

As part of Experiment Tracking, users have indicated that it's important for them to be able to log the username of the person executing the run.

These fields already appear on the UI, so we just need to make sure they're stored in the Session store.


## Development notes
<!-- What have you changed, and how has this been tested? -->
Using `getpass.getuser()` to get the username from environment.

## Checklist

- [x] Read the [contributing](https://github.com/kedro-org/kedro/blob/main/CONTRIBUTING.md) guidelines
- [ ] Opened this PR as a 'Draft Pull Request' if it is work-in-progress
- [ ] Updated the documentation to reflect the code changes
- [x] Added a description of this change in the [`RELEASE.md`](https://github.com/kedro-org/kedro/blob/main/RELEASE.md) file
- [x] Added tests to cover my changes


<a href="https://gitpod.io/#https://github.com/kedro-org/kedro/pull/1366"><img src="https://gitpod.io/button/open-in-gitpod.svg"/></a>



Problem statement:
Experiment Tracking: Store username in the session store
As part of Experiment Tracking, users have indicated that it's important for them to be able to log the gitsha and username of the person executing the run. 

These fields already appear on the UI, so we just need to make sure they're stored in the Session store.

_Update: we already log git data if it's a git project_
- ~git_sha~
- username
- ~optional: git branch - check if wanted~

Hints from issue discussion:

Commit message: lint

Signed-off-by: SajidAlamQB <90610031+SajidAlamQB@users.noreply.github.com>

Patch to review:
diff --git a/kedro/framework/session/session.py b/kedro/framework/session/session.py
index 2adcf7b32c..baf308768b 100644
--- a/kedro/framework/session/session.py
+++ b/kedro/framework/session/session.py
@@ -190,6 +190,8 @@ def create(  # pylint: disable=too-many-arguments
         if extra_params:
             session_data["extra_params"] = extra_params
 
+        session_data["username"] = os.environ.get("USER") or os.environ.get("USERNAME")
+
         session._store.update(session_data)
 
         # we need a ConfigLoader registered in order to be able to set up logging
diff --git a/tests/framework/session/test_session.py b/tests/framework/session/test_session.py
index 938f891cc4..dd7deb9f00 100644
--- a/tests/framework/session/test_session.py
+++ b/tests/framework/session/test_session.py
@@ -204,6 +204,13 @@ def fake_project(tmp_path, local_logging_config, mock_package_name):
     return fake_project_dir
 
 
+@pytest.fixture
+def fake_username(monkeypatch):
+    username = "user1"
+    monkeypatch.setenv("USER", username)
+    return username
+
+
 class FakeException(Exception):
     """Fake exception class for testing purposes"""
 
@@ -225,6 +232,7 @@ def test_create(
         mocker,
         env,
         extra_params,
+        fake_username,
     ):
         mock_click_ctx = mocker.patch("click.get_current_context").return_value
         session = KedroSession.create(
@@ -247,6 +255,8 @@ def test_create(
             expected_store["env"] = env
         if extra_params:
             expected_store["extra_params"] = extra_params
+        if fake_username:
+            expected_store["username"] = fake_username
 
         assert session.store == expected_store
         # called for logging setup
@@ -267,6 +277,7 @@ def test_create_no_env_extra_params(
         fake_session_id,
         mock_package_name,
         mocker,
+        fake_username,
     ):
         mock_click_ctx = mocker.patch("click.get_current_context").return_value
         session = KedroSession.create(mock_package_name, fake_project)
@@ -284,6 +295,9 @@ def test_create_no_env_extra_params(
             "cli": expected_cli_data,
         }
 
+        if fake_username:
+            expected_store["username"] = fake_username
+
         assert session.store == expected_store
         mock_context_class.assert_called_once_with(
             project_path=fake_project,


Code review comments:
diff_hunk: @@ -190,6 +190,8 @@ def create(  # pylint: disable=too-many-arguments
         if extra_params:
             session_data["extra_params"] = extra_params
 
+        session_data["username"] = os.environ.get("USER") or os.environ.get("USERNAME")
path: kedro/framework/session/session.py
text: @user1:
Alternatively, you could use [`getpass.getuser()`](https://docs.python.org/3/library/getpass.html#getpass.getuser), which checks all environment variables that could store the logged in user.

diff_hunk: @@ -247,6 +255,8 @@ def test_create(
             expected_store["env"] = env
         if extra_params:
             expected_store["extra_params"] = extra_params
+        if fake_username:
path: tests/framework/session/test_session.py
text: @user1:
I don't think you need the if statement here. We use it for the `extra_params` because those are parameterised and some values are `None`, but afaik the `fake_username` is used in all cases.