#5435 Add info about changed files to push notification payload
Merged a year ago by ngompa. Opened a year ago by nforro.
nforro/pagure changed_files  into  master

file modified
+7
@@ -189,6 +189,12 @@ 

          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 @@ 

              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)

file modified
+10
@@ -1478,6 +1478,16 @@ 

      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

  

@@ -3210,6 +3210,47 @@ 

              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."""

  

@@ -38,13 +38,15 @@ 

      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 @@ 

  

      @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 @@ 

              "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__":

Packit relies on org.fedoraproject.prod.git.receive fedmsg messages. In order to filter which events to react to it needs to know which files have been changed. This PR adds such an info to org.fedoraproject.prod.pagure.git.receive messages, making it possible for Packit to migrate to it.

This looks great, thanks!

Pull-Request has been merged by ngompa

a year ago