就在昨天,我遇到了同样的问题。ZAPI 文档确实令人困惑。我找到了以下解决方案:
获取作业进度的GET请求具有以下形式:http://jira/rest/zapi/latest/execution/jobProgress/0001498157843923-5056b64fdb-0001
其中0001498157843923-5056b64fdb-0001是特定 jobProgressToken 的值
由于某些异步操作而获得 jobProgressToken 后,我的代码正在等待进度变为 1(它从零上升到 1)
//get the job progress token as a result of some async operation invocation
String jobProgressToken = new JSONObject(zapiResponse).getString("jobProgressToken");
waitAsyncJobToBeCompleted(jobProgressToken);
void waitAsyncJobToBeCompleted(String jobProgressToken) {
double jobProgress;
do {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
LOG.error("Error while try to make the thread sleeping 500ms. " + e.getLocalizedMessage());
e.printStackTrace();
}
jobProgress = getJobProgress(jobProgressToken);
} while (Double.compare(jobProgress, 1) <0);
private double getJobProgress(String jobProgressToken) {
URI uri = makeUriFromString(String.format(
GET_GetJobProgress, //Get request pattern
connectionParameters.getJiraUrl(),//host
jobProgressToken)); //parameters
HttpResponse response = executeHttpRequestWithResponse(new HttpGet(uri));
String zapiResponse = null;
try {
zapiResponse = EntityUtils.toString(response.getEntity());
LOG.trace("ZAPI RESPONSE: " + zapiResponse);
EntityUtils.consumeQuietly(response.getEntity()); //cleanup the HTTP response
double progress = new JSONObject(zapiResponse).getDouble("progress");
LOG.debug("Job progress: " + progress);
return progress;
} catch (IOException e) {
String err = String.format("Error while getting Zephyr API response: %s",
e.getLocalizedMessage());
LOG.fatal(err);
throw new RestApiException(err, e);
} catch (JSONException e) {
String err = String.format("Error while retrieving the job progress from JSON: %s\n%s",
zapiResponse, e.getLocalizedMessage());
LOG.fatal(err);
throw new RestApiException(err, e);
}
}
这都是魔法:)
以下两个日志:第一个用于克隆测试周期,第二个用于删除
ZephyrClient.invokeHttpPost - URI=http://jira/rest/zapi/latest/cycle JSON payload={"projectId": "13795","clonedCycleId": 2643,"name": "ZAPI client test","description": "Created With ZAPI client unit test","versionId": "-1"}
ZephyrClient.cloneTestCycle - RESPONSE JSON: {"jobProgressToken":"0001498218783350-5056b64fdb-0001"}
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/execution/jobProgress/0001498218783350-5056b64fdb-0001 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.getJobProgress - ZAPI RESPONSE: {"timeTaken":"0 min, 1 sec","stepMessage":"","summaryMessage":"","errorMessage":"","progress":0.56,"message":"","stepLabel":"","stepMessages":[]}
ZephyrClient.getJobProgress - Job progress: 0.56
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/execution/jobProgress/0001498218783350-5056b64fdb-0001 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.getJobProgress - ZAPI RESPONSE: {"timeTaken":"0 min, 1 sec","stepMessage":"","summaryMessage":"","errorMessage":"","progress":0.98,"message":"","stepLabel":"","stepMessages":[]}
ZephyrClient.getJobProgress - Job progress: 0.98
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/execution/jobProgress/0001498218783350-5056b64fdb-0001 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.getJobProgress - ZAPI RESPONSE: {"timeTaken":"0 min, 2 sec","stepMessage":"","summaryMessage":"","errorMessage":"","progress":0.98,"message":"","stepLabel":"","stepMessages":[]}
ZephyrClient.getJobProgress - Job progress: 0.98
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/execution/jobProgress/0001498218783350-5056b64fdb-0001 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.getJobProgress - ZAPI RESPONSE: {"timeTaken":"0 min, 3 sec","stepMessage":"","summaryMessage":"","errorMessage":"","progress":1.0,"message":"Cycle ZAPI client test created successfully.","stepLabel":"","stepMessages":[]}
ZephyrClient.getJobProgress - Job progress: 1.0
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/cycle?projectId=13795&versionId=-1 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.invokeHttpDelete - URI=http://jira/rest/zapi/latest/cycle/2727
ZephyrClient.deleteTestCycle - RESPONSE JSON: {"jobProgressToken":"0001498218815183-5056b64fdb-0001"}
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/execution/jobProgress/0001498218815183-5056b64fdb-0001 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
ZephyrClient.getJobProgress - ZAPI RESPONSE: {"timeTaken":"0 min, 0 sec","stepMessage":"","summaryMessage":"","errorMessage":"","progress":1.0,"message":"{\"success\":\"Cycle ZAPI client test успешно удален\"}","stepLabel":"","stepMessages":[]}
ZephyrClient.getJobProgress - Job progress: 1.0
ZephyrClient.executeHttpRequestWithResponse - HTTP REQUEST: GET http://jira/rest/zapi/latest/cycle?projectId=13795&versionId=-1 HTTP/1.1
ZephyrClient.executeHttpRequestWithResponse - HTTP RESPONSE: HTTP/1.1 200 OK
PS这不是关于应该如何做的。这是关于它如何为我工作;)