forked from MokkeMeguru/pr-labeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-action.js
94 lines (87 loc) · 2.58 KB
/
run-action.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
const createLabelIfNotExists = require('./github/create-label-if-not-exists');
const addLabel = require('./github/add-label');
const removeLabel = require('./github/remove-label');
const existsLabel = require('./github/exists-label');
const getLabelConfig = (tools) => {
const labelConfig = [
{
name: 'SIZE: very small',
size: tools.inputs.xs_max_size,
color: 'abdee6',
},
{
name: 'SIZE: small',
size: tools.inputs.s_max_size,
color: 'abdee6',
},
{
name: 'SIZE: medium',
size: tools.inputs.m_max_size,
color: 'abdee6',
},
{
name: 'SIZE: large',
size: tools.inputs.l_max_size,
color: 'abdee6',
},
{
name: 'SIZE: very large',
size: Infinity,
color: 'abdee6',
},
];
return labelConfig;
};
const createLabelsIfNotExists = async (tools, labelConfig) => {
await Promise.all(
labelConfig.map((item) =>
createLabelIfNotExists(tools, item.name, { color: item.color }),
),
);
};
/**
* @param {import('actions-toolkit').Toolkit} tools
*/
const getNumberOfLines = async (tools) => {
try {
tools.log.info(`Getting the number of lines`);
const { data: files } = await tools.github.pulls.listFiles({
...tools.context.repo,
pull_number: tools.context.issue.number,
});
const numberOfLines = files.reduce((accumulator, file) => {
if (file.filename.match(tools.inputs.exclude_files)) {
tools.log.info(`Excluding file from the counting ${file.filename}`);
return accumulator;
}
tools.log.info(`Adding file to the counting: ${file.filename} The number of lines is: ${file.changes}`);
return accumulator + file.changes;
}, 0);
tools.log.info(`Number of lines changed: ${numberOfLines}`);
return numberOfLines;
} catch (error) {
tools.log.info(
`Error happens when we listing the files of the pull request: ${error}`,
);
}
};
const assignLabelForLineChanges = async (tools, numberOfLines, labelConfig) => {
await Promise.all(
labelConfig.map(async (item) => {
const { name } = item;
if (await existsLabel(tools, name)) {
await removeLabel(tools, name);
}
}),
);
const element = labelConfig.find((elem) => numberOfLines <= elem.size);
if (element) {
await addLabel(tools, element.name);
}
};
module.exports = async (tools) => {
const labelConfig = getLabelConfig(tools);
await createLabelsIfNotExists(tools, labelConfig);
const numberOfLines = await getNumberOfLines(tools);
await assignLabelForLineChanges(tools, numberOfLines, labelConfig);
};