From 1ebb1f432e5e510e1e771d8e686286ed5a6c3a5b Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Nov 10 2023 15:32:01 +0000 Subject: [PATCH 1/2] Fix the `usernames` property on issues and pull requests Also, fix some wrong comments and factor some code. Fixes: #12 Signed-off-by: Aurélien Bompard --- diff --git a/pagure_messages/base.py b/pagure_messages/base.py index c1c5203..03466b9 100644 --- a/pagure_messages/base.py +++ b/pagure_messages/base.py @@ -398,6 +398,13 @@ GROUP = { } +def pretty_list(value): + """Return a de-duplicated, sorted, and None-filtered list.""" + value = list(set(val for val in value if val is not None)) + value.sort() + return value + + class PagureMessage(message.Message): """ A sub-class of a Fedora message that defines a message schema for messages @@ -441,3 +448,28 @@ class PagureMessage(message.Message): @property def usernames(self): return [self.agent_name] + + +class IssueOrPullRequestMessage(PagureMessage): + """ + A sub-class of a Fedora message that defines a message schema for messages + published by pagure about an issue or a pull request. + """ + + object_type = None + + @property + def url(self): + return self.body[self.object_type]["full_url"] + + @property + def usernames(self): + usernames = [ + self.agent_name, + self.body[self.object_type]["user"]["name"], + ] + try: + usernames.append(self.body[self.object_type]["assignee"]["name"]) + except (KeyError, TypeError): + pass + return pretty_list(usernames) diff --git a/pagure_messages/issue_schema.py b/pagure_messages/issue_schema.py index 0358e63..7565c26 100644 --- a/pagure_messages/issue_schema.py +++ b/pagure_messages/issue_schema.py @@ -14,13 +14,22 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from .base import ISSUE, PROJECT, PagureMessage, SCHEMA_URL +from .base import ( + ISSUE, + IssueOrPullRequestMessage, + PROJECT, + SCHEMA_URL, +) -class IssueAssignedAddedV1(PagureMessage): +class IssueMessage(IssueOrPullRequestMessage): + object_type = "issue" + + +class IssueAssignedAddedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when an issue is deleted. + published by pagure when an issue is assigned. """ topic = "pagure.issue.assigned.added" @@ -28,7 +37,6 @@ class IssueAssignedAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -57,15 +65,11 @@ class IssueAssignedAddedV1(PagureMessage): assignee=self.body["issue"]["assignee"]["name"], ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueAssignedResetV1(PagureMessage): +class IssueAssignedResetV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when an issue is deleted. + published by pagure when an issue is un-assigned. """ topic = "pagure.issue.assigned.reset" @@ -73,7 +77,6 @@ class IssueAssignedResetV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -100,15 +103,11 @@ class IssueAssignedResetV1(PagureMessage): id=self.body["issue"]["id"], ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueCommentAddedV1(PagureMessage): +class IssueCommentAddedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when an issue is deleted. + published by pagure when a comment is added to an issue. """ topic = "pagure.issue.comment.added" @@ -116,7 +115,6 @@ class IssueCommentAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -153,10 +151,10 @@ class IssueCommentAddedV1(PagureMessage): ) -class IssueDependencyAddedV1(PagureMessage): +class IssueDependencyAddedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when an issue is deleted. + published by pagure when a dependency is added to an issue. """ topic = "pagure.issue.dependency.added" @@ -164,7 +162,6 @@ class IssueDependencyAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -196,12 +193,8 @@ class IssueDependencyAddedV1(PagureMessage): depissueid=self.body["added_dependency"], ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueDependencyRemovedV1(PagureMessage): +class IssueDependencyRemovedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when an issue is deleted. @@ -212,7 +205,6 @@ class IssueDependencyRemovedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -252,12 +244,8 @@ class IssueDependencyRemovedV1(PagureMessage): ) ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueDropV1(PagureMessage): +class IssueDropV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when an issue is deleted. @@ -268,7 +256,6 @@ class IssueDropV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -303,7 +290,7 @@ class IssueDropV1(PagureMessage): return "{full_url}/issues".format(full_url=full_url) -class IssueEditV1(PagureMessage): +class IssueEditV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when an issue is updated. @@ -314,7 +301,6 @@ class IssueEditV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -346,12 +332,8 @@ class IssueEditV1(PagureMessage): ) ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueNewV1(PagureMessage): +class IssueNewV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when a new thing is created. @@ -362,7 +344,6 @@ class IssueNewV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -390,12 +371,8 @@ class IssueNewV1(PagureMessage): title=self.body["issue"]["title"], ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueTagAddedV1(PagureMessage): +class IssueTagAddedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when an issue is deleted. @@ -406,7 +383,6 @@ class IssueTagAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -436,12 +412,8 @@ class IssueTagAddedV1(PagureMessage): tags=", ".join(self.body["tags"]), ) - @property - def url(self): - return self.body["issue"]["full_url"] - -class IssueTagRemovedV1(PagureMessage): +class IssueTagRemovedV1(IssueMessage): """ A sub-class of a Fedora message that defines a message schema for messages published by pagure when an issue is deleted. @@ -452,7 +424,6 @@ class IssueTagRemovedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -481,7 +452,3 @@ class IssueTagRemovedV1(PagureMessage): id=self.body["issue"]["id"], tags=", ".join(self.body["tags"]), ) - - @property - def url(self): - return self.body["issue"]["full_url"] diff --git a/pagure_messages/pull_requests_schema.py b/pagure_messages/pull_requests_schema.py index 3952746..37040c2 100644 --- a/pagure_messages/pull_requests_schema.py +++ b/pagure_messages/pull_requests_schema.py @@ -14,13 +14,23 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from .base import COMMIT_FLAG, PROJECT, PULL_REQUEST, PagureMessage, SCHEMA_URL +from .base import ( + COMMIT_FLAG, + IssueOrPullRequestMessage, + PROJECT, + PULL_REQUEST, + SCHEMA_URL, +) -class PullRequestAssignedAddedV1(PagureMessage): +class PullRequestMessage(IssueOrPullRequestMessage): + object_type = "pullrequest" + + +class PullRequestAssignedAddedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a pull request is assigned. """ topic = "pagure.pull-request.assigned.added" @@ -28,7 +38,6 @@ class PullRequestAssignedAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -56,15 +65,11 @@ class PullRequestAssignedAddedV1(PagureMessage): assignee=self.body["pullrequest"]["assignee"]["name"], ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestAssignedResetV1(PagureMessage): +class PullRequestAssignedResetV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a pull request is un-assigned. """ topic = "pagure.pull-request.assigned.reset" @@ -72,7 +77,6 @@ class PullRequestAssignedResetV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -99,15 +103,11 @@ class PullRequestAssignedResetV1(PagureMessage): username=self.agent_name, ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestClosedV1(PagureMessage): +class PullRequestClosedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a pull request is closed. """ topic = "pagure.pull-request.closed" @@ -115,7 +115,6 @@ class PullRequestClosedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -146,15 +145,11 @@ class PullRequestClosedV1(PagureMessage): action="merged" if self.body["merged"] else "closed without merging", ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestCommentAddedV1(PagureMessage): +class PullRequestCommentAddedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a comment is added to a PR. """ topic = "pagure.pull-request.comment.added" @@ -162,7 +157,6 @@ class PullRequestCommentAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -200,10 +194,10 @@ class PullRequestCommentAddedV1(PagureMessage): ) -class PullRequestCommentEditedV1(PagureMessage): +class PullRequestCommentEditedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a comment is edited on a PR. """ topic = "pagure.pull-request.comment.edited" @@ -211,7 +205,6 @@ class PullRequestCommentEditedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -249,10 +242,10 @@ class PullRequestCommentEditedV1(PagureMessage): ) -class PullRequestFlagAddedV1(PagureMessage): +class PullRequestFlagAddedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a flag is added on a PR. """ topic = "pagure.pull-request.flag.added" @@ -260,7 +253,6 @@ class PullRequestFlagAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -288,15 +280,11 @@ class PullRequestFlagAddedV1(PagureMessage): status=self.body["flag"]["status"], ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestFlagUpdatedV1(PagureMessage): +class PullRequestFlagUpdatedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a flag is updated on a PR """ topic = "pagure.pull-request.flag.updated" @@ -304,7 +292,6 @@ class PullRequestFlagUpdatedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -334,15 +321,11 @@ class PullRequestFlagUpdatedV1(PagureMessage): status=self.body["flag"]["status"], ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestInitialCommentEditedV1(PagureMessage): +class PullRequestInitialCommentEditedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when an initial PR comment is edited. """ topic = "pagure.pull-request.initial_comment.edited" @@ -350,7 +333,6 @@ class PullRequestInitialCommentEditedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -379,15 +361,11 @@ class PullRequestInitialCommentEditedV1(PagureMessage): username=self.agent_name, ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestNewV1(PagureMessage): +class PullRequestNewV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a pull request is created. """ topic = "pagure.pull-request.new" @@ -395,7 +373,6 @@ class PullRequestNewV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -422,15 +399,11 @@ class PullRequestNewV1(PagureMessage): title=self.body["pullrequest"]["title"], ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestRebasedV1(PagureMessage): +class PullRequestRebasedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a PR is rebased. """ topic = "pagure.pull-request.rebased" @@ -438,7 +411,6 @@ class PullRequestRebasedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -464,15 +436,11 @@ class PullRequestRebasedV1(PagureMessage): username=self.agent_name, ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestReopenedV1(PagureMessage): +class PullRequestReopenedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a PR is reopened. """ topic = "pagure.pull-request.reopened" @@ -480,7 +448,6 @@ class PullRequestReopenedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -506,15 +473,11 @@ class PullRequestReopenedV1(PagureMessage): username=self.agent_name, ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestTagAddedV1(PagureMessage): +class PullRequestTagAddedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a tag is added on a PR. """ topic = "pagure.pull-request.tag.added" @@ -522,7 +485,6 @@ class PullRequestTagAddedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -551,15 +513,11 @@ class PullRequestTagAddedV1(PagureMessage): tags=", ".join(self.body["tags"]), ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestTagRemovedV1(PagureMessage): +class PullRequestTagRemovedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a tag is removed on a PR. """ topic = "pagure.pull-request.tag.removed" @@ -596,15 +554,11 @@ class PullRequestTagRemovedV1(PagureMessage): tags=", ".join(self.body["tags"]), ) - @property - def url(self): - return self.body["pullrequest"]["full_url"] - -class PullRequestUpdatedV1(PagureMessage): +class PullRequestUpdatedV1(PullRequestMessage): """ A sub-class of a Fedora message that defines a message schema for messages - published by pagure when a new thing is created. + published by pagure when a PR is updated. """ topic = "pagure.pull-request.updated" @@ -612,7 +566,6 @@ class PullRequestUpdatedV1(PagureMessage): body_schema = { "id": SCHEMA_URL + topic, "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new project is created", "type": "object", "properties": { "agent": {"type": "string"}, @@ -637,7 +590,3 @@ class PullRequestUpdatedV1(PagureMessage): id=self.body["pullrequest"]["id"], username=self.agent_name, ) - - @property - def url(self): - return self.body["pullrequest"]["full_url"] diff --git a/pagure_messages/tests/test_pull_request_comment_added.py b/pagure_messages/tests/test_pull_request_comment_added.py index 8a960e2..b8518a2 100644 --- a/pagure_messages/tests/test_pull_request_comment_added.py +++ b/pagure_messages/tests/test_pull_request_comment_added.py @@ -72,3 +72,25 @@ def test_summary(): message = PullRequestCommentAddedV1(body=body) message.validate() assert expected_summary == message.summary + + +def test_usernames(): + """Assert the usernames is correct.""" + body = { + "agent": "dummy-user", + "pullrequest": PULL_REQUEST, + } + message = PullRequestCommentAddedV1(body=body) + message.validate() + assert message.usernames == ["dummy-user", "jjames"] + + +def test_usernames_self_reply(): + """Assert the usernames is correct.""" + body = { + "agent": "jjames", + "pullrequest": PULL_REQUEST, + } + message = PullRequestCommentAddedV1(body=body) + message.validate() + assert message.usernames == ["jjames"] diff --git a/tox.ini b/tox.ini index 173f456..503541c 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,7 @@ usedevelop = True deps = pytest pytest-cov -whitelist_externals = +allowlist_externals = rm commands = rm -rf htmlcov coverage.xml From 84ba5c49890bb94e668e12dd7379dd4d3a3e662b Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Nov 10 2023 15:38:31 +0000 Subject: [PATCH 2/2] Drop support for Python 3.6, it's too old for the ecosystem Signed-off-by: Aurélien Bompard --- diff --git a/setup.cfg b/setup.cfg index b7a8d7e..1a83762 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,10 +12,6 @@ license = GPLv2+ classifiers = License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+) Operating System :: POSIX :: Linux - Programming Language :: Python :: 2 - Programming Language :: Python :: 2.7 - Programming Language :: Python :: 3 - Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 diff --git a/tox.ini b/tox.ini index 503541c..a89f549 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py36,py37,py38,bandit,lint,format +envlist = py37,py38,bandit,lint,format [testenv] passenv = HOME