From 1a6e219da8fd7b9d76c8f00a3777aab2ed5ccfac Mon Sep 17 00:00:00 2001 From: Slavek Kabrda Date: Aug 29 2018 12:51:50 +0000 Subject: Add an API endpoint to retrieve pull requests by uid --- diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 58c5eeb..05ce6cc 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -598,6 +598,9 @@ def api(): api_pull_request_views_doc = load_doc(fork.api_pull_request_views) api_pull_request_view_doc = load_doc(fork.api_pull_request_view) + api_pull_request_by_uid_view_doc = load_doc( + fork.api_pull_request_by_uid_view + ) api_pull_request_merge_doc = load_doc(fork.api_pull_request_merge) api_pull_request_close_doc = load_doc(fork.api_pull_request_close) api_pull_request_add_comment_doc = load_doc( @@ -659,6 +662,7 @@ def api(): requests=[ api_pull_request_views_doc, api_pull_request_view_doc, + api_pull_request_by_uid_view_doc, api_pull_request_merge_doc, api_pull_request_close_doc, api_pull_request_add_comment_doc, diff --git a/pagure/api/fork.py b/pagure/api/fork.py index a87e6ac..375741f 100644 --- a/pagure/api/fork.py +++ b/pagure/api/fork.py @@ -218,6 +218,91 @@ def api_pull_request_views(repo, username=None, namespace=None): return flask.jsonify(jsonout) +@API.route("/pull-requests/") +@api_method +def api_pull_request_by_uid_view(uid): + """ + Pull-request by UID information + ------------------------------- + Retrieve information of a pull request specified by uid. + + :: + + GET /api/0/pull-requests/ + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "assignee": null, + "branch": "master", + "branch_from": "master", + "closed_at": null, + "closed_by": null, + "comments": [], + "commit_start": null, + "commit_stop": null, + "date_created": "1431414800", + "id": 1, + "project": { + "close_status": [], + "custom_keys": [], + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": null, + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "repo_from": { + "date_created": "1431414800", + "description": "test project #1", + "id": 1, + "name": "test", + "parent": null, + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "status": "Open", + "title": "test pull-request", + "uid": "1431414800", + "updated_on": "1431414800", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + + """ + request = pagure.lib.get_request_by_uid(flask.g.session, uid) + if not request: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOREQ) + + # we don't really need the repo, but we need to make sure + # that we're allowed to access it + username = request.project.user.user if request.project.is_fork else None + repo = get_authorized_api_project( + flask.g.session, request.project.name, + user=username, namespace=request.project.namespace) + if repo is None: + raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOREQ) + + if not repo.settings.get("pull_requests", True): + raise pagure.exceptions.APIError( + 404, error_code=APIERROR.EPULLREQUESTSDISABLED + ) + + jsonout = flask.jsonify(request.to_json(public=True, api=True)) + return jsonout + + @API.route("//pull-request/") @API.route("///pull-request/") @API.route("/fork///pull-request/") diff --git a/tests/test_pagure_flask_api_fork.py b/tests/test_pagure_flask_api_fork.py index 524b6b5..020f121 100644 --- a/tests/test_pagure_flask_api_fork.py +++ b/tests/test_pagure_flask_api_fork.py @@ -640,6 +640,166 @@ class PagureFlaskApiForktests(tests.Modeltests): self.assertDictEqual(data, data2) @patch('pagure.lib.notify.send_email') + def test_api_pull_request_by_uid_view(self, send_email): + """ Test the api_pull_request_by_uid_view method of the flask api. """ + send_email.return_value = True + tests.create_projects(self.session) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + # Create a pull-request + repo = pagure.lib.get_authorized_project(self.session, 'test') + forked_repo = pagure.lib.get_authorized_project(self.session, 'test') + req = pagure.lib.new_pull_request( + session=self.session, + repo_from=forked_repo, + branch_from='master', + repo_to=repo, + branch_to='master', + title='test pull-request', + user='pingou', + requestfolder=None, + ) + self.session.commit() + self.assertEqual(req.id, 1) + self.assertEqual(req.title, 'test pull-request') + uid = req.uid + + # Invalid request + output = self.app.get('/api/0/pull-requests/{}'.format(uid + 'aaa')) + self.assertEqual(output.status_code, 404) + data = json.loads(output.get_data(as_text=True)) + self.assertDictEqual( + data, + { + "error": "Pull-Request not found", + "error_code": "ENOREQ", + } + ) + + # Valid issue + output = self.app.get('/api/0/pull-requests/{}'.format(uid)) + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + data['date_created'] = '1431414800' + data['updated_on'] = '1431414800' + data['project']['date_created'] = '1431414800' + data['project']['date_modified'] = '1431414800' + data['repo_from']['date_created'] = '1431414800' + data['repo_from']['date_modified'] = '1431414800' + data['last_updated'] = '1431414800' + expected_data = { + "assignee": None, + "branch": "master", + "branch_from": "master", + "cached_merge_status": "unknown", + "closed_at": None, + "closed_by": None, + "comments": [], + "commit_start": None, + "commit_stop": None, + "date_created": "1431414800", + "id": 1, + "initial_comment": None, + "last_updated": "1431414800", + "project": { + "access_groups": { + "admin": [], + "commit": [], + "ticket": [] + }, + "access_users": { + "admin": [], + "commit": [], + "owner": ["pingou"], + "ticket": [] + }, + "close_status": [ + "Invalid", + "Insufficient data", + "Fixed", + "Duplicate" + ], + "custom_keys": [], + "date_created": "1431414800", + "date_modified": "1431414800", + "description": "test project #1", + "fullname": "test", + "url_path": "test", + "id": 1, + "milestones": {}, + "name": "test", + "namespace": None, + "parent": None, + "priorities": {}, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "remote_git": None, + "repo_from": { + "access_groups": { + "admin": [], + "commit": [], + "ticket": []}, + "access_users": { + "admin": [], + "commit": [], + "owner": ["pingou"], + "ticket": []}, + "close_status": [ + "Invalid", + "Insufficient data", + "Fixed", + "Duplicate"], + "custom_keys": [], + "date_created": "1431414800", + "date_modified": "1431414800", + "description": "test project #1", + "fullname": "test", + "url_path": "test", + "id": 1, + "milestones": {}, + "name": "test", + "namespace": None, + "parent": None, + "priorities": {}, + "tags": [], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + "status": "Open", + "title": "test pull-request", + "uid": uid, + "updated_on": "1431414800", + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + self.assertDictEqual(data, expected_data) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # Access Pull-Request authenticated + output = self.app.get('/api/0/pull-requests/{}'.format(uid), headers=headers) + self.assertEqual(output.status_code, 200) + data2 = json.loads(output.get_data(as_text=True)) + data2['date_created'] = '1431414800' + data2['project']['date_created'] = '1431414800' + data2['project']['date_modified'] = '1431414800' + data2['repo_from']['date_created'] = '1431414800' + data2['repo_from']['date_modified'] = '1431414800' + data2['date_created'] = '1431414800' + data2['updated_on'] = '1431414800' + data2['last_updated'] = '1431414800' + self.assertDictEqual(data, data2) + + @patch('pagure.lib.notify.send_email') def test_api_pull_request_close_pr_disabled(self, send_email): """ Test the api_pull_request_close method of the flask api. """ send_email.return_value = True