Skip to content

Commit

Permalink
System comments
Browse files Browse the repository at this point in the history
Show system comments at bugs.
  • Loading branch information
csordasmarton committed Dec 6, 2019
1 parent a3222fc commit fdbc3fa
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 63 deletions.
8 changes: 7 additions & 1 deletion web/api/v6/report_server.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ enum ExtendedReportDataType {
FIXIT = 20,
}

enum CommentKind {
USER, // User-given comments.
SYSTEM // System events.
}

struct SourceFileData {
1: i64 fileId,
2: string filePath,
Expand Down Expand Up @@ -280,7 +285,8 @@ struct CommentData {
1: i64 id,
2: string author,
3: string message,
4: string createdAt
4: string createdAt,
5: CommentKind kind
}
typedef list<CommentData> CommentDataList

Expand Down
11 changes: 11 additions & 0 deletions web/codechecker_web/shared/webserver_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(self, package_root, pckg_layout, cfg_dict):
self._package_root = package_root
self._severity_map = SeverityMap(
load_json_or_empty(self.checkers_severity_map_file, {}))
self.__system_comment_map = \
load_json_or_empty(self.system_comment_map_file, {})
self.__package_version = None
self.__package_build_date = None
self.__package_git_hash = None
Expand Down Expand Up @@ -110,6 +112,15 @@ def package_git_tag(self):
def version_file(self):
return os.path.join(self._package_root, 'config', 'web_version.json')

@property
def system_comment_map(self):
return self.__system_comment_map

@property
def system_comment_map_file(self):
return os.path.join(self._package_root, 'config',
'system_comment_kinds.json')

@property
def path_plist_to_html_dist(self):
return os.path.join(self.package_root, 'lib', 'python2.7',
Expand Down
5 changes: 5 additions & 0 deletions web/config/system_comment_kinds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rev_st_changed": "%author% changed review status from <b class=\"old-review-status\">{0}</b> to <b class=\"new-review-status\">{1}</b> %date%",
"rev_st_changed_msg": "%author% changed review status from <b class=\"old-review-status\">{0}</b> to <b class=\"new-review-status\">{1}</b> with <i>{2}</i> message %date%",
"comment_changed": "%author% changed comment message from <b class=\"old-comment-message\">{0}</b> to <b class=\"new-comment-message\">{1}</b> %date%"
}
94 changes: 84 additions & 10 deletions web/server/codechecker_server/api/report_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io
import os
import re
import shlex
import tempfile
import zipfile
import zlib
Expand All @@ -40,6 +41,8 @@
from codechecker_common.logger import get_logger
from codechecker_common.report import get_report_path_hash

from codechecker_web.shared import webserver_context

from codechecker_server.profiler import timeit

from .. import permissions
Expand Down Expand Up @@ -445,6 +448,20 @@ def extended_data_db_to_api(erd):
fileId=erd.file_id)


def comment_kind_str(status):
if status == ttypes.CommentKind.USER:
return 'user'
elif status == ttypes.CommentKind.SYSTEM:
return 'system'


def comment_kind_enum(kind):
if kind == 'user':
return ttypes.CommentKind.USER
elif kind == 'system':
return ttypes.CommentKind.SYSTEM


def unzip(b64zip, output_dir):
"""
This function unzips the base64 encoded zip file. This zip is extracted
Expand Down Expand Up @@ -629,6 +646,17 @@ def sort_run_data_query(query, sort_mode):
return query


def escape_whitespaces(s, whitespaces=None):
if not whitespaces:
whitespaces = [' ', '\n', '\t', '\r']

escaped = s
for w in whitespaces:
escaped = escaped.replace(w, '\\{0}'.format(w))

return escaped


class ThriftRequestHandler(object):
"""
Connect to database and handle thrift client requests.
Expand Down Expand Up @@ -712,7 +740,14 @@ def __get_run_ids_to_query(session, cmp_data=None):

return run_ids

@exc_to_thrift_reqfail
def __add_comment(self, bug_id, message, kind='user'):
user = self.__get_username()
return Comment(bug_id,
user,
message,
kind,
datetime.now())

@timeit
def getRunData(self, run_filter, limit, offset, sort_mode):
self.__require_access()
Expand Down Expand Up @@ -1307,12 +1342,29 @@ def _setReviewStatus(self, report_id, status, message, session):

user = self.__get_username()

old_status = review_status.status if review_status.status \
else review_status_str(ttypes.ReviewStatus.UNREVIEWED)

review_status.status = review_status_str(status)
review_status.author = user
review_status.message = message.encode('utf8')
review_status.date = datetime.now()

session.add(review_status)

if message:
system_comment_msg = 'rev_st_changed_msg {0} {1} {2}'.format(
escape_whitespaces(old_status.capitalize()),
escape_whitespaces(review_status.status.capitalize()),
escape_whitespaces(message))
else:
system_comment_msg = 'rev_st_changed {0} {1}'.format(
escape_whitespaces(old_status.capitalize()),
escape_whitespaces(review_status.status.capitalize()))

system_comment = self.__add_comment(review_status.bug_hash,
system_comment_msg,
'system')
session.add(system_comment)
session.flush()

return True
Expand Down Expand Up @@ -1373,12 +1425,27 @@ def getComments(self, report_id):
.order_by(Comment.created_at.desc()) \
.all()

context = webserver_context.get_context()
for comment in comments:
message = comment.message

sys_comment = comment_kind_str(ttypes.CommentKind.SYSTEM)
if comment.kind == sys_comment:
elements = shlex.split(message)
system_comment = context.system_comment_map.get(
elements[0])
if system_comment:
for idx, value in enumerate(elements[1:]):
system_comment = system_comment.replace(
'{' + str(idx) + '}', value)
message = system_comment

result.append(CommentData(
comment.id,
comment.author,
comment.message,
str(comment.created_at)))
message,
str(comment.created_at),
comment_kind_enum(comment.kind)))

return result
else:
Expand Down Expand Up @@ -1416,12 +1483,8 @@ def addComment(self, report_id, comment_data):
with DBSession(self.__Session) as session:
report = session.query(Report).get(report_id)
if report:
user = self.__get_username()
comment = Comment(report.bug_id,
user,
comment_data.message,
datetime.now())

comment = self.__add_comment(report.bug_id,
comment_data.message)
session.add(comment)
session.commit()

Expand Down Expand Up @@ -1452,8 +1515,18 @@ def updateComment(self, comment_id, content):
raise codechecker_api_shared.ttypes.RequestFailed(
codechecker_api_shared.ttypes.ErrorCode.UNAUTHORIZED,
'Unathorized comment modification!')
# Create system comment.
system_comment_msg = 'comment_changed {0} {1}'.format(
escape_whitespaces(comment.message),
escape_whitespaces(content))
system_comment = self.__add_comment(comment.bug_hash,
system_comment_msg,
'system')
session.add(system_comment)

comment.message = content
session.add(comment)

session.commit()
return True
else:
Expand Down Expand Up @@ -2371,6 +2444,7 @@ def checker_is_unavailable(checker_name):
detection_status = 'new'
detected_at = run_history_time

old_status = None
if bug_id in hash_map_reports:
old_report = hash_map_reports[bug_id][0]
old_status = old_report.detection_status
Expand Down
8 changes: 7 additions & 1 deletion web/server/codechecker_server/database/run_db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,18 @@ class Comment(Base):
bug_hash = Column(String, nullable=False, index=True)
author = Column(String, nullable=False)
message = Column(Binary, nullable=False)
kind = Column(Enum('user',
'system',
name='comment_kind'),
nullable=False,
server_default='user')
created_at = Column(DateTime, nullable=False)

def __init__(self, bug_hash, author, message, created_at):
def __init__(self, bug_hash, author, message, kind, created_at):
self.bug_hash = bug_hash
self.author = author
self.message = message
self.kind = kind
self.created_at = created_at


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""System comments
Revision ID: 6cb6a3a41967
Revises: 3e91d0612422
Create Date: 2019-04-02 16:12:46.794131
"""

# revision identifiers, used by Alembic.
revision = '6cb6a3a41967'
down_revision = '3e91d0612422'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('comments', sa.Column('kind', sa.Enum(u'user',
u'system',
name='comment_kind'),
server_default=u'user',
nullable=False))


def downgrade():
op.drop_column('comments', 'kind')
Loading

0 comments on commit fdbc3fa

Please sign in to comment.