waveterm/wavesrv/pkg/releasechecker/releasechecker.go
Evan Simkowitz b733724c7d
Add sidebar banner when new release is available (#147)
* Server impl

* add update check setting

* add commands

* fix capitalization of commands

* apply suggestions

* add migration and fix backend bugs

* Add sidebar banner

* remove installedversion, add 5s timeout

* add icon, capture and log errors from release check

* missing return nil

* remove highlight

* remove commented less

* do not fail releasecheckoncommand if release check operation fails

* remove debug condition

* fix update on auto check, move banner display logic into frontend

* remove unnecessary import

* simplify null check

* clean up the invoking of the releasechecker
2023-12-15 17:43:54 -08:00

76 lines
2.1 KiB
Go

package releasechecker
import (
"context"
"fmt"
"github.com/google/go-github/v57/github"
"golang.org/x/mod/semver"
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbase"
"github.com/wavetermdev/waveterm/wavesrv/pkg/sstore"
)
type ReleaseCheckResult int
const (
NotNeeded ReleaseCheckResult = 0
Success ReleaseCheckResult = 1
Failure ReleaseCheckResult = 2
Disabled ReleaseCheckResult = 3
)
// CheckNewRelease checks for a new release and updates the release info in the DB.
// If force is true, the release info is updated even if it is fresh or if the automatic release check is disabled.
func CheckNewRelease(ctx context.Context, force bool) (ReleaseCheckResult, error) {
clientData, err := sstore.EnsureClientData(ctx)
if err != nil {
return Failure, fmt.Errorf("error getting client data: %w", err)
}
if !force && clientData.ClientOpts.NoReleaseCheck {
return Disabled, nil
}
if !force && semver.Compare(scbase.WaveVersion, clientData.ReleaseInfo.LatestVersion) < 0 {
// We have already notified the frontend about a new release and the record is fresh. There is no need to check again.
return NotNeeded, nil
}
// Initialize an unauthenticated client
client := github.NewClient(nil)
// Get the latest release from the repository
release, rsp, err := client.Repositories.GetLatestRelease(ctx, "wavetermdev", "waveterm")
releaseInfoLatest := sstore.ReleaseInfoType{
LatestVersion: scbase.WaveVersion,
}
if err != nil {
return Failure, fmt.Errorf("error getting latest release: %w", err)
}
if rsp.StatusCode != 200 {
return Failure, fmt.Errorf("response from Github is not success: %v", rsp)
}
releaseInfoLatest.LatestVersion = *release.TagName
// Update the release info in the DB
err = sstore.SetReleaseInfo(ctx, releaseInfoLatest)
if err != nil {
return Failure, fmt.Errorf("error updating release info: %w", err)
}
clientData, err = sstore.EnsureClientData(ctx)
if err != nil {
return Failure, fmt.Errorf("error getting updated client data: %w", err)
}
update := &sstore.ModelUpdate{
ClientData: clientData,
}
sstore.MainBus.SendUpdate(update)
return Success, nil
}