forked from bcgov/SIMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadResult.ts
57 lines (54 loc) · 1.68 KB
/
uploadResult.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
import axios, { AxiosResponse } from "axios";
import FormData from "form-data";
import fs from "fs";
const TQ_USERNAME = process.env.TQ_USER_NAME;
const TQ_PASSWORD = process.env.TQ_PASSWORD;
const TQ_SECRET = process.env.TQ_SECRET;
const TQ_RUN_ID = process.env.TQ_RUN_ID; //( default id is 26606. Will need to change once we have integrated TQ account for the team)
const TQ_API_URL = 'https://api.testquality.com/api'
async function getAuthToken (): Promise<AxiosResponse<String>> {
const auth_url = `${TQ_API_URL}/oauth/access_token`;
const body = {
grant_type: "password",
client_id: "2",
client_secret: TQ_SECRET,
username: TQ_USERNAME,
password: TQ_PASSWORD,
};
const settings = {
headers: {
"Content-Type": "application/json",
},
};
try {
console.log("Fetching the token from TestQuality");
const response = await axios.post(auth_url, body, settings);
console.log("Token Fetched");
return response.data["access_token"];
} catch (error) {
console.error(error);
throw error;
}
};
async function uploadResultFile() {
const auth = await getAuthToken();
const url = `${TQ_API_URL}/plan/${TQ_RUN_ID}/junit_xml`;
var formData = new FormData();
var resultFile = fs.createReadStream("test-results.xml");
formData.append("file", resultFile, "test-results.xml");
try {
const settings = {
headers: {
...formData.getHeaders(),
Authorization: `Bearer ${auth}`,
},
};
await axios.post(url, formData, settings);
console.log("Successfully uploaded the test results file to TestQuality");
return true;
} catch (error) {
console.error(error);
throw error;
}
}
uploadResultFile();