From 20c54521c8ceb0d1ddc03e2e69ff21f08541db90 Mon Sep 17 00:00:00 2001 From: Engin Date: Sat, 9 Mar 2024 14:15:22 +0300 Subject: [PATCH 1/6] Remove unnecessary code --- internal/terminal/handler/handler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/terminal/handler/handler.go b/internal/terminal/handler/handler.go index 6bb387f..616a345 100644 --- a/internal/terminal/handler/handler.go +++ b/internal/terminal/handler/handler.go @@ -230,7 +230,6 @@ func (m *model) syncTerminal(msg tea.Msg) { if !m.terminalSizeReady { m.viewport = viewport.New(msg.Width, msg.Height) - m.viewport.YPosition = headerHeight m.terminalSizeReady = true m.viewport.YPosition = headerHeight + 1 } else { From 47b4eb67308da4da17fb315052e894a79bcade58 Mon Sep 17 00:00:00 2001 From: Engin Date: Sat, 9 Mar 2024 14:42:06 +0300 Subject: [PATCH 2/6] Add context support into version --- internal/terminal/handler/information/information.go | 2 +- internal/version/repository/ports.go | 4 +++- internal/version/repository/repository.go | 4 ++-- internal/version/usecase/ports.go | 4 +++- internal/version/usecase/usecase.go | 5 +++-- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/terminal/handler/information/information.go b/internal/terminal/handler/information/information.go index d035e82..3f61501 100644 --- a/internal/terminal/handler/information/information.go +++ b/internal/terminal/handler/information/information.go @@ -81,7 +81,7 @@ func (m *ModelInfo) Init() tea.Cmd { } func (m *ModelInfo) checkUpdates(ctx context.Context) { - isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable() + isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable(ctx) if err != nil { m.modelError.SetError(err) m.modelError.SetErrorMessage("failed to check updates") diff --git a/internal/version/repository/ports.go b/internal/version/repository/ports.go index b980393..9562eb4 100644 --- a/internal/version/repository/ports.go +++ b/internal/version/repository/ports.go @@ -1,6 +1,8 @@ package repository +import "context" + type Repository interface { CurrentVersion() string - LatestVersion() (string, error) + LatestVersion(ctx context.Context) (string, error) } diff --git a/internal/version/repository/repository.go b/internal/version/repository/repository.go index c7b5655..5336c04 100644 --- a/internal/version/repository/repository.go +++ b/internal/version/repository/repository.go @@ -36,12 +36,12 @@ func (r *Repo) CurrentVersion() string { return r.currentVersion } -func (r *Repo) LatestVersion() (string, error) { +func (r *Repo) LatestVersion(ctx context.Context) (string, error) { var result struct { TagName string `json:"tag_name"` } - err := r.do(context.Background(), nil, &result, requestOptions{ + err := r.do(ctx, nil, &result, requestOptions{ method: "GET", path: fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo), accept: "application/vnd.github+json", diff --git a/internal/version/usecase/ports.go b/internal/version/usecase/ports.go index 63b07aa..e282386 100644 --- a/internal/version/usecase/ports.go +++ b/internal/version/usecase/ports.go @@ -1,6 +1,8 @@ package usecase +import "context" + type UseCase interface { CurrentVersion() string - IsUpdateAvailable() (isAvailable bool, version string, err error) + IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) } diff --git a/internal/version/usecase/usecase.go b/internal/version/usecase/usecase.go index 2e537f8..a84bebf 100644 --- a/internal/version/usecase/usecase.go +++ b/internal/version/usecase/usecase.go @@ -1,6 +1,7 @@ package usecase import ( + "context" "github.com/Masterminds/semver/v3" vr "github.com/termkit/gama/internal/version/repository" @@ -20,13 +21,13 @@ func (u *useCase) CurrentVersion() string { return u.versionRepository.CurrentVersion() } -func (u *useCase) IsUpdateAvailable() (isAvailable bool, version string, err error) { +func (u *useCase) IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) { currentVersion := u.versionRepository.CurrentVersion() if currentVersion == "under development" { return false, currentVersion, nil } - latestVersion, err := u.versionRepository.LatestVersion() + latestVersion, err := u.versionRepository.LatestVersion(ctx) if err != nil { return false, currentVersion, err } From 39a7d77d1cdb1cd056cb70667ef52e7e9d58038c Mon Sep 17 00:00:00 2001 From: Engin Date: Sat, 9 Mar 2024 14:44:18 +0300 Subject: [PATCH 3/6] Add ctx into test --- internal/version/repository/repository_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/version/repository/repository_test.go b/internal/version/repository/repository_test.go index 8fa041f..af3f0a4 100644 --- a/internal/version/repository/repository_test.go +++ b/internal/version/repository/repository_test.go @@ -1,6 +1,7 @@ package repository import ( + "context" "testing" ) @@ -9,9 +10,10 @@ func NewRepository() Repository { } func TestRepo_LatestVersion(t *testing.T) { + ctx := context.Background() repo := NewRepository() - _, err := repo.LatestVersion() + _, err := repo.LatestVersion(ctx) if err != nil { t.Error(err) } From 54c79fb0329dfe687bfc5d1210a8c10f04a7c117 Mon Sep 17 00:00:00 2001 From: Engin Date: Sun, 10 Mar 2024 18:37:13 +0300 Subject: [PATCH 4/6] Minor code tweaks --- internal/terminal/handler/information/information.go | 6 +++--- internal/version/repository/repository.go | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/terminal/handler/information/information.go b/internal/terminal/handler/information/information.go index 3f61501..ebf488e 100644 --- a/internal/terminal/handler/information/information.go +++ b/internal/terminal/handler/information/information.go @@ -48,7 +48,7 @@ const ( ) var ( - gamaVersion string + currentVersion string newVersionAvailableMsg string applicationDescription string ) @@ -72,8 +72,8 @@ func SetupModelInfo(githubUseCase gu.UseCase, versionUseCase vu.UseCase, lockTab } func (m *ModelInfo) Init() tea.Cmd { - gamaVersion = m.versionUseCase.CurrentVersion() - applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", gamaVersion) + currentVersion = m.versionUseCase.CurrentVersion() + applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", currentVersion) go m.testConnection(context.Background()) go m.checkUpdates(context.Background()) diff --git a/internal/version/repository/repository.go b/internal/version/repository/repository.go index 5336c04..8dcf9d2 100644 --- a/internal/version/repository/repository.go +++ b/internal/version/repository/repository.go @@ -47,7 +47,8 @@ func (r *Repo) LatestVersion(ctx context.Context) (string, error) { accept: "application/vnd.github+json", }) // client time out error - if errors.As(err, &context.DeadlineExceeded) { + var deadlineExceededError *url.Error + if errors.As(err, &deadlineExceededError) && deadlineExceededError.Timeout() { return "", errors.New("request timed out") } else if err != nil { return "", err From a4ceacc96ffb52101b67e44e015e2673cb9648a8 Mon Sep 17 00:00:00 2001 From: Engin Date: Sun, 10 Mar 2024 18:41:36 +0300 Subject: [PATCH 5/6] Reorder code --- internal/terminal/handler/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/terminal/handler/handler.go b/internal/terminal/handler/handler.go index 616a345..94262bc 100644 --- a/internal/terminal/handler/handler.go +++ b/internal/terminal/handler/handler.go @@ -230,8 +230,8 @@ func (m *model) syncTerminal(msg tea.Msg) { if !m.terminalSizeReady { m.viewport = viewport.New(msg.Width, msg.Height) - m.terminalSizeReady = true m.viewport.YPosition = headerHeight + 1 + m.terminalSizeReady = true } else { m.viewport.Width = msg.Width m.viewport.Height = msg.Height From 612644b8230f4a02f00a70cdc6595991deccceb1 Mon Sep 17 00:00:00 2001 From: Engin Date: Sun, 10 Mar 2024 18:52:02 +0300 Subject: [PATCH 6/6] Improve the test --- internal/version/repository/repository_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/version/repository/repository_test.go b/internal/version/repository/repository_test.go index af3f0a4..b70f3e0 100644 --- a/internal/version/repository/repository_test.go +++ b/internal/version/repository/repository_test.go @@ -3,6 +3,8 @@ package repository import ( "context" "testing" + + "github.com/stretchr/testify/assert" ) func NewRepository() Repository { @@ -13,8 +15,9 @@ func TestRepo_LatestVersion(t *testing.T) { ctx := context.Background() repo := NewRepository() - _, err := repo.LatestVersion(ctx) - if err != nil { - t.Error(err) - } + t.Run("Get latest version", func(t *testing.T) { + res, err := repo.LatestVersion(ctx) + assert.NoError(t, err) + assert.NotEmpty(t, res) + }) }