Skip to content

Commit 5a74ad8

Browse files
committed
Add API token support
1 parent 018cd95 commit 5a74ad8

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.assertthat.plugins</groupId>
77
<artifactId>assertthat-bdd-standalone</artifactId>
8-
<version>1.9.10</version>
8+
<version>1.9.11</version>
99
<name>assertthat-bdd-standalone</name>
1010
<description>AssertThat BDD Jira plugin client standalone</description>
1111
<url>http://www.assertthat.com</url>

src/main/java/com/assertthat/plugins/standalone/APIUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class APIUtil {
4141
private String reportUrl;
4242
private OkHttpClient client;
4343

44-
public APIUtil(String projectId, String accessKey, String secretKey, String proxyURI, String proxyUsername, String proxyPassword, String jiraServerURL, boolean ignoreCertErrors) {
44+
public APIUtil(String projectId, String accessKey, String secretKey, String token, String proxyURI, String proxyUsername, String proxyPassword, String jiraServerURL, boolean ignoreCertErrors) {
4545
if (jiraServerURL != null) {
4646
this.featuresUrl = jiraServerURL + "/rest/assertthat/latest/project/" + projectId + "/client/features";
4747
this.reportUrl = jiraServerURL + "/rest/assertthat/latest/project/" + projectId + "/client/report";
@@ -50,7 +50,8 @@ public APIUtil(String projectId, String accessKey, String secretKey, String prox
5050
this.reportUrl = "https://bdd.assertthat.app/rest/api/1/project/" + projectId + "/report";
5151
}
5252
OkHttpClientBuilder builder = new OkHttpClientBuilder();
53-
builder.authenticated(accessKey, secretKey);
53+
builder.authenticated(accessKey, secretKey, token);
54+
5455
if (ignoreCertErrors) {
5556
builder.ignoringCertificate();
5657
}
@@ -110,7 +111,6 @@ public Long upload(Long runId, String runName, String filePath, String type, Str
110111
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
111112
.addFormDataPart("file", fileToUpload.getName(),
112113
RequestBody.create(MediaType.parse("application/json"), fileToUpload))
113-
.addFormDataPart("some-field", "some-value")
114114
.build();
115115
HttpUrl.Builder httpBuilder = HttpUrl.parse(this.reportUrl).newBuilder();
116116
httpBuilder.addQueryParameter("runName", runName);

src/main/java/com/assertthat/plugins/standalone/Arguments.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ public class Arguments {
4343
private String jiraServerUrl;
4444
private String type = "cucumber";
4545
private String tags;
46+
private String token;
4647
private boolean numbered;
4748
private boolean ignoreCertErrors;
4849

4950
private boolean cleanupFeatures = true;
5051

5152
Arguments(String accessKey,
5253
String secretKey,
54+
String token,
5355
String projectId,
5456
String runName,
5557
String outputFolder,
@@ -69,12 +71,16 @@ public class Arguments {
6971
boolean cleanupFeatures) {
7072
this.accessKey = System.getenv("ASSERTTHAT_ACCESS_KEY");
7173
this.secretKey = System.getenv("ASSERTTHAT_SECRET_KEY");
74+
this.token = System.getenv("ASSERTTHAT_TOKEN");
7275
if (accessKey != null && !accessKey.trim().isEmpty()) {
7376
this.accessKey = accessKey;
7477
}
7578
if (secretKey != null && !secretKey.trim().isEmpty()) {
7679
this.secretKey = secretKey;
7780
}
81+
if (token != null && !token.trim().isEmpty()) {
82+
this.token = token;
83+
}
7884
this.projectId = projectId;
7985
if (outputFolder != null && !outputFolder.trim().isEmpty()) {
8086
this.outputFolder = outputFolder;
@@ -265,4 +271,12 @@ public boolean isCleanupFeatures() {
265271
public void setCleanupFeatures(boolean cleanupFeatures) {
266272
this.cleanupFeatures = cleanupFeatures;
267273
}
274+
275+
public String getToken() {
276+
return token;
277+
}
278+
279+
public void setToken(String token) {
280+
this.token = token;
281+
}
268282
}

src/main/java/com/assertthat/plugins/standalone/ArgumentsFeatures.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class ArgumentsFeatures extends Arguments{
44

55
public ArgumentsFeatures(String accessKey,
66
String secretKey,
7+
String token,
78
String projectId,
89
String outputFolder,
910
String proxyURI,
@@ -18,6 +19,7 @@ public ArgumentsFeatures(String accessKey,
1819
boolean cleanupFeatures) {
1920
super(accessKey,
2021
secretKey,
22+
token,
2123
projectId,
2224
null,
2325
outputFolder,

src/main/java/com/assertthat/plugins/standalone/ArgumentsReport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class ArgumentsReport extends Arguments{
44

55
public ArgumentsReport(String accessKey,
66
String secretKey,
7+
String token,
78
String projectId,
89
String runName,
910
String jsonReportFolder,
@@ -19,6 +20,7 @@ public ArgumentsReport(String accessKey,
1920

2021
super(accessKey,
2122
secretKey,
23+
token,
2224
projectId,
2325
runName,
2426
null,

src/main/java/com/assertthat/plugins/standalone/Main.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Main {
3434
public static void main(String[] args) throws IOException, JSONException {
3535
String ASSERTTHAT_ACCESS_KEY = System.getenv("ASSERTTHAT_ACCESS_KEY");
3636
String ASSERTTHAT_SECRET_KEY = System.getenv("ASSERTTHAT_SECRET_KEY");
37+
String ASSERTTHAT_TOKEN = System.getenv("ASSERTTHAT_TOKEN");
3738

3839
Options options = new Options();
3940

@@ -51,6 +52,13 @@ public static void main(String[] args) throws IOException, JSONException {
5152
secretKeyOption.setArgName("ASSERTTHAT_SECRET_KEY");
5253
options.addOption(secretKeyOption);
5354

55+
Option tokenOption = new Option("token", true, "API token");
56+
if (ASSERTTHAT_TOKEN == null || ASSERTTHAT_TOKEN.trim().isEmpty()) {
57+
tokenOption.setRequired(true);
58+
}
59+
tokenOption.setArgName("ASSERTTHAT_TOKEN");
60+
options.addOption(tokenOption);
61+
5462
Option projectIdOption = new Option("projectId", true, "Jira project id");
5563
projectIdOption.setRequired(true);
5664
projectIdOption.setArgName("ID");
@@ -186,6 +194,7 @@ public static void main(String[] args) throws IOException, JSONException {
186194
Arguments arguments = new Arguments(
187195
cmd.getOptionValue("accessKey"),
188196
cmd.getOptionValue("secretKey"),
197+
cmd.getOptionValue("token"),
189198
cmd.getOptionValue("projectId"),
190199
cmd.getOptionValue("runName"),
191200
cmd.getOptionValue("outputFolder"),
@@ -205,7 +214,7 @@ public static void main(String[] args) throws IOException, JSONException {
205214
cleanupFeaturesVal
206215
);
207216

208-
APIUtil apiUtil = new APIUtil(arguments.getProjectId(), arguments.getAccessKey(), arguments.getSecretKey(), arguments.getProxyURI(), arguments.getProxyUsername(), arguments.getProxyPassword(), arguments.getJiraServerUrl(), ignoreCertErrorsVal);
217+
APIUtil apiUtil = new APIUtil(arguments.getProjectId(), arguments.getAccessKey(), arguments.getSecretKey(), arguments.getToken(), arguments.getProxyURI(), arguments.getProxyUsername(), arguments.getProxyPassword(), arguments.getJiraServerUrl(), ignoreCertErrorsVal);
209218

210219
if (cmd.hasOption("features")) {
211220
File inZip =

src/main/java/com/assertthat/plugins/standalone/OkHttpClientBuilder.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class OkHttpClientBuilder {
1818

1919
private OkHttpClient.Builder builder = new OkHttpClient.Builder();
2020

21-
OkHttpClientBuilder authenticated(String username, String password) {
22-
builder.addInterceptor(new BasicAuthInterceptor(username, password));
21+
OkHttpClientBuilder authenticated(String username, String password, String token) {
22+
builder.addInterceptor(token != null? new TokenAuthInterceptor(token) : new BasicAuthInterceptor(username, password));
2323
return this;
2424
}
2525

@@ -94,4 +94,22 @@ public Response intercept(Chain chain) throws IOException {
9494

9595
}
9696

97+
class TokenAuthInterceptor implements Interceptor {
98+
99+
private String token;
100+
101+
TokenAuthInterceptor(String token) {
102+
this.token = token;
103+
}
104+
105+
@Override
106+
public Response intercept(Chain chain) throws IOException {
107+
Request request = chain.request();
108+
Request authenticatedRequest = request.newBuilder()
109+
.header("Authorization", "Bearer " + token).build();
110+
return chain.proceed(authenticatedRequest);
111+
}
112+
113+
}
114+
97115
}

0 commit comments

Comments
 (0)