-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
164 lines (143 loc) · 4.72 KB
/
helpers.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
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
const fs = require('fs');
const path = require('path');
/**
* @param {Octokit} octokit
* @param {Object} githubContext
* @param {string} pathLogs
* @param {boolean} keepOnlyErrorFiles
* @return {Promise<void>}
*/
const retrieveLogs = async (octokit, githubContext, pathLogs, keepOnlyErrorFiles) => {
await octokit.request(`GET ${githubContext.payload.workflow_run.logs_url}`, {}).then((response) => {
downloadFile(response.url, pathLogs, githubContext.runNumber + '.zip');
});
await getJobs(octokit, githubContext.payload.workflow_run.jobs_url, pathLogs, keepOnlyErrorFiles);
executeCommand(`ls -al ${pathLogs}`);
};
/**
* @param {Octokit} octokit
* @param {string} jobsUrl
* @param {string} pathLogs
* @param {boolean} keepOnlyErrorFiles
* @return {Promise<void>}
*/
const getJobs = async (octokit, jobsUrl, pathLogs, keepOnlyErrorFiles) => {
const jobs = await octokit.request(`GET ${jobsUrl}`, {
}).then((response) => {
console.log('response getJobs: ', response.data.jobs);
return response.data.jobs;
});
if (keepOnlyErrorFiles) {
console.log('Removing all files but error files');
removeAllFileButErrorFiles(pathLogs, jobs);
}
};
/**
* Jobs and steps has conclusion property, which can be success, failure, cancelled, skipped, timed_out, or action_required.
* We want to keep only the logs of the failed jobs and steps.
* In the archive we have a folder for each job, and inside files for each step
* -> we keep only the files of the failed steps, and move them into the folder ${pathLogs}.
* In the top folder, we also have a file for each job. --> We remove them
* @param {string} pathLogs
* @param {Object[]} jobs
*/
const removeAllFileButErrorFiles = (pathLogs, jobs) => {
removeFileFromFolder(pathLogs);
// We loop through the jobs and steps, and we delete the steps that are not is failure status, and move into the top folder the files of the failed steps
jobs.forEach((job) => {
const jobStepsFilePath = path.join(pathLogs, job.name);
job.steps.forEach((step) => {
const stepFiles = fs.readdirSync(jobStepsFilePath);
// Get the file for the steps, based that his name start with his step number : 1_ , 2_ , 3_ ...
const regex = new RegExp(`^${step.number}_.*`);
const matchingFiles = stepFiles.filter((file) => {
return regex.test(file);
});
// Delete all the files that are matching the regex
matchingFiles.forEach((file) => {
const absolutePath = path.join(jobStepsFilePath, file);
if (step.conclusion !== 'failure') {
console.log(`Deleting file ${absolutePath}`);
fs.unlinkSync(absolutePath);
}
});
});
});
// list all file in folder, and subfolder
executeCommand(`find ${pathLogs} -type f -maxdepth 2`);
};
/**
* We remove the files from the folder
* @param {string} pathLogs
*/
const removeFileFromFolder = (pathLogs) => {
const files = fs.readdirSync(pathLogs);
files.forEach((file) => {
const absolutePath = path.join(pathLogs, file);
if (fs.lstatSync(absolutePath).isFile()) {
console.log(`Deleting file ${absolutePath}`);
fs.unlinkSync(absolutePath);
}
});
};
/**
* @param {string} command
*/
const executeCommand = (command) => {
const execSync = require('child_process').execSync;
console.debug('Will execute command : ', command);
// get current working directory
execSync(command, {stdio: 'inherit'}, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
};
/**
* @param {string} folderName
*/
const createFolderIfNotExist = (folderName) => {
executeCommand(`mkdir -p ${folderName}`);
};
/**
* @param {string} url
* @param {string} fileName
*/
const downloadFromUrl = (url, fileName) => {
executeCommand(`curl -L "${url}" > ${fileName}`);
};
/**
* Use unzip to extract the archive into the folder
* @param {string} archiveNameWithPath
* @param {string} folderName
*/
const unzipArchiveIntoFolder = (archiveNameWithPath, folderName) => {
executeCommand(`unzip -o ${archiveNameWithPath} -d ${folderName}`);
};
/**
* @param {string} fileName
*/
const removeFile = (fileName) => {
executeCommand(`rm -f ${fileName}`);
};
/**
* @param {string} url
* @param {string} folderName
* @param {string} archiveName
*/
const downloadFile = (url, folderName, archiveName) => {
const archiveNameWithPath = `${folderName}/${archiveName}`;
createFolderIfNotExist(folderName);
downloadFromUrl(url, archiveNameWithPath);
unzipArchiveIntoFolder(archiveNameWithPath, folderName);
removeFile(archiveNameWithPath);
};
module.exports = {
retrieveLogs,
};