From d540a900554d711ccbd6d55b532fe9bae75b9dde Mon Sep 17 00:00:00 2001 From: Otto Urpelainen Date: Mar 27 2021 07:52:55 +0000 Subject: Add total_commits field to git.receive authors Having commit count per author may be useful for writing rules for Fedora Badges. Before, type GIT_RECEIVE_USER was used as author type for all Git messages. Renaming the type to GIT_USER and adding new GIT_RECEIVE_USER with additional field total_commits, only used for git.receive action. This way, other messages that git.receive do not change and type names stay consistent with their use. In order to avoid creating new schema versions, the new field is marked optional. --- diff --git a/pagure_messages/base.py b/pagure_messages/base.py index bd522fe..585fc89 100644 --- a/pagure_messages/base.py +++ b/pagure_messages/base.py @@ -81,13 +81,26 @@ USER = { } +GIT_USER = { + "type": "object", + "properties": { + "name": {"oneOf": [{"type": "null"}, {"type": "string"}]}, + "fullname": {"type": "string"}, + "url_path": {"oneOf": [{"type": "null"}, {"type": "string"}]}, + }, + "required": ["name", "fullname", "url_path"], +} + + GIT_RECEIVE_USER = { "type": "object", "properties": { "name": {"oneOf": [{"type": "null"}, {"type": "string"}]}, "fullname": {"type": "string"}, "url_path": {"oneOf": [{"type": "null"}, {"type": "string"}]}, + "total_commits": {"type": "number"}, }, + # "total_commits" is new, optional for now to retain compatibility "required": ["name", "fullname", "url_path"], } diff --git a/pagure_messages/git_schema.py b/pagure_messages/git_schema.py index 2759d18..8fec7e4 100644 --- a/pagure_messages/git_schema.py +++ b/pagure_messages/git_schema.py @@ -14,7 +14,7 @@ # 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 GIT_RECEIVE_USER, PROJECT, PagureMessage, SCHEMA_URL +from .base import GIT_USER, GIT_RECEIVE_USER, PROJECT, PagureMessage, SCHEMA_URL class GitBranchCreationV1(PagureMessage): @@ -35,7 +35,7 @@ class GitBranchCreationV1(PagureMessage): "repo": PROJECT, "tag": {"type": "string"}, "rev": {"type": "string"}, - "authors": {"type": "array", "items": GIT_RECEIVE_USER}, + "authors": {"type": "array", "items": GIT_USER}, }, "required": ["agent", "repo", "branch", "rev", "authors"], } @@ -87,7 +87,7 @@ class GitBranchDeletionV1(PagureMessage): "repo": PROJECT, "tag": {"type": "string"}, "rev": {"type": "string"}, - "authors": {"type": "array", "items": GIT_RECEIVE_USER}, + "authors": {"type": "array", "items": GIT_USER}, }, "required": ["agent", "repo", "branch", "rev", "authors"], } @@ -202,7 +202,7 @@ class GitTagCreationV1(PagureMessage): "repo": PROJECT, "tag": {"type": "string"}, "rev": {"type": "string"}, - "authors": {"type": "array", "items": GIT_RECEIVE_USER}, + "authors": {"type": "array", "items": GIT_USER}, }, "required": ["agent", "repo", "tag", "rev", "authors"], } @@ -252,7 +252,7 @@ class GitTagDeletionV1(PagureMessage): "repo": PROJECT, "tag": {"type": "string"}, "rev": {"type": "string"}, - "authors": {"type": "array", "items": GIT_RECEIVE_USER}, + "authors": {"type": "array", "items": GIT_USER}, }, "required": ["agent", "repo", "tag", "rev", "authors"], } diff --git a/pagure_messages/tests/test_git_receive.py b/pagure_messages/tests/test_git_receive.py index 166ff36..d3b66dc 100644 --- a/pagure_messages/tests/test_git_receive.py +++ b/pagure_messages/tests/test_git_receive.py @@ -78,6 +78,35 @@ def test_minimal_short_branch(): assert message.url == "https://pagure.io/fedora-infra/fedocal-messages/tree/develop" +def test_maximal(): + """ + Assert the message schema validates a message with both required and + optional fields. + """ + body = { + "agent": "dummy-user", + "forced": False, + "repo": PROJECT, + "old_commit": "hash_commit_old", + "branch": "refs/heads/develop", + "authors": [ + { + "fullname": "dummy-user", + "url_path": "user/dummy-user", + "name": "dummy-user", + "email": "dummy@example.com", + "total_commits": 42, + } + ], + "total_commits": 42, + "start_commit": "hash_commit_start", + "end_commit": "hash_commit_stop", + } + message = GitReceiveV1(body=body) + message.validate() + assert message.url == "https://pagure.io/fedora-infra/fedocal-messages/tree/develop" + + def test_missing_fields(): """Assert an exception is actually raised on validation failure.""" minimal_message = {