From cd940d0c9f70bcd503d15ec60c827d76254d8b17 Mon Sep 17 00:00:00 2001 From: Matt Prahl Date: Apr 26 2017 18:19:22 +0000 Subject: Add the //git/branches API --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 85dc480..ae37788 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -15,6 +15,7 @@ from sqlalchemy.exc import SQLAlchemyError import pagure import pagure.exceptions import pagure.lib +import pagure.lib.git from pagure import SESSION, APP, authenticated from pagure.api import API, api_method, APIERROR, api_login_required @@ -65,6 +66,52 @@ def api_git_tags(repo, username=None, namespace=None): return jsonout +@API.route('//git/branches') +@API.route('///git/branches') +@API.route('/fork///git/branches') +@API.route('/fork////git/branches') +def api_git_branches(repo, username=None, namespace=None): + ''' + List all the branches of a git repo + ----------------------------------- + List the branches associated with a Pagure git repository + + :: + + GET /api/0//git/branches + GET /api/0///git/branches + + :: + + GET /api/0/fork///git/branches + GET /api/0/fork////git/branches + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "total_branches": 2, + "branches": ["master", "dev"] + } + + ''' + repo = pagure.get_authorized_project( + SESSION, repo, user=username, namespace=namespace) + if repo is None: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) + + branches = pagure.lib.git.get_git_branches(repo) + + return flask.jsonify( + { + 'total_branches': len(branches), + 'branches': branches + } + ) + + @API.route('/projects') @api_method def api_projects(): diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 4831a7f..50f9237 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -1698,3 +1698,12 @@ def reinit_git(project, repofolder): repo_path, bare=True, mode=pygit2.C.GIT_REPOSITORY_INIT_SHARED_GROUP ) + + +def get_git_branches(project): + ''' Return a list of branches for the project + :arg project: The Project instance to get the branches for + ''' + repo_path = pagure.get_repo_path(project) + repo_obj = pygit2.Repository(repo_path) + return repo_obj.listall_branches() diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 838e11a..70016f6 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -110,6 +110,66 @@ class PagureFlaskApiProjecttests(tests.Modeltests): shutil.rmtree(newpath) + def test_api_git_branches(self): + """ Test the api_git_branches method of the flask api. """ + # Create a git repo to add branches to + tests.create_projects(self.session) + repo_path = os.path.join(self.path, 'repos', 'test.git') + tests.add_content_git_repo(repo_path) + new_repo_path = tempfile.mkdtemp(prefix='pagure-api-git-branches-test') + clone_repo = pygit2.clone_repository(repo_path, new_repo_path) + + # Create two other branches based on master + for branch in ['pats-win-49', 'pats-win-51']: + clone_repo.create_branch(branch, clone_repo.head.get_object()) + refname = 'refs/heads/{0}:refs/heads/{0}'.format(branch) + PagureRepo.push(clone_repo.remotes[0], refname) + + # Check that the branches show up on the API + output = self.app.get('/api/0/test/git/branches') + # Delete the cloned git repo after the API call + shutil.rmtree(new_repo_path) + + # Verify the API data + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + 'branches': ['master', 'pats-win-49', 'pats-win-51'], + 'total_branches': 3 + } + ) + + def test_api_git_branches_empty_repo(self): + """ Test the api_git_branches method of the flask api when the repo is + empty. + """ + # Create a git repo without any branches + tests.create_projects(self.session) + repo_base_path = os.path.join(self.path, 'repos') + tests.create_projects_git(repo_base_path) + + # Check that no branches show up on the API + output = self.app.get('/api/0/test/git/branches') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + 'branches': [], + 'total_branches': 0 + } + ) + + def test_api_git_branches_no_repo(self): + """ Test the api_git_branches method of the flask api when there is no + repo on a project. + """ + tests.create_projects(self.session) + output = self.app.get('/api/0/test/git/branches') + self.assertEqual(output.status_code, 404) + def test_api_projects(self): """ Test the api_projects method of the flask api. """ tests.create_projects(self.session)