Merge ~cjwatson/turnip:refs-exclude into turnip:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 06e56b081d6fba02489966dce452f61395ae8389
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/turnip:refs-exclude
Merge into: turnip:master
Diff against target: 85 lines (+31/-4)
3 files modified
turnip/api/store.py (+5/-3)
turnip/api/tests/test_api.py (+23/-0)
turnip/api/views.py (+3/-1)
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+359204@code.launchpad.net

Commit message

Add exclude_prefix query param to /repo/{}/refs

This allows us to exclude some ref namespaces from being scanned by
Launchpad.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)
~cjwatson/turnip:refs-exclude updated
06e56b0... by Colin Watson

Shorten ref prefix exclusion checks

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/turnip/api/store.py b/turnip/api/store.py
index 38d6294..650a3d8 100644
--- a/turnip/api/store.py
+++ b/turnip/api/store.py
@@ -283,7 +283,7 @@ def repack(repo_path, ignore_alternates=False, single=False,
283 stderr=subprocess.PIPE, stdout=subprocess.PIPE)283 stderr=subprocess.PIPE, stdout=subprocess.PIPE)
284284
285285
286def get_refs(repo_store, repo_name):286def get_refs(repo_store, repo_name, exclude_prefixes=None):
287 """Return all refs for a git repository."""287 """Return all refs for a git repository."""
288 with open_repo(repo_store, repo_name) as repo:288 with open_repo(repo_store, repo_name) as repo:
289 refs = {}289 refs = {}
@@ -292,11 +292,13 @@ def get_refs(repo_store, repo_name):
292 # Filter non-unicode refs, as refs are treated as unicode292 # Filter non-unicode refs, as refs are treated as unicode
293 # given json is unable to represent arbitrary byte strings.293 # given json is unable to represent arbitrary byte strings.
294 try:294 try:
295 ref.decode('utf-8')295 ref = ref.decode('utf-8')
296 except UnicodeDecodeError:296 except UnicodeDecodeError:
297 pass297 pass
298 else:298 else:
299 refs.update(format_ref(ref, git_object))299 if not any(ref.startswith(exclude_prefix)
300 for exclude_prefix in (exclude_prefixes or [])):
301 refs.update(format_ref(ref, git_object))
300 return refs302 return refs
301303
302304
diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
index 4ca1aac..f435d75 100644
--- a/turnip/api/tests/test_api.py
+++ b/turnip/api/tests/test_api.py
@@ -21,6 +21,10 @@ from fixtures import (
21 TempDir,21 TempDir,
22 )22 )
23from testtools import TestCase23from testtools import TestCase
24from testtools.matchers import (
25 Equals,
26 MatchesSetwise,
27 )
24from webtest import TestApp28from webtest import TestApp
2529
26from turnip import api30from turnip import api
@@ -233,6 +237,25 @@ class ApiTestCase(TestCase):
233 refs = resp.json237 refs = resp.json
234 self.assertEqual(2, len(refs.keys()))238 self.assertEqual(2, len(refs.keys()))
235239
240 def test_repo_get_refs_exclude_prefixes(self):
241 """Refs matching an excluded prefix are not returned."""
242 factory = RepoFactory(self.repo_store, num_commits=1)
243 factory.build()
244 for ref_path in (
245 'refs/heads/refs/changes/1',
246 'refs/changes/2',
247 'refs/pull/3/head',
248 ):
249 factory.repo.references.create(ref_path, factory.commits[0])
250
251 resp = self.app.get(
252 '/repo/{}/refs'
253 '?exclude_prefix=refs/changes/'
254 '&exclude_prefix=refs/pull/'.format(self.repo_path))
255 refs = resp.json
256 self.assertThat(list(refs), MatchesSetwise(
257 Equals('refs/heads/master'), Equals('refs/heads/refs/changes/1')))
258
236 def test_repo_get_ref(self):259 def test_repo_get_ref(self):
237 RepoFactory(self.repo_store, num_commits=1).build()260 RepoFactory(self.repo_store, num_commits=1).build()
238 ref = 'refs/heads/master'261 ref = 'refs/heads/master'
diff --git a/turnip/api/views.py b/turnip/api/views.py
index abdfaca..90644dc 100644
--- a/turnip/api/views.py
+++ b/turnip/api/views.py
@@ -166,8 +166,10 @@ class RefAPI(BaseAPI):
166166
167 @validate_path167 @validate_path
168 def collection_get(self, repo_store, repo_name):168 def collection_get(self, repo_store, repo_name):
169 exclude_prefixes = self.request.params.getall('exclude_prefix')
169 try:170 try:
170 return store.get_refs(repo_store, repo_name)171 return store.get_refs(
172 repo_store, repo_name, exclude_prefixes=exclude_prefixes)
171 except (KeyError, GitError):173 except (KeyError, GitError):
172 return exc.HTTPNotFound() # 404174 return exc.HTTPNotFound() # 404
173175

Subscribers

People subscribed via source and target branches