Skip to content

Commit b91b90d

Browse files
committed
Move the user id from project name to a label #387
Signed-off-by: tdruez <[email protected]>
1 parent 4c82a1d commit b91b90d

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

component_catalog/tests/test_scancodeio.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,31 @@ def test_scancodeio_submit_scan_task(self, mock_submit_scan, mock_request_head):
7676
@mock.patch("requests.sessions.Session.get")
7777
def test_scancodeio_fetch_scan_list(self, mock_session_get):
7878
scancodeio = ScanCodeIO(self.dataspace)
79-
self.assertIsNone(scancodeio.fetch_scan_list())
80-
self.assertFalse(mock_session_get.called)
79+
dataspace_uid = get_hash_uid(self.dataspace.uuid)
80+
user_uid = get_hash_uid(self.basic_user.uuid)
8181

82-
scancodeio.fetch_scan_list(user=self.basic_user)
82+
scancodeio.fetch_scan_list()
8383
params = mock_session_get.call_args.kwargs["params"]
84-
expected = {"format": "json", "name__endswith": get_hash_uid(self.basic_user.uuid)}
84+
expected = {
85+
"format": "json",
86+
"name__contains": dataspace_uid,
87+
}
8588
self.assertEqual(expected, params)
8689

87-
scancodeio.fetch_scan_list(dataspace=self.basic_user.dataspace)
90+
scancodeio.fetch_scan_list(user=self.basic_user)
8891
params = mock_session_get.call_args.kwargs["params"]
8992
expected = {
9093
"format": "json",
91-
"name__contains": get_hash_uid(self.basic_user.dataspace.uuid),
94+
"name__contains": dataspace_uid,
95+
"label": user_uid,
9296
}
9397
self.assertEqual(expected, params)
9498

95-
scancodeio.fetch_scan_list(
96-
user=self.basic_user,
97-
dataspace=self.basic_user.dataspace,
98-
extra_params="extra",
99-
)
99+
scancodeio.fetch_scan_list(extra_params="extra")
100100
params = mock_session_get.call_args.kwargs["params"]
101101
expected = {
102102
"format": "json",
103-
"name__contains": get_hash_uid(self.basic_user.dataspace.uuid),
104-
"name__endswith": get_hash_uid(self.basic_user.uuid),
103+
"name__contains": get_hash_uid(self.dataspace.uuid),
105104
"extra_params": "extra",
106105
}
107106
self.assertEqual(expected, params)
@@ -115,16 +114,11 @@ def test_scancodeio_fetch_scan_info(self, mock_session_get):
115114
params = mock_session_get.call_args.kwargs["params"]
116115
expected = {
117116
"name__startswith": get_hash_uid(uri),
118-
"name__contains": get_hash_uid(self.basic_user.dataspace.uuid),
117+
"name__contains": get_hash_uid(self.dataspace.uuid),
119118
"format": "json",
120119
}
121120
self.assertEqual(expected, params)
122121

123-
scancodeio.fetch_scan_info(uri=uri, user=self.basic_user)
124-
params = mock_session_get.call_args.kwargs["params"]
125-
expected["name__endswith"] = get_hash_uid(self.basic_user.uuid)
126-
self.assertEqual(expected, params)
127-
128122
@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.request_get")
129123
def test_scancodeio_find_project(self, mock_request_get):
130124
scancodeio = ScanCodeIO(self.dataspace)

component_catalog/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,6 @@ def get_queryset(self):
17421742
scancodeio = ScanCodeIO(dataspace)
17431743
self.list_data = (
17441744
scancodeio.fetch_scan_list(
1745-
dataspace=dataspace,
17461745
user=user if self.request.GET.get("created_by_me") else None,
17471746
**filters,
17481747
)

dejacode_toolkit/scancodeio.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,21 @@ def get_project_info(self, download_url):
6262
return scan_info.get("results")[0]
6363

6464
def submit_scan(self, uri, user_uuid, dataspace_uuid):
65+
"""
66+
Submit package scan request to ScanCode.io.
67+
An unique ID for the user is set as a project label, available for filtering.
68+
"""
69+
webhook_url = get_webhook_url("notifications:send_scan_notification", user_uuid)
70+
6571
data = {
66-
"name": get_project_name(uri, user_uuid, dataspace_uuid),
72+
"name": get_project_name(uri, dataspace_uuid),
6773
"input_urls": uri,
6874
"pipeline": "scan_single_package",
6975
"execute_now": True,
76+
"webhook_url": webhook_url,
77+
"labels": [get_hash_uid(user_uuid)],
7078
}
7179

72-
webhook_url = get_webhook_url("notifications:send_scan_notification", user_uuid)
73-
data["webhook_url"] = webhook_url
74-
7580
logger.debug(f'{self.label}: submit scan uri="{uri}" webhook_url="{webhook_url}"')
7681
return self.request_post(url=self.project_api_url, json=data)
7782

@@ -102,14 +107,11 @@ def start_pipeline(self, run_url):
102107
start_pipeline_url = run_url + "start_pipeline/"
103108
return self.request_post(url=start_pipeline_url)
104109

105-
def fetch_scan_list(self, user=None, dataspace=None, **extra_payload):
106-
payload = {}
107-
108-
if dataspace:
109-
payload["name__contains"] = get_hash_uid(dataspace.uuid)
110+
def fetch_scan_list(self, user=None, **extra_payload):
111+
payload = {"name__contains": get_hash_uid(self.dataspace.uuid)}
110112

111113
if user:
112-
payload["name__endswith"] = get_hash_uid(user.uuid)
114+
payload["label"] = get_hash_uid(user.uuid)
113115

114116
payload.update(extra_payload)
115117
if not payload:
@@ -131,15 +133,11 @@ def find_project(self, **kwargs):
131133
if response.get("count") == 1:
132134
return response.get("results")[0]
133135

134-
def fetch_scan_info(self, uri, user=None):
136+
def fetch_scan_info(self, uri):
135137
payload = {
136138
"name__startswith": get_hash_uid(uri),
137139
"name__contains": get_hash_uid(self.dataspace.uuid),
138140
}
139-
140-
if user:
141-
payload["name__endswith"] = get_hash_uid(user.uuid)
142-
143141
logger.debug(f'{self.label}: fetch scan info uri="{uri}"')
144142
return self.request_get(url=self.project_api_url, params=payload)
145143

@@ -491,18 +489,16 @@ def get_hash_uid(value):
491489
return md5(str(value).encode("utf-8"), usedforsecurity=False).hexdigest()[:10]
492490

493491

494-
def get_project_name(uri, user_uuid, dataspace_uuid):
492+
def get_project_name(uri, dataspace_uuid):
495493
"""
496494
Return a project name based on a hash of the provided `uri` combined with a hash
497-
of the `user_uuid` and `dataspace_uuid`.
495+
of the `dataspace_uuid`.
498496
499-
project_name = "uri_hash.dataspace_uuid_hash.user_uuid_hash"
497+
project_name = "uri_hash.dataspace_uuid_hash"
500498
"""
501499
uri_hash = get_hash_uid(uri)
502500
dataspace_hash = get_hash_uid(dataspace_uuid)
503-
user_hash = get_hash_uid(user_uuid)
504-
505-
return f"{uri_hash}.{dataspace_hash}.{user_hash}"
501+
return f"{uri_hash}.{dataspace_hash}"
506502

507503

508504
def get_webhook_url(view_name, user_uuid):

0 commit comments

Comments
 (0)