-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.js
executable file
·98 lines (76 loc) · 2.81 KB
/
entrypoint.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
#!/usr/bin/env node
const fs = require('fs');
const yaml = require('js-yaml');
const { Spectral, isOpenApiv3 } = require('@stoplight/spectral');
const { stylish } = require('@stoplight/spectral/dist/cli/formatters/stylish');
const SPECTRAL_CONFIG = '.spectral.yaml';
const ISP_RULES_PREFIX = process.env.ISP_RULES_PREFIX || './';
// Mapping of Spectral severity to GitHub Actions message level
const SEV_MAP = ['error', 'warning', 'debug', 'debug'];
// Figure out what we are checking.
let filename = 'openapi.yaml';
if (process.argv.length >= 3) {
filename = process.argv[2];
}
const ISP_RULES_PATH = `${ISP_RULES_PREFIX}isp-rules.yaml`;
// Make sure Spectral's config is set up properly to point to our custom rules.
if (fs.existsSync(SPECTRAL_CONFIG)) {
// Modify existing file.
const doc = yaml.safeLoad(fs.readFileSync(SPECTRAL_CONFIG, 'utf8'));
if (!doc.extends) {
doc.extends = [];
}
if (!doc.extends.includes(ISP_RULES_PATH)) {
doc.extends.push(ISP_RULES_PATH);
}
fs.writeFileSync(SPECTRAL_CONFIG, yaml.dump(doc));
} else {
// Create dummy file.
fs.writeFileSync(SPECTRAL_CONFIG, `extends:\n - ${ISP_RULES_PATH}\n`);
}
// Run the thing!
let failLimit = 0;
if (process.env.FAIL_ON_WARNINGS) {
failLimit = 1;
}
const doc = fs.readFileSync(filename, 'utf8');
const spectral = new Spectral({
computeFingerprint: (rule, hash) => {
// https://stoplight.io/p/docs/gh/stoplightio/spectral/docs/guides/javascript.md#using-custom-de-duplication-strategy
let id = String(rule.code);
if (rule.path && rule.path.length) {
id += JSON.stringify(rule.path);
} else if (rule.range) {
id += JSON.stringify(rule.range);
}
if (rule.source) id += rule.source;
// Dedupe based on the error message as well, since some rules can
// generate many different messages (e.g. English for blocks of text).
id += rule.message;
return hash(id);
}
});
spectral.registerFormat('oas3', isOpenApiv3);
spectral
.loadRuleset(SPECTRAL_CONFIG)
.then(() => spectral.run(doc, { resolve: { documentUri: filename } }))
.then(results => {
let errors = 0;
for (let r of results) {
if (r.severity <= failLimit) {
errors++;
}
// If we are running in GitHub, output metadata to nicely annotate the UI.
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-error-message-error
if (process.env.GITHUB_ACTIONS) {
console.log(
`::${SEV_MAP[r.severity]} file=${r.source},line=${r.range.start.line +
1},col=${r.range.start.character}::${r.message}`
);
}
}
// Use the nice default formatter to display results.
console.log(stylish(results));
// Set the exit code (0 for success, 1 for failure).
process.exit(errors > 0);
});