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

Automatically add deprecation_date field to buildpack dependencies if requested #345

Merged
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
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 := internal.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
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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/mitchellh/hashstructure/v2 v2.0.2
github.com/onsi/gomega v1.33.1
github.com/sclevine/spec v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ 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/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
110 changes: 110 additions & 0 deletions internal/eol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package internal

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/Masterminds/semver/v3"
)

const eolBaseURL = "https://endoflife.date/api"

func GetEolDate(eolID, version string) (string, error) {
cycleList, err := getProjectCycleList(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 release cycle: %w", err)
}

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

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

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

func selectCycle(version string, cycles cycleList) (*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 == v {
return c, nil
}
}
}

return nil, fmt.Errorf("no release cycle found for the version %s", version)
}

func getProjectCycleList(id string) (cycleList, error) {
res, err := http.Get(fmt.Sprintf("%s/%s.json", eolBaseURL, id))
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch release cycles, status: %d", res.StatusCode)
}

cycles := cycleList{}
if err := json.NewDecoder(res.Body).Decode(&cycles); err != nil {
return nil, err
}

return cycles, nil
}

type cycleList []*cycle

type cycle struct {
Cycle string
EOL string
}

func (c *cycle) UnmarshalJSON(data []byte) error {
var i map[string]interface{}

if err := json.Unmarshal(data, &i); err != nil {
return err
}

if val, ok := i["cycle"]; ok {
switch t := val.(type) {
case string:
c.Cycle = t
case int, float64:
c.Cycle = fmt.Sprintf("%d", t)
default:
return fmt.Errorf("invalid type of the \"cycle\" field: %T", t)
}
}

if val, ok := i["eol"]; ok {
switch t := val.(type) {
case string:
c.EOL = t
case bool:
c.EOL = ""
default:
return fmt.Errorf("invalid type of the \"eol\" field: %T", t)
}
}

return nil
}
86 changes: 86 additions & 0 deletions internal/eol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package internal_test

import (
"net/http"
"testing"

"github.com/buildpacks/libcnb/mocks"
"github.com/jarcoal/httpmock"
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/libpak/internal"
"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": "2026-12-31",
"minJavaVersion": 8,
"latest": "10.0.27",
"latestReleaseDate": "2025-10-03",
"lts": false
},
{
"cycle": "9",
"releaseDate": "2017-09-27",
"eol": "2023-12-31",
"minJavaVersion": 8,
"latest": "9.0.89",
"latestReleaseDate": "2021-05-03",
"lts": false
}
]`)))
})

it("finds release cycle by major and minor version", func() {
eolDate, err := internal.GetEolDate("foo", "10.0.1")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2026-12-31T00:00:00Z"))
})

it("finds release cycle by major version", func() {
eolDate, err := internal.GetEolDate("foo", "9.5.4")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2023-12-31T00:00:00Z"))
})

it("returns empty eol date if eol is a bool", func() {
eolDate, err := internal.GetEolDate("foo", "10.1.0")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal(""))
})

})

}
1 change: 1 addition & 0 deletions internal/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ func TestUnit(t *testing.T) {
suite("ExitHandler", testExitHandler)
suite("TOMLWriter", testTOMLWriter)
suite("TOMLMarshal", testTOMLMarshal)
suite("EOL", testGetEolDate)
suite.Run(t)
}