From b608113da16b72e9b9217fe6c83944294a37d628 Mon Sep 17 00:00:00 2001 From: Otto Urpelainen Date: Jun 19 2021 00:33:02 +0000 Subject: [PATCH 1/2] Add __pycache__ to .gitignore __pycache__ is a cache, it should never committed. --- diff --git a/.gitignore b/.gitignore index 12d9b09..e17c3ff 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ pip-wheel-metadata/ *.conf .coverage *~ +__pycache__/ # Ignore files generated by run_ci_tests pep8.out From e7e8285b57224f16e4248ffa1f72c69ed1dc6f3f Mon Sep 17 00:00:00 2001 From: Otto Urpelainen Date: Jun 19 2021 00:33:02 +0000 Subject: [PATCH 2/2] Add total_commits per author to git.receive notification Currently, Fedora Badge "Long Life to Pagure" does not function correctly. The issues it has are described at https://pagure.io/fedora-badges/pull-request/806 One of the issues can be solved by simply making Pagure's git.receive notification list commit count per author. Exactly that is added here. The notification schema is only changed by adding a new field, which should be non breaking. --- diff --git a/pagure/hooks/default.py b/pagure/hooks/default.py index deb4c9d..7f84b69 100644 --- a/pagure/hooks/default.py +++ b/pagure/hooks/default.py @@ -10,6 +10,7 @@ from __future__ import unicode_literals, print_function, absolute_import +from collections import defaultdict import logging import pygit2 @@ -170,15 +171,15 @@ def send_notifications( pushed. """ - auths = set() + auths_counts = defaultdict(int) for rev in revs: email = pagure.lib.git.get_author_email(rev, repodir) name = pagure.lib.git.get_author(rev, repodir) author = pagure.lib.query.search_user(session, email=email) or name - auths.add(author) + auths_counts[author] += 1 authors = [] - for author in auths: + for author in auths_counts: if not isinstance(author, six.string_types): author = author.to_json(public=True) else: @@ -186,6 +187,7 @@ def send_notifications( "fullname": author, "name": None, "url_path": None, + "total_commits": auths_counts[author] } authors.append(author) diff --git a/tests/test_pagure_send_notification.py b/tests/test_pagure_send_notification.py index f0de5b7..0bb9cd7 100644 --- a/tests/test_pagure_send_notification.py +++ b/tests/test_pagure_send_notification.py @@ -38,13 +38,14 @@ class PagureHooksDefault(tests.SimplePagureTest): def init_test_repo(self): tests.add_content_git_repo(self.projects[0]) repo = pygit2.Repository(self.projects[0]) - sha = repo.references["refs/heads/master"].peel().hex + sha = repo.revparse_single("HEAD").hex + sha_parent = repo.revparse_single("HEAD^").hex project = pagure.lib.query.get_authorized_project(self.session, "test") - return project, sha + return project, sha, sha_parent @mock.patch("pagure.hooks.default.send_fedmsg_notifications") def test_send_action_notification(self, fedmsg): - project, sha = self.init_test_repo() + project, sha, _ = self.init_test_repo() pagure.hooks.default.send_action_notification( self.session, "tag", @@ -63,7 +64,7 @@ class PagureHooksDefault(tests.SimplePagureTest): @mock.patch("pagure.hooks.default.send_fedmsg_notifications") def test_send_notifications(self, fedmsg): oldrev = "9e5f51c951c6cab20fe81419320ed740533e2f2f" - project, sha = self.init_test_repo() + project, sha, _ = self.init_test_repo() pagure.hooks.default.send_notifications( self.session, project, @@ -80,6 +81,31 @@ class PagureHooksDefault(tests.SimplePagureTest): self.assertEqual(args[2]["start_commit"], sha) self.assertEqual(args[2]["forced"], False) self.assertEqual(args[2]["old_commit"], oldrev) + self.assertEqual(args[2]["authors"][0]["fullname"], "Alice Author") + self.assertEqual(args[2]["authors"][0]["total_commits"], 1) + + @mock.patch("pagure.hooks.default.send_fedmsg_notifications") + def test_send_notifications_two_commits(self, fedmsg): + project, sha, sha_parent = self.init_test_repo() + pagure.hooks.default.send_notifications( + self.session, + project, + self.folder, + "pingou", + "master", + [sha, sha_parent], + False, + None, + ) + (_, args, kwargs) = fedmsg.mock_calls[0] + self.assertEqual(args[1], "git.receive") + self.assertEqual(args[2]["repo"]["name"], "test") + self.assertEqual(args[2]["start_commit"], sha_parent) + self.assertEqual(args[2]["end_commit"], sha) + self.assertEqual(args[2]["forced"], False) + self.assertEqual(args[2]["old_commit"], None) + self.assertEqual(args[2]["authors"][0]["fullname"], "Alice Author") + self.assertEqual(args[2]["authors"][0]["total_commits"], 2) if __name__ == "__main__":