Merge lp:~cjwatson/turnip/default-branch into lp:turnip

Proposed by Colin Watson
Status: Merged
Merged at revision: 159
Proposed branch: lp:~cjwatson/turnip/default-branch
Merge into: lp:turnip
Diff against target: 123 lines (+84/-0)
3 files modified
turnip/api/store.py (+10/-0)
turnip/api/tests/test_api.py (+39/-0)
turnip/api/views.py (+35/-0)
To merge this branch: bzr merge lp:~cjwatson/turnip/default-branch
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+259493@code.launchpad.net

Commit message

Add GET and PATCH methods to RepoAPI to allow getting and setting the default branch (a.k.a. "HEAD").

Description of the change

Add GET and PATCH methods to RepoAPI to allow getting and setting the default branch (a.k.a. "HEAD").

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
=== modified file 'turnip/api/store.py'
--- turnip/api/store.py 2015-04-30 19:26:04 +0000
+++ turnip/api/store.py 2015-05-19 12:55:50 +0000
@@ -157,6 +157,16 @@
157 yield Repository(repo_path)157 yield Repository(repo_path)
158158
159159
160def get_default_branch(repo_path):
161 repo = Repository(repo_path)
162 return repo.lookup_reference('HEAD').target
163
164
165def set_default_branch(repo_path, target):
166 repo = Repository(repo_path)
167 repo.set_head(target)
168
169
160def delete_repo(repo_path):170def delete_repo(repo_path):
161 """Permanently delete a git repository from repo store."""171 """Permanently delete a git repository from repo store."""
162 shutil.rmtree(repo_path)172 shutil.rmtree(repo_path)
163173
=== modified file 'turnip/api/tests/test_api.py'
--- turnip/api/tests/test_api.py 2015-04-30 19:26:04 +0000
+++ turnip/api/tests/test_api.py 2015-05-19 12:55:50 +0000
@@ -35,6 +35,11 @@
35 self.commit = {'ref': 'refs/heads/master', 'message': 'test commit.'}35 self.commit = {'ref': 'refs/heads/master', 'message': 'test commit.'}
36 self.tag = {'ref': 'refs/tags/tag0', 'message': 'tag message'}36 self.tag = {'ref': 'refs/tags/tag0', 'message': 'tag message'}
3737
38 def assertReferencesEqual(self, repo, expected, observed):
39 self.assertEqual(
40 repo.lookup_reference(expected).peel().oid,
41 repo.lookup_reference(observed).peel().oid)
42
38 def get_ref(self, ref):43 def get_ref(self, ref):
39 resp = self.app.get('/repo/{}/{}'.format(self.repo_path, ref))44 resp = self.app.get('/repo/{}/{}'.format(self.repo_path, ref))
40 return resp.json45 return resp.json
@@ -74,6 +79,40 @@
74 self.assertEqual(200, resp.status_code)79 self.assertEqual(200, resp.status_code)
75 self.assertIn(new_repo_path, resp.json['repo_url'])80 self.assertIn(new_repo_path, resp.json['repo_url'])
7681
82 def test_repo_get(self):
83 """The GET method on a repository returns its properties."""
84 factory = RepoFactory(self.repo_store, num_branches=2, num_commits=1)
85 factory.build()
86 factory.repo.set_head('refs/heads/branch-0')
87
88 resp = self.app.get('/repo/{}'.format(self.repo_path))
89 self.assertEqual(200, resp.status_code)
90 self.assertEqual({'default_branch': 'refs/heads/branch-0'}, resp.json)
91
92 def test_repo_get_default_branch_missing(self):
93 """default_branch is returned even if that branch has been deleted."""
94 factory = RepoFactory(self.repo_store, num_branches=2, num_commits=1)
95 factory.build()
96 factory.repo.set_head('refs/heads/branch-0')
97 factory.repo.lookup_reference('refs/heads/branch-0').delete()
98
99 resp = self.app.get('/repo/{}'.format(self.repo_path))
100 self.assertEqual(200, resp.status_code)
101 self.assertEqual({'default_branch': 'refs/heads/branch-0'}, resp.json)
102
103 def test_repo_patch_default_branch(self):
104 """A repository's default branch ("HEAD") can be changed."""
105 factory = RepoFactory(self.repo_store, num_branches=2, num_commits=1)
106 factory.build()
107 factory.repo.set_head('refs/heads/branch-0')
108 self.assertReferencesEqual(factory.repo, 'refs/heads/branch-0', 'HEAD')
109
110 resp = self.app.patch_json(
111 '/repo/{}'.format(self.repo_path),
112 {'default_branch': 'refs/heads/branch-1'})
113 self.assertEqual(204, resp.status_code)
114 self.assertReferencesEqual(factory.repo, 'refs/heads/branch-1', 'HEAD')
115
77 def test_cross_repo_merge_diff(self):116 def test_cross_repo_merge_diff(self):
78 """Merge diff can be requested across 2 repositories."""117 """Merge diff can be requested across 2 repositories."""
79 factory = RepoFactory(self.repo_store)118 factory = RepoFactory(self.repo_store)
80119
=== modified file 'turnip/api/views.py'
--- turnip/api/views.py 2015-04-29 04:42:05 +0000
+++ turnip/api/views.py 2015-05-19 12:55:50 +0000
@@ -79,6 +79,41 @@
79 return exc.HTTPConflict() # 40979 return exc.HTTPConflict() # 409
8080
81 @validate_path81 @validate_path
82 def get(self, repo_store, repo_name):
83 """Get properties of an existing git repository."""
84 repo_path = os.path.join(repo_store, repo_name)
85 if not os.path.exists(repo_path):
86 self.request.errors.add(
87 'body', 'name', 'repository does not exist')
88 raise exc.HTTPNotFound()
89 return {
90 'default_branch': store.get_default_branch(repo_path),
91 }
92
93 def _patch_default_branch(self, repo_path, value):
94 try:
95 store.set_default_branch(repo_path, value)
96 except (KeyError, ValueError, GitError):
97 raise exc.HTTPBadRequest()
98
99 @validate_path
100 def patch(self, repo_store, repo_name):
101 """Change properties of an existing git repository."""
102 repo_path = os.path.join(repo_store, repo_name)
103 if not os.path.exists(repo_path):
104 self.request.errors.add(
105 'body', 'name', 'repository does not exist')
106 raise exc.HTTPNotFound()
107 data = extract_json_data(self.request)
108 for key in data:
109 if not hasattr(self, "_patch_%s" % key):
110 self.request.errors.add('body', key, 'unknown property')
111 raise exc.HTTPBadRequest()
112 for key, value in data.items():
113 getattr(self, "_patch_%s" % key)(repo_path, value)
114 return exc.HTTPNoContent()
115
116 @validate_path
82 def delete(self, repo_store, repo_name):117 def delete(self, repo_store, repo_name):
83 """Delete an existing git repository."""118 """Delete an existing git repository."""
84 try:119 try:

Subscribers

People subscribed via source and target branches

to all changes: