-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetricsMethods.js
227 lines (199 loc) · 5.64 KB
/
metricsMethods.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
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
const path = require("path");
const fs = require("fs");
const util = require("util");
const execSync = require("child_process").execSync;
const logger = require("pino")({
prettyPrint: { colorize: true },
translateTime: false
});
const _ = require("lodash");
const readFile = util.promisify(fs.readFile);
/**
* metricsMethods
*
* Contains all methods responsible for calculating the metrics
*
* @author Henrique Dias ([email protected])
*/
let metrics = [], metricNames = [], metricGroups = [];
// eslint-disable-next-line one-var
let lastReport;
/**
* Used to load the metrics from the config
* @param {Array} configMetrics
* @returns {boolean}
*/
function loadMetrics(configMetrics) {
if (_.isArray(configMetrics)) {
metrics = configMetrics;
return true;
}
return false;
}
/**
*Sets the local last report with `report` passed by argument
* @param {Array} report
*/
function setLastReport(report) {
lastReport = report;
}
/**
* Returns an array with the last report metrics
* @returns {Array}
*/
function getLastReport() {
return lastReport;
}
/**
* Finds a repo report from the last report using a given `repoName`
* @param {string} repoName
* @returns {object}
*/
function getRepoFromLastReport(repoName) {
let repo;
if (_.isArray(lastReport)) {
lastReport.forEach((r) => {
if (r.repository === repoName) {
repo = r;
}
});
}
return repo;
}
/**
* Groups all metrics by "group"
* @returns {Object}
*/
function getMetricGroups() {
return _.groupBy(metricGroups, "group");
}
/**
* Returns the last `metricName`for a `repo`
* @param {object} repo
* @param {string} metricName
* @returns {object | undefined}
*/
function lastReportMetric(repo, metricName) {
let result;
if (_.isArray(lastReport)) {
lastReport.forEach((el) => {
if (el.repository === repo.label) {
el.metrics.forEach((metric) => {
if (metric.info.name === metricName) {
result = metric;
}
});
}
});
}
return result;
}
/**
* Executes a `command` in a `folder`
* @param {string} folder
* @param {string} command
* @returns {string}
*/
function executeCommandSync(folder, command) {
try {
return execSync(`cd ${folder} && ${command}`);
} catch (err) {
logger.error(`Error executing ${command} on ${folder}`);
return null;
}
}
/**
* Gets last commit hash for a given repo
* @param {string} repoFolder
* @returns {string}
*/
function getLastCommit(repoFolder) {
const result = executeCommandSync(repoFolder, "git rev-parse HEAD").toString();
result.replace("\n", "");
return result;
}
/**
* Runs all `metrics` (previously loaded) on a given `repo`
* `repoFolder` and `packageJson` are used to pass information to the metric being evaluated.
* @param {Object} repo
* @param {Object} repoFolder
* @param {Object} packageJson
*/
async function runMetrics(repo, repoFolder, packageJson) {
return await Promise.all(metrics.map((Metric) => {
const current = new Metric(repo, repoFolder, packageJson);
if (metricNames.indexOf(current.info().name) === -1) {
metricNames.push(current.info().name);
metricGroups.push(current.info());
}
const hash = getLastCommit(repoFolder);
const lastResult = lastReportMetric(repo, current.info().name);
if (_.isObject(lastResult) && (hash === lastResult.hashLastCommit)) {
logger.info(`'${current.info().name}' for '${repo.label}' used from last report.`);
return lastResult;
}
return current.verify().then((verify) => {
if (verify) {
return current.execute().then((metricResult) => {
if (_.isObject(metricResult) && _.isBoolean(metricResult.result)) {
return {
info: current.info(),
result: { result: metricResult.result.toString() },
hashLastCommit: hash
};
}
return {
info: current.info(),
result: metricResult,
hashLastCommit: hash
};
});
}
logger.warn(`'${current.info().name}' metric not available for '${repo.label}' repository.`);
return {
info: current.info(),
result: "-",
hashLastCommit: hash
};
});
}));
}
/**
* Calculates all metrics for a give `repo`
* @param {object} repo
* @param {string} repoFolder
* @returns {object}
*/
async function getMetricsForRepo(repo, repoFolder) {
let packageJson;
try {
packageJson = JSON.parse(await readFile(path.join(repoFolder, "package.json"), "utf8"));
} catch (error) {
packageJson = null;
logger.error("cannot read 'package.json'");
return null;
}
if (_.isObject(packageJson)) {
return runMetrics(repo, repoFolder, packageJson).then((res) => {
return {
repository: repo.label,
metrics: res,
installedGitHash: repo.installedGitHash
};
});
}
throw new Error("folder does not exist");
}
function getMetricNames() {
return metricNames;
}
module.exports = {
runMetrics,
loadMetrics,
getMetricsForRepo,
getMetricNames,
getMetricGroups,
setLastReport,
getLastReport,
getRepoFromLastReport
};