Merge lp:~cjwatson/launchpad/git-ref-scan-exclude-prefixes into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 18826
Proposed branch: lp:~cjwatson/launchpad/git-ref-scan-exclude-prefixes
Merge into: lp:launchpad
Diff against target: 119 lines (+40/-5)
6 files modified
lib/lp/code/interfaces/githosting.py (+3/-2)
lib/lp/code/model/githosting.py (+4/-2)
lib/lp/code/model/gitrepository.py (+3/-1)
lib/lp/code/model/tests/test_githosting.py (+10/-0)
lib/lp/code/model/tests/test_gitrepository.py (+17/-0)
lib/lp/services/config/schema-lazr.conf (+3/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/git-ref-scan-exclude-prefixes
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+359208@code.launchpad.net

Commit message

Exclude refs starting with refs/changes/ from Git ref scans.

Description of the change

This should help with repositories imported from Gerrit with very large numbers of refs/changes/ refs.

https://code.launchpad.net/~cjwatson/turnip/+git/turnip/+merge/359204 must be deployed to production first.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/interfaces/githosting.py'
2--- lib/lp/code/interfaces/githosting.py 2016-05-14 09:54:32 +0000
3+++ lib/lp/code/interfaces/githosting.py 2018-11-22 16:50:19 +0000
4@@ -1,4 +1,4 @@
5-# Copyright 2015-2016 Canonical Ltd. This software is licensed under the
6+# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
7 # GNU Affero General Public License version 3 (see the file LICENSE).
8
9 """Interface for communication with the Git hosting service."""
10@@ -37,10 +37,11 @@
11 :param props: Properties to set.
12 """
13
14- def getRefs(path):
15+ def getRefs(path, exclude_prefixes=None):
16 """Get all refs in this repository.
17
18 :param path: Physical path of the repository on the hosting service.
19+ :param exclude_prefixes: An optional list of ref prefixes to exclude.
20 :return: A dict mapping ref paths to dicts representing the objects
21 they point to.
22 """
23
24=== modified file 'lib/lp/code/model/githosting.py'
25--- lib/lp/code/model/githosting.py 2018-07-30 18:08:34 +0000
26+++ lib/lp/code/model/githosting.py 2018-11-22 16:50:19 +0000
27@@ -116,10 +116,12 @@
28 raise GitRepositoryScanFault(
29 "Failed to set properties of Git repository: %s" % unicode(e))
30
31- def getRefs(self, path):
32+ def getRefs(self, path, exclude_prefixes=None):
33 """See `IGitHostingClient`."""
34 try:
35- return self._get("/repo/%s/refs" % path)
36+ return self._get(
37+ "/repo/%s/refs" % path,
38+ params={"exclude_prefix": exclude_prefixes})
39 except requests.RequestException as e:
40 raise GitRepositoryScanFault(
41 "Failed to get refs from Git repository: %s" % unicode(e))
42
43=== modified file 'lib/lp/code/model/gitrepository.py'
44--- lib/lp/code/model/gitrepository.py 2018-11-22 07:28:33 +0000
45+++ lib/lp/code/model/gitrepository.py 2018-11-22 16:50:19 +0000
46@@ -672,7 +672,9 @@
47 """See `IGitRepository`."""
48 hosting_client = getUtility(IGitHostingClient)
49 new_refs = {}
50- for path, info in hosting_client.getRefs(hosting_path).items():
51+ exclude_prefixes = config.codehosting.git_exclude_ref_prefixes.split()
52+ for path, info in hosting_client.getRefs(
53+ hosting_path, exclude_prefixes=exclude_prefixes).items():
54 try:
55 new_refs[path] = self._convertRefInfo(info)
56 except ValueError as e:
57
58=== modified file 'lib/lp/code/model/tests/test_githosting.py'
59--- lib/lp/code/model/tests/test_githosting.py 2018-05-31 10:23:03 +0000
60+++ lib/lp/code/model/tests/test_githosting.py 2018-11-22 16:50:19 +0000
61@@ -145,6 +145,16 @@
62 self.assertEqual({"refs/heads/master": {}}, refs)
63 self.assertRequest("repo/123/refs", method="GET")
64
65+ def test_getRefs_exclude_prefixes(self):
66+ with self.mockRequests("GET", json={"refs/heads/master": {}}):
67+ refs = self.client.getRefs(
68+ "123", exclude_prefixes=["refs/changes/", "refs/pull/"])
69+ self.assertEqual({"refs/heads/master": {}}, refs)
70+ self.assertRequest(
71+ "repo/123/refs"
72+ "?exclude_prefix=refs%2Fchanges%2F&exclude_prefix=refs%2Fpull%2F",
73+ method="GET")
74+
75 def test_getRefs_failure(self):
76 with self.mockRequests("GET", status=400):
77 self.assertRaisesWithContent(
78
79=== modified file 'lib/lp/code/model/tests/test_gitrepository.py'
80--- lib/lp/code/model/tests/test_gitrepository.py 2018-11-21 23:59:18 +0000
81+++ lib/lp/code/model/tests/test_gitrepository.py 2018-11-22 16:50:19 +0000
82@@ -1488,6 +1488,23 @@
83 self.assertEqual(expected_upsert, refs_to_upsert)
84 self.assertEqual(set(), refs_to_remove)
85
86+ def test_planRefChanges_excludes_configured_prefixes(self):
87+ # planRefChanges excludes some ref prefixes by default, and can be
88+ # configured otherwise.
89+ repository = self.factory.makeGitRepository()
90+ hosting_fixture = self.useFixture(GitHostingFixture())
91+ repository.planRefChanges("dummy")
92+ self.assertEqual(
93+ [{"exclude_prefixes": ["refs/changes/"]}],
94+ hosting_fixture.getRefs.extract_kwargs())
95+ hosting_fixture.getRefs.calls = []
96+ self.pushConfig(
97+ "codehosting", git_exclude_ref_prefixes="refs/changes/ refs/pull/")
98+ repository.planRefChanges("dummy")
99+ self.assertEqual(
100+ [{"exclude_prefixes": ["refs/changes/", "refs/pull/"]}],
101+ hosting_fixture.getRefs.extract_kwargs())
102+
103 def test_fetchRefCommits(self):
104 # fetchRefCommits fetches detailed tip commit metadata for the
105 # requested refs.
106
107=== modified file 'lib/lp/services/config/schema-lazr.conf'
108--- lib/lp/services/config/schema-lazr.conf 2018-07-30 18:09:23 +0000
109+++ lib/lp/services/config/schema-lazr.conf 2018-11-22 16:50:19 +0000
110@@ -376,6 +376,9 @@
111 # datatype: urlbase
112 git_ssh_root: none
113
114+# A space-separated list of Git ref prefixes to exclude from scans.
115+git_exclude_ref_prefixes: refs/changes/
116+
117 # The upper limit on the number of bugs to link to a merge proposal based on
118 # Git commit metadata.
119 related_bugs_from_source_limit: 1000