Skip to content

Commit

Permalink
Start working through new attributes
Browse files Browse the repository at this point in the history
- start working through new attributes
- start thinking about the fluent API for creating the report
- reference some of the spec
  • Loading branch information
owenrumney-f3 committed Mar 3, 2021
1 parent fda5757 commit 38b8007
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 129 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
}
166 changes: 61 additions & 105 deletions models/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,152 +2,102 @@ package models

// Result represents the results block in the sarif report
type Result struct {
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"`
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"`
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"`
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"`
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"`
Fixes []*fix `json:"fixes,omitempty"`
OccurrenceCount *uint `json:"occurrenceCount,omitempty"`
}

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

type toolComponentReference struct {
Name *string `json:"name"`
Index *uint `json:"index"`
Guid *string `json:"guid"`
Name *string `json:"name"`
Index *uint `json:"index"`
Guid *string `json:"guid"`
}

type Message struct {
Text *string `json:"text,omitempty"`
Markdown *string `json:"markdown,omitempty"`
Id *string `json:"id,omitempty"`
Arguments []string `json:"arguments,omitempty"`
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 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"`
}

type region struct {
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 artifactContent struct {
Text *string `json:"text,omitempty"`
Binary *string `json:"binary,omitempty"`
Rendered *multiformatMessageString `json:"rendered,omitempty"`
}

type multiformatMessageString struct {
Text string `json:"text"`
Markdown *string `json:"markdown,omitempty"`
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"`
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 artifactLocation struct {
URI *string `json:"uri,omitempty"`
URIBaseId *string `json:"uriBaseId,omitempty"`
Index *uint `json:"index,omitempty"`
Description *Message `json:"description,omitempty"`
}


type suppression struct {
Kind string `json:"kind"`
Status *string `json:"status"`
Location *location `json:"location"`
Guid *string `json:"guid"`
Justification *string `json:"justification"`
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
Description *Message `json:"description,omitempty"`
ArtifactChanges []*artifactChange `json:"artifactChanges"` // required
}

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

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

Expand All @@ -169,6 +119,12 @@ func (result *Result) WithMessage(message string) *Result {
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 {
physicalLocation := &physicalLocation{
Expand Down
Loading

0 comments on commit 38b8007

Please sign in to comment.