From 9742019b58e32ad9f61be92f4de72f84a9d75ea1 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 05 2018 12:41:42 +0000 Subject: [PATCH 1/2] When a pushed in made to a branch in a PR, update the PR Fixes https://pagure.io/pagure/issue/3684 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/default.py b/pagure/hooks/default.py index 0502080..342b8d4 100644 --- a/pagure/hooks/default.py +++ b/pagure/hooks/default.py @@ -208,6 +208,9 @@ def inform_pull_request_urls( " %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) diff --git a/pagure/lib/tasks.py b/pagure/lib/tasks.py index 6949be9..ea23871 100644 --- a/pagure/lib/tasks.py +++ b/pagure/lib/tasks.py @@ -459,7 +459,7 @@ def delete_branch(self, session, name, namespace, user, branchname): ) -@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 @@ def sync_pull_ref(self, session, name, namespace, user, requestid): 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): From 7bcddc43b02042bcd29c18a96aaec09c626adc03 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 05 2018 12:41:42 +0000 Subject: [PATCH 2/2] Add tests for pagure.lib.tasks.update_pull_request Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/test_pagure_flask_ui_fork.py b/tests/test_pagure_flask_ui_fork.py index 1f950b4..d1a4194 100644 --- a/tests/test_pagure_flask_ui_fork.py +++ b/tests/test_pagure_flask_ui_fork.py @@ -33,6 +33,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure import pagure.lib +import pagure.lib.tasks import tests from pagure.lib.repo import PagureRepo @@ -294,6 +295,90 @@ class PagureFlaskForktests(tests.Modeltests): 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( + 'PR#1: PR from the feature branch - test\n - Pagure', + output_text) + self.assertIn( + 'title="View file as of 2a552b">sources', 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: + 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