Skip to content

Commit c2bdbc1

Browse files
VADC-904 (#134)
* add username to workflow details Co-authored-by: pieterlukasse <pieterlukasse@gmail.com>
1 parent ae204e5 commit c2bdbc1

File tree

3 files changed

+57
-33
lines changed

3 files changed

+57
-33
lines changed

src/argowrapper/engine/helpers/argo_engine_helper.py

+48-6
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ def parse_common_details(
5151
elif workflow_type == "archived_workflow":
5252
pass
5353

54+
user_name_str = ""
55+
if workflow_details["metadata"].get("labels"):
56+
user_name_str = convert_username_label_to_gen3username(
57+
workflow_details["metadata"].get("labels").get(GEN3_USER_METADATA_LABEL)
58+
)
59+
5460
return {
5561
"name": workflow_details["metadata"].get("name"),
5662
"phase": phase,
63+
GEN3_USER_METADATA_LABEL: user_name_str,
5764
"submittedAt": workflow_details["metadata"].get("creationTimestamp"),
5865
"startedAt": workflow_details["status"].get("startedAt"),
5966
"finishedAt": workflow_details["status"].get("finishedAt"),
@@ -72,12 +79,18 @@ def parse_details(
7279
result["arguments"] = workflow_details["spec"].get("arguments")
7380
result["progress"] = workflow_details["status"].get("progress")
7481
result["outputs"] = workflow_details["status"].get("outputs", {})
75-
result[GEN3_USER_METADATA_LABEL] = (
76-
workflow_details["metadata"].get("labels").get(GEN3_USER_METADATA_LABEL)
77-
)
78-
result[GEN3_TEAM_PROJECT_METADATA_LABEL] = convert_pod_label_to_gen3teamproject(
79-
workflow_details["metadata"].get("labels").get(GEN3_TEAM_PROJECT_METADATA_LABEL)
80-
)
82+
if workflow_details["metadata"].get("labels"):
83+
result[GEN3_USER_METADATA_LABEL] = convert_username_label_to_gen3username(
84+
workflow_details["metadata"].get("labels").get(GEN3_USER_METADATA_LABEL)
85+
)
86+
result[GEN3_TEAM_PROJECT_METADATA_LABEL] = convert_pod_label_to_gen3teamproject(
87+
workflow_details["metadata"]
88+
.get("labels")
89+
.get(GEN3_TEAM_PROJECT_METADATA_LABEL)
90+
)
91+
else:
92+
result[GEN3_USER_METADATA_LABEL] = None
93+
result[GEN3_TEAM_PROJECT_METADATA_LABEL] = None
8194
return result
8295

8396

@@ -212,3 +225,32 @@ def get_username_from_token(header_and_or_token: str) -> str:
212225
username = decoded.get("context", {}).get("user", {}).get("name")
213226
logger.info(f"{username} is submitting a workflow")
214227
return username
228+
229+
230+
def convert_username_label_to_gen3username(label: str) -> str:
231+
"""this function will reverse the conversion of a username to label as
232+
defined by the convert_gen3username_to_pod_label function. eg "user--21" -> "!"
233+
234+
Args:
235+
label (str): _description_
236+
237+
Returns:
238+
: _description_
239+
"""
240+
if label:
241+
label = label.replace("user-", "", 1)
242+
regex = r"-[0-9A-Za-z]{2}"
243+
return re.sub(regex, _convert_to_label, label)
244+
else:
245+
return ""
246+
247+
248+
def _convert_to_label(special_character_match: str) -> str:
249+
if match := special_character_match.group():
250+
match = match.strip("-")
251+
try:
252+
byte_array = bytearray.fromhex(match)
253+
return byte_array.decode()
254+
except:
255+
logger.info("match is not hex value, return original")
256+
return "-" + match

test/test_argo_engine.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def test_argo_engine_get_workflows_for_user_and_team_projects_suceeded():
293293
"uid": "uid_one",
294294
"creationTimestamp": "2023-03-22T16:48:51Z",
295295
"labels": {
296-
GEN3_USER_METADATA_LABEL: "dummyuser",
296+
GEN3_USER_METADATA_LABEL: "user-cdis-2edummy-test-40gmail-2ecom",
297297
GEN3_TEAM_PROJECT_METADATA_LABEL: "",
298298
},
299299
},
@@ -312,7 +312,6 @@ def test_argo_engine_get_workflows_for_user_and_team_projects_suceeded():
312312
"uid": "uid_2",
313313
"creationTimestamp": "2023-03-22T17:47:51Z",
314314
"labels": {
315-
GEN3_USER_METADATA_LABEL: "dummyuser",
316315
GEN3_TEAM_PROJECT_METADATA_LABEL: argo_engine_helper.convert_gen3teamproject_to_pod_label(
317316
"dummyteam"
318317
),
@@ -356,7 +355,7 @@ def test_argo_engine_get_workflows_for_user_and_team_projects_suceeded():
356355
"uid": "uid_3",
357356
"creationTimestamp": "2023-03-22T19:59:59Z",
358357
"labels": {
359-
GEN3_USER_METADATA_LABEL: "dummyuser",
358+
GEN3_USER_METADATA_LABEL: "",
360359
GEN3_TEAM_PROJECT_METADATA_LABEL: argo_engine_helper.convert_gen3teamproject_to_pod_label(
361360
"dummyteam"
362361
),
@@ -409,6 +408,10 @@ def test_argo_engine_get_workflows_for_user_and_team_projects_suceeded():
409408
assert "Canceled" == uniq_workflow_list[0]["phase"]
410409
assert "custom_name_active1" == uniq_workflow_list[0]["wf_name"]
411410
assert "2023-03-22T16:48:51Z" == uniq_workflow_list[0]["submittedAt"]
411+
assert (
412+
"cdis.dummy-test@gmail.com"
413+
== uniq_workflow_list[0][GEN3_USER_METADATA_LABEL]
414+
)
412415
assert (
413416
GEN3_USER_METADATA_LABEL
414417
in engine.api_instance.list_workflows.call_args[1][
@@ -449,6 +452,7 @@ def test_argo_engine_get_workflows_for_user_and_team_projects_suceeded():
449452
assert "custom_name_active2" == uniq_workflow_list[0]["wf_name"]
450453
assert "custom_name_archived" == uniq_workflow_list[1]["wf_name"]
451454
assert "2023-03-22T19:59:59Z" == uniq_workflow_list[1]["submittedAt"]
455+
assert "" == uniq_workflow_list[1][GEN3_USER_METADATA_LABEL]
452456

453457

454458
def test_argo_engine_get_workflows_for_user_failed():

test/test_argo_engine_helper.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,6 @@
1212
from test.constants import EXAMPLE_AUTH_HEADER, EXAMPLE_JUST_TOKEN
1313

1414

15-
def _convert_to_label(special_character_match: str) -> str:
16-
if match := special_character_match.group():
17-
match = match.strip("-")
18-
byte_array = bytearray.fromhex(match)
19-
return byte_array.decode()
20-
21-
22-
def convert_username_label_to_gen3username(label: str) -> str:
23-
"""this function will reverse the conversion of a username to label as
24-
defined by the convert_gen3username_to_pod_label function. eg "user--21" -> "!"
25-
26-
Args:
27-
label (str): _description_
28-
29-
Returns:
30-
: _description_
31-
"""
32-
label = label.replace("user-", "", 1)
33-
regex = r"-[0-9A-Za-z]{2}"
34-
return re.sub(regex, _convert_to_label, label)
35-
36-
3715
@pytest.fixture(scope="module")
3816
def setup():
3917
print("*****SETUP*****")
@@ -48,7 +26,7 @@ def setup():
4826

4927
UsernameLabelPair = namedtuple("UsernameLabelPair", "username label")
5028
user_label_data = [
51-
UsernameLabelPair("abc123", "user-abc123"),
29+
UsernameLabelPair("abc123-test", "user-abc123-2dtest"),
5230
UsernameLabelPair("48@!(CEab***", "user-48-40-21-28CEab-2a-2a-2a"),
5331
UsernameLabelPair("-scott.VA@gmail.com", "user--2dscott-2eVA-40gmail-2ecom"),
5432
]
@@ -61,7 +39,7 @@ def test_convert_username_label_to_gen3username(username, label):
6139

6240
@pytest.mark.parametrize("username,label", user_label_data)
6341
def test_convert_gen3username_to_pod_label(username, label):
64-
assert convert_username_label_to_gen3username(label) == username
42+
assert argo_engine_helper.convert_username_label_to_gen3username(label) == username
6543

6644

6745
def test_convert_gen3teamproject_to_pod_label_and_back():

0 commit comments

Comments
 (0)