Skip to content

Commit

Permalink
Add bill of materials entry facility for postal.Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and sophiewigmore committed Mar 31, 2021
1 parent cb4becf commit a4b5f8e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
12 changes: 6 additions & 6 deletions postal/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ type Dependency struct {
// ID is the identifier used to specify the dependency.
ID string `toml:"id"`

// Version is the specific version of the dependency.
Version string `toml:"version"`

// Name is the human-readable name of the dependency.
Name string `toml:"name"`

// URI is the uri location of the built dependency.
URI string `toml:"uri"`

// SHA256 is the hex-encoded SHA256 checksum of the built dependency.
SHA256 string `toml:"sha256"`

Expand All @@ -36,6 +30,12 @@ type Dependency struct {

// Stacks is a list of stacks for which the dependency is built.
Stacks []string `toml:"stacks"`

// URI is the uri location of the built dependency.
URI string `toml:"uri"`

// Version is the specific version of the dependency.
Version string `toml:"version"`
}

func parseBuildpack(path, name string) ([]Dependency, string, error) {
Expand Down
27 changes: 27 additions & 0 deletions postal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"regexp"
"sort"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/cargo"
"github.com/paketo-buildpacks/packit/postal/internal"
"github.com/paketo-buildpacks/packit/vacation"
Expand Down Expand Up @@ -177,3 +179,28 @@ func (s Service) Deliver(dependency Dependency, cnbPath, layerPath, platformPath
func (s Service) Install(dependency Dependency, cnbPath, layerPath string) error {
return s.Deliver(dependency, cnbPath, layerPath, "/platform")
}

// GenerateBillOfMaterials will generate a list of BOMEntry values given a
// collection of Dependency values.
func (s Service) GenerateBillOfMaterials(dependencies ...Dependency) []packit.BOMEntry {
var entries []packit.BOMEntry
for _, dependency := range dependencies {
entry := packit.BOMEntry{
Name: dependency.Name,
Metadata: map[string]interface{}{
"sha256": dependency.SHA256,
"stacks": dependency.Stacks,
"uri": dependency.URI,
"version": dependency.Version,
},
}

if (dependency.DeprecationDate != time.Time{}) {
entry.Metadata["deprecation-date"] = dependency.DeprecationDate
}

entries = append(entries, entry)
}

return entries
}
57 changes: 56 additions & 1 deletion postal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/postal"
"github.com/paketo-buildpacks/packit/postal/fakes"
"github.com/sclevine/spec"
Expand All @@ -38,7 +39,6 @@ func testService(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())

path = file.Name()

_, err = file.WriteString(`
[[metadata.dependencies]]
deprecation_date = 2022-04-01T00:00:00Z
Expand Down Expand Up @@ -823,4 +823,59 @@ version = "this is super not semver"
})
})
})

context("GenerateBillOfMaterials", func() {
var deprecationDate time.Time

it.Before(func() {
var err error
deprecationDate, err = time.Parse(time.RFC3339, "2022-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
})

it("returns a list of BOMEntry values", func() {
entries := service.GenerateBillOfMaterials(
postal.Dependency{
DeprecationDate: deprecationDate,
ID: "some-entry",
Name: "Some Entry",
SHA256: "some-sha",
Source: "some-source",
Stacks: []string{"some-stack"},
URI: "some-uri",
Version: "1.2.3",
},
postal.Dependency{
ID: "other-entry",
Name: "Other Entry",
SHA256: "other-sha",
Source: "other-source",
Stacks: []string{"other-stack"},
URI: "other-uri",
Version: "4.5.6",
},
)
Expect(entries).To(Equal([]packit.BOMEntry{
{
Name: "Some Entry",
Metadata: map[string]interface{}{
"deprecation-date": deprecationDate,
"sha256": "some-sha",
"stacks": []string{"some-stack"},
"uri": "some-uri",
"version": "1.2.3",
},
},
{
Name: "Other Entry",
Metadata: map[string]interface{}{
"sha256": "other-sha",
"stacks": []string{"other-stack"},
"uri": "other-uri",
"version": "4.5.6",
},
},
}))
})
})
}

0 comments on commit a4b5f8e

Please sign in to comment.