-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.js
137 lines (121 loc) · 5.22 KB
/
report.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
const github = require("@actions/github");
Array.prototype.drop = function(start, end = this.length - start) {
this.splice(start, end);
return this;
}
const splitAndFilter = (item) => (char) => (clause) => item.split(char).filter(value => value !== clause);
const condenseStderr = item => {
const packageExp = new RegExp(`'(${item.package}(?:@.+)?)'`);
const err_array = item.stderr.split("\n");
const code = err_array[0];
const stderr = err_array
.drop(0,1)
.filter(value => value !== "")
.map(value => value.replace(/^npm ERR! (?:\d+)?/, "").trim())
.map(value => value.replace(packageExp, "`$1`"))
.join(" ");
return item.shortMessage !== ""
? `${code}\n${item.shortMessage}\n${stderr}`
: `${code}\n${stderr}`;
}
const markItemFailed = item => `- ![failed] \`${item.package}\`\n > ${condenseStderr(item)}\n`;
const setInstalledItem = item => {
if (item.failed) {
return `- ![failed] \`${item.package}\`\n > ${condenseStderr(item)}\n`;
} else {
return item.stdout.split("\n")
.filter(value => value !== "")
.drop(2,2)
.map(value => value.startsWith("+ ") ? value.replace(/(\+ )(\S+)/, `- ![success] \`$2\``) : ` > ${value}`)
.join("\n");
}
}
const setUpdatedItem = item => {
if (item.failed) {
return `- ![failed] \`${item.package}\`\n > ${condenseStderr(item)}\n`
} else {
return item.stdout.split("\n")
.filter(value => value !== "")
.drop(2,2)
.map(value => value.startsWith("+ ") ? value.replace(/(\+ )(\S+)/, `- ![success] \`$2\``) : ` > ${value}`)
.join("\n");
}
};
const setUninstalledItem = item => {
if (item.failed) {
return `- ![failed] \`${item.package}\`\n > ${condenseStderr(item)}\n`;
} else {
const stdout_array = item.stdout.split("\n").filter(value => value !== "").drop(1,2).map(value => ` > ${value}`);
const just_passed = stdout_array[0].startsWith(" > audited");
const package_state = `- ![${just_passed ? "passed" : "success"}] \`${item.package}\`\n`;
if (just_passed) stdout_array.unshift(` > Package does not seem to have been _installed_.`);
return package_state + stdout_array.join("\n");
}
};
const buildList = title => items => {
if (items.length < 1) return "";
const list = items.reduce((list, item) => {
let listItem;
switch (title) {
case "Installed":
listItem = setInstalledItem(item);
break;
case "Updated":
listItem = setUpdatedItem(item);
break;
case "Uninstalled":
listItem = setUninstalledItem(item);
break;
}
return list += `${listItem}\n`;
}, `### ${title}\n`) + `\n`;
return list;
};
const getConfigHtmlUrl = path => async octokit => {
try {
const file = await octokit.repos.getContent({
owner: github.context.payload.repository.owner.login,
repo: github.context.payload.repository.name,
path: path.replace(/^(?:\.\/|\/)/, ""),
ref: process.env.GITHUB_REF.replace(/^refs\/heads\//, "")
});
return `[\`${file.data.name}\`](${file.data.html_url})`;
} catch (error) {
const file_name = path.replace(/(?:(?!npm).)*/, "");
return `\`${file_name}\``;
}
}
const buildDescription = (install, update, uninstall, config) => {
const numberOfPackages = items => {
const number = items.length;
return `${number} package${number === 1 ? "" : "s"}`;
}
const tag = title => items => items.length > 0 ? `_${title}_ ${numberOfPackages(items)}` : "";
const installed = tag("install")(install);
const updated = tag("update")(update);
const uninstalled = tag("uninstall")(uninstall);
const opperations = ([installed, updated, uninstalled]
.filter(item => item !== "")
.map((item, index, array) => array.length > 1 && index === array.length - 1 ? `and ${item}` : item)
).join(", ");
return `An update to ${config} requested [\`@npm-worker\`][marketplace] ${opperations}.\n`;
}
const buildActivityReport = (install, update, uninstall) => config_path => async octokit => {
const marketplace = "[marketplace]: https://github.com/marketplace/actions/npm-worker";
const icon = "[icon]: https://github.com/mudlabs/npm-worker/raw/master/npm_worker_icon.png";
const success = "[success]: https://via.placeholder.com/15/15f06e/000000?text=+";
const failed = "[failed]: https://via.placeholder.com/15/f03c15/000000?text=+";
const passed = "[passed]: https://via.placeholder.com/15/e6c620/000000?text=+";
const sender = github.context.payload.sender;
const requester = `Requester [\`@${sender.login}\`](https://github.com/${sender.login})`;
const trigger = `Trigger ${github.context.sha}`;
const config_text_url = await getConfigHtmlUrl(config_path)(octokit);
const description = buildDescription(install, update, uninstall, config_text_url);
const installed = buildList("Installed")(install)
const updated = buildList("Updated")(update)
const uninstalled = buildList("Uninstalled")(uninstall);
const header = `> [![icon]][marketplace]\n> ${requester}\n> ${trigger}\n`;
const footer = `${marketplace}\n${icon}\n${success}\n${failed}\n${passed}`;
return `${header}\n\n${description}\n${installed}\n${updated}\n${uninstalled}\n\n${footer}`
}
exports.buildActivityReport = buildActivityReport;