-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (59 loc) · 2.08 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
const core = require('@actions/core');
const github = require('@actions/github');
const { execSync } = require("child_process");
const { parseFile, parseChart, parseFormats } = require('./utils/parse.js');
const createBarChart = require('./utils/chart.js');
const exec = (command) => {
return execSync(command, {encoding: 'utf8'}).trim();
}
const run = async () => {
try {
const options = core.getInput('options');
const arguments = core.getInput('arguments');
const token = core.getInput('token');
const context = github.context;
const { pull_request } = context.payload;
const octokit = github.getOctokit(token);
const output = exec(`npx jscpd ${options} ${arguments} | sed 's/\x1b\[[0-9;]*m//g'`);
if(output.split(/\r\n|\r|\n/).length === 1){
await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_request.number,
body: "dry-code-action: **No Clone Found**"
});
return;
}
const result = parseFile(output);
const extension = /(?:\.([^.]+))?$/;
const files = result[0].split(" ").map((element) => {
if(extension.exec(element)[1]){
return `\`${element}\``;
}
return element;
}).join(' ');
const chart = parseChart(result[1])
const Formats = parseFormats(chart[1]);
const duplicatedLinesObject = Object.fromEntries(
new Map(Formats.map(row => [
row[0], parseInt(row[5])
]))
);
const duplicatedLinesChart = createBarChart(duplicatedLinesObject);
const duplicatedTokensObject = Object.fromEntries(
new Map(Formats.map(row => [
row[0], parseInt(row[6])
]))
);
const duplicatedTokensChart = createBarChart(duplicatedTokensObject);
const barCharts = "Duplicated Lines\n\n" + duplicatedLinesChart + "\n\nDuplicated Tokens\n\n" + duplicatedTokensChart
const body = files + barCharts;
await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_request.number,
body: body
});
} catch (error) {
core.setFailed(error.message);
}
}
run();