}
public IssueComment addComment(String issueKey, String text, UserSession userSession) {
verifyLoggedIn(userSession);
if (StringUtils.isBlank(text)) {
throw new BadRequestException("Cannot add empty comments to an issue");
}
DbSession session = dbClient.openSession(false);
try {
DefaultIssue issue = issueService.getByKeyForUpdate(session, issueKey).toDefaultIssue();
IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.login());
updater.addComment(issue, text, context);
issueService.saveIssue(session, issue, context, text);
session.commit();
List<DefaultIssueComment> comments = findComments(issueKey);
if (comments.isEmpty()) {
throw new BadRequestException(String.format("Fail to add a comment on issue %s", issueKey));
}
return comments.get(comments.size() - 1);
} finally {
session.close();
}