mirror of
https://github.com/bitwarden/server.git
synced 2025-01-22 21:51:22 +01:00
[PS-293] Update admin portal to use the new version.json (#2006)
* PS-293: Get latest version no comes from GitHub instead of DockerHub. * PS-293: format fixes * PS-293: code refactor and clean up * PS-293: deserialization to class, argument typification. * PS-293: formating fix * PS-293: Moved ProjectType to HomeController * PS-293: updated version endpoint to CDN * PS-293: Update endpoint to CF protected
This commit is contained in:
parent
ee5946c4b9
commit
904193560c
@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Admin.Controllers
|
||||
{
|
||||
@ -42,26 +43,22 @@ namespace Bit.Admin.Controllers
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> GetLatestDockerHubVersion(string repository, CancellationToken cancellationToken)
|
||||
|
||||
public async Task<IActionResult> GetLatestVersion(ProjectType project, CancellationToken cancellationToken)
|
||||
{
|
||||
var requestUri = $"https://hub.docker.com/v2/repositories/bitwarden/{repository}/tags/";
|
||||
var requestUri = $"https://selfhost.bitwarden.com/version.json";
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync(requestUri, cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
|
||||
var root = jsonDocument.RootElement;
|
||||
|
||||
var results = root.GetProperty("results");
|
||||
foreach (var result in results.EnumerateArray())
|
||||
var latestVersions = JsonConvert.DeserializeObject<LatestVersions>(await response.Content.ReadAsStringAsync());
|
||||
return project switch
|
||||
{
|
||||
var name = result.GetProperty("name").GetString();
|
||||
if (!string.IsNullOrWhiteSpace(name) && name.Length > 0 && char.IsNumber(name[0]))
|
||||
{
|
||||
return new JsonResult(name);
|
||||
}
|
||||
}
|
||||
ProjectType.Core => new JsonResult(latestVersions.Versions.WebVersion),
|
||||
ProjectType.Web => new JsonResult(latestVersions.Versions.CoreVersion),
|
||||
_ => throw new System.NotImplementedException(),
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
@ -94,5 +91,29 @@ namespace Bit.Admin.Controllers
|
||||
|
||||
return new JsonResult("-");
|
||||
}
|
||||
|
||||
private class LatestVersions
|
||||
{
|
||||
[JsonProperty("versions")]
|
||||
public Versions Versions { get; set; }
|
||||
}
|
||||
|
||||
private class Versions
|
||||
{
|
||||
[JsonProperty("coreVersion")]
|
||||
public string CoreVersion { get; set; }
|
||||
|
||||
[JsonProperty("webVersion")]
|
||||
public string WebVersion { get; set; }
|
||||
|
||||
[JsonProperty("keyConnectorVersion")]
|
||||
public string KeyConnectorVersion { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public enum ProjectType
|
||||
{
|
||||
Core,
|
||||
Web,
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
@model HomeModel
|
||||
@using Bit.Admin.Controllers
|
||||
@model HomeModel
|
||||
@{
|
||||
ViewData["Title"] = "Dashboard";
|
||||
}
|
||||
@ -9,7 +10,7 @@
|
||||
let loadedWebLatest = false;
|
||||
let loadedWebInstalled = false;
|
||||
|
||||
fetch('@Url.Action("GetLatestDockerHubVersion", new { repository = "web" })').then((response) => {
|
||||
fetch('@Url.Action("GetLatestVersion", new { project = @ProjectType.Web })').then((response) => {
|
||||
return response.json();
|
||||
}).then((version) => {
|
||||
document.getElementById('web-latest').innerText = version;
|
||||
@ -19,7 +20,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
fetch('@Url.Action("GetLatestDockerHubVersion", new { repository = "api" })').then((response) => {
|
||||
fetch('@Url.Action("GetLatestVersion", new { project = @ProjectType.Core })').then((response) => {
|
||||
return response.json();
|
||||
}).then((version) => {
|
||||
document.getElementById('server-latest').innerText = version;
|
||||
|
Loading…
Reference in New Issue
Block a user