diff --git a/swh/deposit/cli/client.py b/swh/deposit/cli/client.py --- a/swh/deposit/cli/client.py +++ b/swh/deposit/cli/client.py @@ -251,6 +251,20 @@ "Please provide an actionable command. See --help for more information" ) + if metadata: + from swh.deposit.utils import parse_xml + + metadata_raw = open(metadata, "r").read() + metadata_dict = parse_xml(metadata_raw).get("swh:deposit", {}) + if ( + "swh:create_origin" not in metadata_dict + and "swh:add_to_origin" not in metadata_dict + ): + logger.warning( + "The metadata file provided should contain " + '"" or "" tag', + ) + if replace and not deposit_id: raise InputError("To update an existing deposit, you must provide its id") diff --git a/swh/deposit/tests/cli/test_client.py b/swh/deposit/tests/cli/test_client.py --- a/swh/deposit/tests/cli/test_client.py +++ b/swh/deposit/tests/cli/test_client.py @@ -805,3 +805,72 @@ catch_exceptions=False, ) # fmt: on + + +@pytest.mark.parametrize( + "metadata_entry_key", ["entry-data-with-add-to-origin", "entry-only-create-origin"] +) +def test_cli_deposit_warning_missing_origin( + sample_archive, + metadata_entry_key, + tmp_path, + atom_dataset, + caplog, + cli_runner, + requests_mock_datadir, +): + """Deposit cli should log warning when the provided metadata xml is missing origins + + """ + # fmt: off + cli_runner.invoke( + cli, + [ + "upload", + "--url", "https://deposit.swh.test/1", + "--username", TEST_USER["username"], + "--password", TEST_USER["password"], + "--name", "test-project", + "--archive", sample_archive["path"], + "--author", "Jane Doe", + ], + ) + # fmt: on + + # we only care for the presence of the warning due to the missing presence of + # create_origin or add_to_origin, so we do not check anything else + warning_log_record = ( + "swh.deposit.cli.client", + logging.WARNING, + "The metadata file provided should contain " + '"" or "" tag', + ) + assert warning_log_record in caplog.record_tuples + + # Clear mocking state + caplog.clear() + + # For the next deposit, no warning should be logged as either or + # are provided + + metadata_raw = atom_dataset[metadata_entry_key] % "some-url" + metadata_path = os.path.join(tmp_path, "metadata-with-origin-tag-to-deposit.xml") + with open(metadata_path, "w") as f: + f.write(metadata_raw) + + # fmt: off + cli_runner.invoke( + cli, + [ + "upload", + "--url", "https://deposit.swh.test/1", + "--username", TEST_USER["username"], + "--password", TEST_USER["password"], + "--metadata", metadata_path, + ], + ) + # fmt: on + + for (_, log_level, _) in caplog.record_tuples: + # all messages are info or below messages so everything is fine + assert log_level < logging.WARNING