diff --git a/shared/bundle_analysis/models.py b/shared/bundle_analysis/models.py index cfb3e892f..70f318ed1 100644 --- a/shared/bundle_analysis/models.py +++ b/shared/bundle_analysis/models.py @@ -112,7 +112,6 @@ def __enter__(self): def __exit__(self, type, value, traceback): self.session.close() - return True def _use_modern_sqlalchemy_session_manager(): diff --git a/shared/bundle_analysis/parsers/v1.py b/shared/bundle_analysis/parsers/v1.py index c6783e441..bf7020efc 100644 --- a/shared/bundle_analysis/parsers/v1.py +++ b/shared/bundle_analysis/parsers/v1.py @@ -91,7 +91,7 @@ def reset(self): self.chunk_list = [] self.module_list = [] - def parse(self, path: str) -> int: + def parse(self, path: str) -> Tuple[int, str]: try: self.reset() @@ -152,7 +152,7 @@ def parse(self, path: str) -> int: self._create_associations() assert self.session.bundle is not None - return self.session.id + return self.session.id, self.session.bundle.name except Exception as e: # Inject the plugin name to the Exception object so we have visibilitity on which plugin # is causing the trouble. diff --git a/shared/bundle_analysis/parsers/v2.py b/shared/bundle_analysis/parsers/v2.py index 3fc0b6638..0025fb137 100644 --- a/shared/bundle_analysis/parsers/v2.py +++ b/shared/bundle_analysis/parsers/v2.py @@ -92,7 +92,7 @@ def reset(self): self.chunk_list = [] self.module_list = [] - def parse(self, path: str) -> int: + def parse(self, path: str) -> Tuple[int, str]: try: self.reset() @@ -110,20 +110,6 @@ def parse(self, path: str) -> int: for event in ijson.parse(f): self._parse_event(event) - if self.asset_list: - insert_asset = Asset.__table__.insert().values(self.asset_list) - self.db_session.execute(insert_asset) - - if self.chunk_list: - insert_chunks = Chunk.__table__.insert().values(self.chunk_list) - self.db_session.execute(insert_chunks) - - if self.module_list: - insert_modules = Module.__table__.insert().values(self.module_list) - self.db_session.execute(insert_modules) - - self.db_session.flush() - # Delete old session/asset/chunk/module with the same bundle name if applicable old_session = ( self.db_session.query(Session) @@ -144,6 +130,20 @@ def parse(self, path: str) -> int: self.db_session.delete(old_session) self.db_session.flush() + if self.asset_list: + insert_asset = Asset.__table__.insert().values(self.asset_list) + self.db_session.execute(insert_asset) + + if self.chunk_list: + insert_chunks = Chunk.__table__.insert().values(self.chunk_list) + self.db_session.execute(insert_chunks) + + if self.module_list: + insert_modules = Module.__table__.insert().values(self.module_list) + self.db_session.execute(insert_modules) + + self.db_session.flush() + # save top level bundle stats info self.session.info = json.dumps(self.info) @@ -153,7 +153,7 @@ def parse(self, path: str) -> int: self._create_associations() assert self.session.bundle is not None - return self.session.id + return self.session.id, self.session.bundle.name except Exception as e: # Inject the plugin name to the Exception object so we have visibilitity on which plugin # is causing the trouble. diff --git a/shared/bundle_analysis/report.py b/shared/bundle_analysis/report.py index 9f08f0813..c35dc99c5 100644 --- a/shared/bundle_analysis/report.py +++ b/shared/bundle_analysis/report.py @@ -228,16 +228,26 @@ def _setup(self, db_session: DbSession): def cleanup(self): os.unlink(self.db_path) - def ingest(self, path: str) -> int: + def ingest(self, path: str, compare_sha: Optional[str] = None) -> Tuple[int, str]: """ Ingest the bundle stats JSON at the given file path. Returns session ID of ingested data. """ with get_db_session(self.db_path) as session: parser = Parser(path, session).get_proper_parser() - session_id = parser.parse(path) + session_id, bundle_name = parser.parse(path) + + # Save custom base commit SHA for doing comparisons if available + if compare_sha: + session.add( + Metadata( + key="compare_sha", + value=compare_sha, + ) + ) + session.commit() - return session_id + return session_id, bundle_name def _associate_bundle_report_assets_by_name( self, curr_bundle_report: BundleReport, prev_bundle_report: BundleReport diff --git a/tests/unit/bundle_analysis/test_bundle_analysis.py b/tests/unit/bundle_analysis/test_bundle_analysis.py index caebbb15c..6cfdc406c 100644 --- a/tests/unit/bundle_analysis/test_bundle_analysis.py +++ b/tests/unit/bundle_analysis/test_bundle_analysis.py @@ -12,6 +12,7 @@ AssetType, Bundle, Chunk, + Metadata, MetadataKey, Module, Session, @@ -58,8 +59,9 @@ def _table_rows_count(db_session: DbSession) -> Tuple[int]: def test_create_bundle_report(): try: report = BundleAnalysisReport() - session_id = report.ingest(sample_bundle_stats_path) + session_id, bundle_name = report.ingest(sample_bundle_stats_path) assert session_id == 1 + assert bundle_name == "sample" assert report.metadata() == { MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION, @@ -143,8 +145,9 @@ def test_create_bundle_report(): def test_bundle_report_asset_filtering(): try: report = BundleAnalysisReport() - session_id = report.ingest(sample_bundle_stats_path) + session_id, bundle_name = report.ingest(sample_bundle_stats_path) assert session_id == 1 + assert bundle_name == "sample" assert report.metadata() == { MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION, @@ -418,8 +421,9 @@ def test_bundle_file_save_unknown_error(): def test_create_bundle_report_v1(): try: report = BundleAnalysisReport() - session_id = report.ingest(sample_bundle_stats_path_4) + session_id, bundle_name = report.ingest(sample_bundle_stats_path_4) assert session_id == 1 + assert bundle_name == "sample" assert report.metadata() == { MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION, @@ -506,11 +510,17 @@ def test_create_bundle_report_v1(): def test_bundle_is_cached(): try: bundle_analysis_report = BundleAnalysisReport() - session_id = bundle_analysis_report.ingest(sample_bundle_stats_path) + session_id, bundle_name = bundle_analysis_report.ingest( + sample_bundle_stats_path + ) assert session_id == 1 + assert bundle_name == "sample" - session_id = bundle_analysis_report.ingest(sample_bundle_stats_path_5) + session_id, bundle_name = bundle_analysis_report.ingest( + sample_bundle_stats_path_5 + ) assert session_id == 2 + assert bundle_name == "sample2" assert bundle_analysis_report.metadata() == { MetadataKey.SCHEMA_VERSION: SCHEMA_VERSION, @@ -543,11 +553,17 @@ def test_bundle_deletion(): try: bundle_analysis_report = BundleAnalysisReport() with get_db_session(bundle_analysis_report.db_path) as db_session: - session_id = bundle_analysis_report.ingest(sample_bundle_stats_path) + session_id, bundle_name = bundle_analysis_report.ingest( + sample_bundle_stats_path + ) assert session_id == 1 + assert bundle_name == "sample" - session_id = bundle_analysis_report.ingest(sample_bundle_stats_path_5) + session_id, bundle_name = bundle_analysis_report.ingest( + sample_bundle_stats_path_5 + ) assert session_id == 2 + assert bundle_name == "sample2" assert _table_rows_count(db_session) == (2, 2, 10, 6, 62) @@ -576,3 +592,27 @@ def test_bundle_deletion(): assert len(res) == 0 finally: bundle_analysis_report.cleanup() + + +def test_create_bundle_report_without_and_with_compare_sha(): + try: + report = BundleAnalysisReport() + with get_db_session(report.db_path) as db_session: + session_id, bundle_name = report.ingest(sample_bundle_stats_path) + assert session_id == 1 + assert bundle_name == "sample" + + res = db_session.query(Metadata).filter_by(key="compare_sha").all() + assert len(res) == 0 + + session_id, bundle_name = report.ingest( + sample_bundle_stats_path, "compare_sha_123" + ) + assert session_id == 2 + assert bundle_name == "sample" + + res = db_session.query(Metadata).filter_by(key="compare_sha").all() + assert len(res) == 1 + assert res[0].value == "compare_sha_123" + finally: + report.cleanup()