Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store source data #53

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
with:
version: v1.61
version: v1.63.4
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ linters-settings:
# https://golangci-lint.run/usage/linters/#exhaustive
default-signifies-exhaustive: true

funlen:
ignore-comments: true

gci:
sections:
- standard
Expand Down Expand Up @@ -201,8 +204,9 @@ linters-settings:
- name: exported
arguments: [checkPrivateReceivers, sayRepetitiveInsteadOfStutters]

# Handled by the `funlen` linter
- name: function-length
exclude: [TEST]
disabled: true

# Handled by the `lll` linter
- name: line-length-limit
Expand Down
27 changes: 19 additions & 8 deletions backends/ent/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package ent_test

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/protobom/protobom/pkg/reader"
"google.golang.org/protobuf/encoding/protojson"

"github.com/protobom/storage/backends/ent"
)
Expand Down Expand Up @@ -57,27 +59,36 @@ func Example() {
// Remove source data URI to allow comparison.
retrieved.GetMetadata().GetSourceData().Uri = nil

output, err := json.MarshalIndent(retrieved, "", " ")
// Produces a different output than the standard [encoding/json] package,
// which does not operate correctly on protocol buffer messages.
output, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(retrieved)
if err != nil {
panic(err)
}

fmt.Println(string(output))
// The output produced by the protojson package is intentionally non-deterministic.
// Format with standard encoding/json package for consistent whitespace.
formatted := bytes.NewBuffer([]byte{})
if err := json.Indent(formatted, output, "", " "); err != nil {
panic(err)
}

fmt.Println(formatted.String())
//nolint:lll
// Output:
// {
// "metadata": {
// "id": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
// "version": "1",
// "date": {},
// "date": "1970-01-01T00:00:00Z",
// "source_data": {
// "format": "application/vnd.cyclonedx+json;version=1.5",
// "hashes": {
// "2": "1ecc17c081f9a0b452b1d8a0d846901bcc40508f",
// "3": "71a3948e45c0bcd83a617ed94674079778d10a0578932e6e536533339b1bbea5",
// "5": "2cc9ac5ab13a8074463e85996e91aa96916a08d33fc3aff9129dd44b24b850884f6176898a21d48dabd9f3824a2dd6bcc1f350e8f13d4be1c564211d1108e43c"
// },
// "size": 5263
// "size": "5263"
// }
// },
// "node_list": {
Expand All @@ -87,7 +98,7 @@ func Example() {
// "name": "Acme Application",
// "version": "9.1.1",
// "primary_purpose": [
// 1
// "APPLICATION"
// ]
// },
// {
Expand All @@ -108,21 +119,21 @@ func Example() {
// "5": "e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282"
// },
// "primary_purpose": [
// 16
// "LIBRARY"
// ]
// },
// {
// "id": "protobom-auto--000000003",
// "name": "mylibrary",
// "version": "1.0.0",
// "primary_purpose": [
// 16
// "LIBRARY"
// ]
// }
// ],
// "edges": [
// {
// "type": 5,
// "type": "contains",
// "from": "protobom-auto--000000001",
// "to": [
// "pkg:npm/acme/[email protected]",
Expand Down
27 changes: 27 additions & 0 deletions backends/ent/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (backend *Backend) saveMetadata(metadata *sbom.Metadata) TxFunc {
for _, fn := range []TxFunc{
backend.savePersons(metadata.GetAuthors()),
backend.saveDocumentTypes(metadata.GetDocumentTypes()),
backend.saveSourceData(metadata.GetSourceData()),
backend.saveTools(metadata.GetTools()),
} {
if err := fn(tx); err != nil {
Expand Down Expand Up @@ -455,6 +456,32 @@ func (backend *Backend) savePurposes(purposes []sbom.Purpose, nodeID uuid.UUID)
}
}

func (backend *Backend) saveSourceData(sourceData *sbom.SourceData) TxFunc {
return func(tx *ent.Tx) error {
id, err := GenerateUUID(sourceData)
if err != nil {
return err
}

newSourceData := tx.SourceData.Create().
SetID(id).
SetProtoMessage(sourceData).
SetFormat(sourceData.GetFormat()).
SetHashes(sourceData.GetHashes()).
SetSize(sourceData.GetSize()).
SetURI(sourceData.GetUri())

setDocumentID(backend.ctx, newSourceData)
setMetadataID(backend.ctx, newSourceData)

if err := newSourceData.OnConflict().Ignore().Exec(backend.ctx); err != nil && !ent.IsConstraintError(err) {
return fmt.Errorf("saving source data: %w", err)
}

return nil
}
}

func (backend *Backend) saveTools(tools []*sbom.Tool) TxFunc {
return func(tx *ent.Tx) error {
builders := []*ent.ToolCreate{}
Expand Down
Loading