-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 2.04 KB
/
index.js
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
const core = require('@actions/core');
const exec = require('@actions/exec');
const https = require('https');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
async function run() {
try {
const repoName = core.getInput('repo_name');
const splittedRepoName = repoName.split('-');
const questId = splittedRepoName[splittedRepoName.length - 1];
const branchName = core.getInput('branch_name');
const testInfo = await getTaskFile(questId, branchName);
if(testInfo){
const fileUrl = `https://devpass-api-bucket.s3.amazonaws.com/testes/${testInfo.test_file}`;
await downloadFile(fileUrl, testInfo.test_path);
for (const command of testInfo.test_command){
await exec.exec(command);
}
} else {
core.setFailed('Test not found!');
}
} catch (error) {
core.setFailed(error.message);
}
}
async function getTaskFile(questId, branchName) {
const testUrl = 'https://devpass-api-bucket.s3.amazonaws.com/testes/testinfo.json';
const testFile = await getFile(testUrl);
const testDictionary = JSON.parse(testFile);
var testInfo = testDictionary[questId][branchName];
return testInfo;
}
async function getFile(fileUrl) {
return new Promise((resolve, reject) => {
https.get(fileUrl, response => {
let fileContent = '';
response.on('data', data => {
fileContent += data;
});
response.on('end', () => {
resolve(fileContent);
});
}).on('error', error => {
reject(error);
});
});
};
async function downloadFile(fileUrl, fileName) {
return new Promise((resolve, reject) => {
const filePath = path.resolve(fileName);
const fileDir = path.dirname(filePath);
fs.mkdirSync(fileDir, { recursive: true });
const file = fs.createWriteStream(filePath);
https.get(fileUrl, response => {
response.pipe(file);
file.on('finish', () => {
file.close(resolve);
});
}).on('error', error => {
fs.unlink(filePath);
reject(error);
});
});
}
run();