-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
IssueCommentService.java
156 lines (133 loc) · 5.57 KB
/
IssueCommentService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.issue;
import com.google.common.base.Strings;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.issue.IssueComment;
import org.sonar.api.utils.System2;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.DefaultIssueComment;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.issue.IssueChangeDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.user.UserSession;
import static com.google.common.collect.Lists.newArrayList;
public class IssueCommentService {
private final DbClient dbClient;
private final IssueService issueService;
private final IssueFieldsSetter updater;
private final UserSession userSession;
public IssueCommentService(DbClient dbClient, IssueService issueService, IssueFieldsSetter updater, UserSession userSession) {
this.dbClient = dbClient;
this.issueService = issueService;
this.updater = updater;
this.userSession = userSession;
}
public List<DefaultIssueComment> findComments(String issueKey) {
return findComments(newArrayList(issueKey));
}
public List<DefaultIssueComment> findComments(DbSession dbSession, String issueKey) {
return findComments(dbSession, newArrayList(issueKey));
}
public List<DefaultIssueComment> findComments(Collection<String> issueKeys) {
DbSession session = dbClient.openSession(false);
try {
return findComments(session, issueKeys);
} finally {
session.close();
}
}
public List<DefaultIssueComment> findComments(DbSession session, Collection<String> issueKeys) {
return dbClient.issueChangeDao().selectCommentsByIssues(session, issueKeys);
}
public IssueComment findComment(String commentKey) {
return dbClient.issueChangeDao().selectCommentByKey(commentKey);
}
public IssueComment addComment(String issueKey, String text) {
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.getLogin());
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();
}
}
public IssueComment deleteComment(String commentKey) {
DefaultIssueComment comment = dbClient.issueChangeDao().selectCommentByKey(commentKey);
if (comment == null) {
throw new NotFoundException("Comment not found: " + commentKey);
}
if (Strings.isNullOrEmpty(comment.userLogin()) || !Objects.equals(comment.userLogin(), userSession.getLogin())) {
throw new ForbiddenException("You can only delete your own comments");
}
// check authorization
issueService.getByKey(comment.issueKey());
dbClient.issueChangeDao().delete(commentKey);
return comment;
}
public IssueComment editComment(String commentKey, String text) {
DefaultIssueComment comment = dbClient.issueChangeDao().selectCommentByKey(commentKey);
if (StringUtils.isBlank(text)) {
throw new BadRequestException("Cannot add empty comments to an issue");
}
if (comment == null) {
throw new NotFoundException("Comment not found: " + commentKey);
}
if (Strings.isNullOrEmpty(comment.userLogin()) || !Objects.equals(comment.userLogin(), userSession.getLogin())) {
throw new ForbiddenException("You can only edit your own comments");
}
// check authorization
issueService.getByKey(comment.issueKey());
IssueChangeDto dto = IssueChangeDto.of(comment);
dto.setUpdatedAt(System2.INSTANCE.now());
dto.setChangeData(text);
dbClient.issueChangeDao().update(dto);
return comment;
}
public boolean canEditOrDelete(IssueChangeDto dto) {
return userSession.isLoggedIn() && userSession.getLogin().equals(dto.getUserLogin());
}
private void verifyLoggedIn(UserSession userSession) {
if (!userSession.isLoggedIn()) {
throw new UnauthorizedException("User is not logged in");
}
}
}