forked from DXHeroes/knowledge-base-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.ts
74 lines (66 loc) · 1.9 KB
/
dangerfile.ts
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
import { message, warn, fail, markdown, danger } from 'danger';
import fetch from 'node-fetch';
import path from 'path';
import fs from 'fs';
const validate = async () => {
const validateArticle = async (article_path: any) => {
const body = fs.readFileSync(path.resolve(__dirname, article_path));
try {
const response = await fetch(
process.env.KB_VALIDATION_URL || 'setTheUrl',
{
method: 'post',
body: JSON.stringify({
repo_path: article_path,
content: body.toString(),
}),
headers: {
'Content-Type': 'application/json',
'X-DX-Auth': `${process.env.X_DX_AUTH_HEADER}`,
Authorization: `${process.env.AUTH_BASE64}`,
},
},
);
const json = await response.json();
console.log(json);
return json;
} catch (error) {
console.log(error);
throw error;
}
};
const changedMds = [
...danger.git.created_files,
...danger.git.modified_files,
].filter(path => path.match(/(problems|practices)\/.*\.md/));
for (const mdFile of changedMds) {
const validationResult: ValidationResult = await validateArticle(mdFile);
for (const info of validationResult.infos) {
message(info, mdFile);
}
for (const warning of validationResult.warns) {
warn(warning, mdFile);
}
for (const failure of validationResult.fails) {
fail(failure, mdFile);
}
if (validationResult.fails.length == 0) {
message(
'See preview of the article at [https://preview.developerexperience.io/](https://preview.developerexperience.io/)',
);
}
}
};
validate()
.then(res => {
res;
})
.catch(error => {
console.error(error);
warn('Something is wrong. The validation failed with server error.');
});
interface ValidationResult {
infos: string[];
warns: string[];
fails: string[];
}