Skip to content

Commit

Permalink
fixes #50 ; add verbose mode (#51)
Browse files Browse the repository at this point in the history
* fixes #50 ; add verbose mode

* clarified verbose mode

* add tests for verbose mode. fixes also #48 for missing arguments on import results for Xray DC

Co-authored-by: Sergio Freire <[email protected]>
  • Loading branch information
bitcoder and Sergio Freire authored Jul 1, 2022
1 parent b3d6660 commit 15f952d
Show file tree
Hide file tree
Showing 16 changed files with 514 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- added "verbose" mode, to debug API requests/responses

### Fixed

## [0.6.0] - 2022-06-08
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ For Xray server/datacenter users (i.e., using Xray on Jira server/datacenter):
| `jiraToken` | `xray.jiraToken` | Jira PAT (Personal Access Token) used instead of username/password |
| `ignoreSslErrors` | `xray.ignoreSslErrors` | ignore SSL errors, e.g., expired certificate (default: false) |
| `timeout` | `xray.timeout` | connection timeout in seconds (default: 50) |
| `verbose` | `xray.verbose` | set to true to enable verbose mode (default: false); information will be visible if running mvn with debug output by using `-X` or `--debug` | true |

For Xray cloud users (i.e., using Xray on Jira cloud):

Expand All @@ -73,6 +74,7 @@ For Xray cloud users (i.e., using Xray on Jira cloud):
| `clientId` | `xray.clientId` | client id of the API key configured on Xray Cloud | xxxx... |
| `clientSecret` | `xray.clientSecret` | client id of the API key configured on Xray Cloud | xxxx... |
| `timeout` | `xray.timeout` | connection timeout in seconds (default: 50) | 50 |
| `verbose` | `xray.verbose` | set to true to enable verbose mode (default: false); information will be visible if running mvn with debug output by using `-X` or `--debug` | true |

In order to obtain the API key (client id + client secret pair) please ask you Jira admin (see reference at bottom).
There are also task specific configurations. More info ahead, on the respective task section.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>app.getxray</groupId>
<artifactId>xray-maven-plugin</artifactId>
<version>0.6.0</version>
<version>0.7.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>xray-maven-plugin</name>
<description>Maven plugin for interacting with Xray (server/datacenter and cloud), used in CI/CD for assisting in test automation flows, such as reporting test results back to Xray and, by consequence, Jira.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class ExportFeaturesMojo extends AbstractMojo {
@Parameter(property = "xray.timeout", required = false, defaultValue = "50")
private Integer timeout;

@Parameter(property = "xray.verbose", required = false)
private Boolean verbose;

/**
* Scope to filter the dependencies.
*/
Expand All @@ -88,6 +91,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withIssueKeys(issueKeys)
.withFilterId(filterId)
.build();
Expand All @@ -98,6 +103,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withIssueKeys(issueKeys)
.withFilterId(filterId)
.build();
Expand All @@ -106,6 +113,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withIssueKeys(issueKeys)
.withFilterId(filterId)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public class ImportFeaturesMojo extends AbstractMojo {
@Parameter(property = "xray.timeout", required = false, defaultValue = "50")
private Integer timeout;

@Parameter(property = "xray.verbose", required = false)
private Boolean verbose;

/**
* Scope to filter the dependencies.
*/
Expand Down Expand Up @@ -121,6 +124,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withProjectKey(projectKey)
.withProjectId(projectId)
.withSource(source)
Expand All @@ -132,6 +137,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withProjectKey(projectKey)
.withupdateRepository(updateRepository)
.build();
Expand All @@ -140,6 +147,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose)
.withProjectKey(projectKey)
.withupdateRepository(updateRepository)
.build();
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/app/getxray/maven/plugin/xray/ImportResultsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public class ImportResultsMojo extends AbstractMojo {
@Parameter(property = "xray.timeout", required = false, defaultValue = "50")
private Integer timeout;

@Parameter(property = "xray.verbose", required = false)
private Boolean verbose;

/**
* Scope to filter the dependencies.
*/
Expand Down Expand Up @@ -173,7 +176,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
app.getxray.xray.XrayResultsImporter.CloudBuilder xrayImporterBuilder = new XrayResultsImporter.CloudBuilder(clientId, clientSecret)
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout);
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose);
if (testInfoJson==null && testExecInfoJson==null) {
if (XrayResultsImporter.XRAY_FORMAT.equals(reportFormat) || XrayResultsImporter.CUCUMBER_FORMAT.equals(reportFormat) || XrayResultsImporter.BEHAVE_FORMAT.equals(reportFormat)) {
xrayImporter = xrayImporterBuilder.build();
Expand Down Expand Up @@ -218,12 +223,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
xrayImporterBuilder = new XrayResultsImporter.ServerDCBuilder(jiraBaseUrl, jiraToken)
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout);
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose);
} else {
xrayImporterBuilder = new XrayResultsImporter.ServerDCBuilder(jiraBaseUrl, jiraUsername, jiraPassword)
.withInternalTestProxy(useInternalTestProxy)
.withIgnoreSslErrors(ignoreSslErrors)
.withTimeout(timeout);
.withTimeout(timeout)
.withLogger(getLog())
.withVerbose(verbose);
}

if (testInfoJson==null && testExecInfoJson==null) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/app/getxray/xray/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.getxray.xray;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
Expand All @@ -9,9 +10,49 @@
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

import org.apache.maven.plugin.logging.Log;

public class CommonUtils {

public static void logRequest(Log logger, Request request) {
if (logger != null) {
logger.debug("REQUEST_URL: " + request.url().toString());
logger.debug("REQUEST_METHOD: " + request.method());

Request copy = request.newBuilder().build();
RequestBody body = copy.body();
if (body != null) {
logger.debug("REQUEST_CONTENT_TYPE: " + body.contentType().toString());
}
}
}

public static void logResponse(Log logger, Response response) {
logResponse(logger, response, true);
}

public static void logResponse(Log logger, Response response, boolean logBody) {
if (logger != null) {
logger.debug("RESPONSE_CONTENT_TYPE:" + response.header("Content-Type"));
logger.debug("RESPONSE_HTTP_STATUS: " + response.code());
if (logBody) {
logger.debug("RESPONSE_BODY:");
logger.debug("=======================");

try (ResponseBody responseBody = response.peekBody(1024 * 1024)) {
logger.debug(responseBody.string());
} catch (IOException e) {
//e.printStackTrace();
}
}
}
}

public static boolean isTrue(Boolean bool) {
return (bool!=null && bool);
}
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/app/getxray/xray/XrayFeaturesExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import okhttp3.RequestBody;
import okhttp3.Response;

import org.apache.maven.plugin.logging.Log;

// https://docs.getxray.app/display/XRAYCLOUD/Exporting+Cucumber+Tests+-+REST+v2
// https://docs.getxray.app/display/XRAY/Exporting+Cucumber+Tests+-+REST

Expand All @@ -40,6 +42,8 @@ public class XrayFeaturesExporter {
private Boolean ignoreSslErrors = false;
private Boolean useInternalTestProxy = false;
private Integer timeout = 50;
private Boolean verbose = false;
private Log logger;

private XrayFeaturesExporter(ServerDCBuilder builder) {
this.jiraBaseUrl = builder.jiraBaseUrl;
Expand All @@ -53,6 +57,8 @@ private XrayFeaturesExporter(ServerDCBuilder builder) {
this.ignoreSslErrors = builder.ignoreSslErrors;
this.useInternalTestProxy = builder.useInternalTestProxy;
this.timeout = builder.timeout;
this.verbose = builder.verbose;
this.logger = builder.logger;
}

private XrayFeaturesExporter(CloudBuilder builder) {
Expand All @@ -65,6 +71,8 @@ private XrayFeaturesExporter(CloudBuilder builder) {
this.ignoreSslErrors = builder.ignoreSslErrors;
this.useInternalTestProxy = builder.useInternalTestProxy;
this.timeout = builder.timeout;
this.verbose = builder.verbose;
this.logger = builder.logger;
}

public static class ServerDCBuilder {
Expand All @@ -80,6 +88,8 @@ public static class ServerDCBuilder {
private Boolean useInternalTestProxy = false;
private Boolean ignoreSslErrors = false;
private Integer timeout = 50;
private Boolean verbose = false;
private Log logger;

public ServerDCBuilder(String jiraBaseUrl, String jiraUsername, String jiraPassword) {
this.jiraBaseUrl = jiraBaseUrl;
Expand Down Expand Up @@ -107,6 +117,16 @@ public ServerDCBuilder withTimeout(Integer timeout) {
return this;
}

public ServerDCBuilder withVerbose(Boolean verbose) {
this.verbose = verbose;
return this;
}

public ServerDCBuilder withLogger(Log logger) {
this.logger = logger;
return this;
}

public ServerDCBuilder withIssueKeys(String issueKeys) {
this.issueKeys = issueKeys;
return this;
Expand Down Expand Up @@ -134,6 +154,8 @@ public static class CloudBuilder {
private Boolean ignoreSslErrors = false;
private Boolean useInternalTestProxy = false;
private Integer timeout = 50;
private Boolean verbose = false;
private Log logger;

public CloudBuilder(String clientId, String clientSecret) {
this.clientId = clientId;
Expand All @@ -155,6 +177,16 @@ public CloudBuilder withTimeout(Integer timeout) {
return this;
}

public CloudBuilder withVerbose(Boolean verbose) {
this.verbose = verbose;
return this;
}

public CloudBuilder withLogger(Log logger) {
this.logger = logger;
return this;
}

public CloudBuilder withIssueKeys(String issueKeys) {
this.issueKeys = issueKeys;
return this;
Expand Down Expand Up @@ -204,9 +236,10 @@ public String submitStandardServerDC(String outputPath) throws Exception {
}

request = new Request.Builder().url(builder.build()).get().addHeader("Authorization", credentials).build();
CommonUtils.logRequest(logger, request);
try {
response = client.newCall(request).execute();
// String responseBody = response.body().string();
CommonUtils.logResponse(logger, response);
if (response.isSuccessful()) {
unzipContentsToFolder(response.body().byteStream(), outputPath);
return ("ok");
Expand All @@ -226,10 +259,13 @@ public String submitStandardCloud(String outputPath) throws Exception {
+ "\" }";
RequestBody body = RequestBody.create(authenticationPayload, MEDIA_TYPE_JSON);
Request request = new Request.Builder().url(xrayCloudAuthenticateUrl).post(body).build();
CommonUtils.logRequest(logger, request);

Response response = null;
String authToken = null;
try {
response = client.newCall(request).execute();
CommonUtils.logResponse(logger, response, false);
String responseBody = response.body().string();
if (response.isSuccessful()) {
authToken = responseBody.replace("\"", "");
Expand All @@ -255,9 +291,10 @@ public String submitStandardCloud(String outputPath) throws Exception {
}

request = new Request.Builder().url(builder.build()).get().addHeader("Authorization", credentials).build();
CommonUtils.logRequest(logger, request);
try {
response = client.newCall(request).execute();
// String responseBody = response.body().string();
CommonUtils.logResponse(logger, response);
if (response.isSuccessful()) {
unzipContentsToFolder(response.body().byteStream(), outputPath);
return ("ok");
Expand Down
Loading

0 comments on commit 15f952d

Please sign in to comment.