Skip to content

Commit 41e56f4

Browse files
committed
update ver_status on each new fetch data
Signed-off-by: Vivek Kumar Sahu <vivekkumarsahu650@gmail.com>
1 parent 47307d5 commit 41e56f4

File tree

2 files changed

+81
-20
lines changed

2 files changed

+81
-20
lines changed

lynkctx.py

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import base64
55
import requests
66

7-
# INTERLYNK_API_URL = 'https://api.interlynk.io/lynkapi'
8-
9-
INTERLYNK_API_URL = 'http://localhost:3000/lynkapi'
7+
INTERLYNK_API_URL = 'https://api.interlynk.io/lynkapi'
108

119
INTERLYNK_API_TIMEOUT = 100
1210

@@ -229,20 +227,16 @@ def resolve_env(self):
229227

230228
def resolve_ver(self):
231229
env = self.env or 'default'
232-
self.data = self._fetch_context()
233-
234230
if not self.ver_id:
235231
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
236232
if product['id'] == self.prod_id:
237233
for env in product['environments']:
238234
if env['id'] == self.env_id:
239235
for ver in env['versions']:
240-
if ver.get('primaryComponent') and ver['primaryComponent'].get('version') == self.ver:
236+
if ver['primaryComponent']['version'] == self.ver:
241237
self.ver_id = ver['id']
242238
self.ver_status = self.vuln_status_to_status(
243-
ver['vulnRunStatus']
244-
)
245-
239+
ver['vulnRunStatus'])
246240
empty_ver = False
247241
if not self.ver:
248242
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
@@ -251,13 +245,11 @@ def resolve_ver(self):
251245
if env['id'] == self.env_id:
252246
for ver in env['versions']:
253247
if ver['id'] == self.ver_id:
254-
if ver.get('primaryComponent'):
255-
self.ver = ver['primaryComponent'].get('version')
248+
self.ver = ver['primaryComponent']['version']
256249
if not self.ver:
257250
empty_ver = True
258251
self.ver_status = self.vuln_status_to_status(
259-
ver['vulnRunStatus']
260-
)
252+
ver['vulnRunStatus'])
261253

262254
return (empty_ver or self.ver) and self.ver_id
263255

@@ -296,6 +288,11 @@ def versions(self):
296288
def status(self):
297289
self.resolve_ver()
298290
return self.ver_status
291+
292+
def live_status(self):
293+
self.resolve_ver_status()
294+
return self.ver_status
295+
299296

300297
def download(self):
301298
logging.debug("Downloading SBOM for environment ID %s, sbom ID %s",
@@ -447,3 +444,60 @@ def vuln_status_to_status(self, status):
447444
result_dict['labelingStatus'] = 'COMPLETED'
448445
result_dict['automationStatus'] = 'COMPLETED'
449446
return result_dict
447+
448+
449+
def resolve_ver_status(self):
450+
"""
451+
Resolve version ID (ver_id) and version (ver) for the current context.
452+
"""
453+
self.data = self._fetch_context()
454+
455+
# ver_id is present
456+
if self.ver_id:
457+
self._update_ver_status()
458+
return self.ver_id
459+
460+
# ver_id is not present
461+
self._resolve_ver_id()
462+
463+
# ver is not present
464+
if not self.ver:
465+
self._resolve_ver_value()
466+
467+
return self.ver_id and self.ver
468+
469+
def _update_ver_status(self):
470+
"""Update the status of the version based on ver_id."""
471+
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
472+
if product['id'] == self.prod_id:
473+
for env in product['environments']:
474+
if env['id'] == self.env_id:
475+
for ver in env['versions']:
476+
if ver['id'] == self.ver_id:
477+
self.ver_status = self.vuln_status_to_status(ver['vulnRunStatus'])
478+
479+
def _resolve_ver_id(self):
480+
"""Resolve ver_id based on ver."""
481+
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
482+
if product['id'] == self.prod_id:
483+
for env in product['environments']:
484+
if env['id'] == self.env_id:
485+
for ver in env['versions']:
486+
if ver.get('primaryComponent') and ver['primaryComponent'].get('version') == self.ver:
487+
self.ver_id = ver['id']
488+
self.ver_status = self.vuln_status_to_status(ver['vulnRunStatus'])
489+
490+
def _resolve_ver_value(self):
491+
"""Resolve the version value (ver) based on ver_id."""
492+
empty_ver = False
493+
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
494+
if product['id'] == self.prod_id:
495+
for env in product['environments']:
496+
if env['id'] == self.env_id:
497+
for ver in env['versions']:
498+
if ver['id'] == self.ver_id:
499+
if ver.get('primaryComponent'):
500+
self.ver = ver['primaryComponent'].get('version')
501+
if not self.ver:
502+
empty_ver = True
503+
return empty_ver

pylynk.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def download_sbom(lynk_ctx):
213213
return 0
214214

215215

216-
def upload_sbom(lynk_ctx, sbom_file):
216+
def upload_sbom(lynk_ctx, sbom_file, download):
217217
"""
218218
Upload SBOM to the lynk_ctx.
219219
@@ -224,22 +224,27 @@ def upload_sbom(lynk_ctx, sbom_file):
224224
Returns:
225225
The result of the upload operation.
226226
"""
227-
# return lynk_ctx.upload(sbom_file)
228227
upload_result = lynk_ctx.upload(sbom_file)
229228
if upload_result != 0:
230229
return 1
231230

232-
if True:
233-
while True:
234-
status = lynk_ctx.status()
231+
if download:
232+
max_retries = 5 # Set the maximum number of retries
233+
retries = 0
234+
while retries < max_retries:
235+
status = lynk_ctx.live_status()
235236

236237
if status.get('automationStatus') == "COMPLETED":
237238
download_sbom(lynk_ctx)
238239
break
239240
else:
240-
print("Waiting for automation status to complete...")
241241
time.sleep(5)
242+
retries += 1
242243

244+
if retries == max_retries:
245+
print("Error: automationStatus could not be completed within the maximum retry limit.")
246+
return 1
247+
243248
return 0
244249

245250

@@ -306,6 +311,7 @@ def setup_args():
306311
upload_parser.add_argument("--token",
307312
required=False,
308313
help="Security token")
314+
upload_parser.add_argument("--download", action="store_true", help="Download SBOM after upload (default: False)")
309315

310316
download_parser = subparsers.add_parser("download", help="Download SBOM")
311317
download_group = download_parser.add_mutually_exclusive_group(
@@ -390,7 +396,8 @@ def main() -> int:
390396
elif args.subcommand == "status":
391397
print_status(lynk_ctx, fmt_json)
392398
elif args.subcommand == "upload":
393-
upload_sbom(lynk_ctx, args.sbom)
399+
download_flag = getattr(args, 'download', False)
400+
upload_sbom(lynk_ctx, args.sbom, download_flag)
394401
elif args.subcommand == "download":
395402
download_sbom(lynk_ctx)
396403
else:

0 commit comments

Comments
 (0)