in pkg/vul/discovery.go [70:130]
func updateDiscoveryOcc(ctx context.Context, parent string, discoveryNoteID string, resourceURL string, c *ca.Client) error {
noteName := fmt.Sprintf("%s/notes/%s", parent, discoveryNoteID)
resourceURL = fmt.Sprintf("https://%s", resourceURL)
occ := &g.Occurrence{
Kind: g.NoteKind_DISCOVERY,
ResourceUri: resourceURL,
NoteName: noteName,
Details: &g.Occurrence_Discovery{
Discovery: &g.DiscoveryOccurrence{
ContinuousAnalysis: g.DiscoveryOccurrence_ACTIVE,
AnalysisStatus: g.DiscoveryOccurrence_COMPLETE,
LastScanTime: timestamppb.New(time.Now()),
},
},
}
listOccReq := &g.ListOccurrencesRequest{
Parent: parent,
Filter: fmt.Sprintf("resourceUrl=\"%s\" AND kind=\"DISCOVERY\" AND noteId=\"%s\"", resourceURL, discoveryNoteID),
}
var listRes []*g.Occurrence
it := c.GetGrafeasClient().ListOccurrences(ctx, listOccReq)
for {
resp, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return err
}
listRes = append(listRes, resp)
}
switch len(listRes) {
// If there were no occurrences, we create the occurrence.
case 0:
req := &g.CreateOccurrenceRequest{
Parent: parent,
Occurrence: occ,
}
_, err := c.GetGrafeasClient().CreateOccurrence(ctx, req)
if err != nil {
return errors.Wrap(err, "error posting discovery occurrence")
}
// If there was one occurrence, we update it.
case 1:
updateReq := &g.UpdateOccurrenceRequest{
Name: listRes[0].GetName(),
Occurrence: occ,
}
if _, err := c.GetGrafeasClient().UpdateOccurrence(ctx, updateReq); err != nil {
return errors.Wrap(err, "error updating occurrence")
}
default:
return errors.New("list occurrence expected to return one " +
"occurrence but more than one was returned")
}
return nil
}