Growth spike: Get latest MM version (#19366)
* tools updates * Revert "tools updates" This reverts commit 6293297b55803c5a263e200ebd80192899666ae9. * new endpoint to fetch the latest release information * adding unit tests * fixing tests and adding check for github response * error translations * fixing translations * chaning error log * changing cache size * empty strings for translations * changing size to 1 Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
46
app/admin.go
46
app/admin.go
@@ -4,6 +4,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -14,11 +15,16 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/config"
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/services/cache"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/i18n"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/mail"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
||||
)
|
||||
|
||||
var latestVersionCache = cache.NewLRU(cache.LRUOptions{
|
||||
Size: 1,
|
||||
})
|
||||
|
||||
func (s *Server) GetLogs(page, perPage int) ([]string, *model.AppError) {
|
||||
var lines []string
|
||||
|
||||
@@ -241,3 +247,43 @@ func (s *Server) serverBusyStateChanged(sbs *model.ServerBusyState) {
|
||||
mlog.Info("server busy state cleared via cluster event - non-critical services enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) GetLatestVersion(latestVersionUrl string) (*model.GithubReleaseInfo, *model.AppError) {
|
||||
var cachedLatestVersion *model.GithubReleaseInfo
|
||||
if cacheErr := latestVersionCache.Get("latest_version_cache", &cachedLatestVersion); cacheErr == nil {
|
||||
return cachedLatestVersion, nil
|
||||
}
|
||||
|
||||
res, err := http.Get(latestVersionUrl)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetLatestVersion", "app.admin.latest_version_external_error.failure", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
responseData, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetLatestVersion", "app.admin.latest_version_read_all.failure", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var releaseInfoResponse *model.GithubReleaseInfo
|
||||
err = json.Unmarshal(responseData, &releaseInfoResponse)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetLatestVersion", "app.admin.latest_version_unmarshal.failure", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if validErr := releaseInfoResponse.IsValid(); validErr != nil {
|
||||
return nil, model.NewAppError("GetLatestVersion", "app.admin.latest_version_external_error.failure", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
err = latestVersionCache.Set("latest_version_cache", releaseInfoResponse)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetLatestVersion", "app.admin.latest_version_set_cache.failure", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return releaseInfoResponse, nil
|
||||
}
|
||||
|
||||
func (a *App) ClearLatestVersionCache() {
|
||||
latestVersionCache.Remove("latest_version_cache")
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user