sarif

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package sarif provides SARIF (Static Analysis Results Interchange Format) parsing and rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizePath

func NormalizePath(uri string) string

NormalizePath strips file:// prefix and cleans a SARIF URI to a relative path.

Types

type ArtifactLocation

type ArtifactLocation struct {
	URI   string `json:"uri"`
	Index int    `json:"index,omitempty"`
}

ArtifactLocation identifies the file.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder constructs valid SARIF 2.1.0 documents. Designed for fo wrap and as an importable library.

func NewBuilder

func NewBuilder(toolName, toolVersion string) *Builder

NewBuilder creates a SARIF builder for the given tool.

func (*Builder) AddResult

func (b *Builder) AddResult(ruleID, level, message, file string, line, col int) *Builder

AddResult adds a diagnostic result to the current run.

func (*Builder) Document

func (b *Builder) Document() *Document

Document returns the constructed SARIF document.

func (*Builder) WriteTo

func (b *Builder) WriteTo(w io.Writer) (int64, error)

WriteTo writes the SARIF document as JSON to w.

type Document

type Document struct {
	Version string `json:"version"`
	Schema  string `json:"$schema,omitempty"`
	Runs    []Run  `json:"runs"`
}

Document represents a SARIF 2.1.0 document. See: https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html

func Read

func Read(r io.Reader) (*Document, error)

Read parses SARIF from an io.Reader.

func ReadBytes

func ReadBytes(data []byte) (*Document, error)

ReadBytes parses SARIF from a byte slice.

type Driver

type Driver struct {
	Name    string `json:"name"`
	Version string `json:"version,omitempty"`
}

Driver describes the tool's identity.

type FileIssue

type FileIssue struct {
	File       string
	IssueCount int
	ErrorCount int
	WarnCount  int
}

FileIssue represents an issue in a specific file for leaderboard rendering.

func TopFiles

func TopFiles(doc *Document, limit int) []FileIssue

TopFiles returns files sorted by issue count (descending).

type GroupedResults

type GroupedResults struct {
	Key     string   // file path or rule ID
	Results []Result // issues in this group
}

GroupedResults organizes results by a grouping key.

func GroupByFile

func GroupByFile(doc *Document) []GroupedResults

GroupByFile organizes results by file path.

type Location

type Location struct {
	PhysicalLocation PhysicalLocation `json:"physicalLocation"`
}

Location identifies where the issue was found.

type Message

type Message struct {
	Text string `json:"text"`
}

Message contains the issue description.

type PhysicalLocation

type PhysicalLocation struct {
	ArtifactLocation ArtifactLocation `json:"artifactLocation"`
	Region           Region           `json:"region,omitempty"`
}

PhysicalLocation pinpoints the file and region.

type Region

type Region struct {
	StartLine   int `json:"startLine,omitempty"`
	StartColumn int `json:"startColumn,omitempty"`
	EndLine     int `json:"endLine,omitempty"`
	EndColumn   int `json:"endColumn,omitempty"`
}

Region identifies the specific location within the file.

type Result

type Result struct {
	RuleID    string          `json:"ruleId"`
	Level     string          `json:"level"` // "error", "warning", "note", "none"
	Message   Message         `json:"message"`
	Locations []Location      `json:"locations,omitempty"`
	Related   []Location      `json:"relatedLocations,omitempty"`
	Props     json.RawMessage `json:"properties,omitempty"`
}

Result represents a single issue found by the tool.

func (*Result) Col

func (r *Result) Col() int

Col returns the start column from a result's primary location.

func (*Result) FilePath

func (r *Result) FilePath() string

FilePath returns the normalized file path from a result's primary location. Strips file:// prefix and normalizes separators.

func (*Result) Line

func (r *Result) Line() int

Line returns the start line from a result's primary location.

type Run

type Run struct {
	Tool    Tool     `json:"tool"`
	Results []Result `json:"results"`
}

Run represents a single analysis run.

type Stats

type Stats struct {
	TotalIssues int
	ByLevel     map[string]int // error, warning, note, none
	ByRule      map[string]int
	ByFile      map[string]int
}

Stats aggregates statistics from SARIF results.

func ComputeStats

func ComputeStats(doc *Document) Stats

ComputeStats calculates aggregate statistics from a SARIF document.

type Tool

type Tool struct {
	Driver Driver `json:"driver"`
}

Tool identifies the analysis tool that produced the results.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL