Skip to content

Commit

Permalink
Merge pull request #11 from owenrumney/working-on-more-attributes
Browse files Browse the repository at this point in the history
Working on more attributes
  • Loading branch information
owenrumney authored Mar 12, 2021
2 parents 80b8c53 + 38b8007 commit d0f48bd
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 67 deletions.
72 changes: 72 additions & 0 deletions models/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package models

type artifact struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10541049
Location *artifactLocation `json:"location,omitempty"`
ParentIndex *uint `json:"parentIndex,omitempty"`
Offset *uint `json:"offset,omitempty"`
Length int `json:"length"`
Roles []string `json:"roles,omitempty"`
MimeType *string `json:"mimeType,omitempty"`
Contents *artifactContent `json:"contents,omitempty"`
Encoding *string `json:"encoding,omitempty"`
SourceLanguage *string `json:"sourceLanguage,omitempty"`
Hashes map[string]string `json:"hashes,omitempty"`
LastModifiedTimeUtc *string `json:"lastModifiedTimeUtc,omitempty"`
Description *Message `json:"description,omitempty"`
}

type artifactLocation struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10540865
URI *string `json:"uri,omitempty"`
URIBaseId *string `json:"uriBaseId,omitempty"`
Index *uint `json:"index,omitempty"`
Description *Message `json:"description,omitempty"`
}

type ArtifactLocationBuilder struct {
artifactLocation *artifactLocation
}

func (alb *ArtifactLocationBuilder) WithUri(uri string) *ArtifactLocationBuilder {
alb.artifactLocation.URI = &uri
return alb
}

func (alb *ArtifactLocationBuilder) WithIndex(index uint) *ArtifactLocationBuilder {
alb.artifactLocation.Index = &index
return alb
}

func (alb *ArtifactLocationBuilder) WithUriBaseId(uriBaseId string) *ArtifactLocationBuilder {
alb.artifactLocation.URIBaseId = &uriBaseId
return alb
}

func (alb *ArtifactLocationBuilder) WithDescription(messageBuilder MessageBuilder) *ArtifactLocationBuilder {
alb.artifactLocation.Description = messageBuilder.Get()
return alb
}

type artifactContent struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10540860
Text *string `json:"text,omitempty"`
Binary *string `json:"binary,omitempty"`
Rendered *multiformatMessageString `json:"rendered,omitempty"`
}

type ArtifactBuilder struct {
run *Run
artifact *artifact
}

func (run *Run) NewArtifactBuilder() *ArtifactBuilder {
return &ArtifactBuilder{
run: run,
artifact: &artifact{
Length: -1,
},
}
}

func (ab *ArtifactBuilder) Add() *Run {
ab.run.Artifacts = append(ab.run.Artifacts, ab.artifact)
return ab.run
}
33 changes: 33 additions & 0 deletions models/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package models

type location struct {
Id *uint `json:"id,omitempty"`
PhysicalLocation *physicalLocation `json:"physicalLocation,omitempty"`
LogicalLocations []*logicalLocation `json:"logicalLocations,omitempty"`
Message *Message `json:"message,omitempty"`
Annotations []*region `json:"annotations,omitempty"`
Relationships []*locationRelationship `json:"relationships,omitempty"`
}

type physicalLocation struct {
ArtifactLocation *artifactLocation `json:"artifactLocation,omitempty"`
Region *region `json:"region,omitempty"`
ContextRegion *region `json:"contextRegion,omitempty"`
Address *address `json:"address,omitempty"`
}

type logicalLocation struct {
Index *uint `json:"index,omitempty"`
Name *string `json:"name,omitempty"`
FullyQualifiedName *string `json:"fullyQualifiedName,omitempty"`
DecoratedName *string `json:"decoratedName,omitempty"`
Kind *string `json:"kind,omitempty"`
ParentIndex *uint `json:"parentIndex,omitempty"`
}

type locationRelationship struct {
Target uint `json:"target"`
Kinds []string `json:"kinds,omitempty"`
Description *Message `json:"description,omitempty"`
}

29 changes: 29 additions & 0 deletions models/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package models

type MessageBuilder struct {
message *Message
}

func (m *MessageBuilder) WithText(text string) *MessageBuilder {
m.message.Text = &text
return m
}

func (m *MessageBuilder) WithMarkdown(markdown string) *MessageBuilder {
m.message.Markdown = &markdown
return m
}

func (m *MessageBuilder) WithId(id string) *MessageBuilder {
m.message.Id = &id
return m
}

func (m *MessageBuilder) WithArguments(args []string) *MessageBuilder {
m.message.Arguments = args
return m
}

func (m *MessageBuilder) Get() *Message {
return m.message
}
29 changes: 29 additions & 0 deletions models/region.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package models

type region struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10541123
StartLine *int `json:"startLine,omitempty"`
StartColumn *int `json:"startColumn,omitempty"`
EndLine *int `json:"endLine,omitempty"`
EndColumn *int `json:"endColumn,omitempty"`
CharOffset *int `json:"charOffset,omitempty"`
CharLength *int `json:"charLength,omitempty"`
ByteOffset *int `json:"byteOffset,omitempty"`
ByteLength *int `json:"byteLength,omitempty"`
Snippet *artifactContent `json:"snippet,omitempty"`
Message *Message `json:"message,omitempty"`
SourceLanguage *string `json:"sourceLanguage,omitempty"`
}

type RegionBuilder struct {
region *region
}

func NewRegionBuilder() *RegionBuilder {
return &RegionBuilder{
region: &region{},
}
}

func (rb *RegionBuilder) Get() *region {
return rb.region
}
131 changes: 102 additions & 29 deletions models/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,142 @@ package models

// Result represents the results block in the sarif report
type Result struct {
Level string `json:"level"`
Message *textBlock `json:"message"`
RuleID string `json:"ruleId"`
RuleIndex int `json:"ruleIndex"`
Locations []*resultLocation `json:"locations,omitempty"`
Guid *string `json:"guid,omitempty"`
CorrelationGuid *string `json:"correlationGuid,omitempty"`
RuleID *string `json:"ruleId,omitempty"`
RuleIndex *uint `json:"ruleIndex,omitempty"`
Rule *reportingDescriptorReference `json:"rule,omitempty"`
Taxa []*reportingDescriptorReference `json:"taxa,omitempty"`
Kind *string `json:"kind,omitempty"`
Level *string `json:"level,omitempty"`
Message Message `json:"message"`
Locations []*location `json:"locations,omitempty"`
AnalysisTarget *artifactLocation `json:"analysisTarget,omitempty"`
// WebRequest *webRequest `json:"webRequest,omitempty"`
// WebResponse *webResponse `json:"webResponse,omitempty"`
Fingerprints map[string]interface{} `json:"fingerprints,omitempty"`
PartialFingerprints map[string]interface{} `json:"partialFingerprints,omitempty"`
// CodeFlows []*codeFlows `json:"codeFlows,omitempty"`
// Graphs []*graphs `json:"graphs,omitempty"`
// GraphTraversals []*graphTraversals `json:"graphTraversals,omitempty"`
// Stacks []*stack `json:"stacks,omitempty"`
RelatedLocations []*location `json:"relatedLocations,omitempty"`
Suppressions []*suppression `json:"suppressions,omitempty"`
BaselineState *string `json:"baselineState,omitempty"`
Rank *float32 `json:"rank,omitempty"`
// Attachments []*attachment `json:"attachments,omitempty"`
WorkItemUris []string `json:"workItemUris,omitempty"` // can be null
HostedViewerUri *string `json:"hostedViewerUri,omitempty"`
// Provenance *resultProvenance `json:"provenance,omitempty"`
Fixes []*fix `json:"fixes,omitempty"`
OccurrenceCount *uint `json:"occurrenceCount,omitempty"`
}

type resultLocation struct {
PhysicalLocation *physicalLocation `json:"physicalLocation,omitempty"`
type reportingDescriptorReference struct {
Id *string `json:"id,omitempty"`
Index *uint `json:"index,omitempty"`
Guid *string `json:"guid,omitempty"`
ToolComponent *toolComponentReference `json:"toolComponent,omitempty"`
}

type physicalLocation struct {
ArtifactLocation *artifactLocation `json:"artifactLocation"`
Region *region `json:"region"`
type toolComponentReference struct {
Name *string `json:"name"`
Index *uint `json:"index"`
Guid *string `json:"guid"`
}

type region struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
type Message struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10540897
Text *string `json:"text,omitempty"`
Markdown *string `json:"markdown,omitempty"`
Id *string `json:"id,omitempty"`
Arguments []string `json:"arguments,omitempty"`
}

type artifactLocation struct {
URI string `json:"uri"`
Index int `json:"index"`






type multiformatMessageString struct {
Text string `json:"text"`
Markdown *string `json:"markdown,omitempty"`
}

type address struct {
Index *uint `json:"index,omitempty"`
AbsoluteAddress *uint `json:"absoluteAddress,omitempty"`
RelativeAddress *int `json:"relativeAddress,omitempty"`
OffsetFromParent *int `json:"offsetFromParent,omitempty"`
Length *int `json:"length,omitempty"`
Name *string `json:"name,omitempty"`
FullyQualifiedName *string `json:"fullyQualifiedName,omitempty"`
Kind *string `json:"kind,omitempty"`
ParentIndex *uint `json:"parentIndex,omitempty"`
}



type suppression struct {
Kind string `json:"kind"`
Status *string `json:"status"`
Location *location `json:"location"`
Guid *string `json:"guid"`
Justification *string `json:"justification"`
}

type fix struct {
Description *Message `json:"description,omitempty"`
ArtifactChanges []*artifactChange `json:"artifactChanges"` // required
}

type artifactChange struct {
ArtifactLocation artifactLocation `json:"artifactLocation"`
Replacements []*replacement `json:"replacements"` //required
}

type location struct {
URI string `json:"uri"`
type replacement struct {
DeletedRegion region `json:"deletedRegion"`
InsertedContent *artifactContent `json:"insertedContent,omitempty"`
}

func newRuleResult(ruleID string) *Result {
return &Result{
RuleID: ruleID,
RuleID: &ruleID,
}
}

// WithLevel specifies the level of the finding, error, warning for a result and returns the updated result
func (result *Result) WithLevel(level string) *Result {
result.Level = level
result.Level = &level
return result
}

// WithMessage specifies the message for a result and returns the updated result
func (result *Result) WithMessage(message string) *Result {
result.Message = &textBlock{
Text: message,
}
result.Message.Text = &message
return result
}

func (result *Result) NewMessageBuilder() *MessageBuilder {
return &MessageBuilder{
message: &result.Message,
}
}

// WithLocationDetails specifies the location details of the Result and returns the update result
func (result *Result) WithLocationDetails(path string, startLine, startColumn int) *Result {
location := &physicalLocation{
physicalLocation := &physicalLocation{
ArtifactLocation: &artifactLocation{
URI: path,
URI: &path,
},
Region: &region{
StartLine: startLine,
StartColumn: startColumn,
StartLine: &startLine,
StartColumn: &startColumn,
},
}
result.Locations = append(result.Locations, &resultLocation{
PhysicalLocation: location,
result.Locations = append(result.Locations, &location{
PhysicalLocation: physicalLocation,
})
return result
}
Loading

0 comments on commit d0f48bd

Please sign in to comment.