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 Apr 3, 2019
1 parent 1af4908 commit bc16dd6
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 51 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 @@ -85,6 +85,11 @@ enum ExtendedReportDataType {
FIXIT = 20,
}

enum CommentKind {
GENERAL, // User based comments.
SYSTEM // System events.
}

struct SourceFileData {
1: i64 fileId,
2: string filePath,
Expand Down Expand Up @@ -256,7 +261,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 @@ -58,6 +58,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.__product_db_version_info = None
self.__run_db_version_info = None
Expand Down Expand Up @@ -140,6 +142,15 @@ def db_version_file(self):
return os.path.join(self._package_root, 'config',
'server_db_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_map.json')

@property
def path_plist_to_html_dist(self):
return os.path.join(self.package_root,
Expand Down
6 changes: 6 additions & 0 deletions web/config/system_comment_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"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%",
"det_st_changed": "%author% changed detection status from <b class=\"old-detection-status\">{0}</b> to <b class=\"new-detection-status\">{1}</b> in run <b>{2}</> %date%",
"comment_changed": "%author% changed comment message from <b class=\"old-comment-message\">{0}</b> to <b class=\"new-comment-message\">{1}</b> %date%"
}
107 changes: 97 additions & 10 deletions web/server/codechecker_server/api/report_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from datetime import datetime, timedelta
import io
import os
import shlex
import tempfile
import zipfile
import zlib
Expand Down Expand Up @@ -313,6 +314,20 @@ def extended_data_db_to_api(erd):
fileId=erd.file_id)


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


def comment_kind_enum(kind):
if kind == 'general':
return ttypes.CommentKind.GENERAL
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 @@ -473,6 +488,17 @@ def check_remove_runs_lock(session, run_ids):
', '.join([r[0] for r in run_locks])))


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 @@ -557,7 +583,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='general'):
user = self.__get_username()
return Comment(bug_id,
user,
message,
kind,
datetime.now())

@timeit
def getRunData(self, run_filter):
self.__require_access()
Expand Down Expand Up @@ -1123,12 +1156,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 @@ -1186,12 +1236,26 @@ def getComments(self, report_id):
.order_by(Comment.created_at.desc()) \
.all()

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

if comment.kind == 'system':
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 @@ -1229,12 +1293,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 @@ -1265,8 +1325,18 @@ def updateComment(self, comment_id, content):
raise shared.ttypes.RequestFailed(
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 @@ -2106,10 +2176,14 @@ def __store_reports(self, session, report_dir, source_root, run_id,
Parse up and store the plist report files.
"""

user_name = self.__get_username()

all_reports = session.query(Report) \
.filter(Report.run_id == run_id) \
.all()

run = session.query(Run).get(run_id)

hash_map_reports = defaultdict(list)
for report in all_reports:
hash_map_reports[report.bug_id].append(report)
Expand Down Expand Up @@ -2188,6 +2262,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 All @@ -2200,6 +2275,18 @@ def checker_is_unavailable(checker_name):
elif checker_is_unavailable(checker_name):
detection_status = 'unavailable'

if old_status and detection_status != old_status:
system_comment_msg = 'det_st_changed {0} {1} {2}'.format(
escape_whitespaces(old_status.capitalize()),
escape_whitespaces(detection_status.capitalize()),
run.name)

session.add(Comment(bug_id,
user_name,
system_comment_msg,
'system',
run_history_time))

report_id = store_handler.addReport(
session,
run_id,
Expand Down
2 changes: 1 addition & 1 deletion web/server/codechecker_server/api/store_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,4 @@ def addFileRecord(session, filepath, content_hash):
.filter(File.content_hash == content_hash,
File.filepath == filepath).one_or_none()

return file_record.id if file_record else None
return file_record.id if file_record else None
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('general',
'system',
name='comment_kind'),
nullable=False,
server_default='general')
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'general',
u'system',
name='comment_kind'),
server_default=u'general',
nullable=False))


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

0 comments on commit bc16dd6

Please sign in to comment.