-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
246 lines (235 loc) · 11.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import AQConstant from './core/AQConstant';
import AQFormValidate from './core/AQFormValidate';
import AQRestClient from './core/AQRestClient';
import { AQUtil } from './core/AQUtil';
const core = require("@actions/core");
async function testConnection(appURL:string, userName:string, apiKey:string, tenantCode:string, jobId:string,
runParam:string, proxyHost:string, proxyPort:string, maxWaitTimeInMins: string) {
AQRestClient.setBaseURL(appURL, tenantCode);
if (proxyHost && proxyHost.length > 0) {
AQRestClient.setProxy(proxyHost, +proxyPort);
} else {
AQRestClient.setProxy("", 0);
}
const runParamJsonPayload = AQUtil.getRunParamJsonPayload(runParam);
const res = await AQRestClient.testConnection(apiKey, userName, jobId, runParamJsonPayload, getMaxWaitTime(maxWaitTimeInMins));
return res;
}
function sleep (milliSeconds:number) {
return new Promise(resolve => setTimeout(resolve, milliSeconds));
}
function isFloat(val: number) {
return typeof val === "number" && !Number.isNaN(val) && !Number.isInteger(val);
}
function inRange(val: number) {
return val >= -1 && val <= 100;
}
function getMaxWaitTime(maxWaitTimeInMins: string) {
if (!maxWaitTimeInMins) {
return AQConstant.JOB_PICKUP_RETRY_TIME_THRESHOLD_IN_MINS;
}
if (Number.isInteger(+maxWaitTimeInMins)) {
return +maxWaitTimeInMins;
}
return AQConstant.JOB_PICKUP_RETRY_TIME_THRESHOLD_IN_MINS;
}
async function executeJob(appURL:string, userName:string, apiKey:string, tenantCode:string, jobId:string,
runParam:string, proxyHost:string, proxyPort:string, stepFailureThreshold: string, maxWaitTimeInMins: string) {
let summaryObj = null;
let realJobPid = 0;
let failureThreshold = +stepFailureThreshold;
try {
AQRestClient.setBaseURL(appURL, tenantCode);
if (proxyHost && proxyHost.length > 0) {
AQRestClient.setProxy(proxyHost, +proxyPort);
} else {
AQRestClient.setProxy("", 0);
}
console.log("******************************************");
console.log("*** Begin: ACCELQ Test Automation Step ***");
console.log("******************************************");
console.log();
const runParamJsonPayload = AQUtil.getRunParamJsonPayload(runParam);
const maxWaitTime = getMaxWaitTime(maxWaitTimeInMins);
const realJobObj = await AQRestClient.triggerJob(apiKey, userName, jobId, runParamJsonPayload, maxWaitTime);
if (realJobObj == null) {
throw new Error("Unable to submit the Job, check plugin log stack");
}
if (realJobObj["cause"] != null) {
throw new Error(realJobObj["cause"]);
}
realJobPid = realJobObj["pid"];
let passCount = 0, failCount = 0, totalCount = 0, notRunCount = 0;
let jobStatus = "";
let attempt = 0;
const startTime = Date.now();
const resultAccessURL = AQRestClient.getResultExternalAccessURL(realJobPid.toString(), tenantCode);
let error = false;
let hasLoggedLinks = false;
while(true) {
try {
summaryObj = await AQRestClient.getJobSummary(realJobPid, apiKey, userName);
error = false;
}catch(e: any) {
error = true;
console.warn("Warn: Issue fetching Job Summary: ", e.message);
}
if (!error) {
if (summaryObj["cause"] != null) {
throw new Error(summaryObj["cause"]);
}
if (summaryObj["summary"] != null) {
summaryObj = summaryObj["summary"];
}
passCount = +summaryObj["pass"];
failCount = +summaryObj["fail"];
notRunCount = +summaryObj["notRun"];
if (attempt == 0) {
attempt = 1;
const jobPurpose = summaryObj["purpose"];
const scenarioName = summaryObj["scnName"];
const testSuiteName = summaryObj["testSuiteName"];
const totalTestCases = summaryObj["testcaseCount"];
if (testSuiteName != null && testSuiteName.length > 0) {
console.log("Test Suite Name: " + testSuiteName);
} else {
console.log("Scenario Name: " + scenarioName);
}
console.log("Purpose: " + jobPurpose);
console.log("Total Test Cases: " + totalTestCases);
console.log("Step Failure threshold: " + stepFailureThreshold);
console.log("Max Wait Time in Minutes: " + maxWaitTime);
console.log();
if (isFloat(failureThreshold)) {
failureThreshold = Math.trunc(failureThreshold);
console.log(`Warning: Invalid value (${failureThreshold}) passed for Step Failure Threshold. Truncating the value to ${failureThreshold} (Only integers between 0 and 100, and -1 are allowed).`);
}
if (!inRange(failureThreshold)) {
console.log(`Warning: Ignoring the Step Failure threshold. Invalid value (${failureThreshold}) passed. Valid values are 0 to 100, or -1 to ignore threshold.`);
failureThreshold = 0;
}
}
jobStatus = summaryObj["status"].toUpperCase();
if (jobStatus !== AQConstant.TEST_JOB_STATUS.SCHEDULED.toUpperCase() && !hasLoggedLinks) {
hasLoggedLinks = true;
console.log("Results Link: " + resultAccessURL);
console.log("Need to abort? Click on the link above, login to ACCELQ and abort the run.");
console.log();
}
if (jobStatus == AQConstant.TEST_JOB_STATUS.COMPLETED.toUpperCase()) {
const res = " " + AQUtil.getFormattedTime(summaryObj["startTimestamp"], summaryObj["completedTimestamp"]);
console.log("Status: " + summaryObj["status"].toUpperCase() + " ("+res.trim()+")");
} else {
console.log("Status: " + summaryObj["status"].toUpperCase());
}
if (hasLoggedLinks) {
totalCount = passCount + failCount + notRunCount;
console.log("Total " + totalCount + ": "
+ "" + passCount +" Pass / " + failCount + " Fail");
console.log();
}
if (jobStatus == AQConstant.TEST_JOB_STATUS.SCHEDULED.toUpperCase() && AQUtil.isWaitTimeExceeded(startTime, maxWaitTime)) {
throw new Error (AQConstant.LOG_DELIMITER + "No agent available to pickup the job");
}
if((jobStatus == AQConstant.TEST_JOB_STATUS.COMPLETED.toUpperCase())
|| (jobStatus == AQConstant.TEST_JOB_STATUS.ABORTED.toUpperCase())
|| (jobStatus == AQConstant.TEST_JOB_STATUS.FAILED.toUpperCase())
|| (jobStatus == AQConstant.TEST_JOB_STATUS.ERROR.toUpperCase())){
break;
}
}
await sleep(AQConstant.JOB_STATUS_POLL_TIME);
}
console.log("Results Link: " + resultAccessURL);
console.log();
const failedPercentage = Math.trunc((failCount / totalCount) * 100);
if (jobStatus == AQConstant.TEST_JOB_STATUS.ABORTED.toUpperCase()
|| jobStatus == AQConstant.TEST_JOB_STATUS.FAILED.toUpperCase()
|| jobStatus == AQConstant.TEST_JOB_STATUS.ERROR.toUpperCase()) {
throw new Error(AQConstant.LOG_DELIMITER + "Run Failed");
} else if(failCount > 0) {
if(failureThreshold != -1 && failedPercentage >= failureThreshold) {
throw new Error(AQConstant.LOG_DELIMITER + "Automation test step failed (test case failure count exceeds the threshold limit)");
}
}
return {
jobId: realJobPid,
status: true,
error: null
};
} catch(e) {
summaryObj = await AQRestClient.getJobSummary(realJobPid, apiKey, userName);
if (summaryObj["cause"] != null) {
throw new Error(summaryObj["cause"]);
}
if (summaryObj["summary"] != null) {
summaryObj = summaryObj["summary"];
}
console.log("Status: " + summaryObj["status"]);
console.log("Pass: " + summaryObj["pass"]);
return {
jobId: realJobPid,
status: false,
error: e,
};
}
}
async function run() {
try {
const appURL = core.getInput('appURL', {required: true}) || "";
const userName = core.getInput('userName', {required: true}) || "";
const apiKey = core.getInput('apiKey', {required: true}) || "";
const tenantCode = core.getInput('tenantCode', {required: true}) || "";
const jobId = core.getInput('jobId', {required: true}) || "";
const runParam = core.getInput('runParam') || "";
const proxyHost = core.getInput('proxyHost') || "";
const proxyPort = core.getInput('proxyPort') || "";
const stepFailureThreshold = core.getInput('stepFailureThreshold') || "";
const maxWaitTimeInMins = core.getInput('maxWaitTimeInMins') || "";
// validateFORM
let res: string | null | {
jobId: number; status: boolean, error?: any };
res = AQFormValidate.validateAppURL(appURL);
if (res != null) {
throw new Error('ACCELQ App URL: ' + res);
}
res = AQFormValidate.validateUserId(userName);
if (res != null) {
throw new Error('ACCELQ User ID: ' + res);
}
res = AQFormValidate.validateAPIKey(apiKey);
if (res != null) {
throw new Error('API Key: ' + res);
}
res = AQFormValidate.validateTenantCode(tenantCode);
if (res != null) {
throw new Error('Tenant Code: ' + res);
}
res = AQFormValidate.validateJobID(jobId);
if (res != null) {
throw new Error('ACCELQ CI Job ID: ' + res);
}
// test connection
res = await testConnection(appURL, userName, apiKey, tenantCode, jobId, runParam, proxyHost, proxyPort, maxWaitTimeInMins);
if (res === null) {
throw new Error("Something went wrong in extension");
} else if (res) {
throw new Error(res);
}
// executeJob
res = await executeJob(appURL, userName, apiKey, tenantCode, jobId, runParam, proxyHost, proxyPort, stepFailureThreshold, maxWaitTimeInMins);
if (res.status) {
console.log('Run Completed!!!');
} else {
core.setFailed(res.error?.message || "Job Failed!!!");
}
}
catch (err: any) {
core.setFailed(err.message);
} finally {
console.log("**********************************************");
console.log("*** Completed: ACCELQ Test Automation Step ***");
console.log("**********************************************");
}
}
run();