Skip to content

Commit

Permalink
get rid of the external dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pbusko committed Jun 19, 2024
1 parent ef30385 commit 179e370
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 60 deletions.
2 changes: 1 addition & 1 deletion carton/buildpack_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (b BuildpackDependency) Update(options ...Option) {
}

if b.EolID != "" {
eolDate, err := GetEolDate(b.EolID, b.Version)
eolDate, err := internal.GetEolDate(b.EolID, b.Version)
if err != nil {
config.exitHandler.Error(fmt.Errorf("unable to fetch deprecation_date"))
return
Expand Down
50 changes: 0 additions & 50 deletions carton/eol.go

This file was deleted.

1 change: 0 additions & 1 deletion carton/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ func TestUnit(t *testing.T) {
suite("Netrc", testNetrc)
suite("Package", testPackage)
suite("PackageDependency", testPackageDependency)
suite("GetEolDate", testGetEolDate)
suite.Run(t)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ 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=
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 relese 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
}
10 changes: 5 additions & 5 deletions carton/eol_test.go → internal/eol_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package carton_test
package internal_test

import (
"net/http"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/buildpacks/libcnb/mocks"
"github.com/jarcoal/httpmock"
. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/libpak/carton"
"github.com/paketo-buildpacks/libpak/internal"
"github.com/sclevine/spec"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -64,7 +64,7 @@ func testGetEolDate(t *testing.T, context spec.G, it spec.S) {
})

it("returns correct cycle", func() {
eolDate, err := carton.GetEolDate("foo", "10.0.0")
eolDate, err := internal.GetEolDate("foo", "10.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2022-10-31T00:00:00Z"))
})
Expand Down Expand Up @@ -96,7 +96,7 @@ func testGetEolDate(t *testing.T, context spec.G, it spec.S) {
})

it("returns correct cycle", func() {
eolDate, err := carton.GetEolDate("foo", "10.1.1")
eolDate, err := internal.GetEolDate("foo", "10.1.1")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal("2024-10-31T00:00:00Z"))
})
Expand Down Expand Up @@ -128,7 +128,7 @@ func testGetEolDate(t *testing.T, context spec.G, it spec.S) {
})

it("returns empty eol date", func() {
eolDate, err := carton.GetEolDate("foo", "10.0.0")
eolDate, err := internal.GetEolDate("foo", "10.0.0")
Expect(err).NotTo(HaveOccurred())
Expect(eolDate).To(Equal(""))
})
Expand Down
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)
}

0 comments on commit 179e370

Please sign in to comment.