2024-02-16 01:45:47 +01:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-12-16 02:43:54 +01:00
|
|
|
package releasechecker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-26 20:39:04 +01:00
|
|
|
"github.com/google/go-github/v59/github"
|
2023-12-16 02:43:54 +01:00
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
|
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbase"
|
2024-02-16 01:45:47 +01:00
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbus"
|
2023-12-16 02:43:54 +01:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:45:47 +01:00
|
|
|
update := scbus.MakeUpdatePacket()
|
|
|
|
update.AddUpdate(clientData)
|
|
|
|
scbus.MainUpdateBus.DoUpdate(update)
|
2023-12-16 02:43:54 +01:00
|
|
|
|
|
|
|
return Success, nil
|
|
|
|
}
|