Skip to content

Commit

Permalink
Merge pull request #4 from tomassatka/master
Browse files Browse the repository at this point in the history
Added basic auth feature
  • Loading branch information
jbadeau authored Oct 26, 2020
2 parents 98a50ed + 96afad5 commit ffcd09f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gauge install xray-report
#### Installing specific version:

```bash
gauge install xray-report --version 0.1.0
gauge install xray-report --version 0.1.1
```

#### Offline installation
Expand Down Expand Up @@ -48,8 +48,11 @@ Add this plugin to your Gauge project by registering it in `manifest.json` file.
jira_url = https://your.jira.url
jira_username = <user>
jira_password = <password>
jira_authentication = cookie/basic
```
> Do **NOT** save the 'jira_password' in source code.
> Do **NOT** save the 'jira_password' in source code.
> Default jira_authentication is cookie if property not provided.
#### Add **TestCaseId:\<XrayTestKey>** tag to scenarios

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.gauge</groupId>
<artifactId>xray-report</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<name>Xray Report</name>
<description>Xray reporter</description>

Expand Down
45 changes: 33 additions & 12 deletions src/main/java/org/gauge/xray/Reporter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gauge.xray;

import com.google.common.net.HttpHeaders;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.gauge.Messages.Message;
Expand All @@ -11,6 +12,8 @@
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

public class Reporter {
Expand All @@ -21,6 +24,11 @@ public class Reporter {

private static final OkHttpClient client;

enum authentication {
cookie,
basic
}

static {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
Expand All @@ -30,7 +38,7 @@ public class Reporter {
}


public static void main(String[] args) throws IOException {
public static void main(String[] args) {
String portEnv = System.getenv("plugin_connection_port");
int port = Integer.parseInt(portEnv);
Socket socket;
Expand All @@ -56,12 +64,15 @@ public static void main(String[] args) throws IOException {
String jiraBaseUrl = getProperty("jira_url");
String jiraUsername = getProperty("jira_username");
String jiraPassword = getProperty("jira_password");
String authentication = System.getenv().getOrDefault("jira_authentication", "cookie");
Credentials credentials = new Credentials(jiraUsername, jiraPassword);
authenticate(jiraBaseUrl, gson
.toJson(credentials));

if (Reporter.authentication.cookie.name().equalsIgnoreCase(authentication)) {
authenticate(jiraBaseUrl, gson.toJson(credentials));
}
System.out.println(String.format("Uploading %d test execution(s) to Jira Xray...", reports.size()));
for (Report report : reports) {
upload(jiraBaseUrl, gson.toJson(report));
upload(jiraBaseUrl, gson.toJson(report), authentication, credentials);
}
System.exit(0);
return;
Expand All @@ -87,18 +98,28 @@ private static String getProperty(String name) {
return value;
}


private static void upload(String baseUrl, String body) throws IOException {
private static void upload(String baseUrl, String body, String authentication, Credentials credentials) throws IOException {
System.out.println(body);
Request request = new Request.Builder()
.url(baseUrl + "/rest/raven/1.0/import/execution")
.post(RequestBody.create(body, JSON))
.build();
Request request;
if (Reporter.authentication.basic.name().equalsIgnoreCase(authentication)) {
String auth = credentials.getUsername() + ":" + credentials.getPassword();
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
request = new Request.Builder()
.url(baseUrl + "/rest/raven/1.0/import/execution")
.header(HttpHeaders.AUTHORIZATION, authHeader)
.post(RequestBody.create(body, JSON))
.build();
} else {
request = new Request.Builder()
.url(baseUrl + "/rest/raven/1.0/import/execution")
.post(RequestBody.create(body, JSON))
.build();
}
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
System.err.println(String.format("Failed to uploaded test execution to Jira Xray %s", response.body().string()));
}
else {
} else {
System.out.println(String.format("Successfully uploaded test execution to Jira Xray"));
}
}
Expand Down

0 comments on commit ffcd09f

Please sign in to comment.