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

Issue:
[cliquet] Implement a contribute.json endpoint
Original: https://github.com/mozilla-services/cliquet/issues/334

oh! mozilla has a micro-protocol https://www.contributejson.org/

Let's implement that view



Discussion:

Resolving patch:
diff --git a/kinto/__init__.py b/kinto/__init__.py
--- a/kinto/__init__.py
+++ b/kinto/__init__.py
@@ -12,7 +12,7 @@
 __version__ = pkg_resources.get_distribution(__package__).version
 
 # Implemented HTTP API Version
-HTTP_API_VERSION = '1.5'
+HTTP_API_VERSION = '1.6'
 
 # Main kinto logger
 logger = logging.getLogger(__name__)
diff --git a/kinto/views/contribute.py b/kinto/views/contribute.py
new file mode 100644
--- /dev/null
+++ b/kinto/views/contribute.py
@@ -0,0 +1,34 @@
+from cornice import Service
+from pyramid.security import NO_PERMISSION_REQUIRED
+
+
+contribute = Service(name='contribute.json',
+                     description='Open-source information',
+                     path='/contribute.json')
+
+
+@contribute.get(permission=NO_PERMISSION_REQUIRED)
+def contribute_get(request):
+    return {
+        "name": "kinto",
+        "description": "A minimalist JSON storage service.",
+        "repository": {
+            "url": "https://github.com/Kinto/kinto",
+            "license": "Apache License (2.0)"
+        },
+        "participate": {
+            "docs": "https://kinto.readthedocs.io/",
+            "mailing-list": "kinto@mozilla.org",
+            "irc": "irc://irc.freenode.net/#kinto",
+        },
+        "keywords": [
+            "JSON",
+            "Python",
+            "Offline",
+            "Sync",
+            "Storage",
+        ],
+        "urls": {
+            "dev": "https://kinto.dev.mozaws.net/v1/"
+        }
+    }


Test patch:
diff --git a/kinto/tests/test_views_contribute.py b/kinto/tests/test_views_contribute.py
new file mode 100644
--- /dev/null
+++ b/kinto/tests/test_views_contribute.py
@@ -0,0 +1,11 @@
+from .support import BaseWebTest, unittest
+
+
+class ContributeViewTest(BaseWebTest, unittest.TestCase):
+
+    def test_returns_info_about_project(self):
+        response = self.app.get('/contribute.json')
+        keys = sorted(response.json.keys())
+        expected = ['description', 'keywords', 'name', 'participate',
+                    'repository', 'urls']
+        self.assertEqual(keys, expected)
Repository: Kinto/kinto

Issue:
[cliquet] Add timeout option for Redis client
original: https://github.com/mozilla-services/cliquet/issues/582

all in the title.



Discussion:

Resolving patch:
diff --git a/kinto/core/storage/redis.py b/kinto/core/storage/redis.py
--- a/kinto/core/storage/redis.py
+++ b/kinto/core/storage/redis.py
@@ -36,6 +36,10 @@ def create_from_config(config, prefix=''):
         "password": uri.password or None,
         "db": int(uri.path[1:]) if uri.path else 0
     }
+    block_timeout = settings.get(prefix + 'pool_timeout')
+    if block_timeout is not None:
+        kwargs["timeout"] = float(block_timeout)
+
     connection_pool = redis.BlockingConnectionPool(**kwargs)
     return redis.StrictRedis(connection_pool=connection_pool)
 


Test patch:
diff --git a/kinto/tests/core/test_cache.py b/kinto/tests/core/test_cache.py
--- a/kinto/tests/core/test_cache.py
+++ b/kinto/tests/core/test_cache.py
@@ -270,6 +270,12 @@ def test_config_is_taken_in_account(self):
             backend.settings,
             {'host': 'peer.loc', 'password': 'secret', 'db': 7, 'port': 4444})
 
+    def test_timeout_is_passed_to_redis_client(self):
+        config = testing.setUp(settings=self.settings)
+        config.add_settings({'cache_pool_timeout': '1.5'})
+        backend = self.backend.load_from_config(config)
+        self.assertEqual(backend._client.connection_pool.timeout, 1.5)
+
 
 @skip_if_no_postgresql
 class PostgreSQLCacheTest(BaseTestCache, unittest.TestCase):
diff --git a/kinto/tests/core/test_permission.py b/kinto/tests/core/test_permission.py
--- a/kinto/tests/core/test_permission.py
+++ b/kinto/tests/core/test_permission.py
@@ -494,6 +494,12 @@ def test_config_is_taken_in_account(self):
             backend.settings,
             {'host': 'db.loc', 'password': 'pass', 'db': 5, 'port': 1234})
 
+    def test_timeout_is_passed_to_redis_client(self):
+        config = testing.setUp(settings=self.settings)
+        config.add_settings({'permission_pool_timeout': '1.5'})
+        backend = self.backend.load_from_config(config)
+        self.assertEqual(backend._client.connection_pool.timeout, 1.5)
+
 
 @skip_if_no_postgresql
 class PostgreSQLPermissionTest(BaseTestPermission, unittest.TestCase):
diff --git a/kinto/tests/core/test_storage.py b/kinto/tests/core/test_storage.py
--- a/kinto/tests/core/test_storage.py
+++ b/kinto/tests/core/test_storage.py
@@ -1148,6 +1148,12 @@ def test_config_is_taken_in_account(self):
             backend.settings,
             {'host': 'store.loc', 'password': 'blah', 'db': 6, 'port': 7777})
 
+    def test_timeout_is_passed_to_redis_client(self):
+        config = testing.setUp(settings=self.settings)
+        config.add_settings({'storage_pool_timeout': '1.5'})
+        backend = self.backend.load_from_config(config)
+        self.assertEqual(backend._client.connection_pool.timeout, 1.5)
+
     def test_backend_error_provides_original_exception(self):
         StorageTest.test_backend_error_provides_original_exception(self)
Repository: Kinto/kinto

Issue:
[cliquet] Add a way to change the port used when using kinto start.
original: https://github.com/mozilla-services/cliquet/issues/686

All in title



Discussion:

Resolving patch:
diff --git a/kinto/__main__.py b/kinto/__main__.py
--- a/kinto/__main__.py
+++ b/kinto/__main__.py
@@ -52,6 +52,11 @@ def main(args=None):
                               help='Restart when code or config changes',
                               required=False,
                               default=False)
+    parser_start.add_argument('--port',
+                              type=int,
+                              help='Listening port number',
+                              required=False,
+                              default=8888)
     parser_start.set_defaults(which='start')
 
     parsed_args = vars(parser.parse_args(args))
@@ -94,6 +99,7 @@ def main(args=None):
         pserve_argv = ['pserve', config_file]
         if parsed_args['reload']:
             pserve_argv.append('--reload')
+        pserve_argv.append('http_port=%s' % parsed_args['port'])
         pserve.main(pserve_argv)
 
     return 0


Test patch:
diff --git a/kinto/tests/test_configuration/test.ini b/kinto/tests/test_configuration/test.ini
--- a/kinto/tests/test_configuration/test.ini
+++ b/kinto/tests/test_configuration/test.ini
@@ -5,7 +5,7 @@
 [server:main]
 use = egg:waitress#main
 host = 0.0.0.0
-port = 8888
+port = %(http_port)s
 
 
 [app:main]
Repository: Kinto/kinto

Issue:
Inconsistency with 404 response on empty collections
- Set `read_only` to true
- Give readonly access to the postgresql user
- Give `read` permission to everyone on a bucket `foo`
- Going to `/buckets/foo/collections/unknown` gives 404
- Going to `/buckets/foo/collections/unknown/records` gives 503

Listing the records of an unknown collection should definitely give 404, except with the `default` bucket plugin.

Very related to https://github.com/Kinto/kinto/issues/558



Discussion:

Resolving patch:
diff --git a/kinto/views/records.py b/kinto/views/records.py
--- a/kinto/views/records.py
+++ b/kinto/views/records.py
@@ -26,23 +26,22 @@ class Record(resource.ShareableResource):
     mapping = RecordSchema()
     schema_field = 'schema'
 
-    def __init__(self, *args, **kwargs):
-        super(Record, self).__init__(*args, **kwargs)
-
-        self.model.id_generator = RelaxedUUID()
-
+    def __init__(self, request, **kwargs):
+        # Before all, first check that the parent collection exists.
         # Check if already fetched before (in batch).
-        collections = self.request.bound_data.setdefault('collections', {})
-        collection_uri = self.get_parent_id(self.request)
+        collections = request.bound_data.setdefault('collections', {})
+        collection_uri = self.get_parent_id(request)
         if collection_uri not in collections:
             # Unknown yet, fetch from storage.
             collection_parent_id = '/buckets/%s' % self.bucket_id
-            collection = object_exists_or_404(self.request,
+            collection = object_exists_or_404(request,
                                               collection_id='collection',
                                               parent_id=collection_parent_id,
                                               object_id=self.collection_id)
             collections[collection_uri] = collection
 
+        super(Record, self).__init__(request, **kwargs)
+        self.model.id_generator = RelaxedUUID()
         self._collection = collections[collection_uri]
 
     def get_parent_id(self, request):


Test patch:
diff --git a/kinto/tests/test_views_records.py b/kinto/tests/test_views_records.py
--- a/kinto/tests/test_views_records.py
+++ b/kinto/tests/test_views_records.py
@@ -37,6 +37,15 @@ def test_unknown_collection_raises_404(self):
         other_collection = self.collection_url.replace('barley', 'pills')
         self.app.get(other_collection, headers=self.headers, status=404)
 
+    def test_unknown_collection_does_not_query_timestamp(self):
+        other_collection = self.collection_url.replace('barley', 'pills')
+        patch = mock.patch.object(self.app.app.registry.storage,
+                                  'collection_timestamp')
+        self.addCleanup(patch.stop)
+        mocked = patch.start()
+        self.app.get(other_collection, headers=self.headers, status=404)
+        self.assertFalse(mocked.called)
+
     def test_parent_collection_is_fetched_only_once_in_batch(self):
         batch = {'requests': []}
         nb_create = 25
Repository: Kinto/kinto

Issue:
The /permissions endpoint is broken
To reproduce just access https://kinto-ota.dev.mozaws.net/v1/permissions

```
  File "/home/ubuntu/venvs/kinto/local/lib/python2.7/site-packages/kinto/core/permission/re
dis.py", line 103, in get_accessible_objects
    _, object_id, permission = key.decode('utf-8').split(':')
ValueError: too many values to unpack
```



Discussion:

Resolving patch:
diff --git a/kinto/core/permission/redis.py b/kinto/core/permission/redis.py
--- a/kinto/core/permission/redis.py
+++ b/kinto/core/permission/redis.py
@@ -100,8 +100,8 @@ def get_accessible_objects(self, principals, bound_permissions=None):
             for key in matched:
                 authorized = self._decode_set(self._client.smembers(key))
                 if len(authorized & principals) > 0:
-                    _, object_id, permission = key.decode('utf-8').split(':')
-                    perms_by_id.setdefault(object_id, set()).add(permission)
+                    _, obj_id, permission = key.decode('utf-8').split(':', 2)
+                    perms_by_id.setdefault(obj_id, set()).add(permission)
 
         return perms_by_id
 


Test patch:
diff --git a/kinto/tests/core/test_permission.py b/kinto/tests/core/test_permission.py
--- a/kinto/tests/core/test_permission.py
+++ b/kinto/tests/core/test_permission.py
@@ -296,14 +296,15 @@ def test_get_authorized_principals_handles_empty_set(self):
 
     def test_accessible_objects(self):
         self.permission.add_principal_to_ace('id1', 'write', 'user1')
-        self.permission.add_principal_to_ace('id1', 'read', 'group')
+        self.permission.add_principal_to_ace('id1', 'record:create', 'group')
         self.permission.add_principal_to_ace('id2', 'read', 'user1')
         self.permission.add_principal_to_ace('id2', 'read', 'user2')
         self.permission.add_principal_to_ace('id3', 'write', 'user2')
         per_object_ids = self.permission.get_accessible_objects(
             ['user1', 'group'])
         self.assertEquals(sorted(per_object_ids.keys()), ['id1', 'id2'])
-        self.assertEquals(per_object_ids['id1'], set(['read', 'write']))
+        self.assertEquals(per_object_ids['id1'],
+                          set(['write', 'record:create']))
         self.assertEquals(per_object_ids['id2'], set(['read']))
 
     def test_accessible_objects_from_permission(self):
Repository: Kinto/kinto

Issue:
[cliquet] pluggable statsd backend
original: https://github.com/mozilla-services/cliquet/issues/643

datadog is getting popular, we should see how we can optionally plug it instead of statsd

[cliquet] pluggable statsd backend
original: https://github.com/mozilla-services/cliquet/issues/643

datadog is getting popular, we should see how we can optionally plug it instead of statsd



Discussion:

Resolving patch:
diff --git a/kinto/core/__init__.py b/kinto/core/__init__.py
--- a/kinto/core/__init__.py
+++ b/kinto/core/__init__.py
@@ -69,6 +69,7 @@
     'project_version': '',
     'readonly': False,
     'retry_after_seconds': 30,
+    'statsd_backend': 'kinto.core.statsd',
     'statsd_prefix': 'kinto.core',
     'statsd_url': None,
     'storage_backend': '',
diff --git a/kinto/core/initialization.py b/kinto/core/initialization.py
--- a/kinto/core/initialization.py
+++ b/kinto/core/initialization.py
@@ -24,7 +24,6 @@
 
 from kinto.core import errors
 from kinto.core import utils
-from kinto.core import statsd
 from kinto.core import cache
 from kinto.core import storage
 from kinto.core import permission
@@ -238,7 +237,9 @@ def setup_statsd(config):
     config.registry.statsd = None
 
     if settings['statsd_url']:
-        client = statsd.load_from_config(config)
+        statsd_mod = settings['statsd_backend']
+        statsd_mod = config.maybe_dotted(statsd_mod)
+        client = statsd_mod.load_from_config(config)
 
         config.registry.statsd = client
 


Test patch:
diff --git a/kinto/tests/test_configuration/test.ini b/kinto/tests/test_configuration/test.ini
--- a/kinto/tests/test_configuration/test.ini
+++ b/kinto/tests/test_configuration/test.ini
@@ -85,6 +85,7 @@ kinto.includes = kinto.plugins.default_bucket
 #
 # https://kinto.readthedocs.io/en/latest/configuration/production.html
 #
+# kinto.statsd_backend = kinto.core.statsd
 # kinto.statsd_url = udp://localhost:8125
 # kinto.statsd_prefix = kinto-prod
Repository: Kinto/kinto

Issue:
kinto.id_generator option doesn't work
#### Issue

As per the [docs](http://kinto.readthedocs.io/en/latest/configuration/settings.html?highlight=id_generator#feature-settings), we should be able to define a `kinto.id_generator` options that will work for records, but this is not the case in the current (3.4.0.dev0) implementation.
#### Easy workaround

As an easy fix, you can specify the `record` type as in `kinto.record_id_generator`.

kinto.id_generator option doesn't work
#### Issue

As per the [docs](http://kinto.readthedocs.io/en/latest/configuration/settings.html?highlight=id_generator#feature-settings), we should be able to define a `kinto.id_generator` options that will work for records, but this is not the case in the current (3.4.0.dev0) implementation.
#### Easy workaround

As an easy fix, you can specify the `record` type as in `kinto.record_id_generator`.



Discussion:
Thanks for this feedback! It's a bug indeed, we should either support the former setting or make this breaking change explicit (would lead to kinto 4.0)

Thanks for this feedback! It's a bug indeed, we should either support the former setting or make this breaking change explicit (would lead to kinto 4.0)


Resolving patch:
diff --git a/kinto/__init__.py b/kinto/__init__.py
--- a/kinto/__init__.py
+++ b/kinto/__init__.py
@@ -95,5 +95,5 @@ def main(global_config, config=None, **settings):
 
     app = config.make_wsgi_app()
 
-    # Install middleware (idempotent if disabled)
+    # Install middleware (no-op if disabled)
     return kinto.core.install_middlewares(app, settings)
diff --git a/kinto/core/storage/__init__.py b/kinto/core/storage/__init__.py
--- a/kinto/core/storage/__init__.py
+++ b/kinto/core/storage/__init__.py
@@ -36,6 +36,7 @@ class StorageBase(object):
     """
 
     id_generator = generators.UUID4()
+    """Id generator used when no one is provided for create."""
 
     def initialize_schema(self, dry_run=False):
         """Create every necessary objects (like tables or indices) in the


Test patch:
Repository: Kinto/kinto

Issue:
Set scheme to https by default in config
Mainly because it's easy to forget during deployment. And kinto is very like to be deployed behind https:

See `url` in the root URL here:

![capture d ecran de 2016-07-06 15-24-57](https://cloud.githubusercontent.com/assets/546692/16619299/da075538-438d-11e6-987d-2df56a3ecc38.png)

/cc @almet 



Discussion:
Can I work on this issue?

The thing is in development (witch is a even more common use case) you need to have http by default.

My take would be to let http by default and add a warning if https is not turned on.

> The thing is in development (witch is a even more common use case) you need to have http by default.

What happens if you set `kinto.http_scheme` to `https` in development (with `kinto start`) ?

It will redirect you from `http://localhost` to `https://localhost/v1/` and it will basically fail with a browser connection error.

@Prashant-Surya, let's follow natim advice and just make a `logger.warning()` if the setting was not set to `https`

@leplatrem can you briefly describe how should I solve this?

Hello @Prashant-Surya you have an example of how to display warnings [in the code](https://github.com/Kinto/kinto/blob/master/kinto/core/initialization.py#L539).

What you need to do is the same things as when project_name is missing but when the http_scheme is http.

@Natim okay 


Resolving patch:
diff --git a/kinto/core/initialization.py b/kinto/core/initialization.py
--- a/kinto/core/initialization.py
+++ b/kinto/core/initialization.py
@@ -549,6 +549,10 @@ def initialize(config, version=None, project_name='', default_settings=None):
     if not project_name:
         warnings.warn('No value specified for `project_name`')
 
+    http_scheme = settings.get('kinto.http_scheme')
+    if http_scheme != 'https':
+        warnings.warn('HTTPS is not enabled')
+
     kinto_core_defaults = DEFAULT_SETTINGS.copy()
 
     if default_settings:


Test patch:
Repository: Kinto/kinto

Issue:
init fails on osx at the backend selection step
I followed the instructions given [here](https://kinto.readthedocs.io/en/stable/tutorials/install.html#from-sources), but when asked:

```
$ Select the backend you would like to use: (1 - postgresql, 2 - redis, default - memory)
```

entering `1` and `2` leads to the following error:

```
Traceback (most recent call last):
  File ".venv/bin/kinto", line 11, in <module>
    load_entry_point('kinto', 'console_scripts', 'kinto')()
  File "/work/git/kinto/kinto/__main__.py", line 108, in main
    answer = input(prompt).strip()
AttributeError: 'int' object has no attribute 'strip'
```

and entering nothing + enter will lead to the following error.

```
Traceback (most recent call last):
  File ".venv/bin/kinto", line 11, in <module>
    load_entry_point('kinto', 'console_scripts', 'kinto')()
  File "/work/git/kinto/kinto/__main__.py", line 108, in main
    answer = input(prompt).strip()
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing
```

It appears that the code expects a `string` but getting a number and null, therefore failing on the `.strip()` call [here](https://github.com/Kinto/kinto/blob/master/kinto/__main__.py#L108).

---

Entering `""`, `"1"` and `"2"` works. I'm assuming that's not the way it's designed to be? 



Discussion:
I had the same error on Friday. I don't know what changed for it to suddenly appear.
It looks like it is not using the correct input function from six. I am planning to work on this today.

Ok this commit break it: https://github.com/Kinto/kinto/commit/ab3db80c0a99a88a4430c1c4fa140f991dd5c833#diff-87f3ff0cfd5b39515d0d84446afef901


Resolving patch:
diff --git a/kinto/__main__.py b/kinto/__main__.py
--- a/kinto/__main__.py
+++ b/kinto/__main__.py
@@ -4,6 +4,7 @@
 import sys
 import logging
 import logging.config
+from six.moves import input
 
 from kinto.core import scripts
 from pyramid.scripts import pserve


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -182,3 +182,9 @@ def test_cli_use_default_logging_logger(self):
             mocked_logging.basicConfig.assert_called_with(
                 level=logging.INFO,
                 format=DEFAULT_LOG_FORMAT)
+
+    def test_cli_uses_six_moves_input_function(self):
+        # In Py2 we want to use raw_input and input in Py3
+        from kinto.__main__ import input as cli_input
+        from six.moves import input as six_input
+        assert cli_input == six_input
Repository: Kinto/kinto

Issue:
deadlock on __heartbeat__
If I set up the same postgresql database/user for the storage, cache and permission connectors, I get a thread deadlock on the second call to **heartbeat**, leading to a blocking call, that makes the HB fails.

Looks like a DB connector race condition issue



Discussion:
Is that as easy as described to reproduce ?

I tried to write a test to reproduce it but I can't. But if you run a server it can be reproduced.

I could reproduce on Travis with kinto-dist https://travis-ci.org/mozilla-services/kinto-dist/jobs/158134738


Resolving patch:
diff --git a/kinto/core/views/heartbeat.py b/kinto/core/views/heartbeat.py
--- a/kinto/core/views/heartbeat.py
+++ b/kinto/core/views/heartbeat.py
@@ -1,4 +1,6 @@
 from concurrent.futures import ThreadPoolExecutor, wait
+
+import transaction
 from pyramid.security import NO_PERMISSION_REQUIRED
 
 from kinto import logger
@@ -17,6 +19,11 @@ def get_heartbeat(request):
     def heartbeat_check(name, func):
         status[name] = False
         status[name] = func(request)
+        # Since the heartbeat checks run concurrently, their transactions
+        # overlap and might end in shared lock errors. By aborting here
+        # we clean-up the state on each heartbeat call instead of once at the
+        # end of the request. See bug Kinto/kinto#804
+        transaction.abort()
 
     # Start executing heartbeats concurrently.
     heartbeats = request.registry.heartbeats


Test patch:
diff --git a/tests/core/test_views_transaction.py b/tests/core/test_views_transaction.py
--- a/tests/core/test_views_transaction.py
+++ b/tests/core/test_views_transaction.py
@@ -69,6 +69,12 @@ def run_failing_post(self):
 @skip_if_no_postgresql
 class TransactionTest(PostgreSQLTest, unittest.TestCase):
 
+    def test_heartbeat_releases_transaction_lock(self):
+        for i in range(4):
+            # 4 calls because we have 3 backends
+            # See bug Kinto/kinto#804
+            self.app.get('/__heartbeat__')
+
     def test_storage_operations_are_committed_on_success(self):
         request_create = {
             'method': 'POST',