From b31474ce1aa5cdd1f450a786128b601e0d2465fb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 26 2014 09:47:46 +0000 Subject: Move the fork related logic from the do_ methods into the routing methods Signed-off-by: Pierre-Yves Chibon --- diff --git a/progit/fork.py b/progit/fork.py index 5a66793..1498df8 100644 --- a/progit/fork.py +++ b/progit/fork.py @@ -27,84 +27,41 @@ import progit.forms from progit import APP, SESSION, LOG, __get_file_in_tree, cla_required -def do_merge_request_pull(repo, requestid, username=None): - """ Merge a request pulling the changes from the fork into the project. +@APP.route('//request-pulls') +@APP.route('/fork///request-pulls') +def request_pulls(repo, username=None): + """ Request pulling the changes from the fork into the project. """ + status = flask.request.args.get('status', True) repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') - request = progit.lib.get_pull_request( - SESSION, project_id=repo.id, requestid=requestid) - - if not request: - flask.abort(404, 'Pull-request not found') - - error_output = flask.url_for( - 'request_pull', repo=repo.name, requestid=requestid) - if username: - error_output = flask.url_for( - 'fork_request_pull', - repo=repo.name, - requestid=requestid, - username=username) - - # Get the fork - repopath = os.path.join( - APP.config['FORK_FOLDER'], request.repo_from.path) - fork_obj = pygit2.Repository(repopath) - - # Get the original repo - parentpath = os.path.join(APP.config['GIT_FOLDER'], request.repo.path) - orig_repo = pygit2.Repository(parentpath) - - if orig_repo.get(request.stop_id, None): - flask.flash('These chanages have already been merged.', 'error') - # Update status - progit.lib.close_pull_request(SESSION, request) - SESSION.commit() - return flask.redirect(error_output) - - # Clone the original repo into a temp folder - newpath = tempfile.mkdtemp() - new_repo = pygit2.clone_repository(parentpath, newpath) - - repo_commit = fork_obj[request.stop_id] - - ori_remote = new_repo.remotes[0] - # Add the fork as remote repo - reponame = '%s_%s' % (request.user, repo.name) - remote = new_repo.create_remote(reponame, repopath) - - # Fetch the commits - remote.fetch() - - merge = new_repo.merge(repo_commit.oid) - master_ref = new_repo.lookup_reference('HEAD').resolve() - - if merge.is_fastforward: - master_ref.target = merge.fastforward_oid - refname = '%s:%s' % (master_ref.name, master_ref.name) - ori_remote.push(refname) - flask.flash('Changes merged!') + if status is False or str(status).lower() == 'closed': + requests = progit.lib.get_pull_requests( + SESSION, project_id=repo.id, status=False) else: - flask.flash( - 'This merge is not fast-forward and cannot be applied via ' - 'progit', 'error') - flask.redirect(error_output) - - # Update status - progit.lib.close_pull_request(SESSION, request) - SESSION.commit() + requests = progit.lib.get_pull_requests( + SESSION, project_id=repo.id, status=status) - return flask.redirect(flask.url_for('view_repo', repo=repo.name)) + return flask.render_template( + 'requests.html', + select='requests', + repo=repo, + username=username, + requests=requests, + status=status, + ) -def do_request_pull(repo, requestid, username=None): +@APP.route('//request-pull/') +@APP.route('/fork///request-pull/') +def request_pull(repo, requestid, username=None): """ Request pulling the changes from the fork into the project. """ + repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: @@ -177,57 +134,80 @@ def do_request_pull(repo, requestid, username=None): ) -def do_request_pulls(repo, username=None, status=True): - """ Returns the list of pull-requests opened on a project. +@APP.route('//request-pull/merge/') +@APP.route('/fork///request-pull/merge/') +def merge_request_pull(repo, requestid, username=None): + """ Request pulling the changes from the fork into the project. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') - if status is False or str(status).lower() == 'closed': - requests = progit.lib.get_pull_requests( - SESSION, project_id=repo.id, status=False) - else: - requests = progit.lib.get_pull_requests( - SESSION, project_id=repo.id, status=status) + request = progit.lib.get_pull_request( + SESSION, project_id=repo.id, requestid=requestid) - return flask.render_template( - 'requests.html', - select='requests', - repo=repo, - username=username, - requests=requests, - status=status, - ) + if not request: + flask.abort(404, 'Pull-request not found') + error_output = flask.url_for( + 'request_pull', repo=repo.name, requestid=requestid) + if username: + error_output = flask.url_for( + 'fork_request_pull', + repo=repo.name, + requestid=requestid, + username=username) -## URLs + # Get the fork + repopath = os.path.join( + APP.config['FORK_FOLDER'], request.repo_from.path) + fork_obj = pygit2.Repository(repopath) + # Get the original repo + parentpath = os.path.join(APP.config['GIT_FOLDER'], request.repo.path) + orig_repo = pygit2.Repository(parentpath) -@APP.route('//request-pulls') -@APP.route('/fork///request-pulls') -def request_pulls(repo, username=None): - """ Request pulling the changes from the fork into the project. - """ - status = flask.request.args.get('status', True) - return do_request_pulls(repo, status=status, username=username) + if orig_repo.get(request.stop_id, None): + flask.flash('These chanages have already been merged.', 'error') + # Update status + progit.lib.close_pull_request(SESSION, request) + SESSION.commit() + return flask.redirect(error_output) + # Clone the original repo into a temp folder + newpath = tempfile.mkdtemp() + new_repo = pygit2.clone_repository(parentpath, newpath) -@APP.route('//request-pull/') -@APP.route('/fork///request-pull/') -def request_pull(repo, requestid, username=None): - """ Request pulling the changes from the fork into the project. - """ - return do_request_pull(repo, requestid, username=username) + repo_commit = fork_obj[request.stop_id] + ori_remote = new_repo.remotes[0] + # Add the fork as remote repo + reponame = '%s_%s' % (request.user, repo.name) + remote = new_repo.create_remote(reponame, repopath) -@APP.route('//request-pull/merge/') -@APP.route('/fork///request-pull/merge/') -def merge_request_pull(repo, requestid, username=None): - """ Request pulling the changes from the fork into the project. - """ - return do_merge_request_pull(repo, requestid, username=username) + # Fetch the commits + remote.fetch() + + merge = new_repo.merge(repo_commit.oid) + master_ref = new_repo.lookup_reference('HEAD').resolve() + + if merge.is_fastforward: + master_ref.target = merge.fastforward_oid + refname = '%s:%s' % (master_ref.name, master_ref.name) + ori_remote.push(refname) + flask.flash('Changes merged!') + else: + flask.flash( + 'This merge is not fast-forward and cannot be applied via ' + 'progit', 'error') + flask.redirect(error_output) + + # Update status + progit.lib.close_pull_request(SESSION, request) + SESSION.commit() + + return flask.redirect(flask.url_for('view_repo', repo=repo.name)) ## Specific actions