From f9a2d7d3fb7084374063b70b455f93ba5a421fd1 Mon Sep 17 00:00:00 2001 From: Nikola Forró Date: Jan 17 2024 18:59:43 +0000 Subject: [PATCH 1/2] Implement pagure.lib.git.get_changed_files() Signed-off-by: Nikola Forró --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 293d172..efa8a5e 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1478,6 +1478,16 @@ def get_commit_subject(commit, abspath): return subject +def get_changed_files(torev, fromrev, abspath): + """Return files changed between HEAD and BASE. + Return as a dict with paths as keys and status letters as values. + """ + cmd = ["diff", "--name-status", "-z", fromrev, torev] + output = pagure.lib.git.read_git_output(cmd, abspath) + items = output.split("\0") + return {k: v for v, k in zip(items[0::2], items[1::2])} + + def get_repo_info_from_path(gitdir, hide_notfound=False): """Returns the name, username, namespace and type of a git directory diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 57eace9..78d3f94 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -3210,6 +3210,47 @@ index 0000000..60f7480 output = pagure.lib.git.get_author_email(githash, gitrepo) self.assertEqual(output, "pagure") + def test_get_changed_files(self): + """Test the get_changed_files method of pagure.lib.git.""" + + self.test_update_git() + + repo = pagure.lib.query.get_authorized_project( + self.session, "test_ticket_repo" + ) + issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) + + gitrepo = os.path.join( + self.path, "repos", "tickets", "test_ticket_repo.git" + ) + output = pagure.lib.git.read_git_lines( + ["log", "-3", "--pretty='%H'"], gitrepo + ) + self.assertEqual(len(output), 2) + + hash0 = output[0].replace("'", "") + hash1 = output[1].replace("'", "") + + # Case 1: hash1 => hash0 + output1 = pagure.lib.git.get_changed_files(hash0, hash1, gitrepo) + self.assertEqual(output1, {issue.uid: "M"}) + + # Case 2: hash0 => hash1 + output2 = pagure.lib.git.get_changed_files(hash1, hash0, gitrepo) + self.assertEqual(output2, {issue.uid: "M"}) + + # Case 3: hash0 => hash0 + output3 = pagure.lib.git.get_changed_files(hash0, hash0, gitrepo) + self.assertEqual(output3, {}) + + # Case 4: NULL => hash0 + output4 = pagure.lib.git.get_changed_files(hash0, "", gitrepo) + self.assertEqual(output4, {}) + + # Case 5: NULL => NULL + output5 = pagure.lib.git.get_changed_files("", "", gitrepo) + self.assertEqual(output5, {}) + def test_get_repo_name(self): """Test the get_repo_name method of pagure.lib.git.""" From 9be5b2dc8c057778262700251584ce38b8338d16 Mon Sep 17 00:00:00 2001 From: Nikola Forró Date: Jan 17 2024 18:59:45 +0000 Subject: [PATCH 2/2] Add changed files to push notification payload Signed-off-by: Nikola Forró --- diff --git a/pagure/hooks/default.py b/pagure/hooks/default.py index 0a0a3d7..5581ff8 100644 --- a/pagure/hooks/default.py +++ b/pagure/hooks/default.py @@ -189,6 +189,12 @@ def send_notifications( authors.append(author) if revs: + changed_files = pagure.lib.git.get_changed_files( + revs[-1], + oldrev, + repodir, + ) + revs.reverse() print("* Publishing information for %i commits" % len(revs)) @@ -201,6 +207,7 @@ def send_notifications( branch=refname, forced=forced, authors=list(authors), + changed_files=changed_files, agent=user, repo=project.to_json(public=True) if not isinstance(project, six.string_types) diff --git a/tests/test_pagure_send_notification.py b/tests/test_pagure_send_notification.py index c6d8fac..0b04c0b 100644 --- a/tests/test_pagure_send_notification.py +++ b/tests/test_pagure_send_notification.py @@ -38,13 +38,15 @@ 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 + commit = repo.references["refs/heads/master"].peel() + sha = commit.hex + oldsha = commit.parents[0].hex project = pagure.lib.query.get_authorized_project(self.session, "test") - return project, sha + return project, sha, oldsha @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", @@ -62,8 +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, oldsha = self.init_test_repo() pagure.hooks.default.send_notifications( self.session, project, @@ -72,14 +73,21 @@ class PagureHooksDefault(tests.SimplePagureTest): "master", [sha], False, - oldrev, + oldsha, ) (_, 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) self.assertEqual(args[2]["forced"], False) - self.assertEqual(args[2]["old_commit"], oldrev) + self.assertEqual(args[2]["old_commit"], oldsha) + self.assertEqual( + args[2]["changed_files"], + { + "folder1/folder2/file": "A", + "folder1/folder2/fileŠ": "A", + }, + ) if __name__ == "__main__":