-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (122 loc) · 5.03 KB
/
index.js
File metadata and controls
147 lines (122 loc) · 5.03 KB
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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs')
const path = require("path")
const https = require('https');
const spellcheck = require('markdown-spellcheck');
async function run() {
try {
const spelling_file_url = core.getInput('spelling-file-url');
const spelling_list = core.getInput('spelling-list');
const validate_visible_sections_only = core.getBooleanInput('validate-visible-sections-only');
const versionrc_path = '.versionrc';
const dictionary_path = path.join(__dirname, 'dictionaries', 'en_US');
const fallback_spelling_path = path.join(__dirname, 'fallback.spelling');
const downloaded_spelling_path = path.join(__dirname, 'downloaded.spelling');
const pull_request_title = github.context.payload.pull_request.title;
const file_to_spellcheck = "pull_request.title";
const spellcheck_options = {
ignoreAcronyms: true,
ignoreNumbers: true,
suggestions: false,
relativeSpellingFiles: true,
dictionary: {
language: "en-us",
file: `${dictionary_path}`}
}
if (!shouldExecuteSpellcheck(validate_visible_sections_only, pull_request_title, versionrc_path)){
return
}
fs.writeFileSync(file_to_spellcheck, pull_request_title);
var spelling_path = fallback_spelling_path;
try {
await downloadFile(spelling_file_url, downloaded_spelling_path);
spelling_path = downloaded_spelling_path;
console.info(`Successfully downloaded .spelling file from ${spelling_file_url}`)
} catch (error) {
core.notice(`Can not download .spelling file from ${spelling_file_url}.\nFallback .spelling will be used.\nError: ${error}`);
}
extendDictionarySpelling(dictionary_path, spelling_path, spelling_list);
const result = spellcheck.default.spellFile(file_to_spellcheck, spellcheck_options);
if (result.errors.length) {
core.setFailed(`${formatErrorMessage(result.errors, pull_request_title)}`);
} else {
core.info(`Text "${pull_request_title}\" is free from spelling errors`);
}
} catch (error) {
core.setFailed(error.message);
}
}
function getExcludedCommitTypes(versionrc_path) {
if (!fs.existsSync(versionrc_path)) {
core.info(`"${versionrc_path}\" is not exists!`);
return []
}
return fs.readFileSync(versionrc_path, 'utf8')
.split('\n')
.filter((item) => item.includes('\"type\":'))
.filter((item) => item.match(/"hidden": *true/))
.map((item) => item.match(/"type": "(?<type>.*)",/).groups.type)
}
function shouldExecuteSpellcheck(validate_visible_sections_only, pull_request_title, versionrc_path) {
if (!validate_visible_sections_only) {
return true
}
const excludedCommitTypes = getExcludedCommitTypes(versionrc_path);
const commit_type = pull_request_title
.split(':')[0]
.split('(')[0]
const shouldExecuteSpellcheck = !excludedCommitTypes.includes(commit_type);
if (!shouldExecuteSpellcheck) {
core.info(`Current pull request title \"${pull_request_title}\" will not be validated.`);
core.info(`This is because \"validate-visible-sections-only\" parameters is set to 'true' and commit type \"${commit_type}\" is set to be hidden in ${versionrc_path} file.`);
}
return shouldExecuteSpellcheck;
}
function extendDictionarySpelling(dictionary_path, spelling_path, spelling_list){
try {
const spelling_from_file_to_add = fs.readFileSync(spelling_path, 'utf8')
fs.appendFileSync(`${dictionary_path}.dic`, spelling_from_file_to_add);
if(spelling_list) {
var spelling_from_list_to_add = spelling_list.split(/\s+/).join('\n');
fs.appendFileSync(`${dictionary_path}.dic`, spelling_from_list_to_add);
}
} catch (error) {
console.error(err)
}
}
function formatErrorMessage(errors, text) {
var message = `${errors.length} spelling errors found in "${text}":\n`;
for (var i = 0; i < errors.length; i++) {
message += `${i+1}) \"${errors[i].word}\" at index: ${errors[i].index} \n`;
}
return message;
}
function downloadFile(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest, { flags: "wx" });
const request = https.get(url, response => {
if (response.statusCode === 200) {
response.pipe(file);
} else {
file.close();
fs.unlink(dest, () => {});
reject(`Response ${response.statusCode}: ${response.statusMessage}`);
}
});
request.on("error", err => {
file.close();
fs.unlink(dest, () => {});
reject(err.message);
});
file.on("finish", () => {
resolve();
});
file.on("error", err => {
file.close();
fs.unlink(dest, () => {});
reject(err.message);
});
});
}
run();