From 116ab2c62e80ea04080f4032e6ca88554541729e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 1/8] Adjust the pagure EV server to support pull-requests as well --- diff --git a/ev-server/pagure-stream-server.py b/ev-server/pagure-stream-server.py index 490cd55..92f0bf5 100644 --- a/ev-server/pagure-stream-server.py +++ b/ev-server/pagure-stream-server.py @@ -38,11 +38,55 @@ if 'PAGURE_CONFIG' not in os.environ \ import pagure import pagure.lib +from pagure.exceptions import PagureEvException clients = {} +def get_obj_from_path(path): + """ Return the Ticket or Request object based on the path provided. + """ + username = None + if path.startswith('/fork'): + username, repo, obj, objid = path.split('/')[2:6] + else: + repo, obj, objid = path.split('/')[1:4] + + repo = pagure.lib.get_project(pagure.SESSION, repo, user=username) + + if repo is None: + raise PagureEvException("Project '%s' not found" % repo) + + output = None + if obj == 'issue': + if not repo.settings.get('issue_tracker', True): + raise PagureEvException("No issue tracker found for this project") + + output = pagure.lib.search_issues( + pagure.SESSION, repo, issueid=objid) + + if output is None or output.project != repo: + raise PagureEvException("Issue '%s' not found" % objid) + + if output.private: + # TODO: find a way to do auth + raise PagureEvException( + "This issue is private and you are not allowed to view it") + else: + if not repo.settings.get('pull_requests', True): + raise PagureEvException( + "No pull-request tracker found for this project") + + output = pagure.lib.search_pull_requests( + pagure.SESSION, project_id=repo.id, requestid=objid) + + if output is None or output.project != repo: + raise PagureEvException("Pull-Request '%s' not found" % objid) + + return output + + @trollius.coroutine def handle_client(client_reader, client_writer): # give client a chance to respond, timeout after 10 seconds @@ -74,32 +118,10 @@ def handle_client(client_reader, client_writer): "Access-Control-Allow-Origin: *\n\n" ).encode()) - username = None - if url.path.startswith('/fork'): - username, repo, issue, issueid = url.path.split('/')[2:6] - else: - repo, issue, issueid = url.path.split('/')[1:4] - - repo = pagure.lib.get_project(pagure.SESSION, repo, user=username) - - if repo is None: - log.warning("Project '%s' not found" % repo) - return - - if not repo.settings.get('issue_tracker', True): - log.warning("No issue tracker found for this project") - return - - issue = pagure.lib.search_issues(pagure.SESSION, repo, issueid=issueid) - - if issue is None or issue.project != repo: - log.warning("Issue '%s' not found" % issueid) - return - - if issue.private: - # TODO: find a way to do auth - log.warning( - "This issue is private and you are not allowed to view it") + try: + obj = get_obj_from_path(url.path) + except PagureEvException as err: + log.warning(err.message) return try: @@ -112,7 +134,7 @@ def handle_client(client_reader, client_writer): subscriber = yield trollius.From(connection.start_subscribe()) # Subscribe to channel. - yield trollius.From(subscriber.subscribe([issue.uid])) + yield trollius.From(subscriber.subscribe([obj.uid])) # Inside a while loop, wait for incoming events. while True: From 6d72f7033e350181cb8a612061f9ad90dbf4eccc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 2/8] Add a new pagure specific exception to be used by the EV server --- diff --git a/pagure/exceptions.py b/pagure/exceptions.py index 3d8d304..611f25e 100644 --- a/pagure/exceptions.py +++ b/pagure/exceptions.py @@ -44,3 +44,9 @@ class BranchNotFoundException(PagureException): found in a repository. ''' pass + + +class PagureEvException(PagureException): + ''' Exceptions used in the pagure-stream-server. + ''' + pass From 62cec475eb1c47721306235ccb5077d0fa90f7f5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 3/8] Adjust add_pull_request_comment to notify redis of the change that occured This way we can start looking at updating the UI of the PR based on the message in redis --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index e0c2871..f094e9e 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -702,7 +702,8 @@ def add_group_to_project(session, project, new_group, user): def add_pull_request_comment(session, request, commit, filename, row, - comment, user, requestfolder, notify=True): + comment, user, requestfolder, notify=True, + redis=None): ''' Add a comment to a pull-request. ''' user_obj = __get_user(session, user) @@ -724,6 +725,18 @@ def add_pull_request_comment(session, request, commit, filename, row, if notify: pagure.lib.notify.notify_pull_request_comment(pr_comment, user_obj) + if redis: + redis.publish(request.uid, json.dumps({ + 'request_id': len(request.comments), + 'comment_added': text2markdown(pr_comment.comment), + 'comment_user': pr_comment.user.user, + 'avatar_url': avatar_url(pr_comment.user.user, size=16), + 'comment_date': pr_comment.date_created.strftime('%Y-%m-%d %H:%M'), + 'commit_id': commit, + 'filename': filename, + 'line': row, + })) + pagure.lib.notify.log( request.project, topic='pull-request.comment.added', From f41e7ecaf6413110502d188d7282c81d1aeabcdf Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 4/8] Forward the connection to the redis server to add_pull_request_comment --- diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 1c217f2..18e957d 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -20,7 +20,7 @@ import pagure.exceptions import pagure.lib import pagure.lib.git import pagure.forms -from pagure import (APP, SESSION, LOG, cla_required, +from pagure import (APP, REDIS, SESSION, LOG, cla_required, is_repo_admin, generate_gitolite_acls) @@ -291,6 +291,7 @@ def pull_request_add_comment( comment=comment, user=flask.g.fas_user.username, requestfolder=APP.config['REQUESTS_FOLDER'], + redis=REDIS, ) SESSION.commit() flask.flash(message) From 20621e63e9df0903991ea8526126f414a64045a8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 5/8] Add event-source support for commenting on a pull-request --- diff --git a/pagure/static/request_ev.js b/pagure/static/request_ev.js new file mode 100644 index 0000000..a293315 --- /dev/null +++ b/pagure/static/request_ev.js @@ -0,0 +1,50 @@ +add_comment = function(data) { + console.log('Adding comment ' + data.comment_added); + if (data.commit_id){ + // Inline comment + console.log('Inline'); + var _data = ' \ + \ + \ +
\ + ' + + data.comment_user + '' + data.comment_date + '
' + + data.comment_added + + '
'; + var field = $('[data-commit="' + data.commit_id + '"]').parent(); + var id = field.children().children().attr('id').split('_')[0]; + var row = $('#' + id + '_' + (parseInt(data.line) + 1)).parent().parent(); + row.before(_data); + } else { + // Generic comment + console.log('generic'); + var field = $('#request_comment'); + var _data = '
\ +
\ + \ + \ + ' + data.comment_user + '\ + - seconds ago \ + ΒΆ \ + \ +
\ +
\ +

' + data.comment_added + '

\ +
\ +
'; + + field.html(field.html() + _data); + } +} + +process_event = function(data, requestid){ + console.log(data); + if (data.comment_added){ + add_comment(data); + } +} diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html index 581883e..910a139 100644 --- a/pagure/templates/pull_request.html +++ b/pagure/templates/pull_request.html @@ -273,13 +273,14 @@ {% if pull_request and pull_request.comments%} -
+
{% for comment in pull_request.discussion %} {% if not comment.commit_id %} - {{ show_comment(comment, loop.index, repo, username, requestid, form, repo_admin) }} + {{ show_comment(comment, loop.index, repo, username, + requestid, form, repo_admin) }} {% endif %} {% endfor %} {{ mergeform.csrf_token }} @@ -516,14 +517,7 @@ function try_async_comment(form) { .done(function(data) { if(data == 'ok') { // We have submitted the comment correctly - var comment_fld = $(form).find("#comment")[0]; - var comment = comment_fld.value; - var comment_view = "" + - ""; - $(form).closest('tr').before($(comment_view)).remove(); + $(form).remove(); } else { // Make the browser submit the form sync $(form).off('submit'); @@ -540,5 +534,31 @@ function try_async_comment(form) { {% endif %} + + +{% if config['EVENTSOURCE_SOURCE'] %} + + + +{% endif %} + {% endblock %} From 3685cc0d60e0c478d7ff36c2337de0f64addb258 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:54 +0000 Subject: [PATCH 6/8] Avoid duplicate commit identifier in the html --- diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html index 910a139..f002fbe 100644 --- a/pagure/templates/pull_request.html +++ b/pagure/templates/pull_request.html @@ -300,7 +300,7 @@ (supports the Markdown syntax) -
+
  • Comment
  • Preview
  • From 32cff54d6015828cb4d5165f55bb80b1c90ae7e1 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:41:55 +0000 Subject: [PATCH 7/8] Allow async comments for the generic comments as well Only remove the form for in-line comments and reset the comment field all the time. --- diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html index f002fbe..b591aec 100644 --- a/pagure/templates/pull_request.html +++ b/pagure/templates/pull_request.html @@ -293,7 +293,7 @@ + method="post" onsubmit="return try_async_comment(this, false)"> {{ mergeform.csrf_token }}
    @@ -512,12 +512,15 @@ }); {% if authenticated %} -function try_async_comment(form) { +function try_async_comment(form, inline) { $.post( form.action + "?js=1", $(form).serialize() ) .done(function(data) { if(data == 'ok') { // We have submitted the comment correctly - $(form).remove(); + $('#comment').val(''); + if (inline){ + $(form).remove(); + } } else { // Make the browser submit the form sync $(form).off('submit'); diff --git a/pagure/templates/pull_request_comment.html b/pagure/templates/pull_request_comment.html index b2c71c3..08558b5 100644 --- a/pagure/templates/pull_request_comment.html +++ b/pagure/templates/pull_request_comment.html @@ -3,7 +3,7 @@ + method="post" onsubmit="return try_async_comment(this, true)">
    From e07ca8cac527e689b6e61f38fb5f6c1e5a74cf51 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 12:55:15 +0000 Subject: [PATCH 8/8] Adjust the eventsource server to be a little more strict about its input --- diff --git a/ev-server/pagure-stream-server.py b/ev-server/pagure-stream-server.py index 92f0bf5..3b2d206 100644 --- a/ev-server/pagure-stream-server.py +++ b/ev-server/pagure-stream-server.py @@ -73,7 +73,7 @@ def get_obj_from_path(path): # TODO: find a way to do auth raise PagureEvException( "This issue is private and you are not allowed to view it") - else: + elif obj == 'pull-request': if not repo.settings.get('pull_requests', True): raise PagureEvException( "No pull-request tracker found for this project") @@ -84,6 +84,9 @@ def get_obj_from_path(path): if output is None or output.project != repo: raise PagureEvException("Pull-Request '%s' not found" % objid) + else: + raise PagureEvException("Invalid object provided: '%s'" % obj) + return output
" + - "{{ g.fas_user.username }}" + - "Now

" + - comment + "