Skip to content

Commit

Permalink
Automatically add deprecation_date field to buildpack dependencies
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Busko <pavel.busko@sap.com>
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
  • Loading branch information
c0d1ngm0nk3y and pbusko committed Jun 19, 2024
1 parent 7f364b3 commit ef30385
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
14 changes: 14 additions & 0 deletions carton/buildpack_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
type BuildpackDependency struct {
BuildpackPath string
ID string
EolID string
Arch string
SHA256 string
URI string
Expand Down Expand Up @@ -67,6 +68,7 @@ func (b BuildpackDependency) Update(options ...Option) {
logger.Headerf("SHA256: %s", b.SHA256)
logger.Headerf("Source: %s", b.Source)
logger.Headerf("SourceSHA256: %s", b.SourceSHA256)
logger.Headerf("EOL ID: %s", b.EolID)

versionExp, err := regexp.Compile(b.VersionPattern)
if err != nil {
Expand Down Expand Up @@ -208,6 +210,18 @@ func (b BuildpackDependency) Update(options ...Option) {
}
}
}

if b.EolID != "" {
eolDate, err := GetEolDate(b.EolID, b.Version)
if err != nil {
config.exitHandler.Error(fmt.Errorf("unable to fetch deprecation_date"))
return
}

if eolDate != "" {
dep["deprecation_date"] = eolDate
}
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions carton/eol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package carton

import (
"context"
"fmt"
"time"

"github.com/Masterminds/semver/v3"
eol "github.com/kobayashi/eol/pkg/api"
)

func GetEolDate(eolID, version string) (string, error) {
cycleList, err := eol.NewHTTPClient().GetProjectCycleList(context.Background(), eolID)
if err != nil {
return "", fmt.Errorf("could not fetch cycle list: %w", err)
}

cycle, err := selectCycle(version, cycleList)
if err != nil {
return "", fmt.Errorf("could not find a relese cycle: %w", err)
}

if cycle.EOL.B != nil {
return "", nil
}

eol, err := time.Parse(time.DateOnly, *cycle.EOL.S)
if err != nil {
return "", fmt.Errorf("could not parse eol %q: %w", *cycle.EOL.S, err)
}

return eol.Format(time.RFC3339), nil
}

func selectCycle(version string, cycles eol.CycleList) (*eol.Cycle, error) {
versionParsed, err := semver.NewVersion(version)
if err != nil {
return nil, err
}

for _, v := range []string{fmt.Sprintf("%d.%d", versionParsed.Major(), versionParsed.Minor()), fmt.Sprintf("%d", versionParsed.Major())} {
for _, c := range cycles {
if (c.Cycle.S != nil && *c.Cycle.S == v) || (c.Cycle.I != nil && fmt.Sprintf("%d", *c.Cycle.I) == v) {
return c, nil
}
}
}

return nil, fmt.Errorf("no release cycle found for the version %s", version)
}
136 changes: 136 additions & 0 deletions carton/eol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package carton_test

import (
"net/http"
"testing"

"github.com/buildpacks/libcnb/mocks"
"github.com/jarcoal/httpmock"
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/libpak/carton"
"github.com/sclevine/spec"
"github.com/stretchr/testify/mock"
)

func testGetEolDate(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

exitHandler *mocks.ExitHandler
)

it.Before(func() {
httpmock.Activate()
exitHandler = &mocks.ExitHandler{}
exitHandler.On("Error", mock.Anything)
})

it.After(func() {
httpmock.DeactivateAndReset()
})

context("finds release cycle by major.minor version", func() {
it.Before(func() {
httpmock.RegisterResponder(http.MethodGet, "https://endoflife.date/api/foo.json", httpmock.NewBytesResponder(200, []byte(`
[
{
"cycle": "10.1",
"releaseDate": "2022-09-23",
"eol": false,
"minJavaVersion": 11,
"latest": "10.1.24",
"latestReleaseDate": "2024-05-09",
"lts": false
},
{
"cycle": "10.0",
"releaseDate": "2020-12-03",
"eol": "2022-10-31",
"minJavaVersion": 8,
"latest": "10.0.27",
"latestReleaseDate": "2022-10-03",
"lts": false
},
{
"cycle": "9",
"releaseDate": "2017-09-27",
"eol": false,
"minJavaVersion": 8,
"latest": "9.0.89",
"latestReleaseDate": "2024-05-03",
"lts": false
}
]`)))
})

it("returns correct cycle", func() {
eolDate, err := carton.GetEolDate("foo", "10.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2022-10-31T00:00:00Z"))
})
})

context("finds release cycle by major version", func() {
it.Before(func() {
httpmock.RegisterResponder(http.MethodGet, "https://endoflife.date/api/foo.json", httpmock.NewBytesResponder(200, []byte(`
[
{
"cycle": "10",
"releaseDate": "2022-09-23",
"eol": "2024-10-31",
"minJavaVersion": 11,
"latest": "10.1.24",
"latestReleaseDate": "2024-05-09",
"lts": false
},
{
"cycle": "9",
"releaseDate": "2020-12-03",
"eol": "2022-10-31",
"minJavaVersion": 8,
"latest": "9.0.27",
"latestReleaseDate": "2022-10-03",
"lts": false
}
]`)))
})

it("returns correct cycle", func() {
eolDate, err := carton.GetEolDate("foo", "10.1.1")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2024-10-31T00:00:00Z"))
})
})

context("cycle.EOL is bool", func() {
it.Before(func() {
httpmock.RegisterResponder(http.MethodGet, "https://endoflife.date/api/foo.json", httpmock.NewBytesResponder(200, []byte(`
[
{
"cycle": "10",
"releaseDate": "2022-09-23",
"eol": false,
"minJavaVersion": 11,
"latest": "10.1.24",
"latestReleaseDate": "2024-05-09",
"lts": false
},
{
"cycle": "9",
"releaseDate": "2020-12-03",
"eol": "2022-10-31",
"minJavaVersion": 8,
"latest": "9.0.27",
"latestReleaseDate": "2022-10-03",
"lts": false
}
]`)))
})

it("returns empty eol date", func() {
eolDate, err := carton.GetEolDate("foo", "10.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal(""))
})
})
}
1 change: 1 addition & 0 deletions carton/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func TestUnit(t *testing.T) {
suite("Netrc", testNetrc)
suite("Package", testPackage)
suite("PackageDependency", testPackageDependency)
suite("GetEolDate", testGetEolDate)
suite.Run(t)
}
1 change: 1 addition & 0 deletions cmd/update-buildpack-dependency/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
flagSet.StringVar(&b.CPEPattern, "cpe-pattern", "", "the cpe version pattern of the dependency, if not set defaults to version-pattern")
flagSet.StringVar(&b.Source, "source", "", "the new uri of the dependency source")
flagSet.StringVar(&b.SourceSHA256, "source-sha256", "", "the new sha256 of the dependency source")
flagSet.StringVar(&b.EolID, "eol-id", "", "id of the dependency for looking up the EOL date on the https://endoflife.date/")

if err := flagSet.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("unable to parse flags\n%w", err))
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require (
github.com/h2non/filetype v1.1.3
github.com/heroku/color v0.0.6
github.com/imdario/mergo v0.3.16
github.com/jarcoal/httpmock v1.3.1
github.com/kobayashi/eol v0.1.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/onsi/gomega v1.33.1
github.com/sclevine/spec v1.4.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ github.com/heroku/color v0.0.6 h1:UTFFMrmMLFcL3OweqP1lAdp8i1y/9oHqkeHjQ/b/Ny0=
github.com/heroku/color v0.0.6/go.mod h1:ZBvOcx7cTF2QKOv4LbmoBtNl5uB17qWxGuzZrsi1wLU=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/kobayashi/eol v0.1.0 h1:0FS4JZ0KBe00IXPc94qvZMxCV0YVI8TivqD7eilqSHw=
github.com/kobayashi/eol v0.1.0/go.mod h1:V+I5wS+ZJK3USqiorangdgvxNFGWzL4+WexJz9MO6Ow=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/onsi/ginkgo/v2 v2.17.2 h1:7eMhcy3GimbsA3hEnVKdw/PQM9XN9krpKVXsZdph0/g=
Expand Down

0 comments on commit ef30385

Please sign in to comment.