Coverage for harbor_cli/output/table/project.py: 86%

62 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.models.models import CVEAllowlist 

6from harborapi.models.models import Project 

7from harborapi.models.models import ProjectMetadata 

8from rich.console import Group 

9from rich.panel import Panel 

10from rich.table import Table 

11 

12from ...logs import logger 

13from ...models import ProjectExtended 

14from ..formatting.builtin import int_str 

15from ..formatting.builtin import str_str 

16from ..formatting.dates import datetime_str 

17from ._utils import get_table 

18 

19 

20def project_table(p: Sequence[Project]) -> Table: 

21 """Display one or more projects in a table.""" 

22 table = get_table("Project", p) 

23 table.add_column("ID") 

24 table.add_column("Name") 

25 table.add_column("Public") 

26 table.add_column("Repositories") 

27 table.add_column("Created") 

28 for project in p: 

29 table.add_row( 

30 str(project.name), 

31 str(project.project_id), 

32 str(project.metadata.public) if project.metadata else "Unknown", 

33 str(project.repo_count), 

34 datetime_str(project.creation_time), 

35 ) 

36 return table 

37 

38 

39def project_extended_panel(p: Sequence[ProjectExtended]) -> Panel: 

40 """Display extended information about one or more projects.""" 

41 if len(p) > 1: 

42 logger.warning("This function should only be used to display a single project.") 

43 pt_table = project_extended_table(p) 

44 pmt_table = project_metadata_table([p.metadata for p in p if p.metadata]) 

45 # TODO: only render allowlist if allowlist is not empty 

46 cve_table = cveallowlist_table([p.cve_allowlist for p in p if p.cve_allowlist]) 

47 return Panel(Group(pt_table, pmt_table, cve_table), title=p[0].name, expand=True) 

48 

49 

50def project_extended_table(p: Sequence[ProjectExtended]) -> Table: 

51 table = get_table( 

52 "Project", 

53 p, 

54 columns=[ 

55 "ID", 

56 "Name", 

57 "Public", 

58 "Owner", 

59 "Repositories", 

60 "Charts", 

61 "Created", 

62 ], 

63 ) 

64 for project in p: 

65 table.add_row( 

66 int_str(project.project_id), 

67 str_str(project.name), 

68 str_str(project.metadata.public) if project.metadata else "Unknown", 

69 str_str(project.owner_name), 

70 int_str(project.repo_count), 

71 int_str(project.chart_count), 

72 datetime_str(project.creation_time), 

73 ) 

74 return table 

75 

76 

77def project_metadata_table(p: Sequence[ProjectMetadata]) -> Table: 

78 table = get_table("Project Metadata", p) 

79 table.add_column("Public") 

80 table.add_column("Content Trust") 

81 table.add_column("Content Trust Cosign") 

82 table.add_column("Vuln Prevention") 

83 table.add_column("Max Severity") 

84 table.add_column("Auto Scan") 

85 table.add_column("Reuse Sys CVE List") 

86 table.add_column("Retention ID") 

87 for metadata in p: 87 ↛ 88line 87 didn't jump to line 88, because the loop on line 87 never started

88 table.add_row( 

89 str_str(metadata.public), 

90 str_str(metadata.enable_content_trust), 

91 str_str(metadata.enable_content_trust_cosign), 

92 str_str(metadata.prevent_vul), 

93 str_str(metadata.severity), 

94 str_str(metadata.auto_scan), 

95 str_str(metadata.reuse_sys_cve_allowlist), 

96 str_str(metadata.retention_id), 

97 ) 

98 return table 

99 

100 

101def cveallowlist_table(c: Sequence[CVEAllowlist]) -> Table: 

102 table = get_table("CVE Allowlist", c) 

103 table.add_column("ID") 

104 table.add_column("Items") 

105 table.add_column("Expires") 

106 table.add_column("Created") 

107 table.add_column("Updated") 

108 for allowlist in c: 108 ↛ 109line 108 didn't jump to line 109, because the loop on line 108 never started

109 if allowlist.items: 

110 items = "\n".join( 

111 str_str(i.cve_id) for i in allowlist.items if i is not None 

112 ) 

113 else: 

114 items = str_str(None) 

115 

116 table.add_row( 

117 int_str(allowlist.id), 

118 items, 

119 datetime_str(allowlist.expires_at), 

120 datetime_str(allowlist.creation_time), 

121 datetime_str(allowlist.update_time), 

122 ) 

123 return table