Merge ~jugmac00/turnip:retrieve-launchpad-yaml-more-efficiently into turnip:master

Proposed by Jürgen Gmach
Status: Merged
Approved by: Jürgen Gmach
Approved revision: 6d8713287f9d59987051dc87fb96ce591b1cadc9
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~jugmac00/turnip:retrieve-launchpad-yaml-more-efficiently
Merge into: turnip:master
Diff against target: 123 lines (+75/-4)
3 files modified
Makefile (+1/-1)
turnip/api/tests/test_api.py (+52/-0)
turnip/api/views.py (+22/-3)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+414251@code.launchpad.net

Commit message

commit API now can filter by blob paths

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) wrote :

This basically looks good, thanks! Just a couple of suggestions.

review: Approve
Revision history for this message
Jürgen Gmach (jugmac00) wrote (last edit ):

Thanks for the review! I applied the suggested changes.

Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :
Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :
Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :
Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :
Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Makefile b/Makefile
2index 98d40a1..035c607 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -84,7 +84,7 @@ test: $(ENV) bootstrap-test
6 # this is a temporary solution to enable selecting single tests more easily
7 # test setup will be redone in https://warthogs.atlassian.net/browse/LP-598
8 # sample command:
9-# make runpytest ARGS="-k filtered_by_blob_paths"
10+# make runpytest ARGS="-k expression"
11 runpytest: $(ENV) bootstrap-test
12 $(PYTHON) -m pip install pdbpp pytest
13 $(PYTHON) -m pytest $(ARGS)
14diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
15index ca47021..0e566b5 100644
16--- a/turnip/api/tests/test_api.py
17+++ b/turnip/api/tests/test_api.py
18@@ -773,6 +773,58 @@ class ApiTestCase(TestCase, ApiRepoStoreMixin):
19 self.assertEqual(1, len(resp.json))
20 self.assertEqual(bulk_commits['commits'][0], resp.json[0]['sha1'])
21
22+ def test_repo_get_launchpad_yaml_from_commit_collection(self):
23+ factory = RepoFactory(self.repo_store, num_commits=10)
24+ factory.build()
25+
26+ # standard configuration file name
27+ c1 = factory.add_commit('bar', '.launchpad.yaml')
28+ # commit which must not be returned
29+ c2 = factory.commits[0]
30+
31+ payload = {
32+ 'filter_paths': [".launchpad.yaml", "debian/.launchpad.yaml"],
33+ "commits": [c1.hex, c2.hex]
34+ }
35+
36+ resp = self.app.post_json(
37+ '/repo/{}/commits'.format(self.repo_path), payload
38+ )
39+
40+ self.assertEqual(1, len(resp.json))
41+ self.assertIn("blobs", resp.json[0])
42+ self.assertEqual(
43+ {'.launchpad.yaml': {'size': 3, 'data': 'YmFy'}},
44+ resp.json[0]["blobs"]
45+ )
46+
47+ def test_repo_get_launchpad_yaml_from_commit_collection_debian_edition(
48+ self
49+ ):
50+ factory = RepoFactory(self.repo_store, num_commits=10)
51+ factory.build()
52+
53+ # debian packaging: typical random files are added under `debian/...`
54+ c1 = factory.add_commit('bar', 'debian/.launchpad.yaml')
55+ # commit which must not be returned
56+ c2 = factory.commits[0]
57+
58+ payload = {
59+ 'filter_paths': [".launchpad.yaml", "debian/.launchpad.yaml"],
60+ "commits": [c1.hex, c2.hex]
61+ }
62+
63+ resp = self.app.post_json(
64+ '/repo/{}/commits'.format(self.repo_path), payload
65+ )
66+
67+ self.assertEqual(1, len(resp.json))
68+ self.assertIn("blobs", resp.json[0])
69+ self.assertEqual(
70+ {'debian/.launchpad.yaml': {'size': 3, 'data': 'YmFy'}},
71+ resp.json[0]["blobs"]
72+ )
73+
74 def test_repo_get_log_signatures(self):
75 """Ensure signatures are correct."""
76 factory = RepoFactory(self.repo_store)
77diff --git a/turnip/api/views.py b/turnip/api/views.py
78index d5cf3d7..2f199af 100644
79--- a/turnip/api/views.py
80+++ b/turnip/api/views.py
81@@ -11,7 +11,10 @@ import pyramid.httpexceptions as exc
82 from pyramid.response import Response
83
84 from turnip.api import store
85-from turnip.api.formatter import format_commit
86+from turnip.api.formatter import (
87+ format_blob,
88+ format_commit,
89+ )
90 from turnip.config import config
91
92
93@@ -357,12 +360,28 @@ class CommitAPI(BaseAPI):
94 @validate_path
95 def collection_post(self, repo_store, repo_name):
96 """Get commits in bulk."""
97- commits = extract_cstruct(self.request)['body'].get('commits')
98+ payload = extract_cstruct(self.request)["body"]
99+ commits = payload.get("commits")
100+ filter_paths = payload.get("filter_paths", [])
101 try:
102 commits = store.get_commits(repo_store, repo_name, commits)
103 except GitError:
104 return exc.HTTPNotFound()
105- return [format_commit(commit) for commit in commits]
106+
107+ # format commits and filter them if applicable
108+ rv = []
109+ for commit in commits:
110+ d = dict()
111+ # apply filter if given
112+ for path in filter_paths:
113+ if path in commit.tree:
114+ if not d.get("blobs"):
115+ d["blobs"] = dict()
116+ d["blobs"][path] = format_blob(commit.tree[path])
117+ if not filter_paths or d:
118+ d.update(format_commit(commit))
119+ rv.append(d)
120+ return rv
121
122
123 @resource(path='/repo/{name}/log/{sha1}')

Subscribers

People subscribed via source and target branches