Skip to content

Commit

Permalink
feat: initial implementation of sarif output using node-sarif-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcarman committed May 7, 2024
1 parent 812776a commit 9163b7f
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/commands/metalint/run.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { pathToFileURL } from 'node:url';
// import * as path from 'node:path';
import { SarifBuilder, SarifRunBuilder, SarifResultBuilder, SarifRuleBuilder } from 'node-sarif-builder';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { fileExists, readAllFiles } from '../util.js';
Expand Down Expand Up @@ -33,13 +36,47 @@ export default class MetalintRun extends SfCommand<MetalintRunResult> {
this.spinner.start('Building list of files to lint...');
const files = (await readAllFiles(dir)) as string[];
this.spinner.stop();
// eslint-disable-next-line no-console
// console.log(files);

// Run rules
const fieldsWithoutDescriptions = fieldsMustHaveDescriptions(files);
// eslint-disable-next-line no-console
console.log(fieldsWithoutDescriptions);

// Setup sarif builder, add rules
const sarifBuilder = new SarifBuilder();
const sarifRunBuilder = new SarifRunBuilder().initSimple({
toolDriverName: 'sf-metadata-linter',
toolDriverVersion: '1.0.0',
});

const sarifRuleBuilder = new SarifRuleBuilder().initSimple({
ruleId: 'fields-must-have-descriptions',
shortDescriptionText: 'Fields must have descriptions',
helpUri: 'http://www.google.com',
});
sarifRunBuilder.addRule(sarifRuleBuilder);

for (const field of fieldsWithoutDescriptions) {
const sarifResultBuilder = new SarifResultBuilder();

// let path = process.env.SARIF_URI_ABSOLUTE ? pathToFileURL(field) : path.relative(process.cwd(), field).replace(/\\/g, '/') as const

const sarifResultInit = {
ruleId: 'fields-must-have-descriptions',
messageText: 'Field is missing a description',
level: 'error' as const,
fileUri: pathToFileURL(field).toString(),
};

sarifResultBuilder.initSimple(sarifResultInit);
sarifRunBuilder.addResult(sarifResultBuilder);

sarifBuilder.addRun(sarifRunBuilder);
const sarifResults = sarifBuilder.buildSarifJsonString({ indent: false });
// eslint-disable-next-line no-console
console.log(sarifResults);
}

return { outcome: 'Complete' };
}
}

0 comments on commit 9163b7f

Please sign in to comment.