|
6 | 6 | import static java.util.concurrent.TimeUnit.SECONDS; |
7 | 7 | import static se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE.ADDED; |
8 | 8 |
|
| 9 | +import com.google.common.annotations.VisibleForTesting; |
| 10 | +import com.google.common.base.Supplier; |
9 | 11 | import java.util.ArrayList; |
10 | 12 | import java.util.List; |
11 | | - |
12 | 13 | import org.slf4j.Logger; |
13 | 14 | import org.slf4j.LoggerFactory; |
14 | | - |
15 | 15 | import se.bjurr.violations.comments.bitbucketserver.lib.client.BitbucketServerClient; |
16 | 16 | import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerComment; |
17 | 17 | import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerDiff; |
|
26 | 26 | import se.bjurr.violations.lib.model.Violation; |
27 | 27 | import se.bjurr.violations.lib.util.Optional; |
28 | 28 |
|
29 | | -import com.google.common.annotations.VisibleForTesting; |
30 | | -import com.google.common.base.Supplier; |
31 | | - |
32 | 29 | public class BitbucketServerCommentsProvider implements CommentsProvider { |
33 | 30 |
|
34 | | - private static final Logger LOG = LoggerFactory.getLogger(BitbucketServerCommentsProvider.class); |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(BitbucketServerCommentsProvider.class); |
| 32 | + |
| 33 | + private final BitbucketServerClient client; |
| 34 | + |
| 35 | + private final Supplier<BitbucketServerDiffResponse> diffResponse = |
| 36 | + memoizeWithExpiration( |
| 37 | + new Supplier<BitbucketServerDiffResponse>() { |
| 38 | + @Override |
| 39 | + public BitbucketServerDiffResponse get() { |
| 40 | + return client.pullRequestDiff(); |
| 41 | + } |
| 42 | + }, |
| 43 | + 10, |
| 44 | + SECONDS); |
35 | 45 |
|
36 | | - private final BitbucketServerClient client; |
| 46 | + private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi; |
| 47 | + |
| 48 | + @VisibleForTesting |
| 49 | + BitbucketServerCommentsProvider() { |
| 50 | + client = null; |
| 51 | + violationCommentsToBitbucketApi = null; |
| 52 | + } |
| 53 | + |
| 54 | + public BitbucketServerCommentsProvider( |
| 55 | + ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) { |
| 56 | + final String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl(); |
| 57 | + final String bitbucketServerProject = violationCommentsToBitbucketApi.getProjectKey(); |
| 58 | + final String bitbucketServerRepo = violationCommentsToBitbucketApi.getRepoSlug(); |
| 59 | + final Integer bitbucketServerPullRequestId = violationCommentsToBitbucketApi.getPullRequestId(); |
| 60 | + final String bitbucketServerUser = violationCommentsToBitbucketApi.getUsername(); |
| 61 | + final String bitbucketServerPassword = violationCommentsToBitbucketApi.getPassword(); |
| 62 | + client = |
| 63 | + new BitbucketServerClient( |
| 64 | + bitbucketServerBaseUrl, |
| 65 | + bitbucketServerProject, |
| 66 | + bitbucketServerRepo, |
| 67 | + bitbucketServerPullRequestId, |
| 68 | + bitbucketServerUser, |
| 69 | + bitbucketServerPassword); |
| 70 | + this.violationCommentsToBitbucketApi = violationCommentsToBitbucketApi; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void createCommentWithAllSingleFileComments(String comment) { |
| 75 | + client.pullRequestComment(comment); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void createSingleFileComment(ChangedFile file, Integer line, String comment) { |
| 80 | + client.pullRequestComment(file.getFilename(), line, comment); |
| 81 | + } |
37 | 82 |
|
38 | | - private final Supplier<BitbucketServerDiffResponse> diffResponse = memoizeWithExpiration( |
39 | | - new Supplier<BitbucketServerDiffResponse>() { |
40 | | - @Override |
41 | | - public BitbucketServerDiffResponse get() { |
42 | | - return client.pullRequestDiff(); |
| 83 | + @Override |
| 84 | + public List<Comment> getComments() { |
| 85 | + final List<Comment> comments = newArrayList(); |
| 86 | + for (final String changedFile : client.pullRequestChanges()) { |
| 87 | + final List<BitbucketServerComment> bitbucketServerCommentsOnFile = |
| 88 | + client.pullRequestComments(changedFile); |
| 89 | + for (final BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) { |
| 90 | + final List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile); |
| 91 | + comments.add(new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics)); |
| 92 | + } |
43 | 93 | } |
44 | | - }, 10, SECONDS); |
45 | | - |
46 | | - private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi; |
47 | | - |
48 | | - @VisibleForTesting |
49 | | - BitbucketServerCommentsProvider() { |
50 | | - client = null; |
51 | | - violationCommentsToBitbucketApi = null; |
52 | | - } |
53 | | - |
54 | | - public BitbucketServerCommentsProvider(ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) { |
55 | | - final String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl(); |
56 | | - final String bitbucketServerProject = violationCommentsToBitbucketApi.getProjectKey(); |
57 | | - final String bitbucketServerRepo = violationCommentsToBitbucketApi.getRepoSlug(); |
58 | | - final Integer bitbucketServerPullRequestId = violationCommentsToBitbucketApi.getPullRequestId(); |
59 | | - final String bitbucketServerUser = violationCommentsToBitbucketApi.getUsername(); |
60 | | - final String bitbucketServerPassword = violationCommentsToBitbucketApi.getPassword(); |
61 | | - client = new BitbucketServerClient(bitbucketServerBaseUrl, bitbucketServerProject, bitbucketServerRepo, |
62 | | - bitbucketServerPullRequestId, bitbucketServerUser, bitbucketServerPassword); |
63 | | - this.violationCommentsToBitbucketApi = violationCommentsToBitbucketApi; |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public void createCommentWithAllSingleFileComments(String comment) { |
68 | | - client.pullRequestComment(comment); |
69 | | - } |
70 | | - |
71 | | - @Override |
72 | | - public void createSingleFileComment(ChangedFile file, Integer line, String comment) { |
73 | | - client.pullRequestComment(file.getFilename(), line, comment); |
74 | | - } |
75 | | - |
76 | | - @Override |
77 | | - public List<Comment> getComments() { |
78 | | - final List<Comment> comments = newArrayList(); |
79 | | - for (final String changedFile : client.pullRequestChanges()) { |
80 | | - final List<BitbucketServerComment> bitbucketServerCommentsOnFile = client.pullRequestComments(changedFile); |
81 | | - for (final BitbucketServerComment fileComment : bitbucketServerCommentsOnFile) { |
82 | | - final List<String> specifics = newArrayList(fileComment.getVersion() + "", changedFile); |
83 | | - comments.add(new Comment(fileComment.getId() + "", fileComment.getText(), null, specifics)); |
84 | | - } |
| 94 | + |
| 95 | + return comments; |
85 | 96 | } |
86 | 97 |
|
87 | | - return comments; |
88 | | - } |
| 98 | + @Override |
| 99 | + public List<ChangedFile> getFiles() { |
| 100 | + final List<ChangedFile> changedFiles = newArrayList(); |
89 | 101 |
|
90 | | - @Override |
91 | | - public List<ChangedFile> getFiles() { |
92 | | - final List<ChangedFile> changedFiles = newArrayList(); |
| 102 | + final List<String> bitbucketServerChangedFiles = client.pullRequestChanges(); |
93 | 103 |
|
94 | | - final List<String> bitbucketServerChangedFiles = client.pullRequestChanges(); |
| 104 | + for (final String changedFile : bitbucketServerChangedFiles) { |
| 105 | + changedFiles.add(new ChangedFile(changedFile, new ArrayList<String>())); |
| 106 | + } |
95 | 107 |
|
96 | | - for (final String changedFile : bitbucketServerChangedFiles) { |
97 | | - changedFiles.add(new ChangedFile(changedFile, new ArrayList<String>())); |
| 108 | + return changedFiles; |
98 | 109 | } |
99 | 110 |
|
100 | | - return changedFiles; |
101 | | - } |
102 | | - |
103 | | - @Override |
104 | | - public void removeComments(List<Comment> comments) { |
105 | | - for (final Comment comment : comments) { |
106 | | - Integer commentId = null; |
107 | | - Integer commentVersion = null; |
108 | | - try { |
109 | | - commentId = Integer.valueOf(comment.getIdentifier()); |
110 | | - commentVersion = Integer.valueOf(comment.getSpecifics().get(0)); |
111 | | - client.pullRequestRemoveComment(commentId, commentVersion); |
112 | | - } catch (final Exception e) { |
113 | | - LOG.warn("Was unable to remove comment " + commentId + " " + commentVersion, e); |
114 | | - } |
| 111 | + @Override |
| 112 | + public void removeComments(List<Comment> comments) { |
| 113 | + for (final Comment comment : comments) { |
| 114 | + Integer commentId = null; |
| 115 | + Integer commentVersion = null; |
| 116 | + try { |
| 117 | + commentId = Integer.valueOf(comment.getIdentifier()); |
| 118 | + commentVersion = Integer.valueOf(comment.getSpecifics().get(0)); |
| 119 | + client.pullRequestRemoveComment(commentId, commentVersion); |
| 120 | + } catch (final Exception e) { |
| 121 | + LOG.warn("Was unable to remove comment " + commentId + " " + commentVersion, e); |
| 122 | + } |
| 123 | + } |
115 | 124 | } |
116 | | - } |
117 | 125 |
|
118 | | - @Override |
119 | | - public boolean shouldComment(ChangedFile changedFile, Integer changedLine) { |
120 | | - if (!violationCommentsToBitbucketApi.getCommentOnlyChangedContent()) { |
121 | | - return true; |
| 126 | + @Override |
| 127 | + public boolean shouldComment(ChangedFile changedFile, Integer changedLine) { |
| 128 | + if (!violationCommentsToBitbucketApi.getCommentOnlyChangedContent()) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + final int context = violationCommentsToBitbucketApi.getCommentOnlyChangedContentContext(); |
| 132 | + final List<BitbucketServerDiff> diffs = diffResponse.get().getDiffs(); |
| 133 | + return shouldComment(changedFile, changedLine, context, diffs); |
122 | 134 | } |
123 | | - final int context = violationCommentsToBitbucketApi.getCommentOnlyChangedContentContext(); |
124 | | - final List<BitbucketServerDiff> diffs = diffResponse.get().getDiffs(); |
125 | | - return shouldComment(changedFile, changedLine, context, diffs); |
126 | | - } |
127 | | - |
128 | | - @VisibleForTesting |
129 | | - boolean shouldComment(ChangedFile changedFile, Integer changedLine, int context, List<BitbucketServerDiff> diffs) { |
130 | | - for (final BitbucketServerDiff diff : diffs) { |
131 | | - final DiffDestination destination = diff.getDestination(); |
132 | | - if (destination != null) { |
133 | | - final String destinationToString = destination.getToString(); |
134 | | - if (!isNullOrEmpty(destinationToString)) { |
135 | | - if (destinationToString.equals(changedFile.getFilename())) { |
136 | | - if (diff.getHunks() != null) { |
137 | | - for (final DiffHunk hunk : diff.getHunks()) { |
138 | | - for (final Segment segment : hunk.getSegments()) { |
139 | | - if (segment.getType() == ADDED) { |
140 | | - for (final Line line : segment.getLines()) { |
141 | | - if (line.getDestination() >= changedLine - context && line.getDestination() <= changedLine + context) { |
142 | | - return true; |
143 | | - } |
| 135 | + |
| 136 | + @VisibleForTesting |
| 137 | + boolean shouldComment( |
| 138 | + ChangedFile changedFile, Integer changedLine, int context, List<BitbucketServerDiff> diffs) { |
| 139 | + for (final BitbucketServerDiff diff : diffs) { |
| 140 | + final DiffDestination destination = diff.getDestination(); |
| 141 | + if (destination != null) { |
| 142 | + final String destinationToString = destination.getToString(); |
| 143 | + if (!isNullOrEmpty(destinationToString)) { |
| 144 | + if (destinationToString.equals(changedFile.getFilename())) { |
| 145 | + if (diff.getHunks() != null) { |
| 146 | + for (final DiffHunk hunk : diff.getHunks()) { |
| 147 | + for (final Segment segment : hunk.getSegments()) { |
| 148 | + if (segment.getType() == ADDED) { |
| 149 | + for (final Line line : segment.getLines()) { |
| 150 | + if (line.getDestination() >= changedLine - context |
| 151 | + && line.getDestination() <= changedLine + context) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
144 | 159 | } |
145 | | - } |
146 | 160 | } |
147 | | - } |
148 | 161 | } |
149 | | - } |
150 | 162 | } |
151 | | - } |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean shouldCreateCommentWithAllSingleFileComments() { |
| 168 | + return violationCommentsToBitbucketApi.getCreateCommentWithAllSingleFileComments(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean shouldCreateSingleFileComment() { |
| 173 | + return violationCommentsToBitbucketApi.getCreateSingleFileComments(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Optional<String> findCommentFormat(ChangedFile changedFile, Violation violation) { |
| 178 | + return Optional.absent(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public boolean shouldKeepOldComments() { |
| 183 | + return violationCommentsToBitbucketApi.getShouldKeepOldComments(); |
152 | 184 | } |
153 | | - return false; |
154 | | - } |
155 | | - |
156 | | - @Override |
157 | | - public boolean shouldCreateCommentWithAllSingleFileComments() { |
158 | | - return violationCommentsToBitbucketApi.getCreateCommentWithAllSingleFileComments(); |
159 | | - } |
160 | | - |
161 | | - @Override |
162 | | - public boolean shouldCreateSingleFileComment() { |
163 | | - return violationCommentsToBitbucketApi.getCreateSingleFileComments(); |
164 | | - } |
165 | | - |
166 | | - @Override |
167 | | - public Optional<String> findCommentFormat(ChangedFile changedFile, Violation violation) { |
168 | | - return Optional.absent(); |
169 | | - } |
170 | | - |
171 | | -@Override |
172 | | -public boolean shouldKeepOldComments() { |
173 | | - return violationCommentsToBitbucketApi.getShouldKeepOldComments(); |
174 | | -} |
175 | 185 | } |
0 commit comments