diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index bd65803..6dab185 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -431,6 +431,7 @@ def api(): api_project_doc = load_doc(project.api_project) api_projects_doc = load_doc(project.api_projects) api_git_tags_doc = load_doc(project.api_git_tags) + api_project_git_urls_doc = load_doc(project.api_project_git_urls) issues = [] if pagure.APP.config.get('ENABLE_TICKETS', True): @@ -494,6 +495,7 @@ def api(): api_project_doc, api_projects_doc, api_git_tags_doc, + api_project_git_urls_doc ], issues=issues, requests=[ diff --git a/pagure/api/project.py b/pagure/api/project.py index b93a687..cee5ef4 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -18,7 +18,7 @@ import pagure.lib import pagure.lib.git from pagure import SESSION, APP, authenticated from pagure.api import (API, api_method, APIERROR, api_login_required, - get_authorized_api_project) + get_authorized_api_project, api_login_optional) @API.route('//git/tags') @@ -144,6 +144,59 @@ def api_project_watchers(repo, username=None, namespace=None): }) +@API.route('//git/urls') +@API.route('///git/urls') +@API.route('/fork///git/urls') +@API.route('/fork////git/urls') +@api_login_optional() +@api_method +def api_project_git_urls(repo, username=None, namespace=None): + ''' + Project Git URLs + ---------------- + List the Git URLS on the project. + + :: + + GET /api/0//git/urls + GET /api/0///git/urls + + :: + + GET /api/0/fork///git/urls + GET /api/0/fork////git/urls + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "total_urls": 2, + "urls": { + "ssh": "ssh://git@pagure.io/mprahl-test123.git", + "git": "https://pagure.io/mprahl-test123.git" + } + } + ''' + repo = get_authorized_api_project( + SESSION, repo, user=username, namespace=namespace) + if repo is None: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) + git_urls = {} + if pagure.APP.config.get('GIT_URL_SSH'): + git_urls['ssh'] = '{0}{1}.git'.format( + pagure.APP.config['GIT_URL_SSH'], repo.fullname) + if pagure.APP.config.get('GIT_URL_GIT'): + git_urls['git'] = '{0}{1}.git'.format( + pagure.APP.config['GIT_URL_GIT'], repo.fullname) + + return flask.jsonify({ + 'total_urls': len(git_urls), + "urls": git_urls + }) + + @API.route('//git/branches') @API.route('///git/branches') @API.route('/fork///git/branches') diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index 0d5cf4f..c5b352d 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -159,6 +159,82 @@ class PagureFlaskApiProjecttests(tests.Modeltests): output = self.app.get('/api/0/test/git/branches') self.assertEqual(output.status_code, 404) + def test_api_git_urls(self): + """ Test the api_project_git_urls method of the flask api. + """ + tests.create_projects(self.session) + output = self.app.get('/api/0/test/git/urls') + self.assertEqual(output.status_code, 200) + expected_rv = { + 'urls': { + 'git': 'git://pagure.org/test.git', + 'ssh': 'ssh://git@pagure.org/test.git' + }, + 'total_urls': 2 + } + data = json.loads(output.data) + self.assertDictEqual(data, expected_rv) + + def test_api_git_urls_no_project(self): + """ Test the api_project_git_urls method of the flask api when there is + no project. + """ + output = self.app.get('/api/0/test1234/git/urls') + self.assertEqual(output.status_code, 404) + expected_rv = { + 'error': 'Project not found', + 'error_code': 'ENOPROJECT' + } + data = json.loads(output.data) + self.assertDictEqual(data, expected_rv) + + @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True}) + def test_api_git_urls_private_project(self): + """ Test the api_project_git_urls method of the flask api when the + project is private. + """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session, 'aaabbbcccddd') + headers = {'Authorization': 'token aaabbbcccddd'} + + test_project = pagure.lib._get_project(self.session, 'test') + test_project.private = True + self.session.add(test_project) + self.session.commit() + + output = self.app.get('/api/0/test/git/urls', headers=headers) + self.assertEqual(output.status_code, 200) + expected_rv = { + 'urls': { + 'git': 'git://pagure.org/test.git', + 'ssh': 'ssh://git@pagure.org/test.git' + }, + 'total_urls': 2 + } + data = json.loads(output.data) + self.assertDictEqual(data, expected_rv) + + @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True}) + def test_api_git_urls_private_project_no_login(self): + """ Test the api_project_git_urls method of the flask api when the + project is private and the user is not logged in. + """ + tests.create_projects(self.session) + test_project = pagure.lib._get_project(self.session, 'test') + test_project.private = True + self.session.add(test_project) + self.session.commit() + + output = self.app.get('/api/0/test/git/urls') + self.assertEqual(output.status_code, 404) + expected_rv = { + 'error': 'Project not found', + 'error_code': 'ENOPROJECT' + } + data = json.loads(output.data) + self.assertDictEqual(data, expected_rv) + def test_api_projects(self): """ Test the api_projects method of the flask api. """ tests.create_projects(self.session)