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

Improve the update feature #61

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions pkg/version/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ type Version interface {
CurrentVersion() string
LatestVersion(ctx context.Context) (string, error)
IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error)
Changelogs(ctx context.Context) ([]Changelog, error)
ChangelogsSinceCurrentVersion(ctx context.Context) ([]Changelog, error)
}
64 changes: 64 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ type version struct {
latestVersion string
}

// Changelog represents a changelog information
type Changelog struct {
// PublishedAt is the date when the changelog was published
PublishedAt time.Time `json:"published_at"`

// TagName is the tag name of the changelog
TagName string `json:"tag_name"`

// Body message of the changelog
Body string `json:"body"`
}

// New creates a new version instance
func New(repositoryOwner, repositoryName, currentVersion string) Version {
return &version{
client: &http.Client{
Expand All @@ -32,10 +45,12 @@ func New(repositoryOwner, repositoryName, currentVersion string) Version {
}
}

// CurrentVersion returns the current version of the repository
func (v *version) CurrentVersion() string {
return v.currentVersion
}

// LatestVersion returns the latest version of the repository
func (v *version) LatestVersion(ctx context.Context) (string, error) {
var result struct {
TagName string `json:"tag_name"`
Expand All @@ -58,6 +73,55 @@ func (v *version) LatestVersion(ctx context.Context) (string, error) {
return result.TagName, nil
}

// Changelogs returns all changelogs of the repository
func (v *version) Changelogs(ctx context.Context) ([]Changelog, error) {
var result []Changelog

err := v.do(ctx, nil, &result, requestOptions{
method: http.MethodGet,
path: fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", v.repositoryOwner, v.repositoryName),
accept: "application/vnd.github+json",
})
// client time out error
var deadlineExceededError *url.Error
if err != nil {
if errors.As(err, &deadlineExceededError) && deadlineExceededError.Timeout() {
return nil, errors.New("request timed out")
}
return nil, err
}

return result, nil
}

// ChangelogsSinceCurrentVersion returns all changelogs since the current version
func (v *version) ChangelogsSinceCurrentVersion(ctx context.Context) ([]Changelog, error) {
changelogs, err := v.Changelogs(ctx)
if err != nil {
return nil, err
}

currentVersion, err := semver.NewVersion(v.CurrentVersion())
if err != nil {
return nil, err
}

var result []Changelog
for _, cl := range changelogs {
tagVersion, err := semver.NewVersion(cl.TagName)
if err != nil {
continue
}

if tagVersion.GreaterThan(currentVersion) {
result = append(result, cl)
}
}

return result, nil
}

// IsUpdateAvailable checks if an update is available
func (v *version) IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) {
currentVersion := v.CurrentVersion()
if currentVersion == "under development" {
Expand Down
52 changes: 42 additions & 10 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import (
)

const (
repositoryOwner = "termkit"
repositoryName = "gama"
testCurrentVersion = "1.0.0"
repositoryOwner = "termkit"
repositoryName = "gama"
)

func NewRepository() Version {
return New(repositoryOwner, repositoryName, testCurrentVersion)
}

func TestVersion_CurrentVersion(t *testing.T) {
repo := NewRepository()
testCurrentVersion := "1.0.0"

repo := New(repositoryOwner, repositoryName, testCurrentVersion)
t.Run("Get current version", func(t *testing.T) {
assert.Equal(t, testCurrentVersion, repo.CurrentVersion())
})
}

func TestVersion_IsUpdateAvailable(t *testing.T) {
ctx := context.Background()
repo := NewRepository()

testCurrentVersion := "1.0.0"

repo := New(repositoryOwner, repositoryName, testCurrentVersion)

t.Run("Check if update is available", func(t *testing.T) {
isAvailable, version, err := repo.IsUpdateAvailable(ctx)
Expand All @@ -36,9 +36,41 @@ func TestVersion_IsUpdateAvailable(t *testing.T) {
})
}

func TestVersion_Changelogs(t *testing.T) {
ctx := context.Background()

testCurrentVersion := "1.0.0"

repo := New(repositoryOwner, repositoryName, testCurrentVersion)

t.Run("Get changelogs", func(t *testing.T) {
res, err := repo.Changelogs(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, res)
})
}

func TestVersion_ChangelogsSinceCurrentVersion(t *testing.T) {
ctx := context.Background()

testCurrentVersion := "1.1.0"

repo := New(repositoryOwner, repositoryName, testCurrentVersion)

t.Run("Get changelogs since current version", func(t *testing.T) {
res, err := repo.ChangelogsSinceCurrentVersion(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, res)
})

}

func TestRepo_LatestVersion(t *testing.T) {
ctx := context.Background()
repo := NewRepository()

testCurrentVersion := "1.0.0"

repo := New(repositoryOwner, repositoryName, testCurrentVersion)

t.Run("Get latest version", func(t *testing.T) {
res, err := repo.LatestVersion(ctx)
Expand Down