/
ignore.ts
78 lines (71 loc) · 2.22 KB
/
ignore.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
75
76
77
78
export = ignore;
import * as policy from 'snyk-policy';
import chalk from 'chalk';
import * as authorization from '../../lib/authorization';
import * as auth from './auth/is-authed';
import { apiTokenExists } from '../../lib/api-token';
import { isCI } from '../../lib/is-ci';
import { MethodResult } from './types';
import * as Debug from 'debug';
const debug = Debug('snyk');
import { MisconfiguredAuthInCI } from '../../lib/errors/misconfigured-auth-in-ci-error';
function ignore(options): Promise<MethodResult> {
debug('snyk ignore called with options: %O', options);
return auth
.isAuthed()
.then((authed) => {
if (!authed) {
if (isCI()) {
throw MisconfiguredAuthInCI();
}
}
apiTokenExists();
})
.then(() => {
return authorization.actionAllowed('cliIgnore', options);
})
.then((cliIgnoreAuthorization) => {
if (!cliIgnoreAuthorization.allowed) {
debug('snyk ignore called when disallowed');
console.log(chalk.bold.red(cliIgnoreAuthorization.reason));
return;
}
if (!options.id) {
throw Error('idRequired');
}
options.expiry = new Date(options.expiry);
if (options.expiry.getTime() !== options.expiry.getTime()) {
debug('No/invalid expiry given, using the default 30 days');
options.expiry = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
}
if (!options.reason) {
options.reason = 'None Given';
}
debug(
'changing policy: ignore "%s", for all paths, reason: "%s", until: %o',
options.id,
options.reason,
options.expiry,
);
return policy
.load(options['policy-path'])
.catch((error) => {
if (error.code === 'ENOENT') {
// file does not exist - create it
return policy.create();
}
throw Error('policyFile');
})
.then(async function ignoreIssue(pol) {
pol.ignore[options.id] = [
{
'*': {
reason: options.reason,
expires: options.expiry,
},
},
];
return await policy.save(pol, options['policy-path']);
});
});
}