-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
12 hover provider for manifest files (#46)
* added policy violations per dependency * added regex method * refactored * code refactor * code refactor * refactored and cleaned * refactored --------- Co-authored-by: Nagarjun Sanji <[email protected]>
- Loading branch information
1 parent
09a0efe
commit 6330445
Showing
22 changed files
with
439 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export enum PolicyRules { | ||
warnPipeline = "Pipeline warning", | ||
failPipeline = "Pipeline failing", | ||
markUnaffected = "Mark vulnerability as unaffected", | ||
markVulnerable = "Flag vulnerability as vulnerable", | ||
sendEmail = "Notified email", | ||
triggerWebhook = "Triggered webhook", | ||
} | ||
|
||
export enum PolicyTriggerEvents { | ||
WARN_PIPELINE = "warnPipeline", | ||
FAIL_PIPELINE = "failPipeline", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class Regex { | ||
static readonly repoId = /\/repository\/(\d+)\//; | ||
static readonly commitId = /\/commit\/(\d+)/; | ||
static readonly packageJson = /"([^"]+)":\s*"([^"]+)"/; | ||
static readonly goMod = | ||
/^(?:require\s+)?(\S+)\s+(v?\d+(?:\.\d+)*(?:-[\w\.-]+)?(?:\+[\w\.-]+)?)(?:\s+\/\/\s+indirect)?/; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Package } from "types"; | ||
import { commonHelper, globalStore } from "../helpers"; | ||
import * as vscode from "vscode"; | ||
import { PolicyTriggerEvents, SecondService } from "../constants"; | ||
|
||
export class DependencyPolicyProvider implements vscode.CodeActionProvider { | ||
constructor(private diagnosticCollection: vscode.DiagnosticCollection) {} | ||
|
||
provideCodeActions(): vscode.ProviderResult<(vscode.CodeAction | vscode.Command)[]> { | ||
return []; | ||
} | ||
|
||
async checkPolicyViolation(document: vscode.TextDocument) { | ||
// Check if the current file is a manifest file | ||
const { isManifestFile, currentManifestFile } = await commonHelper.isCurrentDocManifestFile(document); | ||
|
||
if (!isManifestFile) { | ||
return; | ||
} | ||
|
||
if (currentManifestFile === "package.json") { | ||
const diagnostics: vscode.Diagnostic[] = []; | ||
const content = document.getText(); | ||
const packages: Map<string, Package> = globalStore.getPackages(); | ||
|
||
if (packages && packages.size > 0) { | ||
const manifestData = JSON.parse(content) || {}; | ||
const allDependencies = { | ||
...manifestData.dependencies, | ||
...manifestData.devDependencies, | ||
}; | ||
|
||
for (const [packageName, packageData] of packages) { | ||
if (packageName in allDependencies) { | ||
const range = this.findDependencyRange(document, packageName); | ||
if (range) { | ||
let diagnostic: vscode.Diagnostic | undefined; | ||
packageData.policyRules?.forEach((rule) => { | ||
if (rule.ruleActions?.includes(PolicyTriggerEvents.FAIL_PIPELINE)) { | ||
diagnostic = new vscode.Diagnostic( | ||
range, | ||
`Dependency ${packageName} failed the pipeline`, | ||
vscode.DiagnosticSeverity.Error, | ||
); | ||
} else if (rule.ruleActions?.includes(PolicyTriggerEvents.WARN_PIPELINE)) { | ||
diagnostic = new vscode.Diagnostic( | ||
range, | ||
`Dependency ${packageName} triggered a pipeline warning`, | ||
vscode.DiagnosticSeverity.Warning, | ||
); | ||
} | ||
}); | ||
|
||
if (diagnostic) { | ||
diagnostic.code = { | ||
value: packageData.cve ?? "Unknown reason", | ||
target: vscode.Uri.parse(packageData.cveLink ?? SecondService.debrickedBaseUrl), | ||
}; | ||
diagnostics.push(diagnostic); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const uri = document.uri; | ||
if (!uri.path.endsWith(".git")) { | ||
this.diagnosticCollection.set(uri, diagnostics); | ||
} | ||
} | ||
} | ||
|
||
private findDependencyRange(document: vscode.TextDocument, dependency: string): vscode.Range | null { | ||
const text = document.getText(); | ||
const dependencyPattern = new RegExp(`"${dependency}"\\s*:\\s*"[^"]*"`, "g"); | ||
const match = dependencyPattern.exec(text); | ||
if (match) { | ||
const startPos = document.positionAt(match.index); | ||
const endPos = document.positionAt(match.index + match[0].length); | ||
return new vscode.Range(startPos, endPos); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.