From b42ba1a7e84b2575804e4608bccc3270db90506a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 15 2016 12:10:09 +0000 Subject: Add a new API endpoint returning information about a specific project We had the API endpoint to search for projects, this add a way to get the information for a single project directly. Fixes https://pagure.io/pagure/issue/1608 --- diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 682998e..eb591fe 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -433,8 +433,9 @@ def api_error_codes(): @API.route('/') def api(): ''' Display the api information page. ''' - api_git_tags_doc = load_doc(project.api_git_tags) + 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) issues = [] if pagure.APP.config.get('ENABLE_TICKETS', True): @@ -486,8 +487,9 @@ def api(): api_doc=APIDOC, projects=[ api_new_project_doc, - api_git_tags_doc, + api_project_doc, api_projects_doc, + api_git_tags_doc, ], issues=issues, requests=[ diff --git a/pagure/api/project.py b/pagure/api/project.py index 2d0bd6f..3f26404 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -178,6 +178,49 @@ def api_projects(): return jsonout +@API.route('/') +@API.route('//') +@API.route('/fork//') +@API.route('/fork///') +@api_method +def api_project(repo, username=None, namespace=None): + """ + Project information + ------------------- + Return information about a specific project + + :: + + GET /api/0/ + GET /api/0// + + :: + + GET /api/0/fork// + GET /api/0/fork/// + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "total_tags": 2, + "tags": ["0.0.1", "0.0.2"] + } + + """ + repo = pagure.lib.get_project( + SESSION, repo, user=username, namespace=namespace) + + if repo is None: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) + + jsonout = flask.jsonify(repo.to_json(api=True, public=True)) + return jsonout + + + @API.route('/new/', methods=['POST']) @API.route('/new', methods=['POST']) @api_login_required(acls=['create_project']) diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index e617780..0a8c3f8 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -308,6 +308,67 @@ class PagureFlaskApiProjecttests(tests.Modeltests): } ) + def test_api_project(self): + """ Test the api_project method of the flask api. """ + tests.create_projects(self.session) + + # Check before adding + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual(repo.tags, []) + + # Adding a tag + output = pagure.lib.update_tags( + self.session, repo, 'infra', 'pingou', + ticketfolder=None) + self.assertEqual(output, ['Tag added: infra']) + + # Check after adding + repo = pagure.lib.get_project(self.session, 'test') + self.assertEqual(len(repo.tags), 1) + self.assertEqual(repo.tags_text, ['infra']) + + # Check the API + + # Non-existing project + output = self.app.get('/api/0/random') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'error_code': 'ENOPROJECT', 'error': 'Project not found'} + ) + + # Existing project + output = self.app.get('/api/0/test') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "close_status": [ + "Invalid", + "Insufficient data", + "Fixed", + "Duplicate" + ], + "custom_keys": [], + "date_created": "1436527638", + "description": "test project #1", + "id": 1, + "milestones": {}, + "name": "test", + "namespace": None, + "parent": None, + "priorities": {}, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ) + @patch('pagure.lib.git.generate_gitolite_acls') def test_api_new_project(self, p_gga): """ Test the api_new_project method of the flask api. """