pull_request_api.py
917 lines
| 33.3 KiB
| text/x-python
|
PythonLexer
r1 | # -*- coding: utf-8 -*- | |||
r2487 | # Copyright (C) 2011-2018 RhodeCode GmbH | |||
r1 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import logging | ||||
r1792 | from rhodecode import events | |||
r1769 | from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCValidationError | |||
r1 | from rhodecode.api.utils import ( | |||
has_superadmin_permission, Optional, OAttr, get_repo_or_error, | ||||
get_pull_request_or_error, get_commit_or_error, get_user_or_error, | ||||
r1150 | validate_repo_permissions, resolve_ref_or_error) | |||
r1 | from rhodecode.lib.auth import (HasRepoPermissionAnyApi) | |||
from rhodecode.lib.base import vcs_operation_context | ||||
from rhodecode.lib.utils2 import str2bool | ||||
from rhodecode.model.changeset_status import ChangesetStatusModel | ||||
r1323 | from rhodecode.model.comment import CommentsModel | |||
r1337 | from rhodecode.model.db import Session, ChangesetStatus, ChangesetComment | |||
r1335 | from rhodecode.model.pull_request import PullRequestModel, MergeCheck | |||
r1 | from rhodecode.model.settings import SettingsModel | |||
r1769 | from rhodecode.model.validation_schema import Invalid | |||
r1792 | from rhodecode.model.validation_schema.schemas.reviewer_schema import( | |||
ReviewerListSchema) | ||||
r1 | ||||
log = logging.getLogger(__name__) | ||||
@jsonrpc_method() | ||||
r2395 | def get_pull_request(request, apiuser, pullrequestid, repoid=Optional(None)): | |||
r1 | """ | |||
Get a pull request based on the given ID. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
r2395 | :param repoid: Optional, repository name or repository ID from where | |||
the pull request was opened. | ||||
r1 | :type repoid: str or int | |||
:param pullrequestid: ID of the requested pull request. | ||||
:type pullrequestid: int | ||||
Example output: | ||||
.. code-block:: bash | ||||
"id": <id_given_in_input>, | ||||
"result": | ||||
{ | ||||
"pull_request_id": "<pull_request_id>", | ||||
"url": "<url>", | ||||
"title": "<title>", | ||||
"description": "<description>", | ||||
"status" : "<status>", | ||||
"created_on": "<date_time_created>", | ||||
"updated_on": "<date_time_updated>", | ||||
"commit_ids": [ | ||||
... | ||||
"<commit_id>", | ||||
"<commit_id>", | ||||
... | ||||
], | ||||
"review_status": "<review_status>", | ||||
"mergeable": { | ||||
"status": "<bool>", | ||||
"message": "<message>", | ||||
}, | ||||
"source": { | ||||
"clone_url": "<clone_url>", | ||||
"repository": "<repository_name>", | ||||
"reference": | ||||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
}, | ||||
"target": { | ||||
"clone_url": "<clone_url>", | ||||
"repository": "<repository_name>", | ||||
"reference": | ||||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
}, | ||||
Martin Bornhold
|
r1054 | "merge": { | ||
Martin Bornhold
|
r893 | "clone_url": "<clone_url>", | ||
Martin Bornhold
|
r1054 | "reference": | ||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
Martin Bornhold
|
r893 | }, | ||
r1 | "author": <user_obj>, | |||
"reviewers": [ | ||||
... | ||||
{ | ||||
"user": "<user_obj>", | ||||
"review_status": "<review_status>", | ||||
} | ||||
... | ||||
] | ||||
}, | ||||
"error": null | ||||
""" | ||||
r2395 | ||||
r1 | pull_request = get_pull_request_or_error(pullrequestid) | |||
r2395 | if Optional.extract(repoid): | |||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
r1 | if not PullRequestModel().check_user_read( | |||
pull_request, apiuser, api=True): | ||||
r2395 | raise JSONRPCError('repository `%s` or pull request `%s` ' | |||
'does not exist' % (repoid, pullrequestid)) | ||||
r1 | data = pull_request.get_api_data() | |||
return data | ||||
@jsonrpc_method() | ||||
def get_pull_requests(request, apiuser, repoid, status=Optional('new')): | ||||
""" | ||||
Get all pull requests from the repository specified in `repoid`. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
r2395 | :param repoid: Optional repository name or repository ID. | |||
r1 | :type repoid: str or int | |||
:param status: Only return pull requests with the specified status. | ||||
Valid options are. | ||||
* ``new`` (default) | ||||
* ``open`` | ||||
* ``closed`` | ||||
:type status: str | ||||
Example output: | ||||
.. code-block:: bash | ||||
"id": <id_given_in_input>, | ||||
"result": | ||||
[ | ||||
... | ||||
{ | ||||
"pull_request_id": "<pull_request_id>", | ||||
"url": "<url>", | ||||
"title" : "<title>", | ||||
"description": "<description>", | ||||
"status": "<status>", | ||||
"created_on": "<date_time_created>", | ||||
"updated_on": "<date_time_updated>", | ||||
"commit_ids": [ | ||||
... | ||||
"<commit_id>", | ||||
"<commit_id>", | ||||
... | ||||
], | ||||
"review_status": "<review_status>", | ||||
"mergeable": { | ||||
"status": "<bool>", | ||||
"message: "<message>", | ||||
}, | ||||
"source": { | ||||
"clone_url": "<clone_url>", | ||||
"reference": | ||||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
}, | ||||
"target": { | ||||
"clone_url": "<clone_url>", | ||||
"reference": | ||||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
}, | ||||
Martin Bornhold
|
r1054 | "merge": { | ||
Martin Bornhold
|
r893 | "clone_url": "<clone_url>", | ||
Martin Bornhold
|
r1054 | "reference": | ||
{ | ||||
"name": "<name>", | ||||
"type": "<type>", | ||||
"commit_id": "<commit_id>", | ||||
} | ||||
Martin Bornhold
|
r893 | }, | ||
r1 | "author": <user_obj>, | |||
"reviewers": [ | ||||
... | ||||
{ | ||||
"user": "<user_obj>", | ||||
"review_status": "<review_status>", | ||||
} | ||||
... | ||||
] | ||||
} | ||||
... | ||||
], | ||||
"error": null | ||||
""" | ||||
repo = get_repo_or_error(repoid) | ||||
if not has_superadmin_permission(apiuser): | ||||
_perms = ( | ||||
'repository.admin', 'repository.write', 'repository.read',) | ||||
r1150 | validate_repo_permissions(apiuser, repoid, repo, _perms) | |||
r1 | ||||
status = Optional.extract(status) | ||||
pull_requests = PullRequestModel().get_all(repo, statuses=[status]) | ||||
data = [pr.get_api_data() for pr in pull_requests] | ||||
return data | ||||
@jsonrpc_method() | ||||
r1792 | def merge_pull_request( | |||
r2395 | request, apiuser, pullrequestid, repoid=Optional(None), | |||
r1792 | userid=Optional(OAttr('apiuser'))): | |||
r1 | """ | |||
Merge the pull request specified by `pullrequestid` into its target | ||||
repository. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
r2395 | :param repoid: Optional, repository name or repository ID of the | |||
r1 | target repository to which the |pr| is to be merged. | |||
:type repoid: str or int | ||||
:param pullrequestid: ID of the pull request which shall be merged. | ||||
:type pullrequestid: int | ||||
:param userid: Merge the pull request as this user. | ||||
:type userid: Optional(str or int) | ||||
Example output: | ||||
.. code-block:: bash | ||||
r1603 | "id": <id_given_in_input>, | |||
"result": { | ||||
r1 | "executed": "<bool>", | |||
"failure_reason": "<int>", | ||||
"merge_commit_id": "<merge_commit_id>", | ||||
Martin Bornhold
|
r1055 | "possible": "<bool>", | ||
"merge_ref": { | ||||
"commit_id": "<commit_id>", | ||||
"type": "<type>", | ||||
"name": "<name>" | ||||
} | ||||
r1 | }, | |||
r1603 | "error": null | |||
r1 | """ | |||
r2395 | pull_request = get_pull_request_or_error(pullrequestid) | |||
if Optional.extract(repoid): | ||||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
r1 | if not isinstance(userid, Optional): | |||
if (has_superadmin_permission(apiuser) or | ||||
HasRepoPermissionAnyApi('repository.admin')( | ||||
user=apiuser, repo_name=repo.repo_name)): | ||||
apiuser = get_user_or_error(userid) | ||||
else: | ||||
raise JSONRPCError('userid is not the same as your user') | ||||
r2168 | check = MergeCheck.validate( | |||
pull_request, user=apiuser, translator=request.translate) | ||||
r1335 | merge_possible = not check.failed | |||
if not merge_possible: | ||||
r1759 | error_messages = [] | |||
for err_type, error_msg in check.errors: | ||||
error_msg = request.translate(error_msg) | ||||
error_messages.append(error_msg) | ||||
reasons = ','.join(error_messages) | ||||
r1 | raise JSONRPCError( | |||
r1335 | 'merge not possible for following reasons: {}'.format(reasons)) | |||
r1 | ||||
target_repo = pull_request.target_repo | ||||
extras = vcs_operation_context( | ||||
request.environ, repo_name=target_repo.repo_name, | ||||
username=apiuser.username, action='push', | ||||
scm=target_repo.repo_type) | ||||
Martin Bornhold
|
r1055 | merge_response = PullRequestModel().merge( | ||
pull_request, apiuser, extras=extras) | ||||
if merge_response.executed: | ||||
r1 | PullRequestModel().close_pull_request( | |||
pull_request.pull_request_id, apiuser) | ||||
r506 | Session().commit() | |||
Martin Bornhold
|
r1055 | |||
# In previous versions the merge response directly contained the merge | ||||
# commit id. It is now contained in the merge reference object. To be | ||||
# backwards compatible we have to extract it again. | ||||
merge_response = merge_response._asdict() | ||||
merge_response['merge_commit_id'] = merge_response['merge_ref'].commit_id | ||||
return merge_response | ||||
r1 | ||||
@jsonrpc_method() | ||||
r2394 | def get_pull_request_comments( | |||
request, apiuser, pullrequestid, repoid=Optional(None)): | ||||
""" | ||||
Get all comments of pull request specified with the `pullrequestid` | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
:param repoid: Optional repository name or repository ID. | ||||
:type repoid: str or int | ||||
:param pullrequestid: The pull request ID. | ||||
:type pullrequestid: int | ||||
Example output: | ||||
.. code-block:: bash | ||||
id : <id_given_in_input> | ||||
result : [ | ||||
{ | ||||
"comment_author": { | ||||
"active": true, | ||||
"full_name_or_username": "Tom Gore", | ||||
"username": "admin" | ||||
}, | ||||
"comment_created_on": "2017-01-02T18:43:45.533", | ||||
"comment_f_path": null, | ||||
"comment_id": 25, | ||||
"comment_lineno": null, | ||||
"comment_status": { | ||||
"status": "under_review", | ||||
"status_lbl": "Under Review" | ||||
}, | ||||
"comment_text": "Example text", | ||||
"comment_type": null, | ||||
"pull_request_version": null | ||||
} | ||||
], | ||||
error : null | ||||
""" | ||||
pull_request = get_pull_request_or_error(pullrequestid) | ||||
if Optional.extract(repoid): | ||||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
if not PullRequestModel().check_user_read( | ||||
pull_request, apiuser, api=True): | ||||
raise JSONRPCError('repository `%s` or pull request `%s` ' | ||||
'does not exist' % (repoid, pullrequestid)) | ||||
(pull_request_latest, | ||||
pull_request_at_ver, | ||||
pull_request_display_obj, | ||||
at_version) = PullRequestModel().get_pr_version( | ||||
pull_request.pull_request_id, version=None) | ||||
versions = pull_request_display_obj.versions() | ||||
ver_map = { | ||||
ver.pull_request_version_id: cnt | ||||
for cnt, ver in enumerate(versions, 1) | ||||
} | ||||
# GENERAL COMMENTS with versions # | ||||
q = CommentsModel()._all_general_comments_of_pull_request(pull_request) | ||||
q = q.order_by(ChangesetComment.comment_id.asc()) | ||||
general_comments = q.all() | ||||
# INLINE COMMENTS with versions # | ||||
q = CommentsModel()._all_inline_comments_of_pull_request(pull_request) | ||||
q = q.order_by(ChangesetComment.comment_id.asc()) | ||||
inline_comments = q.all() | ||||
data = [] | ||||
for comment in inline_comments + general_comments: | ||||
full_data = comment.get_api_data() | ||||
pr_version_id = None | ||||
if comment.pull_request_version_id: | ||||
pr_version_id = 'v{}'.format( | ||||
ver_map[comment.pull_request_version_id]) | ||||
# sanitize some entries | ||||
full_data['pull_request_version'] = pr_version_id | ||||
full_data['comment_author'] = { | ||||
'username': full_data['comment_author'].username, | ||||
'full_name_or_username': full_data['comment_author'].full_name_or_username, | ||||
'active': full_data['comment_author'].active, | ||||
} | ||||
if full_data['comment_status']: | ||||
full_data['comment_status'] = { | ||||
'status': full_data['comment_status'][0].status, | ||||
'status_lbl': full_data['comment_status'][0].status_lbl, | ||||
} | ||||
else: | ||||
full_data['comment_status'] = {} | ||||
data.append(full_data) | ||||
return data | ||||
@jsonrpc_method() | ||||
r1337 | def comment_pull_request( | |||
r2395 | request, apiuser, pullrequestid, repoid=Optional(None), | |||
message=Optional(None), commit_id=Optional(None), status=Optional(None), | ||||
r1337 | comment_type=Optional(ChangesetComment.COMMENT_TYPE_NOTE), | |||
r1338 | resolves_comment_id=Optional(None), | |||
r1337 | userid=Optional(OAttr('apiuser'))): | |||
r1 | """ | |||
Comment on the pull request specified with the `pullrequestid`, | ||||
in the |repo| specified by the `repoid`, and optionally change the | ||||
review status. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
r2395 | :param repoid: Optional repository name or repository ID. | |||
r1 | :type repoid: str or int | |||
:param pullrequestid: The pull request ID. | ||||
:type pullrequestid: int | ||||
r1269 | :param commit_id: Specify the commit_id for which to set a comment. If | |||
given commit_id is different than latest in the PR status | ||||
change won't be performed. | ||||
:type commit_id: str | ||||
r1337 | :param message: The text content of the comment. | |||
:type message: str | ||||
:param status: (**Optional**) Set the approval status of the pull | ||||
request. One of: 'not_reviewed', 'approved', 'rejected', | ||||
'under_review' | ||||
:type status: str | ||||
:param comment_type: Comment type, one of: 'note', 'todo' | ||||
:type comment_type: Optional(str), default: 'note' | ||||
r1 | :param userid: Comment on the pull request as this user | |||
:type userid: Optional(str or int) | ||||
Example output: | ||||
.. code-block:: bash | ||||
r1603 | id : <id_given_in_input> | |||
result : { | ||||
r1 | "pull_request_id": "<Integer>", | |||
r1269 | "comment_id": "<Integer>", | |||
"status": {"given": <given_status>, | ||||
"was_changed": <bool status_was_actually_changed> }, | ||||
r1603 | }, | |||
error : null | ||||
r1 | """ | |||
r2395 | pull_request = get_pull_request_or_error(pullrequestid) | |||
if Optional.extract(repoid): | ||||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
r1 | if not isinstance(userid, Optional): | |||
if (has_superadmin_permission(apiuser) or | ||||
HasRepoPermissionAnyApi('repository.admin')( | ||||
user=apiuser, repo_name=repo.repo_name)): | ||||
apiuser = get_user_or_error(userid) | ||||
else: | ||||
raise JSONRPCError('userid is not the same as your user') | ||||
if not PullRequestModel().check_user_read( | ||||
pull_request, apiuser, api=True): | ||||
raise JSONRPCError('repository `%s` does not exist' % (repoid,)) | ||||
message = Optional.extract(message) | ||||
status = Optional.extract(status) | ||||
r1269 | commit_id = Optional.extract(commit_id) | |||
r1337 | comment_type = Optional.extract(comment_type) | |||
r1338 | resolves_comment_id = Optional.extract(resolves_comment_id) | |||
r1269 | ||||
r1 | if not message and not status: | |||
r1269 | raise JSONRPCError( | |||
'Both message and status parameters are missing. ' | ||||
'At least one is required.') | ||||
r1 | ||||
if (status not in (st[0] for st in ChangesetStatus.STATUSES) and | ||||
status is not None): | ||||
r1269 | raise JSONRPCError('Unknown comment status: `%s`' % status) | |||
if commit_id and commit_id not in pull_request.revisions: | ||||
raise JSONRPCError( | ||||
'Invalid commit_id `%s` for this pull request.' % commit_id) | ||||
r1 | ||||
allowed_to_change_status = PullRequestModel().check_user_change_status( | ||||
pull_request, apiuser) | ||||
r1269 | ||||
# if commit_id is passed re-validated if user is allowed to change status | ||||
# based on latest commit_id from the PR | ||||
if commit_id: | ||||
commit_idx = pull_request.revisions.index(commit_id) | ||||
if commit_idx != 0: | ||||
allowed_to_change_status = False | ||||
r1338 | if resolves_comment_id: | |||
comment = ChangesetComment.get(resolves_comment_id) | ||||
if not comment: | ||||
raise JSONRPCError( | ||||
'Invalid resolves_comment_id `%s` for this pull request.' | ||||
% resolves_comment_id) | ||||
if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO: | ||||
raise JSONRPCError( | ||||
'Comment `%s` is wrong type for setting status to resolved.' | ||||
% resolves_comment_id) | ||||
r1 | text = message | |||
r1269 | status_label = ChangesetStatus.get_status_lbl(status) | |||
r1 | if status and allowed_to_change_status: | |||
r1269 | st_message = ('Status change %(transition_icon)s %(status)s' | |||
% {'transition_icon': '>', 'status': status_label}) | ||||
r1 | text = message or st_message | |||
rc_config = SettingsModel().get_all_settings() | ||||
renderer = rc_config.get('rhodecode_markup_renderer', 'rst') | ||||
r1269 | ||||
status_change = status and allowed_to_change_status | ||||
r1323 | comment = CommentsModel().create( | |||
r1 | text=text, | |||
repo=pull_request.target_repo.repo_id, | ||||
user=apiuser.user_id, | ||||
pull_request=pull_request.pull_request_id, | ||||
f_path=None, | ||||
line_no=None, | ||||
r1269 | status_change=(status_label if status_change else None), | |||
status_change_type=(status if status_change else None), | ||||
r1 | closing_pr=False, | |||
r1337 | renderer=renderer, | |||
r1338 | comment_type=comment_type, | |||
resolves_comment_id=resolves_comment_id | ||||
r1 | ) | |||
if allowed_to_change_status and status: | ||||
ChangesetStatusModel().set_status( | ||||
pull_request.target_repo.repo_id, | ||||
status, | ||||
apiuser.user_id, | ||||
comment, | ||||
pull_request=pull_request.pull_request_id | ||||
) | ||||
Session().flush() | ||||
Session().commit() | ||||
data = { | ||||
'pull_request_id': pull_request.pull_request_id, | ||||
r1269 | 'comment_id': comment.comment_id if comment else None, | |||
'status': {'given': status, 'was_changed': status_change}, | ||||
r1 | } | |||
return data | ||||
@jsonrpc_method() | ||||
def create_pull_request( | ||||
request, apiuser, source_repo, target_repo, source_ref, target_ref, | ||||
r2859 | title=Optional(''), description=Optional(''), reviewers=Optional(None)): | |||
r1 | """ | |||
Creates a new pull request. | ||||
Accepts refs in the following formats: | ||||
* branch:<branch_name>:<sha> | ||||
* branch:<branch_name> | ||||
* bookmark:<bookmark_name>:<sha> (Mercurial only) | ||||
* bookmark:<bookmark_name> (Mercurial only) | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
:param source_repo: Set the source repository name. | ||||
:type source_repo: str | ||||
:param target_repo: Set the target repository name. | ||||
:type target_repo: str | ||||
:param source_ref: Set the source ref name. | ||||
:type source_ref: str | ||||
:param target_ref: Set the target ref name. | ||||
:type target_ref: str | ||||
r2859 | :param title: Optionally Set the pull request title, it's generated otherwise | |||
r1 | :type title: str | |||
:param description: Set the pull request description. | ||||
:type description: Optional(str) | ||||
:param reviewers: Set the new pull request reviewers list. | ||||
r1795 | Reviewer defined by review rules will be added automatically to the | |||
defined list. | ||||
r1 | :type reviewers: Optional(list) | |||
r873 | Accepts username strings or objects of the format: | |||
r1603 | ||||
r1795 | [{'username': 'nick', 'reasons': ['original author'], 'mandatory': <bool>}] | |||
r1 | """ | |||
r873 | ||||
r1795 | source_db_repo = get_repo_or_error(source_repo) | |||
target_db_repo = get_repo_or_error(target_repo) | ||||
r1 | if not has_superadmin_permission(apiuser): | |||
_perms = ('repository.admin', 'repository.write', 'repository.read',) | ||||
r1795 | validate_repo_permissions(apiuser, source_repo, source_db_repo, _perms) | |||
r1 | ||||
r1795 | full_source_ref = resolve_ref_or_error(source_ref, source_db_repo) | |||
full_target_ref = resolve_ref_or_error(target_ref, target_db_repo) | ||||
r2859 | ||||
source_scm = source_db_repo.scm_instance() | ||||
target_scm = target_db_repo.scm_instance() | ||||
r1795 | source_commit = get_commit_or_error(full_source_ref, source_db_repo) | |||
target_commit = get_commit_or_error(full_target_ref, target_db_repo) | ||||
r2859 | ||||
ancestor = source_scm.get_common_ancestor( | ||||
source_commit.raw_id, target_commit.raw_id, target_scm) | ||||
if not ancestor: | ||||
raise JSONRPCError('no common ancestor found') | ||||
# recalculate target ref based on ancestor | ||||
target_ref_type, target_ref_name, __ = full_target_ref.split(':') | ||||
full_target_ref = ':'.join((target_ref_type, target_ref_name, ancestor)) | ||||
r1 | ||||
commit_ranges = target_scm.compare( | ||||
target_commit.raw_id, source_commit.raw_id, source_scm, | ||||
merge=True, pre_load=[]) | ||||
if not commit_ranges: | ||||
raise JSONRPCError('no commits found') | ||||
r873 | reviewer_objects = Optional.extract(reviewers) or [] | |||
r1795 | ||||
r2859 | # serialize and validate passed in given reviewers | |||
r1769 | if reviewer_objects: | |||
schema = ReviewerListSchema() | ||||
try: | ||||
reviewer_objects = schema.deserialize(reviewer_objects) | ||||
except Invalid as err: | ||||
raise JSONRPCValidationError(colander_exc=err) | ||||
r1 | ||||
r1795 | # validate users | |||
for reviewer_object in reviewer_objects: | ||||
user = get_user_or_error(reviewer_object['username']) | ||||
reviewer_object['user_id'] = user.user_id | ||||
r2859 | get_default_reviewers_data, validate_default_reviewers = \ | |||
r1795 | PullRequestModel().get_reviewer_functions() | |||
r2859 | # recalculate reviewers logic, to make sure we can validate this | |||
r1795 | reviewer_rules = get_default_reviewers_data( | |||
apiuser.get_instance(), source_db_repo, | ||||
source_commit, target_db_repo, target_commit) | ||||
r2859 | # now MERGE our given with the calculated | |||
reviewer_objects = reviewer_rules['reviewers'] + reviewer_objects | ||||
r1795 | ||||
try: | ||||
r2859 | reviewers = validate_default_reviewers( | |||
r1795 | reviewer_objects, reviewer_rules) | |||
except ValueError as e: | ||||
raise JSONRPCError('Reviewers Validation: {}'.format(e)) | ||||
r1 | ||||
r2859 | title = Optional.extract(title) | |||
if not title: | ||||
title_source_ref = source_ref.split(':', 2)[1] | ||||
title = PullRequestModel().generate_pullrequest_title( | ||||
source=source_repo, | ||||
source_ref=title_source_ref, | ||||
target=target_repo | ||||
) | ||||
description = Optional.extract(description) | ||||
pull_request = PullRequestModel().create( | ||||
r1 | created_by=apiuser.user_id, | |||
source_repo=source_repo, | ||||
source_ref=full_source_ref, | ||||
target_repo=target_repo, | ||||
target_ref=full_target_ref, | ||||
r2859 | revisions=[commit.raw_id for commit in reversed(commit_ranges)], | |||
r1769 | reviewers=reviewers, | |||
r1 | title=title, | |||
r2859 | description=description, | |||
reviewer_data=reviewer_rules, | ||||
r1 | ) | |||
Session().commit() | ||||
data = { | ||||
'msg': 'Created new pull request `{}`'.format(title), | ||||
'pull_request_id': pull_request.pull_request_id, | ||||
} | ||||
return data | ||||
@jsonrpc_method() | ||||
def update_pull_request( | ||||
r2395 | request, apiuser, pullrequestid, repoid=Optional(None), | |||
title=Optional(''), description=Optional(''), reviewers=Optional(None), | ||||
r1792 | update_commits=Optional(None)): | |||
r1 | """ | |||
Updates a pull request. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
r2395 | :param repoid: Optional repository name or repository ID. | |||
r1 | :type repoid: str or int | |||
:param pullrequestid: The pull request ID. | ||||
:type pullrequestid: int | ||||
:param title: Set the pull request title. | ||||
:type title: str | ||||
:param description: Update pull request description. | ||||
:type description: Optional(str) | ||||
:param reviewers: Update pull request reviewers list with new value. | ||||
:type reviewers: Optional(list) | ||||
r1769 | Accepts username strings or objects of the format: | |||
r1795 | [{'username': 'nick', 'reasons': ['original author'], 'mandatory': <bool>}] | |||
r1769 | ||||
r1 | :param update_commits: Trigger update of commits for this pull request | |||
:type: update_commits: Optional(bool) | ||||
Example output: | ||||
.. code-block:: bash | ||||
r1603 | id : <id_given_in_input> | |||
result : { | ||||
r1 | "msg": "Updated pull request `63`", | |||
"pull_request": <pull_request_object>, | ||||
"updated_reviewers": { | ||||
"added": [ | ||||
"username" | ||||
], | ||||
"removed": [] | ||||
}, | ||||
"updated_commits": { | ||||
"added": [ | ||||
"<sha1_hash>" | ||||
], | ||||
"common": [ | ||||
"<sha1_hash>", | ||||
"<sha1_hash>", | ||||
], | ||||
"removed": [] | ||||
} | ||||
} | ||||
r1603 | error : null | |||
r1 | """ | |||
pull_request = get_pull_request_or_error(pullrequestid) | ||||
r2395 | if Optional.extract(repoid): | |||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
r1 | if not PullRequestModel().check_user_update( | |||
pull_request, apiuser, api=True): | ||||
raise JSONRPCError( | ||||
'pull request `%s` update failed, no permission to update.' % ( | ||||
pullrequestid,)) | ||||
if pull_request.is_closed(): | ||||
raise JSONRPCError( | ||||
'pull request `%s` update failed, pull request is closed' % ( | ||||
pullrequestid,)) | ||||
r873 | reviewer_objects = Optional.extract(reviewers) or [] | |||
r1795 | ||||
r1769 | if reviewer_objects: | |||
schema = ReviewerListSchema() | ||||
try: | ||||
reviewer_objects = schema.deserialize(reviewer_objects) | ||||
except Invalid as err: | ||||
raise JSONRPCValidationError(colander_exc=err) | ||||
r1 | ||||
r1795 | # validate users | |||
for reviewer_object in reviewer_objects: | ||||
user = get_user_or_error(reviewer_object['username']) | ||||
reviewer_object['user_id'] = user.user_id | ||||
get_default_reviewers_data, get_validated_reviewers = \ | ||||
PullRequestModel().get_reviewer_functions() | ||||
# re-use stored rules | ||||
reviewer_rules = pull_request.reviewer_data | ||||
try: | ||||
reviewers = get_validated_reviewers( | ||||
reviewer_objects, reviewer_rules) | ||||
except ValueError as e: | ||||
raise JSONRPCError('Reviewers Validation: {}'.format(e)) | ||||
else: | ||||
reviewers = [] | ||||
r1 | ||||
title = Optional.extract(title) | ||||
description = Optional.extract(description) | ||||
if title or description: | ||||
PullRequestModel().edit( | ||||
pull_request, title or pull_request.title, | ||||
r1807 | description or pull_request.description, apiuser) | |||
r1 | Session().commit() | |||
commit_changes = {"added": [], "common": [], "removed": []} | ||||
if str2bool(Optional.extract(update_commits)): | ||||
if PullRequestModel().has_valid_update_type(pull_request): | ||||
Martin Bornhold
|
r1077 | update_response = PullRequestModel().update_commits( | ||
r1 | pull_request) | |||
Martin Bornhold
|
r1077 | commit_changes = update_response.changes or commit_changes | ||
r1 | Session().commit() | |||
reviewers_changes = {"added": [], "removed": []} | ||||
r1769 | if reviewers: | |||
r1 | added_reviewers, removed_reviewers = \ | |||
r1807 | PullRequestModel().update_reviewers(pull_request, reviewers, apiuser) | |||
r1 | ||||
reviewers_changes['added'] = sorted( | ||||
[get_user_or_error(n).username for n in added_reviewers]) | ||||
reviewers_changes['removed'] = sorted( | ||||
[get_user_or_error(n).username for n in removed_reviewers]) | ||||
Session().commit() | ||||
data = { | ||||
'msg': 'Updated pull request `{}`'.format( | ||||
pull_request.pull_request_id), | ||||
'pull_request': pull_request.get_api_data(), | ||||
'updated_commits': commit_changes, | ||||
'updated_reviewers': reviewers_changes | ||||
} | ||||
Martin Bornhold
|
r1077 | |||
r1 | return data | |||
r1792 | ||||
@jsonrpc_method() | ||||
def close_pull_request( | ||||
r2395 | request, apiuser, pullrequestid, repoid=Optional(None), | |||
r1792 | userid=Optional(OAttr('apiuser')), message=Optional('')): | |||
""" | ||||
Close the pull request specified by `pullrequestid`. | ||||
:param apiuser: This is filled automatically from the |authtoken|. | ||||
:type apiuser: AuthUser | ||||
:param repoid: Repository name or repository ID to which the pull | ||||
request belongs. | ||||
:type repoid: str or int | ||||
:param pullrequestid: ID of the pull request to be closed. | ||||
:type pullrequestid: int | ||||
:param userid: Close the pull request as this user. | ||||
:type userid: Optional(str or int) | ||||
:param message: Optional message to close the Pull Request with. If not | ||||
specified it will be generated automatically. | ||||
:type message: Optional(str) | ||||
Example output: | ||||
.. code-block:: bash | ||||
"id": <id_given_in_input>, | ||||
"result": { | ||||
"pull_request_id": "<int>", | ||||
"close_status": "<str:status_lbl>, | ||||
"closed": "<bool>" | ||||
}, | ||||
"error": null | ||||
""" | ||||
_ = request.translate | ||||
r2395 | pull_request = get_pull_request_or_error(pullrequestid) | |||
if Optional.extract(repoid): | ||||
repo = get_repo_or_error(repoid) | ||||
else: | ||||
repo = pull_request.target_repo | ||||
r1792 | if not isinstance(userid, Optional): | |||
if (has_superadmin_permission(apiuser) or | ||||
HasRepoPermissionAnyApi('repository.admin')( | ||||
user=apiuser, repo_name=repo.repo_name)): | ||||
apiuser = get_user_or_error(userid) | ||||
else: | ||||
raise JSONRPCError('userid is not the same as your user') | ||||
if pull_request.is_closed(): | ||||
raise JSONRPCError( | ||||
'pull request `%s` is already closed' % (pullrequestid,)) | ||||
# only owner or admin or person with write permissions | ||||
allowed_to_close = PullRequestModel().check_user_update( | ||||
pull_request, apiuser, api=True) | ||||
if not allowed_to_close: | ||||
raise JSONRPCError( | ||||
'pull request `%s` close failed, no permission to close.' % ( | ||||
pullrequestid,)) | ||||
# message we're using to close the PR, else it's automatically generated | ||||
message = Optional.extract(message) | ||||
# finally close the PR, with proper message comment | ||||
comment, status = PullRequestModel().close_pull_request_with_comment( | ||||
pull_request, apiuser, repo, message=message) | ||||
status_lbl = ChangesetStatus.get_status_lbl(status) | ||||
Session().commit() | ||||
data = { | ||||
'pull_request_id': pull_request.pull_request_id, | ||||
'close_status': status_lbl, | ||||
'closed': True, | ||||
} | ||||
return data | ||||