From dc21df88b5f2408f0a9cb4081ae6f6cc8007a1e9 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 13 2016 11:36:35 +0000 Subject: Add a new API endpoint to fork a project --- diff --git a/pagure/api/project.py b/pagure/api/project.py index 1fe9d02..bd5aa04 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -266,3 +266,83 @@ def api_new_project(): jsonout = flask.jsonify(output) return jsonout + + +@API.route('/fork/', methods=['POST']) +@API.route('/fork', methods=['POST']) +@api_login_required(acls=['fork_project']) +@api_method +def api_fork_project(): + """ + Fork a project + -------------------- + Fork a project on this pagure instance. + + :: + + POST /api/0//fork + + + Input + ^^^^^ + + +------------------+---------+--------------+---------------------------+ + | Key | Type | Optionality | Description | + +==================+=========+==============+===========================+ + | ``repo`` | string | Mandatory | | The name of the project | + | | | | to fork. | + +------------------+---------+--------------+---------------------------+ + | ``username`` | string | Optional | | The username of the user| + | | | | of the fork. | + +------------------+---------+--------------+---------------------------+ + + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "message": 'Repo "test" cloned to "pingou/test"' + } + + """ + output = {} + + form = pagure.forms.ForkRepoForm(csrf_enabled=False) + if form.validate_on_submit(): + repo = form.repo.data + username = form.username.data or None + + repo = pagure.lib.get_project(SESSION, repo, user=username) + if repo is None: + raise pagure.exceptions.APIError( + 404, error_code=APIERROR.ENOPROJECT) + + try: + message = pagure.lib.fork_project( + SESSION, + user=flask.g.fas_user.username, + repo=repo, + gitfolder=APP.config['GIT_FOLDER'], + docfolder=APP.config['DOCS_FOLDER'], + ticketfolder=APP.config['TICKETS_FOLDER'], + requestfolder=APP.config['REQUESTS_FOLDER'], + ) + SESSION.commit() + pagure.lib.git.generate_gitolite_acls() + output['message'] = message + except pagure.exceptions.PagureException as err: + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.ENOCODE, error=str(err)) + except SQLAlchemyError as err: # pragma: no cover + APP.logger.exception(err) + SESSION.rollback() + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EDBERROR) + else: + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDREQ) + + jsonout = flask.jsonify(output) + return jsonout diff --git a/pagure/forms.py b/pagure/forms.py index 4d8fb47..a9d1e0a 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -410,3 +410,14 @@ class EditCommentForm(wtf.Form): 'Comment*', [wtforms.validators.Required()] ) + + +class ForkRepoForm(wtf.Form): + ''' Form to fork a project in the API. ''' + repo = wtforms.TextField( + 'The project name *', + [wtforms.validators.Required()] + ) + username = wtforms.TextField( + 'User who forked the project', + [wtforms.validators.optional()])