-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
104 lines (93 loc) · 3.27 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
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
const core = require('@actions/core');
const github = require('@actions/github');
const markdownLinkCheck = require('markdown-link-check');
const fs = require('fs').promises;
const klaw = require('klaw');
const util = require('util');
const markdownLinkCheckPromised = util.promisify(markdownLinkCheck);
const quote = '```';
const getBody = (errors) => {
const items = errors.map(({ result, item }) => `- [ ] ${quote}${result.link}${quote} in ${quote}${item.path}${quote}`);
const lines = [
`Hello!`,
`Found following broken links:`,
'',
...items,
'',
`Hope you will able to fix it soon!`,
`Greetings,`,
`Report-link-action-bot!`
];
return lines.join("\n");
}
const main = async () => {
const errors = [];
const octokit = new github.GitHub(core.getInput('github_token'));
for await (const item of klaw('README.md')) {
if (item.stats.isDirectory()) {
continue;
}
if (!item.path.toLowerCase().endsWith('.md')) {
continue;
}
const content = await fs.readFile(item.path, { encoding: 'utf-8' });
console.log("Processing file", item.path);
const results = await markdownLinkCheckPromised(content, {
httpHeaders: [
{ "urls": ["https://docs.github.com/"], "headers": {"Accept-Encoding": "zstd, br, gzip, deflate"}}
],
ignorePatterns: [
{ pattern: new RegExp("^(?!https{0,1}://).*$") }
],
});
results.forEach((result) => {
if (result.status === 'dead') {
core.error(`'${result.link}' in '${item.path}' is ${result.status}: ${result.err}`);
errors.push({ result, item });
} else {
core.debug(`'${result.link}' in '${item.path}' is ${result.status}`)
}
});
}
const context = github.context;
let { data: issues } = await octokit.issues.listForRepo({
...context.repo,
state: 'open',
labels: ['report-link']
});
issues = issues.filter(x => x.title === 'Broken link found!');
body = getBody(errors);
if (errors.length === 0 && issues.length > 0) {
const { data: updatedIssue } = await octokit.issues.update({
...context.repo,
state: 'closed',
issue_number: issues[0].number
});
console.log("Issue closed:", updatedIssue.html_url);
return;
} else if (errors.length > 0 && issues.length == 0) {
const { data: newIssue } = await octokit.issues.create({
...context.repo,
labels: ['report-link'],
title: 'Broken link found!',
body,
});
console.log("New issue created:", newIssue.html_url);
} else if (errors.length > 0 && issues[0].body !== body) {
const { data: updatedIssue } = await octokit.issues.update({
...context.repo,
issue_number: issues[0].number,
body,
});
console.log("Issue updated:", updatedIssue.html_url);
} else {
console.log("Nothing to do!");
}
}
if (require.main === module) {
main().catch(err => {
console.log(err);
core.setFailed(err.message);
})
}
module.exports = main;