From 1878ce5dfaea03b1af211759debdb8f2cf7f7c31 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 06 2017 14:15:12 +0000 Subject: Propose another way to log via notification meta-data changes made to a ticket --- diff --git a/pagure/api/issue.py b/pagure/api/issue.py index 31c628a..afc3b18 100644 --- a/pagure/api/issue.py +++ b/pagure/api/issue.py @@ -16,7 +16,6 @@ from sqlalchemy.exc import SQLAlchemyError import pagure import pagure.exceptions import pagure.lib -from pagure.lib import MetaComment from pagure import ( APP, SESSION, is_repo_admin, api_authenticated, urlpattern ) @@ -627,7 +626,6 @@ def api_change_status_issue(repo, issueid, username=None, namespace=None): form.status.data = new_status if form.validate_on_submit(): - mcomment = MetaComment() try: # Update status message = pagure.lib.edit_issue( @@ -637,7 +635,6 @@ def api_change_status_issue(repo, issueid, username=None, namespace=None): close_status=close_status, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment, ) SESSION.commit() if message: @@ -645,16 +642,14 @@ def api_change_status_issue(repo, issueid, username=None, namespace=None): else: output['message'] = 'No changes' - if mcomment.is_set(): - pagure.lib.add_issue_comment( - SESSION, - issue, - comment='@%s updated metadata\n%s' % ( - flask.g.fas_user.username, mcomment.get()), + if message: + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=message, user=flask.g.fas_user.username, - ticketfolder=APP.config['TICKETS_FOLDER'], - notify=False, - notification=True) + ticketfolder=APP.config['TICKETS_FOLDER'] + ) except pagure.exceptions.PagureException as err: raise pagure.exceptions.APIError( 400, error_code=APIERROR.ENOCODE, error=str(err)) @@ -845,7 +840,6 @@ def api_assign_issue(repo, issueid, username=None, namespace=None): if form.validate_on_submit(): assignee = form.assignee.data or None # Create our metadata comment object - mcomment = MetaComment() try: # New comment message = pagure.lib.add_issue_assignee( @@ -854,20 +848,19 @@ def api_assign_issue(repo, issueid, username=None, namespace=None): assignee=assignee, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment ) SESSION.commit() - if mcomment.is_set(): - pagure.lib.add_issue_comment( - SESSION, - issue, - comment='@%s updated metadata\n%s' % ( - flask.g.fas_user.username, mcomment.get()), + if message: + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=message, user=flask.g.fas_user.username, - ticketfolder=APP.config['TICKETS_FOLDER'], - notify=False, - notification=True) - output['message'] = message + ticketfolder=APP.config['TICKETS_FOLDER'] + ) + output['message'] = message + else: + output['message'] = 'Nothing to change' except pagure.exceptions.PagureException as err: # pragma: no cover raise pagure.exceptions.APIError( 400, error_code=APIERROR.ENOCODE, error=str(err)) @@ -1078,13 +1071,19 @@ def api_update_custom_field( raise pagure.exceptions.APIError( 400, error_code=APIERROR.EINVALIDISSUEFIELD_LINK) try: - mcomment = MetaComment() message = pagure.lib.set_custom_key_value( - SESSION, issue, key, value, mcomment=mcomment) + SESSION, issue, key, value) SESSION.commit() if message: output['message'] = message + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=message, + user=flask.g.fas_user.username, + ticketfolder=APP.config['TICKETS_FOLDER'] + ) else: output['message'] = 'No changes' except pagure.exceptions.PagureException as err: @@ -1095,16 +1094,15 @@ def api_update_custom_field( SESSION.rollback() raise pagure.exceptions.APIError(400, error_code=APIERROR.EDBERROR) - if mcomment.is_set(): - pagure.lib.add_issue_comment( - SESSION, - issue, - comment='@%s updated metadata\n%s' % ( - flask.g.fas_user.username, mcomment.get()), + + if message: + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=message, user=flask.g.fas_user.username, - ticketfolder=None, - notify=False, - notification=True) + ticketfolder=APP.config['TICKETS_FOLDER'] + ) jsonout = flask.jsonify(output) return jsonout diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 5e099c2..56959fb 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -56,41 +56,6 @@ PAGURE_CI = None LOG = None -class MetaComment(): - ''' This class store/builds comments about metadata updates that can be - displayed in a single notification comment in the UI. ''' - def __init__(self): - ''' Initialize the comment ''' - self.comment = "" - - def add(self, field, old_value=None, new_value=None): - ''' Add a new comment about the changes to this field. If the value is - not set/empty we still need to pass a string to markdown or else the - formatting is wrong. So in this case we use "" - ''' - if not old_value: - old_value = '""' - if not new_value: - new_value = '""' - - self.comment += ("**%s** changed from ``%s`` to ``%s``\n" % - (field, old_value, new_value)) - - def add_as_is(self, comment): - ''' Add a comment as is ''' - self.comment += "%s\n" % (comment) - - def is_set(self): - if self.comment != "": - return True - else: - return False - - def get(self): - ''' Return the full comment list ''' - return self.comment - - def set_redis(host, port, dbname): """ Set the redis connection with the specified information. """ global REDIS @@ -422,12 +387,12 @@ def add_tag_obj(session, obj, tags, user, ticketfolder): {'added_tags': added_tags})) if added_tags: - return 'Tag added: %s' % ', '.join(added_tags) + return 'Issue tagged with: %s' % ', '.join(added_tags) else: return 'Nothing to add' -def add_issue_assignee(session, issue, assignee, user, ticketfolder, mcomment, +def add_issue_assignee(session, issue, assignee, user, ticketfolder, notify=True): ''' Add an assignee to an issue, in other words, assigned an issue. ''' user_obj = get_user(session, user) @@ -441,7 +406,7 @@ def add_issue_assignee(session, issue, assignee, user, ticketfolder, mcomment, session.commit() pagure.lib.git.update_git( issue, repo=issue.project, repofolder=ticketfolder) - mcomment.add("Assignee", old_assignee.user, None) + if notify: pagure.lib.notify.notify_assigned_issue(issue, None, user_obj) @@ -464,13 +429,12 @@ def add_issue_assignee(session, issue, assignee, user, ticketfolder, mcomment, return 'Assignee reset' elif not assignee and issue.assignee is None: - return 'Nothing to change' + return # Validate the assignee assignee_obj = get_user(session, assignee) if issue.assignee_id != assignee_obj.id: - mcomment.add("Assignee", old_assignee, assignee_obj.user) issue.assignee_id = assignee_obj.id session.add(issue) session.flush() @@ -499,9 +463,7 @@ def add_issue_assignee(session, issue, assignee, user, ticketfolder, mcomment, REDIS.publish('pagure.%s' % issue.uid, json.dumps( {'assigned': assignee_obj.to_json(public=True)})) - return 'Issue assigned' - else: - return "" + return 'Issue assigned to %s' % assignee def add_pull_request_assignee( @@ -618,7 +580,7 @@ def add_issue_dependency( 'type': 'parent', })) - return 'Dependency added' + return 'Issue marked as depending on: #%s' % issue_blocked.id def remove_issue_dependency( @@ -678,7 +640,8 @@ def remove_issue_dependency( 'type': 'parent', })) - return 'Dependency removed' + return 'Issue **un**marked as depending on: #%s' % ' #'.join( + [str(id) for id in child_del]) def remove_tags(session, project, tags, ticketfolder, user): @@ -699,7 +662,7 @@ def remove_tags(session, project, tags, ticketfolder, user): if tagobj: tag_found = True removed_tags.append(tag) - msgs.append('Removed tag: %s' % tag) + msgs.append('Issue **un**tagged with: %s' % tag) session.delete(tagobj) if not tag_found: @@ -770,7 +733,7 @@ def remove_tags_obj(session, obj, tags, ticketfolder, user): REDIS.publish('pagure.%s' % obj.uid, json.dumps( {'removed_tags': removed_tags})) - return 'Removed tag: %s' % ', '.join(removed_tags) + return 'Issue **un**tagged with: %s' % ', '.join(removed_tags) def edit_issue_tags( @@ -1436,7 +1399,7 @@ def new_tag(session, tag_name, tag_description, tag_color, project_id): return tagobj -def edit_issue(session, issue, ticketfolder, user, mcomment, repo=None, +def edit_issue(session, issue, ticketfolder, user, repo=None, title=None, content=None, status=None, close_status=-1, priority=None, milestone=-1, private=False): ''' Edit the specified issue. @@ -1450,15 +1413,16 @@ def edit_issue(session, issue, ticketfolder, user, mcomment, repo=None, 'depending that are still open.') edit = [] + messages = [] if title and title != issue.title: - mcomment.add("Title", issue.title, title) issue.title = title edit.append('title') + messages.append('Issue title edited') if content and content != issue.content: issue.content = content edit.append('content') + messages.append('Issue description edited') if status and status != issue.status: - mcomment.add("Status", issue.status, status) issue.status = status if status.lower() != 'open': issue.closed_at = datetime.datetime.utcnow() @@ -1466,28 +1430,28 @@ def edit_issue(session, issue, ticketfolder, user, mcomment, repo=None, issue.close_status = None edit.append('close_status') edit.append('status') + messages.append('Issue status updated to: %s' % status) if close_status != -1 and close_status != issue.close_status: - mcomment.add("Closed as", issue.close_status, close_status) issue.close_status = close_status edit.append('close_status') + messages.append('Issue close_status updated to: %s' % close_status) if priority: try: priority = int(priority) except: priority = None if priority != issue.priority: - mcomment.add("Priority", repo.priorities[str(issue.priority)], - repo.priorities[str(priority)]) issue.priority = priority edit.append('priority') + messages.append('Issue priority set to: %s' % priority) if private in [True, False] and private != issue.private: - mcomment.add("Private", str(issue.private), str(private)) issue.private = private edit.append('private') + messages.append('Issue private status set to: %s' % private) if milestone != -1 and milestone != issue.milestone: - mcomment.add("Milestone", issue.milestone, milestone) issue.milestone = milestone edit.append('milestone') + messages.append('Issue set to the milestone: %s' % milestone) issue.last_updated = datetime.datetime.utcnow() # uniquify the list of edited fields edit = list(set(edit)) @@ -1527,9 +1491,7 @@ def edit_issue(session, issue, ticketfolder, user, mcomment, repo=None, if edit: session.add(issue) session.flush() - return 'Successfully edited issue #%s' % issue.id - else: - return "" + return messages def update_project_settings(session, repo, settings, user): @@ -2514,7 +2476,7 @@ def avatar_url_from_email(email, size=64, default='retro', dns=False): hashhex, query) -def update_tags(session, obj, tags, username, ticketfolder, mcomment): +def update_tags(session, obj, tags, username, ticketfolder): """ Update the tags of a specified object (adding or removing them). This object can be either an issue or a project. @@ -2526,36 +2488,24 @@ def update_tags(session, obj, tags, username, ticketfolder, mcomment): torm = set(obj.tags_text) - set(tags) messages = [] if toadd: - messages.append( - add_tag_obj( - session, - obj=obj, - tags=toadd, - user=username, - ticketfolder=ticketfolder, - ) + add_tag_obj( + session, + obj=obj, + tags=toadd, + user=username, + ticketfolder=ticketfolder, ) - if len(toadd) == 1: - mcomment.add_as_is("**Tags** added tag: ``%s``" % list(toadd)[0]) - else: - mcomment.add_as_is("**Tags** added tags: ``" + ', '.join(toadd) + - "``") + messages.append('Issue tagged with: %s' % ', '.join(toadd)) if torm: - messages.append( - remove_tags_obj( - session, - obj=obj, - tags=torm, - user=username, - ticketfolder=ticketfolder, - ) + remove_tags_obj( + session, + obj=obj, + tags=torm, + user=username, + ticketfolder=ticketfolder, ) - if len(torm) == 1: - mcomment.add_as_is("**Tags** removed tag: ``%s``" % list(torm)[0]) - else: - mcomment.add_as_is("**Tags** removed tags: ``" + - ", ".join(torm) + "``") + messages.append('Issue **un**tagged with: %s' % ', '.join(torm)) session.commit() @@ -2563,7 +2513,7 @@ def update_tags(session, obj, tags, username, ticketfolder, mcomment): def update_dependency_issue( - session, repo, issue, depends, username, ticketfolder, mcomment): + session, repo, issue, depends, username, ticketfolder): """ Update the dependency of a specified issue (adding or removing them) """ @@ -2576,9 +2526,8 @@ def update_dependency_issue( comment = "" # Add issue depending - if toadd: - comment += "**Depends on** added dependencies: ``" for depend in toadd: + messages.append("Issue marked as depending on: #%s" % depend) issue_depend = search_issues(session, repo, issueid=depend) if issue_depend is None: continue @@ -2586,26 +2535,17 @@ def update_dependency_issue( # we should never be in this case but better safe than sorry... continue - messages.append( - add_issue_dependency( - session, - issue=issue_depend, - issue_blocked=issue, - user=username, - ticketfolder=ticketfolder, - ) + add_issue_dependency( + session, + issue=issue_depend, + issue_blocked=issue, + user=username, + ticketfolder=ticketfolder, ) - comment += "%s " % (depend) - - if comment: - comment += "``" - mcomment.add_as_is(comment) - comment = "" # Remove issue depending - if torm: - comment += "**Depends on** removed issue dependencies: ``" for depend in torm: + messages.append("Issue **un**marked as depending on: #%s" % depend) issue_depend = search_issues(session, repo, issueid=depend) if issue_depend is None: # pragma: no cover # We cannot test this as it would mean we managed to put in an @@ -2615,27 +2555,20 @@ def update_dependency_issue( # we should never be in this case but better safe than sorry... continue - messages.append( - remove_issue_dependency( - session, - issue=issue, - issue_blocked=issue_depend, - user=username, - ticketfolder=ticketfolder, - ) + remove_issue_dependency( + session, + issue=issue, + issue_blocked=issue_depend, + user=username, + ticketfolder=ticketfolder, ) - comment += "%s " % (depend) - - if comment: - comment += "``" - mcomment.add_as_is(comment) session.commit() return messages def update_blocked_issue( - session, repo, issue, blocks, username, ticketfolder, mcomment): + session, repo, issue, blocks, username, ticketfolder): """ Update the upstream dependency of a specified issue (adding or removing them) @@ -2646,12 +2579,10 @@ def update_blocked_issue( toadd = set(blocks) - set(issue.blocks_text) torm = set(issue.blocks_text) - set(blocks) messages = [] - comment = "" # Add issue blocked - if toadd: - comment += "**Blocked** added blocking issue dependencies: ``" for block in toadd: + messages.append("Issue marked as blocked by: #%s" % block) issue_block = search_issues(session, repo, issueid=block) if issue_block is None: continue @@ -2659,27 +2590,19 @@ def update_blocked_issue( # we should never be in this case but better safe than sorry... continue - messages.append( - add_issue_dependency( - session, - issue=issue, - issue_blocked=issue_block, - user=username, - ticketfolder=ticketfolder, - ) + + add_issue_dependency( + session, + issue=issue, + issue_blocked=issue_block, + user=username, + ticketfolder=ticketfolder, ) session.commit() - comment += "%s " % (block) - - if comment: - comment += "``" - mcomment.add_as_is(comment) - comment = "" # Remove issue blocked - if torm: - comment += "**Blocked** remove blocking issue dependencies: ``" for block in torm: + messages.append("Issue **un**marked as blocked by: #%s" % block) issue_block = search_issues(session, repo, issueid=block) if issue_block is None: # pragma: no cover # We cannot test this as it would mean we managed to put in an @@ -2690,20 +2613,13 @@ def update_blocked_issue( # we should never be in this case but better safe than sorry... continue - messages.append( - remove_issue_dependency( - session, - issue=issue_block, - issue_blocked=issue, - user=username, - ticketfolder=ticketfolder, - ) + remove_issue_dependency( + session, + issue=issue_block, + issue_blocked=issue, + user=username, + ticketfolder=ticketfolder, ) - comment += "%s " % (block) - - if comment: - comment += "``" - mcomment.add_as_is(comment) session.commit() return messages @@ -3592,7 +3508,7 @@ def set_custom_key_fields(session, project, fields, types, data): return 'List of custom fields updated' -def set_custom_key_value(session, issue, key, value, mcomment): +def set_custom_key_value(session, issue, key, value): """ Set or update the value of the specified custom key. """ @@ -3631,9 +3547,6 @@ def set_custom_key_value(session, issue, key, value, mcomment): if not delete: session.add(current_field) - if updated: - mcomment.add(key.name, old_value, value) - if REDIS and updated: if issue.private: REDIS.publish('pagure.%s' % issue.uid, json.dumps({ @@ -3646,7 +3559,10 @@ def set_custom_key_value(session, issue, key, value, mcomment): 'issue': issue.to_json(public=True, with_comments=False), })) - return 'Custom field adjusted' + if value: + return 'Custom field %s adjusted to %s' % (key.name, value) + else: + return 'Custom field %s reset' % key.name def get_yearly_stats_user(session, user, date): @@ -3768,3 +3684,31 @@ def get_active_milestones(session, project): ) return sorted([item[0] for item in query.distinct()]) + + +def add_metadata_update_notif(session, issue, messages, user, ticketfolder): + ''' Add a notification to the specified issue with the given messages + which should reflect changes made to the meta-data of the issue. + ''' + if not messages: + return + + if not isinstance(messages, (list, set)): + messages = [messages] + + user_id = None + if user: + user_obj = get_user(session, user) + user_id = user_obj.id + + issue_comment = model.IssueComment( + issue_uid=issue.uid, + comment='Metadata Update:\n- %s' % ('\n- '.join(messages)), + user_id=user_id, + notification=True, + ) + issue.last_updated = datetime.datetime.utcnow() + session.add(issue) + session.add(issue_comment) + # Make sure we won't have SQLAlchemy error before we continue + session.commit() diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 21edd2b..53c548b 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -28,7 +28,6 @@ import pygit2 import werkzeug from sqlalchemy.exc import SQLAlchemyError -from .__init__ import MetaComment import pagure import pagure.exceptions import pagure.lib @@ -426,7 +425,7 @@ def get_project_from_json( return project -def update_custom_field_from_json(session, repo, issue, json_data, mcomment): +def update_custom_field_from_json(session, repo, issue, json_data): ''' Update the custom fields according to the custom fields of the issue. If the custom field is not present for the repo in it's settings, this will create them. @@ -467,13 +466,20 @@ def update_custom_field_from_json(session, repo, issue, json_data, mcomment): value = new_key.get('value') if value: value = value.strip() - pagure.lib.set_custom_key_value( + messages = pagure.lib.set_custom_key_value( session, issue=issue, key=key_obj, value=value, - mcomment=mcomment ) + if messages: + pagure.lib.add_metadata_update_notif( + session=session, + issue=issue, + messages=messages, + user=None, + ticketfolder=None + ) try: session.commit() except SQLAlchemyError: @@ -501,9 +507,9 @@ def update_ticket_from_git( reponame, username, namespace)) user = get_user_from_json(session, json_data) - mcomment = MetaComment() issue = pagure.lib.get_issue_by_uid(session, issue_uid=issue_uid) + messages = [] if not issue: # Create new issue pagure.lib.new_issue( @@ -525,7 +531,7 @@ def update_ticket_from_git( else: # Edit existing issue - pagure.lib.edit_issue( + messages.extend(pagure.lib.edit_issue( session, issue=issue, ticketfolder=None, @@ -535,8 +541,8 @@ def update_ticket_from_git( status=json_data.get('status'), close_status=json_data.get('close_status'), private=json_data.get('private'), - mcomment=mcomment - ) + )) + session.commit() issue = pagure.lib.get_issue_by_uid(session, issue_uid=issue_uid) @@ -546,7 +552,6 @@ def update_ticket_from_git( repo=repo, issue=issue, json_data=json_data, - mcomment=mcomment ) # Update milestone @@ -564,14 +569,15 @@ def update_ticket_from_git( except SQLAlchemyError: session.rollback() try: - msg = pagure.lib.edit_issue( + msgs = pagure.lib.edit_issue( session, issue=issue, ticketfolder=None, user=user.username, milestone=milestone, - mcomment=mcomment ) + if msgs: + messages.extend(msgs) except SQLAlchemyError: session.rollback() @@ -589,28 +595,27 @@ def update_ticket_from_git( # Update tags tags = json_data.get('tags', []) - pagure.lib.update_tags( - session, issue, tags, username=user.user, ticketfolder=None, - mcomment=mcomment) + messages.extend(pagure.lib.update_tags( + session, issue, tags, username=user.user, ticketfolder=None)) # Update assignee assignee = get_user_from_json(session, json_data, key='assignee') if assignee: - pagure.lib.add_issue_assignee( - session, issue, assignee.username, mcomment=mcomment, - user=user.user, ticketfolder=None, notify=False) + messages.append(pagure.lib.add_issue_assignee( + session, issue, assignee.username, + user=user.user, ticketfolder=None, notify=False)) # Update depends depends = json_data.get('depends', []) - pagure.lib.update_dependency_issue( + messages.extend(pagure.lib.update_dependency_issue( session, issue.project, issue, depends, - username=user.user, ticketfolder=None, mcomment=mcomment) + username=user.user, ticketfolder=None)) # Update blocks blocks = json_data.get('blocks', []) - pagure.lib.update_blocked_issue( + messages.extend(pagure.lib.update_blocked_issue( session, issue.project, issue, blocks, - username=user.user, ticketfolder=None, mcomment=mcomment) + username=user.user, ticketfolder=None)) for comment in json_data['comments']: user = get_user_from_json(session, comment) @@ -627,17 +632,14 @@ def update_ticket_from_git( date_created=datetime.datetime.utcfromtimestamp( float(comment['date_created'])), ) - if mcomment.is_set(): - pagure.lib.add_issue_comment( - session, - issue, - comment='@%s updated metadata\n%s' % ( - user.username, mcomment.get()), + if messages: + pagure.lib.add_metadata_update_notif( + session=session, + issue=issue, + messages=messages, user=user.username, - ticketfolder=None, - notify=False, - notification=True) - + ticketfolder=None + ) session.commit() diff --git a/pagure/templates/issue.html b/pagure/templates/issue.html index d0933d9..fea354b 100644 --- a/pagure/templates/issue.html +++ b/pagure/templates/issue.html @@ -719,7 +719,9 @@ function try_async_comment(form) { {% if authenticated and g.repo_admin %} function take_issue(){ - var _url = "{{ url_for('api_ns.api_assign_issue', repo=repo.name, username=username, issueid=issueid) }}"; + var _url = "{{ url_for('api_ns.api_assign_issue', + repo=repo.name, namespace=repo.namespace, username=username, + issueid=issueid) }}"; var _data = {assignee: "{{ g.fas_user.username }}"}; $.post (_url, _data ).done( function(data) { @@ -741,9 +743,9 @@ function take_issue(){ or issue.user.user == g.fas_user.username or issue.assignee.user == g.fas_user.username) %} function drop_issue(){ - var _url = "{{ url_for( - 'api_ns.api_assign_issue', repo=repo.name, username=username, issueid=issueid - ) }}"; + var _url = "{{ url_for('api_ns.api_assign_issue', + repo=repo.name, namespace=repo.namespace, username=username, + issueid=issueid) }}"; var _data = {assignee: ""}; $.post( _url, _data ).done( function(data) { diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 599f785..b436ff4 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -34,7 +34,6 @@ import pagure.doc_utils import pagure.exceptions import pagure.lib import pagure.lib.encoding_utils -from pagure.lib import MetaComment import pagure.forms from pagure import (APP, SESSION, LOG, __get_file_in_tree, login_required, authenticated, urlpattern) @@ -206,32 +205,27 @@ def update_issue(repo, issueid, username=None, namespace=None): ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() - if message and not is_js: - messages.add(message) - - # Create our metadata comment object which is used to track and - # report of what metadata fields were updated. This is then turned - # into a notificaion comment in the Issue. - mcomment = MetaComment() + if not is_js: + if message: + messages.add(message) # The status field can be updated by both the admin and the # person who opened the ticket. # Update status if repo_admin or flask.g.fas_user.username == issue.user.user: if new_status in status: - message = pagure.lib.edit_issue( + msgs = pagure.lib.edit_issue( SESSION, issue=issue, status=new_status, close_status=close_status, private=issue.private, user=flask.g.fas_user.username, - mcomment=mcomment, ticketfolder=APP.config['TICKETS_FOLDER'] ) SESSION.commit() - if message: - messages.add(message) + if msgs: + messages = messages.union(set(msgs)) # All the other meta-data can be changed only by admins # while other field will be missing for non-admin and thus @@ -242,9 +236,8 @@ def update_issue(repo, issueid, username=None, namespace=None): SESSION, issue, tags, username=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment ) - messages.union(set(msgs)) + messages = messages.union(set(msgs)) # The meta-data can be changed by admins and issue creator, # where issue creators can only change status of their issue while @@ -258,7 +251,6 @@ def update_issue(repo, issueid, username=None, namespace=None): assignee=assignee or None, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment ) SESSION.commit() if message and message != 'Nothing to change': @@ -269,7 +261,7 @@ def update_issue(repo, issueid, username=None, namespace=None): new_priority = None # Update core metadata - message = pagure.lib.edit_issue( + msgs = pagure.lib.edit_issue( SESSION, repo=repo, issue=issue, @@ -278,11 +270,10 @@ def update_issue(repo, issueid, username=None, namespace=None): private=form.private.data, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment ) SESSION.commit() - if message: - messages.add(message) + if msgs: + messages = messages.union(set(msgs)) # Update the custom keys/fields for key in repo.issue_keys: @@ -299,15 +290,17 @@ def update_issue(repo, issueid, username=None, namespace=None): '(%s) has invalid url (%s) ' % (key.name, link)) - pagure.lib.set_custom_key_value( - SESSION, issue, key, value, mcomment=mcomment) + msg = pagure.lib.set_custom_key_value( + SESSION, issue, key, value) + if msg: + messages.add(msg) # Update ticket this one depends on msgs = pagure.lib.update_dependency_issue( SESSION, repo, issue, depends, username=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment) + ) messages.union(set(msgs)) # Update ticket(s) depending on this one @@ -315,26 +308,25 @@ def update_issue(repo, issueid, username=None, namespace=None): SESSION, repo, issue, blocks, username=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], - mcomment=mcomment) + ) messages.union(set(msgs)) - # Add the comment for field updates: - if mcomment.is_set(): - pagure.lib.add_issue_comment( - SESSION, - issue, - comment='@%s updated metadata\n%s' % ( - flask.g.fas_user.username, mcomment.get()), - user=flask.g.fas_user.username, - ticketfolder=APP.config['TICKETS_FOLDER'], - notify=False, - notification=True) - messages.add('Metadata fields updated') - if not is_js: for message in messages: flask.flash(message) + # Add the comment for field updates: + if messages: + not_needed = set(['Comment added', 'Updated comment']) + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=messages - not_needed, + user=flask.g.fas_user.username, + ticketfolder=APP.config['TICKETS_FOLDER'] + ) + messages.add('Metadata fields updated') + except pagure.exceptions.PagureException as err: is_js = False SESSION.rollback() @@ -1098,13 +1090,8 @@ def edit_issue(repo, issueid, username=None, namespace=None): 404, 'No such user found in the database: %s' % ( flask.g.fas_user.username)) - # Create our metadata comment object which is used to track and - # report of what metadata fields were updated. This is then turned - # into a notificaion comment in the Issue. - mcomment = MetaComment() - try: - message = pagure.lib.edit_issue( + messages = pagure.lib.edit_issue( SESSION, issue=issue, title=title, @@ -1113,19 +1100,16 @@ def edit_issue(repo, issueid, username=None, namespace=None): user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], private=private, - mcomment=mcomment ) SESSION.commit() - if mcomment.is_set(): - pagure.lib.add_issue_comment( - SESSION, - issue, - comment='@%s updated metadata\n%s' % ( - flask.g.fas_user.username, mcomment.get()), + if messages: + pagure.lib.add_metadata_update_notif( + session=SESSION, + issue=issue, + messages=messages, user=flask.g.fas_user.username, - ticketfolder=APP.config['TICKETS_FOLDER'], - notify=False, - notification=True) + ticketfolder=APP.config['TICKETS_FOLDER'] + ) # If there is a file attached, attach it. filestream = flask.request.files.get('filestream') @@ -1153,7 +1137,8 @@ def edit_issue(repo, issueid, username=None, namespace=None): issue.content = issue.content.replace('', url) SESSION.add(issue) SESSION.commit() - flask.flash(message) + for message in messages: + flask.flash(message) url = flask.url_for( 'view_issue', username=username, namespace=namespace, repo=repo.name, issueid=issueid) diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index b6148bf..9684ce7 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -50,7 +50,6 @@ import pagure.ui.plugins from pagure import (APP, SESSION, LOG, __get_file_in_tree, login_required, admin_session_timedout) from pagure.lib import encoding_utils -from pagure.lib import MetaComment @APP.route('/.git') @@ -1117,16 +1116,16 @@ def update_project(repo, username=None, namespace=None): form = pagure.forms.ProjectFormSimplified() if form.validate_on_submit(): - mcomment = MetaComment() + try: repo.description = form.description.data repo.avatar_email = form.avatar_email.data.strip() repo.url = form.url.data.strip() - pagure.lib.update_tags( + messages = pagure.lib.update_tags( SESSION, repo, tags=[t.strip() for t in form.tags.data.split(',')], username=flask.g.fas_user.username, - ticketfolder=None, mcomment=mcomment + ticketfolder=None, ) SESSION.add(repo) SESSION.commit()