Skip to content

Commit f8b8d77

Browse files
committed
Personal Access Tokens #47
1 parent 4b644fa commit f8b8d77

File tree

7 files changed

+186
-87
lines changed

7 files changed

+186
-87
lines changed

CHANGELOG.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,50 @@
33

44
Changelog of Violation comments to bitbucket server lib.
55

6-
## Unreleased
6+
## 1.45
77
### No issue
88

9+
**Stepping version to fix faulty release**
10+
11+
12+
[eff118305ad7ca8](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/eff118305ad7ca8) Tomas Bjerre *2018-02-06 18:04:41*
13+
14+
**Setting timeout to 30s**
15+
16+
17+
[3173d02da2df549](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/3173d02da2df549) Tomas Bjerre *2018-02-06 17:59:33*
18+
19+
20+
## 1.43
21+
### No issue
22+
23+
**Google error-prone**
24+
25+
26+
[b39df0b7481f4f8](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/b39df0b7481f4f8) Tomas Bjerre *2018-01-14 11:53:49*
27+
28+
29+
## 1.42
30+
### No issue
31+
32+
**Relocating to correct Java identifier**
33+
34+
35+
[9daaa06247f67a0](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/9daaa06247f67a0) Tomas Bjerre *2017-12-31 11:47:43*
36+
37+
38+
## 1.41
39+
### No issue
40+
41+
**Fixing encoding issues**
42+
43+
44+
[272f1623e559a54](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/272f1623e559a54) Tomas Bjerre *2017-12-25 19:45:51*
45+
946
**Using UTF-8, instead of default**
1047

1148

12-
[79a5a8b0869c4d7](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/79a5a8b0869c4d7) Tomas Bjerre *2017-12-24 22:03:10*
49+
[65dacc19718859e](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/65dacc19718859e) Tomas Bjerre *2017-12-25 16:12:52*
1350

1451

1552
## 1.40

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
./gradlew --refresh-dependencies clean gitChangelogTask eclipse build install -i
2+
./gradlew clean gitChangelogTask eclipse build install -i

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/BitbucketServerCommentsProvider.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,35 @@ public BitbucketServerDiffResponse get() {
5252
}
5353

5454
public BitbucketServerCommentsProvider(
55-
ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) {
55+
final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) {
5656
final String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl();
5757
final String bitbucketServerProject = violationCommentsToBitbucketApi.getProjectKey();
5858
final String bitbucketServerRepo = violationCommentsToBitbucketApi.getRepoSlug();
5959
final Integer bitbucketServerPullRequestId = violationCommentsToBitbucketApi.getPullRequestId();
6060
final String bitbucketServerUser = violationCommentsToBitbucketApi.getUsername();
6161
final String bitbucketServerPassword = violationCommentsToBitbucketApi.getPassword();
62+
final String bitbucketPersonalAccessToken =
63+
violationCommentsToBitbucketApi.getPersonalAccessToken();
6264
client =
6365
new BitbucketServerClient(
6466
bitbucketServerBaseUrl,
6567
bitbucketServerProject,
6668
bitbucketServerRepo,
6769
bitbucketServerPullRequestId,
6870
bitbucketServerUser,
69-
bitbucketServerPassword);
71+
bitbucketServerPassword,
72+
bitbucketPersonalAccessToken);
7073
this.violationCommentsToBitbucketApi = violationCommentsToBitbucketApi;
7174
}
7275

7376
@Override
74-
public void createCommentWithAllSingleFileComments(String comment) {
77+
public void createCommentWithAllSingleFileComments(final String comment) {
7578
client.pullRequestComment(comment);
7679
}
7780

7881
@Override
79-
public void createSingleFileComment(ChangedFile file, Integer line, String comment) {
82+
public void createSingleFileComment(
83+
final ChangedFile file, final Integer line, final String comment) {
8084
client.pullRequestComment(file.getFilename(), line, comment);
8185
}
8286

@@ -109,7 +113,7 @@ public List<ChangedFile> getFiles() {
109113
}
110114

111115
@Override
112-
public void removeComments(List<Comment> comments) {
116+
public void removeComments(final List<Comment> comments) {
113117
for (final Comment comment : comments) {
114118
Integer commentId = null;
115119
Integer commentVersion = null;
@@ -124,7 +128,7 @@ public void removeComments(List<Comment> comments) {
124128
}
125129

126130
@Override
127-
public boolean shouldComment(ChangedFile changedFile, Integer changedLine) {
131+
public boolean shouldComment(final ChangedFile changedFile, final Integer changedLine) {
128132
if (!violationCommentsToBitbucketApi.getCommentOnlyChangedContent()) {
129133
return true;
130134
}
@@ -135,7 +139,10 @@ public boolean shouldComment(ChangedFile changedFile, Integer changedLine) {
135139

136140
@VisibleForTesting
137141
boolean shouldComment(
138-
ChangedFile changedFile, Integer changedLine, int context, List<BitbucketServerDiff> diffs) {
142+
final ChangedFile changedFile,
143+
final Integer changedLine,
144+
final int context,
145+
final List<BitbucketServerDiff> diffs) {
139146
for (final BitbucketServerDiff diff : diffs) {
140147
final DiffDestination destination = diff.getDestination();
141148
if (destination != null) {
@@ -174,7 +181,8 @@ public boolean shouldCreateSingleFileComment() {
174181
}
175182

176183
@Override
177-
public Optional<String> findCommentFormat(ChangedFile changedFile, Violation violation) {
184+
public Optional<String> findCommentFormat(
185+
final ChangedFile changedFile, final Violation violation) {
178186
return Optional.absent();
179187
}
180188

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/ViolationCommentsToBitbucketServerApi.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package se.bjurr.violations.comments.bitbucketserver.lib;
22

3-
import static com.google.common.base.Objects.firstNonNull;
43
import static com.google.common.base.Preconditions.checkNotNull;
54
import static se.bjurr.violations.comments.lib.CommentsCreator.createComments;
5+
import static se.bjurr.violations.lib.util.Utils.firstNonNull;
6+
import static se.bjurr.violations.lib.util.Utils.isNullOrEmpty;
67

78
import java.util.List;
89
import se.bjurr.violations.comments.lib.model.CommentsProvider;
@@ -12,6 +13,7 @@ public class ViolationCommentsToBitbucketServerApi {
1213
private static final Integer BITBUCKET_MAX_COMMENT_SIZE = 32767;
1314
public static final String DEFAULT_PROP_VIOLATIONS_PASSWORD = "VIOLATIONS_PASSWORD";
1415
public static final String DEFAULT_PROP_VIOLATIONS_USERNAME = "VIOLATIONS_USERNAME";
16+
public static final String DEFAULT_PROP_PERSONAL_ACCESS_TOKEN = "VIOLATIONS_PAT";
1517

1618
public static ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketServerApi() {
1719
return new ViolationCommentsToBitbucketServerApi();
@@ -24,28 +26,38 @@ public static ViolationCommentsToBitbucketServerApi violationCommentsToBitbucket
2426
private String projectKey;
2527
private String propPassword = DEFAULT_PROP_VIOLATIONS_PASSWORD;
2628
private String propUsername = DEFAULT_PROP_VIOLATIONS_USERNAME;
29+
private String propPersonalAccessToken = DEFAULT_PROP_PERSONAL_ACCESS_TOKEN;
2730
private int pullRequestId;
2831
private String repoSlug;
2932
private String username;
3033
private List<Violation> violations;
3134
private boolean commentOnlyChangedContent = false;
3235
private int commentOnlyChangedContentContext;
3336
private boolean shouldKeepOldComments;
37+
private String personalAccessToken;
3438

3539
private ViolationCommentsToBitbucketServerApi() {}
3640

3741
private void checkState() {
38-
if (username == null || password == null) {
42+
final boolean noUsername = isNullOrEmpty(username) || isNullOrEmpty(password);
43+
final boolean noPat = isNullOrEmpty(personalAccessToken);
44+
if (noUsername && noPat) {
3945
throw new IllegalStateException(
40-
"User and Password must be set! They can be set with the API or by setting properties.\n"
46+
"User and Password, or personal access token, must be set! They can be set with the API or by setting properties.\n"
4147
+ //
4248
"Username/password:\n"
4349
+ //
4450
"-D"
4551
+ DEFAULT_PROP_VIOLATIONS_USERNAME
4652
+ "=theuser -D"
4753
+ DEFAULT_PROP_VIOLATIONS_PASSWORD
48-
+ "=thepassword");
54+
+ "=thepassword"
55+
+ //
56+
"\n\nPersonal access token:\n"
57+
+ //
58+
"-D"
59+
+ DEFAULT_PROP_PERSONAL_ACCESS_TOKEN
60+
+ "=asdasd");
4961
}
5062
checkNotNull(bitbucketServerUrl, "BitbucketServerURL");
5163
checkNotNull(pullRequestId, "PullRequestId");
@@ -108,6 +120,10 @@ private void populateFromEnvironmentVariables() {
108120
if (System.getProperty(propPassword) != null) {
109121
password = firstNonNull(password, System.getProperty(propPassword));
110122
}
123+
if (System.getProperty(propPassword) != null) {
124+
personalAccessToken =
125+
firstNonNull(personalAccessToken, System.getProperty(propPersonalAccessToken));
126+
}
111127
}
112128

113129
public void toPullRequest() throws Exception {
@@ -117,80 +133,91 @@ public void toPullRequest() throws Exception {
117133
createComments(commentsProvider, violations, BITBUCKET_MAX_COMMENT_SIZE);
118134
}
119135

120-
public ViolationCommentsToBitbucketServerApi withBitbucketServerUrl(String bitbucketServerUrl) {
136+
public ViolationCommentsToBitbucketServerApi withBitbucketServerUrl(
137+
final String bitbucketServerUrl) {
121138
this.bitbucketServerUrl = bitbucketServerUrl;
122139
return this;
123140
}
124141

125142
public ViolationCommentsToBitbucketServerApi withCommentOnlyChangedContent(
126-
boolean commentOnlyChangedContent) {
143+
final boolean commentOnlyChangedContent) {
127144
this.commentOnlyChangedContent = commentOnlyChangedContent;
128145
return this;
129146
}
130147

131148
public ViolationCommentsToBitbucketServerApi withCommentOnlyChangedContentContext(
132-
int commentOnlyChangedContentContext) {
149+
final int commentOnlyChangedContentContext) {
133150
this.commentOnlyChangedContentContext = commentOnlyChangedContentContext;
134151
return this;
135152
}
136153

137154
public ViolationCommentsToBitbucketServerApi withCreateCommentWithAllSingleFileComments(
138-
boolean createCommentWithAllSingleFileComments) {
155+
final boolean createCommentWithAllSingleFileComments) {
139156
this.createCommentWithAllSingleFileComments = createCommentWithAllSingleFileComments;
140157
return this;
141158
}
142159

143160
public ViolationCommentsToBitbucketServerApi withCreateSingleFileComments(
144-
boolean createSingleFileComments) {
161+
final boolean createSingleFileComments) {
145162
this.createSingleFileComments = createSingleFileComments;
146163
return this;
147164
}
148165

149-
public ViolationCommentsToBitbucketServerApi withPassword(String password) {
166+
public ViolationCommentsToBitbucketServerApi withPassword(final String password) {
150167
this.password = password;
151168
return this;
152169
}
153170

154-
public ViolationCommentsToBitbucketServerApi withProjectKey(String projectKey) {
171+
public ViolationCommentsToBitbucketServerApi withProjectKey(final String projectKey) {
155172
this.projectKey = projectKey;
156173
return this;
157174
}
158175

159-
public void withPropPassword(String envPassword) {
176+
public void withPropPassword(final String envPassword) {
160177
propPassword = envPassword;
161178
}
162179

163-
public void withPropUsername(String envUsername) {
180+
public void withPropUsername(final String envUsername) {
164181
propUsername = envUsername;
165182
}
166183

167-
public ViolationCommentsToBitbucketServerApi withPullRequestId(int pullRequestId) {
184+
public ViolationCommentsToBitbucketServerApi withPropPersonalAccessToken(
185+
final String propPersonalAccessToken) {
186+
this.propPersonalAccessToken = propPersonalAccessToken;
187+
return this;
188+
}
189+
190+
public ViolationCommentsToBitbucketServerApi withPullRequestId(final int pullRequestId) {
168191
this.pullRequestId = pullRequestId;
169192
return this;
170193
}
171194

172-
public ViolationCommentsToBitbucketServerApi withRepoSlug(String repoSlug) {
195+
public ViolationCommentsToBitbucketServerApi withRepoSlug(final String repoSlug) {
173196
this.repoSlug = repoSlug;
174197
return this;
175198
}
176199

177-
public ViolationCommentsToBitbucketServerApi withUsername(String username) {
200+
public ViolationCommentsToBitbucketServerApi withUsername(final String username) {
178201
this.username = username;
179202
return this;
180203
}
181204

182-
public ViolationCommentsToBitbucketServerApi withViolations(List<Violation> violations) {
205+
public ViolationCommentsToBitbucketServerApi withViolations(final List<Violation> violations) {
183206
this.violations = violations;
184207
return this;
185208
}
186209

187210
public ViolationCommentsToBitbucketServerApi withShouldKeepOldComments(
188-
boolean shouldKeepOldComments) {
211+
final boolean shouldKeepOldComments) {
189212
this.shouldKeepOldComments = shouldKeepOldComments;
190213
return this;
191214
}
192215

193216
public boolean getShouldKeepOldComments() {
194217
return shouldKeepOldComments;
195218
}
219+
220+
public String getPersonalAccessToken() {
221+
return personalAccessToken;
222+
}
196223
}

0 commit comments

Comments
 (0)