Coverage for harbor_cli/output/table/artifact.py: 50%

46 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3from typing import Sequence 

4 

5from harborapi.ext.artifact import ArtifactInfo 

6from harborapi.models.models import Artifact 

7from rich.table import Table 

8 

9from ..formatting.builtin import float_str 

10from ..formatting.builtin import int_str 

11from ..formatting.builtin import str_str 

12from ..formatting.bytes import bytesize_str 

13from ..formatting.dates import datetime_str 

14from ._utils import get_table 

15 

16 

17def artifact_table(artifacts: Sequence[Artifact]) -> Table: 

18 """Display one or more repositories in a table.""" 

19 table = get_table("Artifact", artifacts) 

20 table.add_column("ID") 

21 table.add_column("Project ID") 

22 table.add_column("Repository ID") 

23 table.add_column("Digest", overflow="fold") 

24 table.add_column("Created") 

25 table.add_column("Size") 

26 for artifact in artifacts: 

27 table.add_row( 

28 int_str(artifact.id), 

29 int_str(artifact.project_id), 

30 int_str(artifact.repository_id), 

31 str_str(artifact.digest), 

32 datetime_str(artifact.push_time), 

33 bytesize_str(artifact.size or 0), 

34 ) 

35 return table 

36 

37 

38def artifactinfo_table(artifacts: Sequence[ArtifactInfo]): 

39 """Display one or more artifacts in a table.""" 

40 table = table = get_table("Artifact", artifacts) 

41 table.add_column("Project") 

42 table.add_column("Repository") 

43 table.add_column("Tags") 

44 table.add_column("Digest") 

45 table.add_column("Created") 

46 table.add_column("Size") 

47 for artifact in artifacts: 

48 table.add_row( 

49 str_str(artifact.project_name), 

50 str_str(artifact.repository_name), 

51 str_str(artifact.tags), 

52 str_str(artifact.artifact.digest), 

53 datetime_str(artifact.artifact.push_time), 

54 bytesize_str(artifact.artifact.size or 0), 

55 ) 

56 return table 

57 

58 

59def artifact_vulnerabilities_table(artifact: ArtifactInfo): 

60 vulns = artifact.report.vulnerabilities 

61 table = table = get_table("Vulnerability", vulns, show_lines=True) 

62 table.add_column("CVE ID") 

63 table.add_column("Severity") 

64 table.add_column("Score") 

65 table.add_column("Package") 

66 table.add_column("Version", overflow="fold") 

67 table.add_column("Fix Version", overflow="fold") 

68 table.add_column("Description") 

69 for vulnerability in vulns: 

70 table.add_row( 

71 str_str(vulnerability.id), 

72 str_str(vulnerability.severity.value), 

73 float_str(vulnerability.get_cvss_score()), 

74 str_str(vulnerability.package), 

75 str_str(vulnerability.version), 

76 str_str(vulnerability.fix_version), 

77 str_str(vulnerability.description), 

78 ) 

79 return table