#3109 Added Functionality which helps to sort PR's by recent activity.
Merged 5 years ago by pingou. Opened 6 years ago by ymdatta.
ymdatta/pagure master  into  master

file modified
+16 -7
@@ -2743,15 +2743,18 @@ 

  def search_pull_requests(

          session, requestid=None, project_id=None, project_id_from=None,

          status=None, author=None, assignee=None, count=False,

-         offset=None, limit=None, updated_after=None, branch_from=None):

-     ''' Retrieve the specified issue

+         offset=None, limit=None, updated_after=None, branch_from=None,

+         order='desc', order_key=None):

+     ''' Retrieve the specified pull-requests.

      '''

  

-     query = session.query(

-         model.PullRequest

-     ).order_by(

-         model.PullRequest.id.desc()

-     )

+     query = session.query(model.PullRequest)

+ 

+     # column by default is modelled after date_created.

+     column = model.PullRequest.date_created

+ 

+     if order_key == 'last_updated':

+         column = model.PullRequest.last_updated

  

      if requestid:

          query = query.filter(
@@ -2832,6 +2835,12 @@ 

              model.PullRequest.branch_from == branch_from

          )

  

+     # Depending on the order, the query is sorted(default is desc)

+     if order == 'asc':

+         query = query.order_by(asc(column))

+     else:

+         query = query.order_by(desc(column))

+ 

      if requestid:

          output = query.first()

      elif count:

@@ -90,8 +90,20 @@ 

          <tr>

              <th class="stretch-table-column">Pull Request</th>

              <th class="branch_to nowrap">To</th>

-             <th class="open_date nowrap">Opened</th>

-             <th class="last_updated nowrap">Modified</th>

+            <th class="open_date"><a href="{{ url_for('ui_ns.request_pulls',

+             repo=repo.name, username=username, namespace=repo.namespace,

+             tags=tags, author=author, assignee=assignee, priority=priority,

+             status=status or 'all', order_key='date_created',

+             order='date_created' | table_get_link_order(order_key, order)) }}">Opened</a>

+             {{ 'date_created' | table_sort_arrow(order_key, order) | safe }}</th>

+ 

+            <th class="mod_date"><a href="{{ url_for('ui_ns.request_pulls',

+             repo=repo.name, username=username, namespace=repo.namespace,

+             tags=tags, author=author, assignee=assignee, priority=priority,

+             status=status or 'all', order_key='last_updated',

+             order='last_updated' | table_get_link_order(order_key, order)) }}">Modified</a>

+             {{ 'last_updated' | table_sort_arrow(order_key, order) | safe }}</th>

+         

              {% if status|lower not in ['open', 'true'] %}

              <th class="closed_date nowrap">Closed</th>

              {% endif %}

file modified
+9 -1
@@ -65,11 +65,13 @@ 

  @UI_NS.route('/fork/<username>/<namespace>/<repo>/pull-requests/')

  @UI_NS.route('/fork/<username>/<namespace>/<repo>/pull-requests')

  def request_pulls(repo, username=None, namespace=None):

-     """ Create a pull request with the changes from the fork into the project.

+     """ List all Pull-requests associated to a repo

      """

      status = flask.request.args.get('status', 'Open')

      assignee = flask.request.args.get('assignee', None)

      author = flask.request.args.get('author', None)

+     order = flask.request.args.get('order', 'desc')

+     order_key = flask.request.args.get('order_key', 'date_created')

  

      repo = flask.g.repo

  
@@ -86,6 +88,8 @@ 

              flask.g.session,

              project_id=repo.id,

              status=True,

+             order=order,

+             order_key=order_key,

              assignee=assignee,

              author=author,

              offset=flask.g.offset,
@@ -108,6 +112,8 @@ 

          requests = pagure.lib.search_pull_requests(

              flask.g.session,

              project_id=repo.id,

+             order=order,

+             order_key=order_key,

              assignee=assignee,

              author=author,

              status=status,
@@ -146,6 +152,8 @@ 

          requests=requests,

          requests_cnt=requests_cnt,

          oth_requests=oth_requests,

+         order=order,

+         order_key=order_key,

          status=status,

          assignee=assignee,

          author=author,

@@ -20,11 +20,13 @@ 

  import tempfile

  import time

  import os

+ import re

  

  import pygit2

  import six

  from mock import patch, MagicMock

  from bs4 import BeautifulSoup

+ from datetime import datetime, timedelta

  

  sys.path.insert(0, os.path.join(os.path.dirname(

      os.path.abspath(__file__)), '..'))
@@ -837,6 +839,138 @@ 

          shutil.rmtree(newpath)

  

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

+     def test_request_pulls_order(self, send_email):

+         """Test the request_pulls

+         

+         i.e Make sure that the results are displayed

+         in the order required by the user"""

+         send_email.return_value = True

+ 

+         #Initially no project

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

+         self.assertEqual(output.status_code, 404)

+ 

+         tests.create_projects(self.session)

+         tests.create_projects_git(

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

+ 

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

+         item = pagure.lib.model.Project(

+             user_id=2,  

+             name='test',

+             description='test project #1',

+             hook_token='aaabbb',

+             is_fork=True,

+             parent_id=1,

+         )

+         self.session.add(item)

+         self.session.commit()

+ 

+         # create PR's to play with

+         # PR-1

+         req = pagure.lib.new_pull_request(

+             session=self.session,

+             repo_to=repo,

+             repo_from=item,

+             branch_from='feature',

+             branch_to='master',

+             title='PR from the feature branch',

+             user='pingou',

+             status='Open',

+             requestfolder=None,

+         )

+         self.session.commit()

+         self.assertEqual(req.id, 1)

+         self.assertEqual(req.title, 'PR from the feature branch')

+ 

+         # PR-2

+         req = pagure.lib.new_pull_request(

+             session=self.session,

+             repo_to=repo,

+             branch_to='master',

+             branch_from='feature',

+             repo_from=item,

+             title='test PR',

+             user='pingou',

+             status='Open',

+             requestfolder=None,

+         )

+         self.session.commit()

+         self.assertEqual(req.title, 'test PR')

+ 

+         # PR-3

+         req = pagure.lib.new_pull_request(

+             session=self.session,

+             repo_to=repo,

+             branch_from='feature',

+             branch_to='master',

+             repo_from=item,

+             title='test Invalid PR',

+             user='pingou',

+             status='Closed',

+             requestfolder=None,

+         )

+         self.session.commit()

+         self.assertEqual(req.title, 'test Invalid PR')

+ 

+         # PR-4

+         req = pagure.lib.new_pull_request(

+             session=self.session,

+             repo_to=repo,

+             branch_from='feature',

+             title='test PR for sort',

+             repo_from=item,

+             user='pingou',

+             branch_to='master',

+             status='Open',

+             requestfolder=None,

+         )

+         self.session.commit()

+         self.assertEqual(req.title, 'test PR for sort')

+ 

+         # sort by last_updated

+         output = self.app.get('/test/pull-requests?order_key=last_updated')

+         tr_elements = re.findall(r'<tr>(.*?)</tr>', output.data, re.M | re.S)

+         self.assertEqual(output.status_code, 200)

+         arrowed_th = ('Modified</a>\n            <span class="oi" data-glyph='

+                       '"arrow-thick-bottom"></span>')

+         # First table row is the header

+         self.assertIn(arrowed_th, tr_elements[0])

+         # Make sure that issue four is first since it was modified last

+         self.assertIn('href="/test/pull-request/4"', tr_elements[1])

+         self.assertIn('href="/test/pull-request/2"', tr_elements[2])

+         self.assertIn('href="/test/pull-request/1"', tr_elements[3])

+ 

+         pr_one = pagure.lib.search_pull_requests(

+             self.session, project_id=1, requestid=1)

+         pr_one.last_updated = datetime.utcnow() + timedelta(seconds=2)

+         self.session.add(pr_one)

+         self.session.commit()

+ 

+         # sort by last_updated

+         output = self.app.get('/test/pull-requests?order_key=last_updated')

+         tr_elements = re.findall(r'<tr>(.*?)</tr>', output.data, re.M | re.S)

+         self.assertEqual(output.status_code, 200)

+         # Make sure that PR four is first since it was modified last

+         self.assertIn('href="/test/pull-request/1"', tr_elements[1])

+         # Make sure that PR two is second since it was modified second

+         self.assertIn('href="/test/pull-request/4"', tr_elements[2])

+         # Make sure that PR one is last since it was modified first

+         self.assertIn('href="/test/pull-request/2"', tr_elements[3])

+ 

+ 

+         # Now query so that the results are ascending

+         output = self.app.get('/test/pull-requests?'

+                 'order_key=last_updated&order=asc')

+         tr_elements = re.findall(r'<tr>(.*?)</tr>', output.data, re.M | re.S)

+         arrowed_th = ('Modified</a>\n            <span class="oi" data-glyph='

+                       '"arrow-thick-top"></span>')

+         self.assertIn(arrowed_th, tr_elements[0])

+         self.assertIn('href="/test/pull-request/2"', tr_elements[1])

+         self.assertIn('href="/test/pull-request/4"', tr_elements[2])

+         self.assertIn('href="/test/pull-request/1"', tr_elements[3])

+ 

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

      def test_request_pulls(self, send_email):

          """ Test the request_pulls endpoint. """

          send_email.return_value = True
@@ -899,6 +1033,7 @@ 

          self.assertIn(

              '<h2 class="p-b-1">\n    0 Closed/Merged Pull Requests (of 0)\n  </h2>',

              output_text)

+ 

          # Close is primary

          self.assertIn(

              '<a class="btn btn-secondary btn-sm" '

Changed /lib/init.py to accept order in which the PR's are to
be sorted from requests.html, so that if either Opened/Modified
is clicked, the PR's are sorted accordingly with an arrow symbol
beside them describing the way they were sorted.

It fixes https://pagure.io/pagure/issue/1404

There is a .swo file that we should remove :)

While interesting this change isn't related to this commit and isn't used later on, so let's remove it for now, or make it another commit and finish it :)

This looks like a good start, but should add some tests to it though :)

1 new commit added

  • "Removed .swo file and modified fork.py"
6 years ago

why is the docstring changed?

@farhaan, there are two functions with very similar names: request_pull & request_pulls.

They both had the same docstring though the functionality of request_pulls is to list all the PR's, whereas the functionality of request_pull is to create PR related to changes from the fork, so I changed it.

Did I do something wrong?

Ohh! That is fine then :smile:

What is the progress with test cases?

1 new commit added

  • Added test cased for sorting of PR's.
6 years ago

@pingou i found a bug which is when we open PR's page, Modified date present is actually not last_updated date, so changed it in order to show correct modified date.

Also added test cases, pls tell me if there's anything wrong (or) i should improve anything :)

1 new commit added

  • "small changes in fork.py"
6 years ago

No you actually want the updated_on as otherwise any change made to the entry in the database will affect this field, including resetting the merged status.
So if I merge a PR, all the other PRs will have the last_updated field changed and this ends up being more confusing than helpful.

@pingou Yeah..you're right, i tried this on my machine and other PR's last_updated field changed when i merged a PR.

There are some mistakes with the test cases also, will get back on this.

1 new commit added

  • "Revert changes related to last_updated in requests.html"
6 years ago

We'll want to squash all the commits into one (or max two) and rebase on the top of master, but from a quick look it looks good :)

rebased onto 430d59e4b0e3fb2fa0a64c5581e1e86249dfe530

5 years ago

1 new commit added

  • Added unit tests to check sorting functionality of PR's.
5 years ago

rebased onto 45514142dd469f4c7fe655738640a6ebefacb30d

5 years ago

2 new commits added

  • Added unit-tests which checks sorting functionality of PR's.
  • Added Functionality which helps to sort PR's by recent activity.
5 years ago

2 new commits added

  • Added unit-tests which checks sorting functionality of PR's.
  • Added Functionality which helps to sort PR's by recent activity.
5 years ago

@pingou , i added tests for sorting functionality and also rebased, let me know if i need to add anything more. :)

Could you rebase, I'd like to give it a final test :)

rebased onto 832ab80

5 years ago

@pingou Done :)
Are there any more changes i should make?

I am going to fix a small wording in the first commit and merge this PR manually (if it passes the tests which are currently running locally).

Thanks for working on this and sorry it took so long to get it in :(

Looking in the details of the two commits (and not the combined diff), I will squash both commits into one as the second commit undo a mistake from the first and as a little code style change so there is no real point to have both.

@pingou , Reason i made two different commits was, one has the functionality and other the tests, i thought it would be better to separate both of them. If you think its ok, to squash them, feel free to do.

If you find any problems, please let me know :)

Commit 9ab5d00 fixes this pull-request

Pull-Request has been merged by pingou

5 years ago

Reason i made two different commits was, one has the functionality and other the tests

Somewhere the split got lost, see: https://pagure.io/fork/ymdatta/pagure/c/596624b715ab2ea24d25112c80a48b7affa77918