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
1diff --git a/turnip/api/store.py b/turnip/api/store.py
2index 38d6294..650a3d8 100644
3--- a/turnip/api/store.py
4+++ b/turnip/api/store.py
5@@ -283,7 +283,7 @@ def repack(repo_path, ignore_alternates=False, single=False,
6 stderr=subprocess.PIPE, stdout=subprocess.PIPE)
7
8
9-def get_refs(repo_store, repo_name):
10+def get_refs(repo_store, repo_name, exclude_prefixes=None):
11 """Return all refs for a git repository."""
12 with open_repo(repo_store, repo_name) as repo:
13 refs = {}
14@@ -292,11 +292,13 @@ def get_refs(repo_store, repo_name):
15 # Filter non-unicode refs, as refs are treated as unicode
16 # given json is unable to represent arbitrary byte strings.
17 try:
18- ref.decode('utf-8')
19+ ref = ref.decode('utf-8')
20 except UnicodeDecodeError:
21 pass
22 else:
23- refs.update(format_ref(ref, git_object))
24+ if not any(ref.startswith(exclude_prefix)
25+ for exclude_prefix in (exclude_prefixes or [])):
26+ refs.update(format_ref(ref, git_object))
27 return refs
28
29
30diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
31index 4ca1aac..f435d75 100644
32--- a/turnip/api/tests/test_api.py
33+++ b/turnip/api/tests/test_api.py
34@@ -21,6 +21,10 @@ from fixtures import (
35 TempDir,
36 )
37 from testtools import TestCase
38+from testtools.matchers import (
39+ Equals,
40+ MatchesSetwise,
41+ )
42 from webtest import TestApp
43
44 from turnip import api
45@@ -233,6 +237,25 @@ class ApiTestCase(TestCase):
46 refs = resp.json
47 self.assertEqual(2, len(refs.keys()))
48
49+ def test_repo_get_refs_exclude_prefixes(self):
50+ """Refs matching an excluded prefix are not returned."""
51+ factory = RepoFactory(self.repo_store, num_commits=1)
52+ factory.build()
53+ for ref_path in (
54+ 'refs/heads/refs/changes/1',
55+ 'refs/changes/2',
56+ 'refs/pull/3/head',
57+ ):
58+ factory.repo.references.create(ref_path, factory.commits[0])
59+
60+ resp = self.app.get(
61+ '/repo/{}/refs'
62+ '?exclude_prefix=refs/changes/'
63+ '&exclude_prefix=refs/pull/'.format(self.repo_path))
64+ refs = resp.json
65+ self.assertThat(list(refs), MatchesSetwise(
66+ Equals('refs/heads/master'), Equals('refs/heads/refs/changes/1')))
67+
68 def test_repo_get_ref(self):
69 RepoFactory(self.repo_store, num_commits=1).build()
70 ref = 'refs/heads/master'
71diff --git a/turnip/api/views.py b/turnip/api/views.py
72index abdfaca..90644dc 100644
73--- a/turnip/api/views.py
74+++ b/turnip/api/views.py
75@@ -166,8 +166,10 @@ class RefAPI(BaseAPI):
76
77 @validate_path
78 def collection_get(self, repo_store, repo_name):
79+ exclude_prefixes = self.request.params.getall('exclude_prefix')
80 try:
81- return store.get_refs(repo_store, repo_name)
82+ return store.get_refs(
83+ repo_store, repo_name, exclude_prefixes=exclude_prefixes)
84 except (KeyError, GitError):
85 return exc.HTTPNotFound() # 404
86

Subscribers

People subscribed via source and target branches