diff --git a/messaging/copr_messaging/private/hierarchy.py b/messaging/copr_messaging/private/hierarchy.py index 80ceccda3..981fd1bfd 100644 --- a/messaging/copr_messaging/private/hierarchy.py +++ b/messaging/copr_messaging/private/hierarchy.py @@ -159,6 +159,19 @@ def package_full_name(self): release=release, ) + @property + def usernames(self): + """ + List of usernames that are affected by the action that caused this message. + + Those users will be notified by FMN if they chose to receive their notifications. + """ + usernames = [self.submitter, self.body.get("owner")] + # filter out nones and groups and deduplicate + usernames = set(u for u in usernames if isinstance(u, str) and not u.startswith("@")) + # return it sorted + return list(sorted(usernames)) + class _BuildChrootMessage(_BuildMessage): @property diff --git a/messaging/copr_messaging/private/schema_old.py b/messaging/copr_messaging/private/schema_old.py index a44ea2243..9da5181ee 100644 --- a/messaging/copr_messaging/private/schema_old.py +++ b/messaging/copr_messaging/private/schema_old.py @@ -128,6 +128,11 @@ def status(self): def package_name(self): return self.body.get('pkg') + @property + def submitter(self): + """The username of the person who submitted the build.""" + return self.body.get('user') + def _evr(self): evr = self.body.get('version') if not evr: diff --git a/messaging/copr_messaging/private/schema_stomp_old.py b/messaging/copr_messaging/private/schema_stomp_old.py index 401fb8a18..681fa7db8 100644 --- a/messaging/copr_messaging/private/schema_stomp_old.py +++ b/messaging/copr_messaging/private/schema_stomp_old.py @@ -139,3 +139,8 @@ def _evr(self): def package_name(self): name, _, _, _ = self._nevr() return name + + @property + def submitter(self): + """The username of the person who submitted the build.""" + return self.body.get('submitter') diff --git a/messaging/copr_messaging/schema.py b/messaging/copr_messaging/schema.py index fe16d85b4..0cc76b809 100644 --- a/messaging/copr_messaging/schema.py +++ b/messaging/copr_messaging/schema.py @@ -46,6 +46,14 @@ def __str__(self): self.status, ) + @property + def agent_name(self): + """The username who caused the action that generated this message.""" + if self.status == "canceled": + return self.submitter + # The other statuses are not caused by the user who started the build + return None + class BuildChrootStarted(_BuildChrootMessage): """ @@ -58,6 +66,11 @@ def __str__(self): self.chroot, ) + @property + def agent_name(self): + """The username who caused the action that generated this message.""" + return self.submitter + class BuildChrootStartedV1(_PreFMBuildMessage, BuildChrootStarted): """ diff --git a/messaging/copr_messaging/tests/test_schema.py b/messaging/copr_messaging/tests/test_schema.py index 9d72e6fde..bc073adfb 100644 --- a/messaging/copr_messaging/tests/test_schema.py +++ b/messaging/copr_messaging/tests/test_schema.py @@ -108,6 +108,14 @@ def test_nevr(self): assert message.package_release == '1.fc31' assert message.package_epoch is None + def test_agent_name(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.agent_name == "churchyard" + + def test_usernames(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.usernames == ["churchyard"] + class BuildChrootEndedV1Test(BuildChrootStartedV1Test): msg_class = schema.BuildChrootEndedV1 @@ -150,6 +158,20 @@ def test_nevr(self): assert message.package_release == '1.fc30' assert message.package_epoch is None + def test_agent_name(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.agent_name is None + + def test_agent_name_canceled(self): + body = copy.deepcopy(self.fedmsg_message["msg"]) + body["status"] = 2 + message = self.msg_class(body=body) + assert message.agent_name == "kbaig" + + def test_usernames(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.usernames == ["kbaig"] + class BuildChrootStartedV1DontUseTest(BuildChrootStartedV1Test): msg_class = schema.BuildChrootStartedV1DontUse @@ -192,6 +214,14 @@ def test_nevr(self): assert message.package_release == "0" assert message.package_epoch == '10' + def test_agent_name(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.agent_name == "praiskup" + + def test_usernames(self): + message = self.msg_class(body=self.fedmsg_message["msg"]) + assert message.usernames == ["praiskup"] + class BuildChrootStompOldStartTest(unittest.TestCase): msg_class = schema.BuildChrootStartedV1Stomp