From 2b1a1422560c1b430975075a74adda498d9bc2d0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 11:28:14 +0000 Subject: [PATCH 1/6] Add an internal API endpoint listing the possible options for PR This returns the branch and the list of commits that differ between this branch and the default one. --- diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py index 4f38d78..b541fc4 100644 --- a/pagure/internal/__init__.py +++ b/pagure/internal/__init__.py @@ -12,6 +12,7 @@ Internal endpoints. import shutil import tempfile +import os import flask import pygit2 @@ -230,3 +231,89 @@ def mergeable_request_pull(): 'code': merge_status, 'short_code': MERGE_OPTIONS[merge_status]['short_code'], 'message': MERGE_OPTIONS[merge_status]['message']}) + + +@PV.route('pull-request/ready', methods=['POST']) +def get_pull_request_ready_branch(): + """ Return the list of branches that have commits not in the main + branch/repo (thus for which one could open a PR) and the number of + commits that differ. + """ + form = pagure.forms.ConfirmationForm() + if not form.validate_on_submit(): + response = flask.jsonify({ + 'code': 'ERROR', + 'message': 'Invalid input submitted', + }) + response.status_code = 400 + return response + + repo = pagure.lib.get_project( + pagure.SESSION, + flask.request.form.get('repo', '').strip() or None, + user=flask.request.form.get('repouser', '').strip() or None) + + if not repo: + response = flask.jsonify({ + 'code': 'ERROR', + 'message': 'No repo found with the information provided', + }) + response.status_code = 404 + return response + + reponame = pagure.get_repo_path(repo) + repo_obj = pygit2.Repository(reponame) + + branches = {} + + for branchname in repo_obj.listall_branches(): + branch = repo_obj.lookup_branch(branchname) + + diff_commits = [] + if repo.is_fork: + parentname = os.path.join( + pagure.APP.config['GIT_FOLDER'], repo.parent.path) + if repo.parent.is_fork: + parentname = os.path.join( + pagure.APP.config['FORK_FOLDER'], repo.parent.path) + else: + parentname = os.path.join(pagure.APP.config['GIT_FOLDER'], repo.path) + + orig_repo = pygit2.Repository(parentname) + + if not repo_obj.is_empty and not orig_repo.is_empty \ + and repo_obj.listall_branches() > 1: + + if not orig_repo.head_is_unborn: + compare_branch = orig_repo.lookup_branch( + orig_repo.head.shorthand) + else: + compare_branch = None + + compare_commits = [] + + if compare_branch: + compare_commits = [ + commit.oid.hex + for commit in orig_repo.walk( + compare_branch.get_object().hex, + pygit2.GIT_SORT_TIME) + ] + + repo_commit = repo_obj[branch.get_object().hex] + + for commit in repo_obj.walk( + repo_commit.oid.hex, pygit2.GIT_SORT_TIME): + if commit.oid.hex in compare_commits: + break + diff_commits.append(commit.oid.hex) + + if diff_commits: + branches[branchname] = diff_commits + + return flask.jsonify( + { + 'code': 'OK', + 'message': branches, + } + ) From 6c40338f28d031221ee0e28832c67c159633106d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 11:30:02 +0000 Subject: [PATCH 2/6] Fix styling and show possible PR if authenticated and admin --- diff --git a/pagure/templates/repo_info.html b/pagure/templates/repo_info.html index 47fbcf7..099a56a 100644 --- a/pagure/templates/repo_info.html +++ b/pagure/templates/repo_info.html @@ -335,17 +335,44 @@ $(document).ready(function() { $(".extra_gits").hide(); }); - $(function() { - $( "#more_gits" ).click( - function() { - if ($( "#more_gits" ).html() == 'more') { - $( "#more_gits" ).html('less'); - } else { - $( "#more_gits" ).html('more'); + $( "#more_gits" ).click(function() { + if ($( "#more_gits" ).html() == 'more') { + $( "#more_gits" ).html('less'); + } else { + $( "#more_gits" ).html('more'); + } + }); + + {% if authenticated and repo_admin %} + $.ajax({ + url: '{{ url_for("internal_ns.get_pull_request_ready_branch") }}' , + type: 'POST', + data: { + repo: "{{ repo.name }}", + repouser: "{{ repo.user.user if repo.is_fork else '' }}", + csrf_token: "{{ form.csrf_token.current_token }}", + }, + dataType: 'json', + success: function(res) { + if (res.code == 'OK'){ + for (branch in res.message){ + html = ''; + $($('.bodycontent').find('.row').children()[0]).before(html) } } - ); + } + }); + {% endif %} }); From 92e3b93337668222ce1f6257817fba98c6ee9f8e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 11:30:02 +0000 Subject: [PATCH 3/6] Show the possible PR on the page listing the PRs --- diff --git a/pagure/templates/requests.html b/pagure/templates/requests.html index d512a9c..956650d 100644 --- a/pagure/templates/requests.html +++ b/pagure/templates/requests.html @@ -21,7 +21,7 @@ Closed - Reset Filters @@ -90,3 +90,42 @@ {% endblock %} + + +{% block jscripts %} +{{ super() }} +{% if authenticated and repo_admin %} + +{% endblock %} diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 9493871..26bc6f9 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -174,6 +174,7 @@ def request_pulls(repo, username=None): assignee=assignee, author=author, repo_admin=is_repo_admin(repo), + form=pagure.forms.ConfirmationForm(), ) From 3c8f18be613095086d57ea7ccf77d7ce037a0934 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 11:30:02 +0000 Subject: [PATCH 4/6] Just style --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 0b10a49..f816c77 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -322,7 +322,8 @@ def view_commits(repo, branchname=None, username=None): and repo_obj.listall_branches() > 1: if not orig_repo.head_is_unborn: - compare_branch = orig_repo.lookup_branch(orig_repo.head.shorthand) + compare_branch = orig_repo.lookup_branch( + orig_repo.head.shorthand) else: compare_branch = None From a6a4eecb156cac997030d597e739a27eef913074 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 11:30:29 +0000 Subject: [PATCH 5/6] Styling and fix the More/Less button for the git URLs --- diff --git a/pagure/templates/repo_info.html b/pagure/templates/repo_info.html index 099a56a..40d598b 100644 --- a/pagure/templates/repo_info.html +++ b/pagure/templates/repo_info.html @@ -111,7 +111,9 @@ git push -u origin master endif %}" style="position:relative;"> {% if diff_commits and commit.oid.hex in diff_commits %}
+ style="position:absolute; left:0; top:0; padding:5px; + padding-top:20px; height:100%; background:#eee; + vertical-align:middle;">
{% endif %} @@ -292,7 +294,12 @@ git push -u origin master {% if last_commits %}
- Recent Commits in {{branchname}} + Recent Commits in + + + {{branchname}} + +
From 29899538aa65718be75e0ea1f38e4f2184d19b8b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 22 2016 15:12:40 +0000 Subject: [PATCH 6/6] Rename variable parentname to parentpath as per @puiterwijk's suggestion --- diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py index b541fc4..ac2aca5 100644 --- a/pagure/internal/__init__.py +++ b/pagure/internal/__init__.py @@ -271,15 +271,16 @@ def get_pull_request_ready_branch(): diff_commits = [] if repo.is_fork: - parentname = os.path.join( + parentpath = os.path.join( pagure.APP.config['GIT_FOLDER'], repo.parent.path) if repo.parent.is_fork: - parentname = os.path.join( + parentpath = os.path.join( pagure.APP.config['FORK_FOLDER'], repo.parent.path) else: - parentname = os.path.join(pagure.APP.config['GIT_FOLDER'], repo.path) + parentpath = os.path.join( + pagure.APP.config['GIT_FOLDER'], repo.path) - orig_repo = pygit2.Repository(parentname) + orig_repo = pygit2.Repository(parentpath) if not repo_obj.is_empty and not orig_repo.is_empty \ and repo_obj.listall_branches() > 1: