Skip to content

Commit b1cdba5

Browse files
authored
feat: Add support for Bitbucket API Tokens (#9)
1 parent a86ee84 commit b1cdba5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The runnable can be found in [NPM](https://www.npmjs.com/package/violation-comme
1313
Run it with:
1414

1515
```shell
16+
# Using application password
1617
npx violation-comments-to-bitbucket-cloud-command-line \
1718
-u tomasbjerre \
1819
-p MY-APPLICATION-PASSWORD \
@@ -21,9 +22,20 @@ npx violation-comments-to-bitbucket-cloud-command-line \
2122
-prid 1 \
2223
-v "CHECKSTYLE" "." ".*checkstyle/main\.xml$" "Checkstyle" \
2324
-v "JSHINT" "." ".*jshint/report\.xml$" "JSHint"
25+
26+
# Using API token
27+
npx violation-comments-to-bitbucket-cloud-command-line \
28+
-t MY-PROJECT-OR-REPOSITORY-APPLICATION-PASSWORD \
29+
-ws tomasbjerre \
30+
-rs violations-test \
31+
-prid 1 \
32+
-v "CHECKSTYLE" "." ".*checkstyle/main\.xml$" "Checkstyle" \
33+
-v "JSHINT" "." ".*jshint/report\.xml$" "JSHint"
2434
```
2535

26-
Create **application passwords** like this: https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html
36+
Create **application passwords** like this: https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html. Pass the username (`-u`) and the application password (`-p`) to the command.
37+
38+
Alternatively, the plugin supports **api tokens**; create them like this: https://support.atlassian.com/bitbucket-cloud/docs/api-tokens/. Pass the api token (`-t`) to the command.
2739

2840
If using it from **Jenkins**, you may integrate with Bitbucket Cloud with this plugin: https://github.com/jenkinsci/generic-webhook-trigger-plugin
2941

@@ -138,6 +150,15 @@ Missing a format? Open an issue [here](https://github.com/tomasbjerre/violations
138150
# Usage
139151

140152
```shell
153+
-api-token, -t <string> You can create an
154+
'api token' in Bitbucket
155+
to use here. See https://support.
156+
atlassian.com/bitbucket-cloud/
157+
docs/api-tokens/
158+
Note: If you pass this argument,
159+
`-u` and `-p` shall be omitted.
160+
<string>: any string
161+
Default:
141162
-comment-only-changed-content, -cocc <boolean> True if only changed
142163
parts of the changed files
143164
should be commented. False if
@@ -172,6 +193,9 @@ ccwasfc <boolean> Default: false
172193
//confluence.atlassian.
173194
com/bitbucket/app-passwords-828781300.
174195
html
196+
Note: If you pass this argument,
197+
`-u` is required, and `-t` shall
198+
be omitted.
175199
<string>: any string
176200
Default:
177201
-pull-request-id, -prid <string> <string>: any string [Required]
@@ -185,7 +209,11 @@ ccwasfc <boolean> Default: false
185209
and supply output when
186210
reporting bugs.
187211
Default: disabled
188-
-username, -u <string> <string>: any string
212+
-username, -u <string> Username used in authenticating requests.
213+
Note: If you pass this argument,
214+
`-p` is required, and `-t` shall
215+
be omitted.
216+
<string>: any string
189217
Default:
190218
--violations, -v <string> The violations to look
191219
for. <PARSER> <FOLDER>

src/main/java/se/bjurr/violations/main/Runner.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class Runner {
4343
private String repositorySlug;
4444
private String username;
4545
private String password;
46+
private String apiToken;
4647
private boolean shouldCommentOnlyChangedContent;
4748
private boolean shouldCommentOnlyChangedFiles;
4849
private Integer maxNumberOfViolations;
@@ -103,6 +104,12 @@ public void main(final String args[]) throws Exception {
103104
.description(
104105
"You can create an 'application password' in Bitbucket to use here. See https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html")
105106
.build();
107+
final Argument<String> apiTokenArg =
108+
stringArgument("-api-token", "-t")
109+
.defaultValue("")
110+
.description(
111+
"You can create an 'API token' in Bitbucket to use here. See https://support.atlassian.com/bitbucket-cloud/docs/api-tokens/")
112+
.build();
106113
final Argument<Boolean> shouldCommentOnlyChangedContentArg =
107114
booleanArgument("-comment-only-changed-content", "-cocc")
108115
.defaultValue(true)
@@ -136,6 +143,7 @@ public void main(final String args[]) throws Exception {
136143
repositorySlugArg, //
137144
usernameArg, //
138145
passwordArg, //
146+
apiTokenArg,
139147
shouldCommentOnlyChangedContentArg, //
140148
shouldCommentOnlyChangedFilesArg, //
141149
maxNumberOfViolationsArg //
@@ -155,6 +163,7 @@ public void main(final String args[]) throws Exception {
155163
this.repositorySlug = parsed.get(repositorySlugArg);
156164
this.username = parsed.get(usernameArg);
157165
this.password = parsed.get(passwordArg);
166+
this.apiToken = parsed.get(apiTokenArg);
158167
this.shouldCommentOnlyChangedContent = parsed.get(shouldCommentOnlyChangedContentArg);
159168
this.shouldCommentOnlyChangedFiles = parsed.get(shouldCommentOnlyChangedFilesArg);
160169
this.maxNumberOfViolations = parsed.get(maxNumberOfViolationsArg);
@@ -169,6 +178,10 @@ public void main(final String args[]) throws Exception {
169178
+ this.toString());
170179
}
171180

181+
if (this.apiToken != null && (this.username != null || this.password != null)) {
182+
throw new Exception("API tokens and application passwords cannot be used simultaneously. Specify either one of them.");
183+
}
184+
172185
} catch (final ArgumentException exception) {
173186
System.out.println(exception.getMessageAndUsage());
174187
System.exit(1);
@@ -220,6 +233,11 @@ public void log(final Level level, final String string, final Throwable t) {
220233
.withPassword(this.password);
221234
}
222235

236+
if (!this.apiToken.isEmpty()){
237+
violationCommentsToBitbucketServerApi
238+
.withApiToken(this.apiToken);
239+
}
240+
223241
violationCommentsToBitbucketServerApi //
224242
.withPullRequestId(this.pullRequestId) //
225243
.withWorkspace(this.workspace) //

0 commit comments

Comments
 (0)