From ef043e2c144ae6beddce0fb73cb7fd236acec867 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Feb 28 2017 15:48:29 +0000 Subject: PR#319 Added support for CG provided owner Merges #319 Fixes #299 --- diff --git a/docs/source/content_generator_metadata.rst b/docs/source/content_generator_metadata.rst index 87f6b7e..ef3df70 100644 --- a/docs/source/content_generator_metadata.rst +++ b/docs/source/content_generator_metadata.rst @@ -42,6 +42,8 @@ The build map contains the following entries: - start\_time: The time the build started, in seconds since the epoch. - end\_time: The time the build was completed, in seconds since the epoch. +- owner: The owner of the build task in username format. This field + is optional. - extra: A map of extra metadata associated with the build, which must include one of: @@ -157,7 +159,8 @@ The below JSON is based loosely on the output of a docker image build. "source": "git://git.engineering.redhat.com/users/vpavlin/tdl_templates.git#a14f145244", "extra": {}, "start_time": 1423148398, - "end_time": 1423148828}, + "end_time": 1423148828, + "owner": "jdoe"}, "buildroots": [{"id": 1, "host": {"os": "rhel-7", "arch": "x86_64"}, diff --git a/hub/kojihub.py b/hub/kojihub.py index dfc76e8..2f555f4 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -5095,6 +5095,11 @@ class CG_Importer(object): datetime.datetime.fromtimestamp(float(metadata['build']['start_time'])).isoformat(' ') buildinfo['completion_time'] = \ datetime.datetime.fromtimestamp(float(metadata['build']['end_time'])).isoformat(' ') + owner = metadata['build'].get('owner', None) + if owner: + if not isinstance(owner, basestring): + raise koji.GenericError("Invalid owner format (expected username): %s" % owner) + buildinfo['owner'] = get_user(owner, strict=True)['id'] self.buildinfo = buildinfo koji.check_NVR(buildinfo, strict=True) diff --git a/tests/test_hub/cg_importer_json/default.json b/tests/test_hub/cg_importer_json/default.json index c03c78f..e8a2094 100644 --- a/tests/test_hub/cg_importer_json/default.json +++ b/tests/test_hub/cg_importer_json/default.json @@ -5,7 +5,8 @@ "source": "git://git.engineering.redhat.com/users/vpavlin/tdl_templates.git#a14f145244", "extra": {}, "start_time": 1423148398, - "end_time": 1423148828}, + "end_time": 1423148828, + "owner": "koji"}, "buildroots": [{"id": 1, "host": {"os": "rhel-7", "arch": "x86_64"}, diff --git a/tests/test_hub/test_cg_importer.py b/tests/test_hub/test_cg_importer.py index 635f4c0..f77ce67 100644 --- a/tests/test_hub/test_cg_importer.py +++ b/tests/test_hub/test_cg_importer.py @@ -68,12 +68,14 @@ class TestCGImporter(unittest.TestCase): assert x.cgs assert isinstance(x.cgs, set) + @mock.patch("kojihub.get_user") @mock.patch('kojihub.context') @mock.patch("koji.pathinfo.work") - def test_prep_build(self, work, context): + def test_prep_build(self, work, context, get_user): work.return_value = os.path.dirname(__file__) cursor = mock.MagicMock() context.cnx.cursor.return_value = cursor + get_user.return_value = {'id': 123} x = kojihub.CG_Importer() x.get_metadata('default.json', 'cg_importer_json') x.prep_build() @@ -90,16 +92,18 @@ class TestCGImporter(unittest.TestCase): with self.assertRaises(GenericError): x.prep_build() + @mock.patch("kojihub.get_user") @mock.patch('kojihub.get_build') @mock.patch('kojihub.new_build') @mock.patch('kojihub.context') @mock.patch("koji.pathinfo.work") - def test_get_build(self, work, context, new_build_id, get_build): + def test_get_build(self, work, context, new_build_id, get_build, get_user): work.return_value = os.path.dirname(__file__) cursor = mock.MagicMock() context.cnx.cursor.return_value = cursor new_build_id.return_value = 42 get_build.return_value = False + get_user.return_value = {'id': 123} x = kojihub.CG_Importer() x.get_metadata('default.json', 'cg_importer_json') x.prep_build()