internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: Nitrate/Nitrate

Issue:
Fix call to DurationField.from_db_value() which will be removed in Django 3.0
Lots of such warning are output.

```
src/tests/xmlrpc/test_testcaseplan.py::TestCasePlanGet::test_get_with_negative_case_id
  /testenv/lib/python3.7/site-packages/django/db/models/sql/compiler.py:995: RemovedInDjango30Warning: Remove the context parameter from DurationField.from_db_value(). Support for it will be removed in Django 3.0.
    RemovedInDjango30Warning,
```


Discussion:

Resolving patch:
diff --git a/src/tcms/core/models/fields.py b/src/tcms/core/models/fields.py
--- a/src/tcms/core/models/fields.py
+++ b/src/tcms/core/models/fields.py
@@ -36,7 +36,7 @@ def to_python(self, value):
         else:
             raise TypeError('Unable to convert %s to timedelta.' % value)
 
-    def from_db_value(self, value, expression, connection, context):
+    def from_db_value(self, value, *args, **kwargs):
         if value is None:
             return value
         return datetime.timedelta(seconds=value)


Test patch:
Repository: Nitrate/Nitrate

Issue:
Traceback error while typing characters to add a tag
```
Internal Server Error: /management/getinfo/
Traceback (most recent call last):
  File "/code/Nitrate/.env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/code/Nitrate/.env/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/code/Nitrate/.env/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/code/Nitrate/.env/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/code/Nitrate/src/tcms/core/ajax.py", line 156, in info
    for o in obj():
  File "/code/Nitrate/src/tcms/core/ajax.py", line 131, in tags
    '|'.join(["models.Q(name__startswith = '%s')" % item for item in seq])
  File "<string>", line 1, in <module>
NameError: name 'models' is not defined
```


Discussion:
But, this does not block adding a tag.

Resolving patch:
diff --git a/src/tcms/core/ajax.py b/src/tcms/core/ajax.py
--- a/src/tcms/core/ajax.py
+++ b/src/tcms/core/ajax.py
@@ -5,10 +5,14 @@
 Most of these functions are use for Ajax.
 """
 import datetime
-import sys
 import json
+import operator
+import sys
+
+from six.moves import reduce
 
 from django import http
+from django.db import models
 from django.contrib.auth.models import User
 from django.core import serializers
 from django.core.exceptions import ObjectDoesNotExist
@@ -127,9 +131,11 @@ def tags(self):
             # case sensitive table
             if query.get('name__startswith'):
                 seq = get_string_combinations(query['name__startswith'])
-                tags = tags.filter(eval(
-                    '|'.join(["models.Q(name__startswith = '%s')" % item for item in seq])
-                ))
+                criteria = reduce(
+                    operator.or_,
+                    (models.Q(name__startswith=item) for item in seq)
+                )
+                tags = tags.filter(criteria)
                 del query['name__startswith']
 
             tags = tags.filter(**query).distinct()


Test patch:
Repository: Nitrate/Nitrate

Issue:
import xml says Worng xml_version
import xml in not working says worng xml_version 1.1

i export the test case and generate xml and try to import same not work

thanks in advance


Discussion:

Resolving patch:
diff --git a/docker/released/product.py b/docker/released/product.py
--- a/docker/released/product.py
+++ b/docker/released/product.py
@@ -44,7 +44,6 @@
 
 # added for nitrate3.4 compatibility
 DEFAULT_GROUPS = ['default']
-TESTOPIA_XML_VERSION = '1.0'
 
 ADMINS = (
 )
diff --git a/src/tcms/settings/product.py b/src/tcms/settings/product.py
--- a/src/tcms/settings/product.py
+++ b/src/tcms/settings/product.py
@@ -60,7 +60,6 @@
 
 # added for nitrate3.4 compatibility
 DEFAULT_GROUPS = ['default']
-TESTOPIA_XML_VERSION = '1.0'
 
 # admin settings
 ADMINS = (


Test patch:
Repository: Nitrate/Nitrate

Issue:
Drop Django 1.11
AC:

- Remove from `tox.ini`
- Remove from `.travis.yml`
- Update Django verison range in `setup.py`


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -14,7 +14,7 @@ def get_long_description():
 
 install_requires = [
     'beautifulsoup4 >= 4.1.1',
-    'django >= 1.11,<3.0',
+    'django >= 2.0,<3.0',
     'django-contrib-comments == 1.8.0',
     'django-tinymce == 2.7.0',
     'django-uuslug == 1.1.8',
@@ -91,7 +91,6 @@ def get_long_description():
     zip_safe=False,
     classifiers=[
         'Framework :: Django',
-        'Framework :: Django :: 1.11',
         'Framework :: Django :: 2.0',
         'Framework :: Django :: 2.1',
         'Intended Audience :: Developers',


Test patch:
Repository: Nitrate/Nitrate

Issue:
Add Django 2.2.x
Add a testenv in tox.ini for Django 2.2.
Add "Framework :: Django :: 2.2" to ``setup.py``.
Update README to add version `2.2`.



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -93,6 +93,7 @@ def get_long_description():
         'Framework :: Django',
         'Framework :: Django :: 2.0',
         'Framework :: Django :: 2.1',
+        'Framework :: Django :: 2.2',
         'Intended Audience :: Developers',
         'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
         'Programming Language :: Python :: 3',


Test patch:
Repository: Nitrate/Nitrate

Issue:
Provide a way to validate a user object's metadata before storing into database
The point to check is the email. A user object must have an email set, and it must be unique for the user object. This should be checked and fail the creation immediately.


Discussion:
Issues #229 #327 should be closed after this is done.

Resolving patch:
diff --git a/src/tcms/auth/apps.py b/src/tcms/auth/apps.py
--- a/src/tcms/auth/apps.py
+++ b/src/tcms/auth/apps.py
@@ -1,8 +1,31 @@
 from django.apps import AppConfig as DjangoAppConfig
+from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 
 
+def user_klass_clean(self):
+    """Add additional validation to avoid using same email address"""
+    from django.contrib.auth.models import User
+
+    # Repeat the real work in User.clean
+    super(User, self).clean()
+    self.email = self.__class__.objects.normalize_email(self.email)
+
+    # Additional checks to validate
+    if self.email and User.objects.filter(email=self.email).exists():
+        raise ValidationError(
+            f'There is already an existing user with email {self.email}')
+
+
+def patch_user_model_clean():
+    from django.contrib.auth.models import User
+    User.clean = user_klass_clean
+
+
 class AppConfig(DjangoAppConfig):
     name = 'tcms.auth'
     label = 'tcms.core.contrib.auth'
     verbose_name = _("Core auth")
+
+    def ready(self):
+        patch_user_model_clean()
diff --git a/src/tcms/auth/migrations/0002_add_index_to_user_email.py b/src/tcms/auth/migrations/0002_add_index_to_user_email.py
new file mode 100644
--- /dev/null
+++ b/src/tcms/auth/migrations/0002_add_index_to_user_email.py
@@ -0,0 +1,16 @@
+# Generated by Django 2.2.7 on 2020-01-31 07:17
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('tcms.core.contrib.auth', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RunSQL(
+            'CREATE INDEX idx_user_email ON auth_user (email)'
+        )
+    ]


Test patch:
Repository: Nitrate/Nitrate

Issue:
When select "Select All" in plan page, fail to create a new run
```
InvalidQuery at /run/new/

Field TestCase.default_tester cannot be both deferred and traversed using select_related at the same time.
```


Discussion:

Resolving patch:
diff --git a/src/tcms/testcases/views.py b/src/tcms/testcases/views.py
--- a/src/tcms/testcases/views.py
+++ b/src/tcms/testcases/views.py
@@ -358,12 +358,6 @@ def query_testcases(request, plan, search_form):
     if plan:
         tcs = tcs.filter(plan=plan)
 
-    tcs = tcs.select_related('author',
-                             'default_tester',
-                             'case_status',
-                             'priority',
-                             'category',
-                             'reviewer')
     return tcs
 
 
@@ -520,6 +514,9 @@ def all(request, template_name="case/all.html"):
     else:
         ttags = get_tags_from_cases((case.pk for case in tcs))
 
+    tcs = tcs.prefetch_related(
+        'author', 'default_tester', 'case_status', 'category', 'priority'
+    )
     tcs = paginate_testcases(request, tcs)
 
     # There are several extra information related to each TestCase to be shown
diff --git a/src/tcms/testruns/views.py b/src/tcms/testruns/views.py
--- a/src/tcms/testruns/views.py
+++ b/src/tcms/testruns/views.py
@@ -96,13 +96,13 @@ def new(request, template_name='run/new.html'):
     num_unconfirmed_cases = tcs.exclude(case_status=confirm_status).count()
     estimated_time = datetime.timedelta(seconds=0)
 
-    tcs_values = (
-        tcs.select_related('author', 'case_status', 'category', 'priority')
-           .only('case_id', 'summary', 'estimated_time', 'author__email',
-                 'create_date', 'category__name', 'priority__value',
-                 'case_status__name')
-           .order_by('case_id')
-    )
+    tcs_values = tcs.select_related(
+        'author', 'case_status', 'category', 'priority'
+    ).only(
+        'case_id', 'summary', 'create_date', 'estimated_time',
+        'author__email', 'case_status__name', 'priority__value',
+        'category__name'
+    ).order_by('case_id')
 
     if request.POST.get('POSTING_TO_CREATE'):
         form = NewRunForm(request.POST)


Test patch:
Repository: Nitrate/Nitrate

Issue:
Remove Django 2.0
Django 2.0 is not supported and marked as insecure. Refer to https://docs.djangoproject.com/en/2.0/


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -14,7 +14,7 @@ def get_long_description():
 
 install_requires = [
     'beautifulsoup4 >= 4.1.1',
-    'django >= 2.0,<3.0',
+    'django >= 2.1,<3.0',
     'django-contrib-comments == 1.9.1',
     'django-tinymce == 2.7.0',
     'django-uuslug == 1.1.8',


Test patch:
Repository: Nitrate/Nitrate

Issue:
Upgrade celery to 4.3.0
As per title. Remove `skipIf` from test `test_uses_celery`.


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -61,7 +61,7 @@ def get_long_description():
 
     # Required packages required to run async tasks
     'async': [
-        'celery == 4.2.0',
+        'celery == 4.4.2',
     ],
 
     'multiauth': [


Test patch:
diff --git a/src/tests/core/test_core.py b/src/tests/core/test_core.py
--- a/src/tests/core/test_core.py
+++ b/src/tests/core/test_core.py
@@ -328,8 +328,6 @@ def test_uses_threading(self, Thread):
             self.assertTrue(thread.daemon)
             thread.start.assert_called_once()
 
-    @unittest.skipIf(PY37, 'Celery<4.3 does not work with Python 3.7. '
-                           'Waiting for 4.3 to be released.')
     @patch('celery.shared_task')
     def test_uses_celery(self, shared_task):
         with patch.object(settings, 'ASYNC_TASK', new=AsyncTask.CELERY.value):
Repository: Nitrate/Nitrate

Issue:
Abstract jQuery.ajax calls
Lots of jQuery.ajax calls in JavaScript code, that repeat same structure and part of the same request arguments.


Discussion:

Resolving patch:
diff --git a/src/tcms/comments/views.py b/src/tcms/comments/views.py
--- a/src/tcms/comments/views.py
+++ b/src/tcms/comments/views.py
@@ -5,7 +5,6 @@
 from django.conf import settings
 from django.contrib.auth.mixins import PermissionRequiredMixin
 from django.http import JsonResponse
-from django.shortcuts import render
 from django.views import generic
 from django.views.decorators.http import require_POST
 
@@ -27,8 +26,10 @@ def post(request, template_name='comments/comments.html'):
         target, _ = post_comment(
             data, request.user, request.META.get('REMOTE_ADDR'))
     except InvalidCommentPostRequest as e:
-        target = e.target
-    return render(request, template_name, context={'object': target})
+        msg = f'Fail to add comment to object {e.target}'
+        log.exception(msg)
+        return JsonResponseBadRequest({'message': msg})
+    return JsonResponse({})
 
 
 class DeleteCommentView(PermissionRequiredMixin, generic.View):


Test patch:
diff --git a/src/tests/test_comments.py b/src/tests/test_comments.py
--- a/src/tests/test_comments.py
+++ b/src/tests/test_comments.py
@@ -12,7 +12,7 @@
 from tests import factories as f, user_should_have_perm, HelperAssertions
 
 
-class TestPostComment(test.TestCase):
+class TestPostComment(HelperAssertions, test.TestCase):
     """Test post comments"""
 
     @classmethod
@@ -68,11 +68,7 @@ def test_post_a_comment_by_authenticated_user(self):
     def test_still_response_comments_even_if_fail_to_add_one(self):
         self.client.login(username=self.tester.username, password='password')
         response = self._post_comment('comment' * 2000)
-        self.assertContains(
-            response,
-            '<ul class="ul-no-format comment"></ul>',
-            html=True
-        )
+        self.assert400(response)
 
 
 class TestDeleteComment(HelperAssertions, test.TestCase):
Repository: Nitrate/Nitrate

Issue:
Adding comment to selected case runs only works for the last selected one
Steps:

* Open a test run page
* Select a few of case runs
* Add comment from the Comment dropdown menu
* The comment is only added to the last selected case run.


Discussion:

Resolving patch:
diff --git a/src/tcms/core/ajax.py b/src/tcms/core/ajax.py
--- a/src/tcms/core/ajax.py
+++ b/src/tcms/core/ajax.py
@@ -765,16 +765,18 @@ def comment_case_runs(request):
     comment = data.get('comment', None)
     if not comment:
         return JsonResponseBadRequest({'message': 'Comments needed'})
-    run_ids = [i for i in data.get('run', '').split(',') if i]
+    run_ids = [int(item) for item in data.getlist('run')]
     if not run_ids:
         return JsonResponseBadRequest({'message': 'No runs selected.'})
-    case_run_ids = TestCaseRun.objects.filter(
-        pk__in=run_ids).values_list('pk', flat=True)
+    case_run_ids = (
+        TestCaseRun.objects.filter(pk__in=run_ids).values_list('pk', flat=True)
+    )
     if not case_run_ids:
         return JsonResponseBadRequest({'message': 'No caserun found.'})
     tcms.comments.models.add_comment(
         request.user, 'testruns.testcaserun', case_run_ids, comment,
-        request.META.get('REMOTE_ADDR'))
+        request.META.get('REMOTE_ADDR')
+    )
     return JsonResponse({})
 
 


Test patch:
diff --git a/src/tests/core/test_views.py b/src/tests/core/test_views.py
--- a/src/tests/core/test_views.py
+++ b/src/tests/core/test_views.py
@@ -144,9 +144,10 @@ def test_refuse_if_missing_no_case_run_pk(self):
     def test_refuse_if_passed_case_run_pks_not_exist(self):
         self.client.login(username=self.tester.username, password='password')
 
-        response = self.client.post(self.many_comments_url,
-                                    {'comment': 'new comment',
-                                     'run': '99999998,1009900'})
+        response = self.client.post(self.many_comments_url, {
+            'comment': 'new comment',
+            'run': [99999998, 1009900]
+        })
         self.assertJsonResponse(
             response,
             {'message': 'No caserun found.'},
@@ -159,7 +160,7 @@ def test_add_comment_to_case_runs(self):
         new_comment = 'new comment'
         response = self.client.post(self.many_comments_url, {
             'comment': new_comment,
-            'run': f'{self.case_run_1.pk},{self.case_run_2.pk}'
+            'run': [self.case_run_1.pk, self.case_run_2.pk]
         })
         self.assertJsonResponse(response, {})
Repository: Nitrate/Nitrate

Issue:
Can't change case run status from the Status dropdown menu
Steps:

* Open a test run page
* Select a few of case runs
* Choose one status to change from Status dropdown menu
* Server side responses 500, the error looks like:




Discussion:

Resolving patch:
diff --git a/src/tcms/logs/views.py b/src/tcms/logs/views.py
--- a/src/tcms/logs/views.py
+++ b/src/tcms/logs/views.py
@@ -44,11 +44,11 @@ def get_log_create_data(self):
             'new_value': self.new_value,
         }
 
-    def make(self, who, new_value, field='', original_value=''):
+    def make(self, who, new_value, field=None, original_value=None):
         """Create new log"""
         self.who = who
-        self.field = field
-        self.original_value = original_value
+        self.field = field or ''
+        self.original_value = original_value or ''
         self.new_value = new_value
 
         model = self.get_new_log_object()


Test patch:
diff --git a/src/tests/core/test_core.py b/src/tests/core/test_core.py
--- a/src/tests/core/test_core.py
+++ b/src/tests/core/test_core.py
@@ -6,6 +6,7 @@
 
 from django import test
 from django.conf import settings
+from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
 from django.core import mail
 
@@ -15,6 +16,7 @@
 from tcms.core.mailto import mailto
 from tcms.core.task import AsyncTask
 from tcms.core.task import Task
+from tcms.management.models import Classification
 from tests import HelperAssertions
 from tests.factories import TestPlanFactory
 
@@ -375,3 +377,34 @@ def test_log_traceback_when_error_is_raised_from_send(self, logger, EmailMessage
         EmailMessage.return_value.send.side_effect = smtplib.SMTPException
         mailto('mail_template', 'Start Test', ['tester@localhost'])
         logger.exception.assert_called_once()
+
+
+class TestModelLogAction(test.TestCase):
+    """Test TCMSModel.log_action"""
+
+    @classmethod
+    def setUpTestData(cls):
+        cls.tester = User.objects.create_user(
+            username='tester', email='tester@localhost'
+        )
+        cls.classification = Classification.objects.create(name='webapp')
+
+    def test_log_action(self):
+        # who, new_value, field, original_value
+        test_cases = (
+            (self.tester, 'new value', None, None),
+            (self.tester, 'new value', '', None),
+            (self.tester, 'new value', '', ''),
+            (self.tester, 'new value', None, ''),
+            (self.tester, 'new value', 'field', None),
+            (self.tester, 'new value', 'field', 'old value'),
+        )
+
+        for log_args in test_cases:
+            log = self.classification.log_action(*log_args)
+
+            who, new_value, field, original_value = log_args
+            self.assertEqual(who, log.who)
+            self.assertEqual(new_value, log.new_value)
+            self.assertEqual(field or '', log.field)
+            self.assertEqual(original_value or '', log.original_value)
Repository: Nitrate/Nitrate

Issue:
Upgrade db image to newer version
Upgrade following images:

- MySQL 8.0.20
- PostgreSQL 12.2
- MariaDB 10.4.12


Discussion:

Resolving patch:
diff --git a/contrib/travis-ci/testrunner.py b/contrib/travis-ci/testrunner.py
--- a/contrib/travis-ci/testrunner.py
+++ b/contrib/travis-ci/testrunner.py
@@ -39,11 +39,11 @@
 DB_CONTAINER_INFO = {
     'mysql': {
         'db_engine': 'mysql',
-        'db_image': 'mysql:5.7',
+        'db_image': 'mysql:8.0.20',
     },
     'mariadb': {
         'db_engine': 'mysql',
-        'db_image': 'mariadb:10.2.21',
+        'db_image': 'mariadb:10.4.12',
     },
     'sqlite': {
         'db_engine': 'sqlite',
@@ -51,7 +51,7 @@
     },
     'postgres': {
         'db_engine': 'pgsql',
-        'db_image': 'postgres:10.6',
+        'db_image': 'postgres:12.2',
     },
 }
 


Test patch:
Repository: Nitrate/Nitrate

Issue:
Replace smart_text() with smart_str()
To suppress warning: `RemovedInDjango40Warning: smart_text() is deprecated in favor of smart_str()`

Run command `runserver`, find out the warning messages from output.


Discussion:

Resolving patch:
diff --git a/src/tcms/logs/views.py b/src/tcms/logs/views.py
--- a/src/tcms/logs/views.py
+++ b/src/tcms/logs/views.py
@@ -5,7 +5,7 @@
 
 from .models import TCMSLogModel
 
-from django.utils.encoding import smart_text as smart_unicode
+from django.utils.encoding import smart_str
 
 
 class TCMSLog:
@@ -62,7 +62,7 @@ def get_query_set(self):
         model = self.get_log_model()
 
         qs = model.objects.filter(content_type=ctype,
-                                  object_pk=smart_unicode(self.model.pk),
+                                  object_pk=smart_str(self.model.pk),
                                   site=settings.SITE_ID)
         qs = qs.select_related('who')
         return qs


Test patch:
Repository: Nitrate/Nitrate

Issue:
Replace hardcode admin url in templates
For example `{{ ADMIN_PREFIX }}/management/version/add/`


Discussion:

Resolving patch:
diff --git a/src/tcms/core/context_processors.py b/src/tcms/core/context_processors.py
--- a/src/tcms/core/context_processors.py
+++ b/src/tcms/core/context_processors.py
@@ -2,13 +2,6 @@
 from django.conf import settings
 
 
-def admin_prefix_processor(request):
-    """
-    Django Admin URL Prefix RequestContext Handler
-    """
-    return {'ADMIN_PREFIX': settings.ADMIN_PREFIX}
-
-
 def request_contents_processor(request):
     """
     Django request contents RequestContext Handler
diff --git a/src/tcms/settings/common.py b/src/tcms/settings/common.py
--- a/src/tcms/settings/common.py
+++ b/src/tcms/settings/common.py
@@ -93,9 +93,6 @@
 # Examples: "http://example.com/media/", "http://media.example.com/"
 MEDIA_URL = ''
 
-# URL prefix for admin absolute URL
-ADMIN_PREFIX = '/admin'
-
 LOGIN_URL = 'nitrate-login'
 LOGIN_REDIRECT_URL = 'user-profile-redirect'
 LOGOUT_REDIRECT_URL = 'nitrate-login'
@@ -148,7 +145,6 @@
 
                 # Added for Nitrate
                 'django.template.context_processors.request',
-                'tcms.core.context_processors.admin_prefix_processor',
                 'tcms.core.context_processors.request_contents_processor',
                 'tcms.core.context_processors.settings_processor',
             ],


Test patch: