From 5c7b9523aef4b893168374b5d8b73ec9356b14cf Mon Sep 17 00:00:00 2001 From: Otto Urpelainen Date: May 17 2024 11:10:55 +0000 Subject: [PATCH 1/2] Add __pycache__ to .gitignore __pycache__ is a cache, it should never committed. --- diff --git a/.gitignore b/.gitignore index 576ddff..3de7dda 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 a2bef9ceafc6af8cee3295a8eb07554d13cf6914 Mon Sep 17 00:00:00 2001 From: Otto Urpelainen Date: May 17 2024 11:10:55 +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 fbda401..48f26e6 100644 --- a/pagure/hooks/default.py +++ b/pagure/hooks/default.py @@ -10,6 +10,7 @@ from __future__ import absolute_import, print_function, unicode_literals +from collections import defaultdict import logging import pygit2 @@ -169,15 +170,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: @@ -185,6 +186,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 973cd8f..0ff800d 100644 --- a/tests/test_pagure_send_notification.py +++ b/tests/test_pagure_send_notification.py @@ -41,12 +41,13 @@ class PagureHooksDefault(tests.SimplePagureTest): commit = repo.references["refs/heads/master"].peel() sha = commit.hex oldsha = commit.parents[0].hex + oldersha = "0" # Repo has only two commits project = pagure.lib.query.get_authorized_project(self.session, "test") - return project, sha, oldsha + return project, sha, oldsha, oldersha @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", @@ -64,7 +65,7 @@ class PagureHooksDefault(tests.SimplePagureTest): @mock.patch("pagure.hooks.default.send_fedmsg_notifications") def test_send_notifications(self, fedmsg): - project, sha, oldsha = self.init_test_repo() + project, sha, oldsha, _ = self.init_test_repo() pagure.hooks.default.send_notifications( self.session, project, @@ -90,6 +91,41 @@ class PagureHooksDefault(tests.SimplePagureTest): }, ) self.assertIsNone(args[2]["pull_request_id"]) + 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, oldsha, oldersha = self.init_test_repo() + pagure.hooks.default.send_notifications( + self.session, + project, + self.folder, + "pingou", + "master", + [sha, oldsha], + False, + oldersha, + 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"], oldsha) + self.assertEqual(args[2]["end_commit"], sha) + self.assertEqual(args[2]["forced"], False) + self.assertEqual(args[2]["old_commit"], oldersha) + self.assertEqual( + args[2]["changed_files"], + { + "folder1/folder2/file": "A", + "folder1/folder2/fileŠ": "A", + "sources": "A", + }, + ) + self.assertIsNone(args[2]["pull_request_id"]) + self.assertEqual(args[2]["authors"][0]["fullname"], "Alice Author") + self.assertEqual(args[2]["authors"][0]["total_commits"], 2) if __name__ == "__main__":