#3832 When a pushed in made to a branch in a PR, update the PR
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

file modified
+3
@@ -208,6 +208,9 @@ 

                  "   %s/%s/pull-request/%s"

                  % (_config["APP_URL"].rstrip("/"), pr.project.url_path, pr.id)

              )

+             # Refresh the PR in the db and everywhere else where needed

+             pagure.lib.tasks.update_pull_request.delay(pr.uid)

+ 

          # If no existing PRs, provide the link to open one

          if not seen:

              print("Create a pull-request for %s" % refname)

file modified
+37 -1
@@ -459,7 +459,7 @@ 

      )

  

  

- @conn.task(queue=pagure_config.get("FAST_CELERY_QUEUE", None), bind=True)

+ @conn.task(queue=pagure_config.get("MEDIUM_CELERY_QUEUE", None), bind=True)

  @pagure_task

  def fork(

      self,
@@ -866,6 +866,42 @@ 

          pagure.lib.git.update_pull_ref(request, repo_obj)

  

  

+ @conn.task(queue=pagure_config.get("FAST_CELERY_QUEUE", None), bind=True)

+ @pagure_task

+ def update_pull_request(self, session, pr_uid):

+     """ Updates a pull-request in the DB once a commit was pushed to it in

+     git.

+     """

+     request = pagure.lib.get_request_by_uid(session, pr_uid)

+ 

+     with request.project.lock("WORKER"):

+ 

+         _log.debug(

+             "Updating pull-request: %s#%s",

+             request.project.fullname,

+             request.id,

+         )

+         if request.remote:

+             repopath = pagure.utils.get_remote_repo_path(

+                 request.remote_git, request.branch_from

+             )

+             parentpath = pagure.utils.get_repo_path(request.project)

+         else:

+             repo_from = request.project_from

+             parentpath = pagure.utils.get_repo_path(request.project)

+             repopath = parentpath

+             if repo_from:

+                 repopath = pagure.utils.get_repo_path(repo_from)

+         _log.debug("   working on the repo in: %s and", repopath, parentpath)

+ 

+         repo_obj = pygit2.Repository(repopath)

+         orig_repo = pygit2.Repository(parentpath)

+ 

+         pagure.lib.git.diff_pull_request(

+             session, request, repo_obj, orig_repo, with_diff=False

+         )

+ 

+ 

  @conn.task(queue=pagure_config.get("MEDIUM_CELERY_QUEUE", None), bind=True)

  @pagure_task

  def update_checksums_file(self, session, folder, filenames):

@@ -33,6 +33,7 @@ 

  

  import pagure

  import pagure.lib

+ import pagure.lib.tasks

  import tests

  from pagure.lib.repo import PagureRepo

  
@@ -294,6 +295,90 @@ 

              output_text)

  

      @patch('pagure.lib.notify.send_email')

+     def test_task_update_request_pull(self, send_email):

+         """ Test the task update_pull_request endpoint. """

+         send_email.return_value = True

+ 

+         tests.create_projects(self.session)

+         tests.create_projects_git(

+             os.path.join(self.path, 'requests'), bare=True)

+ 

+         self.set_up_git_repo(new_project=None, branch_from='feature')

+ 

+         self.session = pagure.lib.create_session(self.dbpath)

+         project = pagure.lib.get_authorized_project(self.session, 'test')

+         self.assertEqual(len(project.requests), 1)

+ 

+         request = project.requests[0]

+         self.assertEqual(len(request.comments), 0)

+         start_commit = request.commit_start

+         stop_commit = request.commit_stop

+ 

+         # View the pull-request

+         output = self.app.get('/test/pull-request/1')

+         self.assertEqual(output.status_code, 200)

+         output_text = output.get_data(as_text=True)

+         self.assertIn(

+             '<title>PR#1: PR from the feature branch - test\n - Pagure</title>',

+             output_text)

+         self.assertIn(

+             'title="View file as of 2a552b">sources</a>', output_text)

+ 

+         # Add a new commit on the repo from

+         newpath = tempfile.mkdtemp(prefix='pagure-fork-test')

+         gitrepo = os.path.join(self.path, 'repos', 'test.git')

+         repopath = os.path.join(newpath, 'test')

+         clone_repo = pygit2.clone_repository(

+             gitrepo, repopath, checkout_branch='feature')

+ 

+         def compatible_signature(name, email):

+             if six.PY2:

Maybe just six.u() on both? Should be automatic no-op on py3, and do the same on py2 as you're doing, I think?

Worth a try :)

We actually want the opposite, bytes not unicode :(

And six.b() doesn't seem to do what we want :(

Going to leave as it

+                 name = name.encode("utf-8")

+                 email = email.encode("utf-8")

+             return pygit2.Signature(name, email)

+ 

+         with open(os.path.join(repopath, '.gitignore'), 'w') as stream:

+                 stream.write('*~')

+         clone_repo.index.add('.gitignore')

+         clone_repo.index.write()

+ 

+         com = clone_repo.revparse_single('HEAD')

+         prev_commit = [com.oid.hex]

+ 

+         # Commits the files added

+         tree = clone_repo.index.write_tree()

+         author = compatible_signature(

+             'Alice Äuthòr', 'alice@äuthòrs.tld')

+         comitter = compatible_signature(

+             'Cecil Cõmmîttër', 'cecil@cõmmîttërs.tld')

+         clone_repo.create_commit(

+             'refs/heads/feature',

+             author,

+             comitter,

+             'Add .gitignore file for testing',

+             # binary string representing the tree object ID

+             tree,

+             # list of binary strings representing parents of the new commit

+             prev_commit

+         )

+         refname = 'refs/heads/feature:refs/heads/feature'

+         ori_remote = clone_repo.remotes[0]

+         PagureRepo.push(ori_remote, refname)

+         shutil.rmtree(newpath)

+ 

+         pagure.lib.tasks.update_pull_request(request.uid)

+ 

+         self.session = pagure.lib.create_session(self.dbpath)

+         project = pagure.lib.get_authorized_project(self.session, 'test')

+         self.assertEqual(len(project.requests), 1)

+         request = project.requests[0]

+         self.assertEqual(len(request.comments), 1)

+         self.assertIsNotNone(request.commit_start)

+         self.assertIsNotNone(request.commit_stop)

+         self.assertNotEqual(start_commit, request.commit_start)

+         self.assertNotEqual(stop_commit, request.commit_stop)

+ 

+     @patch('pagure.lib.notify.send_email')

      def test_merge_request_pull_FF(self, send_email):

          """ Test the merge_request_pull endpoint with a FF PR. """

          send_email.return_value = True

15:35:22 Failed tests:
15:35:22 FAILED test: py-test_style

This should pass after the code is blackened, I think.

rebased onto 96f75d22a8fc05b170fab8af3fea58bf2e153495

5 years ago

I also want to put in some kind of tests before merging this PR :)

rebased onto b815e32322530f101ed4f18f97c9e77d73c88878

5 years ago

1 new commit added

  • Add tests for pagure.lib.tasks.update_pull_request
5 years ago

rebased onto 736f3061ad5dfea992934f241a19b8430cc84728

5 years ago

Maybe just six.u() on both? Should be automatic no-op on py3, and do the same on py2 as you're doing, I think?

One small idea, otherwise +1.

We actually want the opposite, bytes not unicode :(

And six.b() doesn't seem to do what we want :(

Going to leave as it

Thanks for the review! :)

rebased onto 9742019

5 years ago

Pull-Request has been merged by pingou

5 years ago