Skip to content

Commit

Permalink
feat: add comments to policy files
Browse files Browse the repository at this point in the history
  • Loading branch information
joshje committed Dec 14, 2016
1 parent 598df59 commit 03e5897
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/parser/add-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = addComments;

var initialComment = 'Snyk (https://snyk.io) policy file, patches or ignores ' +
'known vulnerabilities.';
var inlineComments = {
ignore: 'ignores vulnerabilities until expiry date; change duration by ' +
'modifying expiry date',
patch: 'patches apply the minimum changes required to fix a vulnerability',
};

function addComment(source, comment, position) {
return source.substr(0, position) + '# ' + comment + '\n' +
source.substr(position);
}

function addComments(policyExport) {
policyExport = addComment(policyExport, initialComment, 0);

Object.keys(inlineComments).forEach(function (key) {
var comment = inlineComments[key];
var position = policyExport.indexOf('\n' + key + ':\n');
if (position !== -1) {
policyExport = addComment(policyExport, comment, position + 1);
}
});

return policyExport;
}
4 changes: 3 additions & 1 deletion lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path');
var cloneDeep = require('lodash.clonedeep');
var semver = require('semver');
var yaml = require('js-yaml');
var addComments = require('./add-comments');

module.exports = {
import: imports,
Expand Down Expand Up @@ -59,7 +60,8 @@ function exports(policy) {

// ensure we always update the version of the policy format
data.version = version();
return yaml.safeDump(data);
// put inline comments into the exported yaml file
return addComments(yaml.safeDump(data));
}

function version() {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ignore/.snyk
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
ignore:
'npm:hawk:20160119':
- sqlite > sqlite3 > node-pre-gyp > request > hawk:
Expand Down
2 changes: 2 additions & 0 deletions test/unit/policy-save.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ test('policy.save', function (t) {
t.equal(writeSpy.args[0][0], filename, 'filename correct');
var parsed = writeSpy.args[0][1].trim();
t.equal(parsed, asText, 'body contains original');
t.match(parsed, '# Snyk (https://snyk.io) policy file, patches or ' +
'ignores known vulnerabilities.', 'body contains comments');
});
});

0 comments on commit 03e5897

Please sign in to comment.